divelet-ml 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ .venv/
2
+ .idea/
3
+ workspace/
4
+ __pycache__/
5
+ *.egg-info/
6
+ coverage.xml
7
+ .coverage
@@ -0,0 +1,97 @@
1
+ # Content License
2
+
3
+ ## Educational Content
4
+
5
+ Educational content in **divelet-ml** that is adapted from
6
+ [Dive into Deep Learning](https://d2l.ai/) is licensed under the
7
+ **Creative Commons Attribution-ShareAlike 4.0 International License
8
+ (CC BY-SA 4.0)**.
9
+
10
+ The full license text is available at:
11
+
12
+ https://creativecommons.org/licenses/by-sa/4.0/legalcode
13
+
14
+ A human-readable summary is available at:
15
+
16
+ https://creativecommons.org/licenses/by-sa/4.0/
17
+
18
+ Under this license, users may:
19
+
20
+ - **Share:** Copy and redistribute the material in any medium or format.
21
+ - **Adapt:** Remix, transform, and build upon the material for any purpose,
22
+ including commercial purposes.
23
+
24
+ These permissions are subject to the license conditions, including:
25
+
26
+ - **Attribution:** Give appropriate credit, provide a link to the license,
27
+ and indicate whether changes were made.
28
+ - **ShareAlike:** Distribute adaptations under CC BY-SA 4.0 or another
29
+ license permitted by its terms.
30
+ - **No additional restrictions:** Do not impose legal terms or technological
31
+ measures that restrict the permissions granted by the license.
32
+
33
+ ## Original Work
34
+
35
+ The original educational material is from:
36
+
37
+ **Dive into Deep Learning**
38
+
39
+ - Website: https://d2l.ai/
40
+ - Source repository: https://github.com/d2l-ai/d2l-en
41
+ - Authors and contributors: As credited in the original project
42
+ - Original content license: CC BY-SA 4.0
43
+
44
+ The initial divelet-ml compatibility assessment references upstream commit:
45
+
46
+ `23d7a5aecceee57d1292c56e90cce307f183bb0a`
47
+
48
+ This revision identifies the initial assessment baseline. Individual adapted
49
+ chapters may reference different upstream revisions, which will be documented
50
+ alongside those chapters.
51
+
52
+ See [NOTICE.md](NOTICE.md) for attribution and modification notices.
53
+
54
+ ## Scope of This License
55
+
56
+ CC BY-SA 4.0 applies to educational material derived from the book that is
57
+ covered by the original book-content license, including adapted explanations,
58
+ mathematical discussions, and exercises.
59
+
60
+ It does not automatically apply to:
61
+
62
+ - Independently developed source code.
63
+ - Source code incorporated from the book's repository under a separate software license.
64
+ - Third-party illustrations, images, or other assets.
65
+ - Datasets or pretrained models distributed under separate terms.
66
+ - Dependencies used to execute the notebooks.
67
+
68
+ These materials remain subject to their respective licenses.
69
+
70
+ ## Source Code
71
+
72
+ The book's original repository includes executable examples and software
73
+ components that may be governed by licensing terms distinct from those
74
+ applicable to the educational text.
75
+
76
+ Before incorporating or adapting such code, divelet-ml will verify the applicable
77
+ license and preserve any required copyright notices and attribution.
78
+
79
+ Original divelet-ml code is licensed under the Apache License 2.0; see `LICENSE`.
80
+ Until then, this document should not be interpreted as granting a blanket
81
+ software license to all files in the repository.
82
+
83
+ ## Attribution and Modifications
84
+
85
+ Adapted chapters will identify their original sources in the book, provide appropriate
86
+ attribution, and describe substantive changes.
87
+
88
+ The original authors retain their rights in the original work.
89
+
90
+ divelet-ml is an independent adaptation and is not affiliated with or endorsed
91
+ by the book's original authors or maintainers.
92
+
93
+ ## Disclaimer
94
+
95
+ This document summarizes the intended licensing treatment of educational
96
+ content in divelet-ml. It does not replace the full text of CC BY-SA 4.0 or
97
+ override the licensing terms of individual third-party materials.
@@ -0,0 +1,202 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
202
+
@@ -0,0 +1,72 @@
1
+ # Attribution and Modification Notices
2
+
3
+ divelet-ml
4
+ Copyright 2026 Emmanuel Decitre
5
+
6
+ ## Original Work
7
+
8
+ **Dive into Deep Learning** is an open-source interactive textbook developed by its original authors and contributors.
9
+
10
+ - **Website:** https://d2l.ai/
11
+ - **Source repository:** https://github.com/d2l-ai/d2l-en
12
+ - **Authors and contributors:** As credited in the original project
13
+ - **Book content license:** Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
14
+ - **License:** https://creativecommons.org/licenses/by-sa/4.0/
15
+
16
+ The initial divelet-ml compatibility assessment used the following upstream revision:
17
+
18
+ `23d7a5aecceee57d1292c56e90cce307f183bb0a`
19
+
20
+ https://github.com/d2l-ai/d2l-en/commit/23d7a5aecceee57d1292c56e90cce307f183bb0a
21
+
22
+ ## Nature of the Adaptation
23
+
24
+ **divelet-ml** is an independent, browser-native adaptation of Dive into Deep Learning for JupyterLite.
25
+
26
+ The project aims to preserve the original book's educational structure, mathematical explanations, learning objectives, and exercises while adapting framework-dependent implementations for execution in browser-native Python environments.
27
+
28
+ Modifications, which vary between chapters, include:
29
+
30
+ - Replacing selected framework-specific implementations with NumPy or other browser-compatible alternatives.
31
+ - Adapting code examples for JupyterLite using Pyodide and xeus-python.
32
+ - Modifying dependency installation, dataset loading, and execution instructions where necessary.
33
+ - Introducing alternative browser-native implementations for selected machine learning tasks.
34
+ - Documenting differences in functionality, performance, and compatibility.
35
+
36
+ The adapted sections are listed in the README and published at https://decitre.github.io/divelet-ml/. Each one's notebook lists the specific changes made to it.
37
+
38
+ ## Attribution of Adapted Chapters
39
+
40
+ The notebook of each adapted section opens with a one-line credit and ends with an "Attribution and changes" section that provides:
41
+
42
+ 1. A reference to the corresponding chapter of the original book.
43
+ 2. Identification of the upstream source revision.
44
+ 3. Attribution to the original authors and contributors.
45
+ 4. A description of substantive modifications.
46
+ 5. Applicable license information.
47
+
48
+ Adapted educational content derived from the book is distributed under the terms of CC BY-SA 4.0.
49
+
50
+ ## Source Code and Third-Party Materials
51
+
52
+ The licensing of executable code, illustrations, datasets, and other third-party materials may differ from that of the book's educational content.
53
+
54
+ Before incorporating such materials, divelet-ml will review their applicable licenses and preserve required copyright notices, attribution, and license terms.
55
+
56
+ The CC BY-SA 4.0 designation for adapted book content does not automatically apply to independently developed software or separately licensed third-party materials.
57
+
58
+ Code adapted from the book's sample code is used under its MIT-0 license. divelet-ml's own source code (the `divelet_ml` package, build scripts and tests) is licensed under the Apache License 2.0; see `LICENSE`.
59
+
60
+ ## Independence
61
+
62
+ divelet-ml is not an official edition of Dive into Deep Learning.
63
+
64
+ It is not affiliated with, sponsored by, or endorsed by the book's original authors or maintainers.
65
+
66
+ divelet-ml is not affiliated with D2L Corporation or its products.
67
+
68
+ The original work and its associated rights remain with their respective authors and copyright holders.
69
+
70
+ ## Project Repository
71
+
72
+ https://github.com/decitre/divelet-ml
@@ -0,0 +1,207 @@
1
+ Metadata-Version: 2.5
2
+ Name: divelet-ml
3
+ Version: 0.1.0
4
+ Summary: Dive into Deep Learning, adapted for JupyterLite
5
+ Project-URL: Homepage, https://github.com/decitre/divelet-ml
6
+ Project-URL: Documentation, https://decitre.github.io/divelet-ml/
7
+ Project-URL: Issues, https://github.com/decitre/divelet-ml/issues
8
+ Author: decitre
9
+ License-Expression: Apache-2.0
10
+ License-File: LICENSE
11
+ License-File: NOTICE.md
12
+ Requires-Python: >=3.11
13
+ Requires-Dist: numpy
14
+ Description-Content-Type: text/markdown
15
+
16
+ <img src="https://raw.githubusercontent.com/decitre/divelet-ml/main/divelet-ml.svg"
17
+ alt="Divelet ML — Deep learning, in your browser"
18
+ width="600">
19
+
20
+ [![Build site][ci-badge]][ci-url]
21
+ [![Coverage][codecov-badge]][codecov-url]
22
+ [![Try xeus-python][xeus-badge]][xeus-tree]
23
+ [![Try Pyodide][pyodide-badge]][pyodide-tree]
24
+ [![Content: CC BY-SA 4.0][license-badge]][license-url]
25
+ [![Code: Apache 2.0][apache-badge]][apache]
26
+
27
+ # divelet-ml
28
+
29
+ **divelet for ML: Dive into Deep Learning, adapted for JupyterLite.**
30
+
31
+ divelet-ml is an experimental, browser-native adaptation of the Jupyter notebooks that accompany the book [*Dive into Deep Learning*][d2l-site] ("the book" below), designed to make its educational content and interactive examples accessible through [JupyterLite][jupyterlite] without requiring a local Python installation or a remote computation server.
32
+
33
+ The project aims to preserve the book's chapter structure, mathematical explanations, learning objectives, and exercises while adapting its framework-dependent Python implementations for execution directly in the browser.
34
+
35
+ **Project status:** Early development. Compatibility assessment is underway. Adapted sections run in the browser with no installation. A section link opens its notebook with the xeus-python kernel; **[p]** opens it with Pyodide instead.
36
+
37
+ | Section | Original |
38
+ |---|---|
39
+ | 1 Introduction (no code) | [d2l.ai][d2l-1] |
40
+ | [2.1 Data Manipulation][xeus-2.1] [\[p\]][pyodide-2.1] | [d2l.ai][d2l-2.1] |
41
+ | [3.3 Synthetic Regression Data][xeus-3.3] [\[p\]][pyodide-3.3] | [d2l.ai][d2l-3.3] |
42
+
43
+ The notebooks' sources are in [`content/`][content].
44
+
45
+ Build the site locally with `./build.sh` (needs uv and micromamba).
46
+
47
+ ## Python package
48
+
49
+ The notebooks use `divelet_ml`, a small NumPy counterpart of the book's `d2l` library. The site's browser kernels already include it. To use it elsewhere:
50
+
51
+ ```bash
52
+ pip install divelet-ml
53
+ ```
54
+
55
+ ```python
56
+ import divelet_ml as d2l
57
+ ```
58
+
59
+ It provides `add_to_class`, `HyperParameters` and `DataModule`, adapted from the book's `d2l/torch.py`, and `ArrayLoader`, which yields minibatches of NumPy arrays in place of PyTorch's `DataLoader`. The package grows only as adapted chapters need more of it. Its code is licensed under the Apache License 2.0; the notebooks are not part of the package.
60
+
61
+ ## Motivation
62
+
63
+ Dive into Deep Learning is an open-source, interactive textbook combining mathematical explanations with executable code.
64
+
65
+ Its implementations rely primarily on deep learning frameworks such as PyTorch, TensorFlow, JAX, and MXNet. These environments can be difficult to reproduce in a browser-only Python runtime.
66
+
67
+ divelet-ml explores whether the same educational progression can be maintained using lightweight, browser-compatible implementations.
68
+
69
+ The objective is not to reproduce the entire deep learning framework ecosystem in the browser, but to make the underlying concepts accessible through executable, pedagogically faithful examples.
70
+
71
+ ## Design Principles
72
+
73
+ - **Educational fidelity:** Preserve the book's explanations, mathematical formulations, learning objectives, and exercises wherever possible.
74
+ - **Browser-native execution:** Run examples entirely in the browser without requiring a remote Python kernel.
75
+ - **NumPy first:** Prefer NumPy implementations when they adequately illustrate the underlying concepts.
76
+ - **Framework independence:** Avoid unnecessary dependencies on full deep learning frameworks.
77
+ - **Progressive adaptation:** Adapt chapters individually, prioritizing educational value and technical feasibility.
78
+ - **Reproducibility:** Document runtime requirements, implementation differences, and compatibility limitations.
79
+
80
+ ## Technical Approach
81
+
82
+ ### JupyterLite
83
+
84
+ divelet-ml targets JupyterLite with two Python execution environments:
85
+
86
+ - **Pyodide:** Python compiled to WebAssembly, with access to a browser-compatible scientific Python ecosystem.
87
+ - **xeus-python:** A browser-native Python kernel built using WebAssembly.
88
+
89
+ Compatibility will be evaluated independently for each environment.
90
+
91
+ ### NumPy and Deep Learning
92
+
93
+ Where practical, framework-specific operations will be replaced with NumPy equivalents.
94
+
95
+ This includes tensor manipulation, linear algebra, probability, optimization, and selected neural network implementations.
96
+
97
+ Some chapters may require more substantial adaptation, particularly those involving automatic differentiation, GPU acceleration, large datasets, or computationally expensive training.
98
+
99
+ The goal is to preserve the concepts being taught rather than provide identical framework APIs.
100
+
101
+ ### Transformers.js and lmLite
102
+
103
+ For selected chapters involving modern language models or inference, divelet-ml may explore integration with [Transformers.js][transformers-js] through [lmLite][lmlite].
104
+
105
+ lmLite provides Python access to browser-native machine learning inference.
106
+
107
+ This integration is optional and is not intended to replace the mathematical and algorithmic foundations of the book.
108
+
109
+ ## Compatibility Assessment
110
+
111
+ An initial static audit of the book's source repository has been developed to identify:
112
+
113
+ - Framework-specific implementations and dependencies.
114
+ - Candidate chapters for direct adaptation.
115
+ - Potential incompatibilities with browser-native Python runtimes.
116
+ - Estimated adaptation effort.
117
+ - Opportunities for alternative browser-native implementations.
118
+
119
+ The audit distinguishes static compatibility estimates from actual execution results.
120
+
121
+ **Static analysis does not establish that a chapter runs successfully in JupyterLite.** Browser execution and educational fidelity must be validated separately.
122
+
123
+ The initial audit references the book's commit:
124
+
125
+ `23d7a5aecceee57d1292c56e90cce307f183bb0a`
126
+
127
+ ## Initial Development Plan
128
+
129
+ The first phase focuses on three representative sections of the book:
130
+
131
+ | Section | Focus | Status |
132
+ |---|---|---|
133
+ | 2.1 `chapter_preliminaries/ndarray.md` | Array operations and tensor fundamentals | Adapted |
134
+ | 3.3 `chapter_linear-regression/synthetic-regression-data.md` | Synthetic datasets and numerical computation | Adapted |
135
+ | 11.3 `chapter_attention-mechanisms-and-transformers/attention-scoring-functions.md` | Attention mechanisms and scoring functions | Planned |
136
+
137
+ The PyTorch implementations serve as the primary reference.
138
+
139
+ Each adaptation will be reviewed for educational fidelity, browser compatibility, dependencies, and reproducibility.
140
+
141
+ The project will progressively expand to additional chapters based on the results of these initial experiments.
142
+
143
+ See [ROADMAP.md][roadmap] for further details.
144
+
145
+ ## Relationship to Dive into Deep Learning
146
+
147
+ divelet-ml is an independent adaptation of the original [Dive into Deep Learning][d2l-repo] project.
148
+
149
+ It is not an official edition of the book and is not affiliated with or endorsed by the original authors.
150
+
151
+ The project intends to preserve the educational contributions of the book while adapting selected implementations for browser-native execution.
152
+
153
+ Adapted chapters will identify their original sources and document substantive modifications.
154
+
155
+ ## License and Attribution
156
+
157
+ The original Dive into Deep Learning book content is distributed under the [Creative Commons Attribution-ShareAlike 4.0 International License][cc-by-sa].
158
+
159
+ Adapted educational content derived from the book will retain the applicable attribution and ShareAlike requirements.
160
+
161
+ divelet-ml's own source code (the `divelet_ml` package, build scripts and tests) is licensed under the [Apache License 2.0][apache]. Third-party materials may be subject to separate licenses; these will be reviewed and their required notices preserved before redistribution.
162
+
163
+ See [CONTENT-LICENSE.md][license-url] and [NOTICE.md][notice] for licensing and attribution details.
164
+
165
+ ## Feedback and Contributions
166
+
167
+ divelet-ml is an experimental project exploring browser-native deep learning education.
168
+
169
+ Feedback is particularly welcome on:
170
+
171
+ - The educational usefulness of framework-independent implementations.
172
+ - The fidelity of NumPy adaptations to the book's original examples.
173
+ - JupyterLite compatibility, performance, and runtime limitations.
174
+ - Opportunities for browser-native machine learning beyond traditional Python frameworks.
175
+
176
+ Please share suggestions, bug reports, and use cases through [GitHub Issues][issues]. To set up a development environment, run the tests or make a release, see [CONTRIBUTING.md][contributing].
177
+
178
+ [ci-badge]: https://github.com/decitre/divelet-ml/actions/workflows/build.yml/badge.svg
179
+ [ci-url]: https://github.com/decitre/divelet-ml/actions/workflows/build.yml
180
+ [codecov-badge]: https://img.shields.io/codecov/c/github/decitre/divelet-ml/main?flag=python&label=Coverage&logo=codecov
181
+ [codecov-url]: https://codecov.io/gh/decitre/divelet-ml
182
+ [xeus-tree]: https://decitre.github.io/divelet-ml/xeus/tree/
183
+ [pyodide-tree]: https://decitre.github.io/divelet-ml/pyodide/tree/
184
+ [xeus-badge]: https://img.shields.io/badge/try-xeus--python-f7dc1e.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA0Ni43IDQ2LjciPgogIDxwYXRoCiAgICBmaWxsPSIjNDBCM0MzIgogICAgZD0iTTUgMGMyLjUuOCA0LjUgMiA0LjYgMi4xIDEuOSAxLjMgMy42IDMuMSA0LjggNS4xLjQuNiA1LjIgNy44IDguNiAxMi45LS44IDEuMi0xLjUgMi4zLTIuMSAzLjEgMC0uMS0uMS0uMS0uMS0uMkMxNy42IDE3LjkgMTEuNCA4LjYgMTEgNy45Yy0uOS0xLjMtMi0yLjUtMy41LTMuNCAwIDAtMS41LTEtMy41LTEuNkMzLjIgMi42IDEuOCAyLjQgMCAyLjJ2NDIuNGMxLjgtLjIgMy4yLS40IDQtLjcgMi0uNiAzLjUtMS42IDMuNS0xLjYgMS41LTEgMi43LTIuMiAzLjUtMy40LjQtLjcgNi42LTkuOSAxMC4xLTE1LjJDMjQgMTkuMSAzMS40IDguMSAzMS45IDcuM2MxLjItMiAyLjktMy44IDQuOC01LjEgMC0uMSAyLjEtMS4zIDQuNi0yLjFMNDEuOCAwSDV6CiAgICAgICBNNDYuNyAyLjJjLTEuOC4yLTMuMi40LTQgLjctMi4xLjYtMy42IDEuNi0zLjYgMS42LTEuNS45LTIuNiA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0NS4wNDUtLjQ2Ny4zNDMtLjQ2N3ptLTEuMDg3IDUuNTM1LjA3LS41MDQtLjQ1OS4zNTFxLS4yOTguMjIzLS4zMDIuNDIyIDAgLjA3LjAyNS4xMzIuMDcuMTgyLjI2NS4xODIuMDc4IDAgLjE0LS4wMjUuMTk5LS4wODcuMjYtLjU1OHptLjg5Ny02LjMyNXEwLS4wNzQuMDM3LS4xNC4wODctLjE2Mi4yNjktLjE2Mi4wNzQgMCAuMTQ0LjAzNy4xNTguMDg3LjE1OC4yNjUgMCAuMDc0LS4wMzguMTQ1LS4wODYuMTU3LS4yNjQuMTU3LS4wNzUgMC0uMTQ1LS4wMzgtLjE2MS0uMDgyLS4xNjEtLjI2NHoiIGFyaWEtbGFiZWw9ImoiIHN0eWxlPSJmaWxsOiMxYTFhMWE7c3Ryb2tlLXdpZHRoOi4yNjQ1ODMzNSIgdHJhbnNmb3JtPSJ0cmFuc2xhdGUoLTc2LjQwOSAtNzkuNDczKSIvPjwvZz48L2c+PC9nPjwvc3ZnPiA=
186
+ [license-badge]: https://img.shields.io/badge/Content-CC%20BY--SA%204.0-lightgrey.svg
187
+ [apache-badge]: https://img.shields.io/badge/Code-Apache%202.0-blue.svg
188
+ [license-url]: https://github.com/decitre/divelet-ml/blob/main/CONTENT-LICENSE.md
189
+ [d2l-site]: https://d2l.ai/
190
+ [jupyterlite]: https://jupyterlite.readthedocs.io/
191
+ [d2l-1]: https://d2l.ai/chapter_introduction/index.html
192
+ [content]: https://github.com/decitre/divelet-ml/tree/main/content
193
+ [xeus-2.1]: https://decitre.github.io/divelet-ml/xeus/notebooks/index.html?path=02_preliminaries/02.01_ndarray.ipynb
194
+ [pyodide-2.1]: https://decitre.github.io/divelet-ml/pyodide/notebooks/index.html?path=02_preliminaries/02.01_ndarray.ipynb
195
+ [d2l-2.1]: https://d2l.ai/chapter_preliminaries/ndarray.html
196
+ [xeus-3.3]: https://decitre.github.io/divelet-ml/xeus/notebooks/index.html?path=03_linear-regression/03.03_synthetic-regression-data.ipynb
197
+ [pyodide-3.3]: https://decitre.github.io/divelet-ml/pyodide/notebooks/index.html?path=03_linear-regression/03.03_synthetic-regression-data.ipynb
198
+ [d2l-3.3]: https://d2l.ai/chapter_linear-regression/synthetic-regression-data.html
199
+ [transformers-js]: https://huggingface.co/docs/transformers.js/
200
+ [lmlite]: https://github.com/decitre/lmlite
201
+ [roadmap]: https://github.com/decitre/divelet-ml/blob/main/ROADMAP.md
202
+ [d2l-repo]: https://github.com/d2l-ai/d2l-en
203
+ [cc-by-sa]: https://creativecommons.org/licenses/by-sa/4.0/
204
+ [notice]: https://github.com/decitre/divelet-ml/blob/main/NOTICE.md
205
+ [issues]: https://github.com/decitre/divelet-ml/issues
206
+ [contributing]: https://github.com/decitre/divelet-ml/blob/main/CONTRIBUTING.md
207
+ [apache]: https://github.com/decitre/divelet-ml/blob/main/LICENSE
@@ -0,0 +1,192 @@
1
+ <img src="https://raw.githubusercontent.com/decitre/divelet-ml/main/divelet-ml.svg"
2
+ alt="Divelet ML — Deep learning, in your browser"
3
+ width="600">
4
+
5
+ [![Build site][ci-badge]][ci-url]
6
+ [![Coverage][codecov-badge]][codecov-url]
7
+ [![Try xeus-python][xeus-badge]][xeus-tree]
8
+ [![Try Pyodide][pyodide-badge]][pyodide-tree]
9
+ [![Content: CC BY-SA 4.0][license-badge]][license-url]
10
+ [![Code: Apache 2.0][apache-badge]][apache]
11
+
12
+ # divelet-ml
13
+
14
+ **divelet for ML: Dive into Deep Learning, adapted for JupyterLite.**
15
+
16
+ divelet-ml is an experimental, browser-native adaptation of the Jupyter notebooks that accompany the book [*Dive into Deep Learning*][d2l-site] ("the book" below), designed to make its educational content and interactive examples accessible through [JupyterLite][jupyterlite] without requiring a local Python installation or a remote computation server.
17
+
18
+ The project aims to preserve the book's chapter structure, mathematical explanations, learning objectives, and exercises while adapting its framework-dependent Python implementations for execution directly in the browser.
19
+
20
+ **Project status:** Early development. Compatibility assessment is underway. Adapted sections run in the browser with no installation. A section link opens its notebook with the xeus-python kernel; **[p]** opens it with Pyodide instead.
21
+
22
+ | Section | Original |
23
+ |---|---|
24
+ | 1 Introduction (no code) | [d2l.ai][d2l-1] |
25
+ | [2.1 Data Manipulation][xeus-2.1] [\[p\]][pyodide-2.1] | [d2l.ai][d2l-2.1] |
26
+ | [3.3 Synthetic Regression Data][xeus-3.3] [\[p\]][pyodide-3.3] | [d2l.ai][d2l-3.3] |
27
+
28
+ The notebooks' sources are in [`content/`][content].
29
+
30
+ Build the site locally with `./build.sh` (needs uv and micromamba).
31
+
32
+ ## Python package
33
+
34
+ The notebooks use `divelet_ml`, a small NumPy counterpart of the book's `d2l` library. The site's browser kernels already include it. To use it elsewhere:
35
+
36
+ ```bash
37
+ pip install divelet-ml
38
+ ```
39
+
40
+ ```python
41
+ import divelet_ml as d2l
42
+ ```
43
+
44
+ It provides `add_to_class`, `HyperParameters` and `DataModule`, adapted from the book's `d2l/torch.py`, and `ArrayLoader`, which yields minibatches of NumPy arrays in place of PyTorch's `DataLoader`. The package grows only as adapted chapters need more of it. Its code is licensed under the Apache License 2.0; the notebooks are not part of the package.
45
+
46
+ ## Motivation
47
+
48
+ Dive into Deep Learning is an open-source, interactive textbook combining mathematical explanations with executable code.
49
+
50
+ Its implementations rely primarily on deep learning frameworks such as PyTorch, TensorFlow, JAX, and MXNet. These environments can be difficult to reproduce in a browser-only Python runtime.
51
+
52
+ divelet-ml explores whether the same educational progression can be maintained using lightweight, browser-compatible implementations.
53
+
54
+ The objective is not to reproduce the entire deep learning framework ecosystem in the browser, but to make the underlying concepts accessible through executable, pedagogically faithful examples.
55
+
56
+ ## Design Principles
57
+
58
+ - **Educational fidelity:** Preserve the book's explanations, mathematical formulations, learning objectives, and exercises wherever possible.
59
+ - **Browser-native execution:** Run examples entirely in the browser without requiring a remote Python kernel.
60
+ - **NumPy first:** Prefer NumPy implementations when they adequately illustrate the underlying concepts.
61
+ - **Framework independence:** Avoid unnecessary dependencies on full deep learning frameworks.
62
+ - **Progressive adaptation:** Adapt chapters individually, prioritizing educational value and technical feasibility.
63
+ - **Reproducibility:** Document runtime requirements, implementation differences, and compatibility limitations.
64
+
65
+ ## Technical Approach
66
+
67
+ ### JupyterLite
68
+
69
+ divelet-ml targets JupyterLite with two Python execution environments:
70
+
71
+ - **Pyodide:** Python compiled to WebAssembly, with access to a browser-compatible scientific Python ecosystem.
72
+ - **xeus-python:** A browser-native Python kernel built using WebAssembly.
73
+
74
+ Compatibility will be evaluated independently for each environment.
75
+
76
+ ### NumPy and Deep Learning
77
+
78
+ Where practical, framework-specific operations will be replaced with NumPy equivalents.
79
+
80
+ This includes tensor manipulation, linear algebra, probability, optimization, and selected neural network implementations.
81
+
82
+ Some chapters may require more substantial adaptation, particularly those involving automatic differentiation, GPU acceleration, large datasets, or computationally expensive training.
83
+
84
+ The goal is to preserve the concepts being taught rather than provide identical framework APIs.
85
+
86
+ ### Transformers.js and lmLite
87
+
88
+ For selected chapters involving modern language models or inference, divelet-ml may explore integration with [Transformers.js][transformers-js] through [lmLite][lmlite].
89
+
90
+ lmLite provides Python access to browser-native machine learning inference.
91
+
92
+ This integration is optional and is not intended to replace the mathematical and algorithmic foundations of the book.
93
+
94
+ ## Compatibility Assessment
95
+
96
+ An initial static audit of the book's source repository has been developed to identify:
97
+
98
+ - Framework-specific implementations and dependencies.
99
+ - Candidate chapters for direct adaptation.
100
+ - Potential incompatibilities with browser-native Python runtimes.
101
+ - Estimated adaptation effort.
102
+ - Opportunities for alternative browser-native implementations.
103
+
104
+ The audit distinguishes static compatibility estimates from actual execution results.
105
+
106
+ **Static analysis does not establish that a chapter runs successfully in JupyterLite.** Browser execution and educational fidelity must be validated separately.
107
+
108
+ The initial audit references the book's commit:
109
+
110
+ `23d7a5aecceee57d1292c56e90cce307f183bb0a`
111
+
112
+ ## Initial Development Plan
113
+
114
+ The first phase focuses on three representative sections of the book:
115
+
116
+ | Section | Focus | Status |
117
+ |---|---|---|
118
+ | 2.1 `chapter_preliminaries/ndarray.md` | Array operations and tensor fundamentals | Adapted |
119
+ | 3.3 `chapter_linear-regression/synthetic-regression-data.md` | Synthetic datasets and numerical computation | Adapted |
120
+ | 11.3 `chapter_attention-mechanisms-and-transformers/attention-scoring-functions.md` | Attention mechanisms and scoring functions | Planned |
121
+
122
+ The PyTorch implementations serve as the primary reference.
123
+
124
+ Each adaptation will be reviewed for educational fidelity, browser compatibility, dependencies, and reproducibility.
125
+
126
+ The project will progressively expand to additional chapters based on the results of these initial experiments.
127
+
128
+ See [ROADMAP.md][roadmap] for further details.
129
+
130
+ ## Relationship to Dive into Deep Learning
131
+
132
+ divelet-ml is an independent adaptation of the original [Dive into Deep Learning][d2l-repo] project.
133
+
134
+ It is not an official edition of the book and is not affiliated with or endorsed by the original authors.
135
+
136
+ The project intends to preserve the educational contributions of the book while adapting selected implementations for browser-native execution.
137
+
138
+ Adapted chapters will identify their original sources and document substantive modifications.
139
+
140
+ ## License and Attribution
141
+
142
+ The original Dive into Deep Learning book content is distributed under the [Creative Commons Attribution-ShareAlike 4.0 International License][cc-by-sa].
143
+
144
+ Adapted educational content derived from the book will retain the applicable attribution and ShareAlike requirements.
145
+
146
+ divelet-ml's own source code (the `divelet_ml` package, build scripts and tests) is licensed under the [Apache License 2.0][apache]. Third-party materials may be subject to separate licenses; these will be reviewed and their required notices preserved before redistribution.
147
+
148
+ See [CONTENT-LICENSE.md][license-url] and [NOTICE.md][notice] for licensing and attribution details.
149
+
150
+ ## Feedback and Contributions
151
+
152
+ divelet-ml is an experimental project exploring browser-native deep learning education.
153
+
154
+ Feedback is particularly welcome on:
155
+
156
+ - The educational usefulness of framework-independent implementations.
157
+ - The fidelity of NumPy adaptations to the book's original examples.
158
+ - JupyterLite compatibility, performance, and runtime limitations.
159
+ - Opportunities for browser-native machine learning beyond traditional Python frameworks.
160
+
161
+ Please share suggestions, bug reports, and use cases through [GitHub Issues][issues]. To set up a development environment, run the tests or make a release, see [CONTRIBUTING.md][contributing].
162
+
163
+ [ci-badge]: https://github.com/decitre/divelet-ml/actions/workflows/build.yml/badge.svg
164
+ [ci-url]: https://github.com/decitre/divelet-ml/actions/workflows/build.yml
165
+ [codecov-badge]: https://img.shields.io/codecov/c/github/decitre/divelet-ml/main?flag=python&label=Coverage&logo=codecov
166
+ [codecov-url]: https://codecov.io/gh/decitre/divelet-ml
167
+ [xeus-tree]: https://decitre.github.io/divelet-ml/xeus/tree/
168
+ [pyodide-tree]: https://decitre.github.io/divelet-ml/pyodide/tree/
169
+ [xeus-badge]: https://img.shields.io/badge/try-xeus--python-f7dc1e.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA0Ni43IDQ2LjciPgogIDxwYXRoCiAgICBmaWxsPSIjNDBCM0MzIgogICAgZD0iTTUgMGMyLjUuOCA0LjUgMiA0LjYgMi4xIDEuOSAxLjMgMy42IDMuMSA0LjggNS4xLjQuNiA1LjIgNy44IDguNiAxMi45LS44IDEuMi0xLjUgMi4zLTIuMSAzLjEgMC0uMS0uMS0uMS0uMS0uMkMxNy42IDE3LjkgMTEuNCA4LjYgMTEgNy45Yy0uOS0xLjMtMi0yLjUtMy41LTMuNCAwIDAtMS41LTEtMy41LTEuNkMzLjIgMi42IDEuOCAyLjQgMCAyLjJ2NDIuNGMxLjgtLjIgMy4yLS40IDQtLjcgMi0uNiAzLjUtMS42IDMuNS0xLjYgMS41LTEgMi43LTIuMiAzLjUtMy40LjQtLjcgNi42LTkuOSAxMC4xLTE1LjJDMjQgMTkuMSAzMS40IDguMSAzMS45IDcuM2MxLjItMiAyLjktMy44IDQuOC01LjEgMC0uMSAyLjEtMS4zIDQuNi0yLjFMNDEuOCAwSDV6CiAgICAgICBNNDYuNyAyLjJjLTEuOC4yLTMuMi40LTQgLjctMi4xLjYtMy42IDEuNi0zLjYgMS42LTEuNS45LTIuNiAyLjEtMy41IDMuNC0uNC43LTYuNiA5LjktMTAuMSAxNS4yLTIuOSA0LjQ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S41MDQtLjQ1OS4zNTFxLS4yOTguMjIzLS4zMDIuNDIyIDAgLjA3LjAyNS4xMzIuMDcuMTgyLjI2NS4xODIuMDc4IDAgLjE0LS4wMjUuMTk5LS4wODcuMjYtLjU1OHptLjg5Ny02LjMyNXEwLS4wNzQuMDM3LS4xNC4wODctLjE2Mi4yNjktLjE2Mi4wNzQgMCAuMTQ0LjAzNy4xNTguMDg3LjE1OC4yNjUgMCAuMDc0LS4wMzguMTQ1LS4wODYuMTU3LS4yNjQuMTU3LS4wNzUgMC0uMTQ1LS4wMzgtLjE2MS0uMDgyLS4xNjEtLjI2NHoiIGFyaWEtbGFiZWw9ImoiIHN0eWxlPSJmaWxsOiMxYTFhMWE7c3Ryb2tlLXdpZHRoOi4yNjQ1ODMzNSIgdHJhbnNmb3JtPSJ0cmFuc2xhdGUoLTc2LjQwOSAtNzkuNDczKSIvPjwvZz48L2c+PC9nPjwvc3ZnPiA=
171
+ [license-badge]: https://img.shields.io/badge/Content-CC%20BY--SA%204.0-lightgrey.svg
172
+ [apache-badge]: https://img.shields.io/badge/Code-Apache%202.0-blue.svg
173
+ [license-url]: https://github.com/decitre/divelet-ml/blob/main/CONTENT-LICENSE.md
174
+ [d2l-site]: https://d2l.ai/
175
+ [jupyterlite]: https://jupyterlite.readthedocs.io/
176
+ [d2l-1]: https://d2l.ai/chapter_introduction/index.html
177
+ [content]: https://github.com/decitre/divelet-ml/tree/main/content
178
+ [xeus-2.1]: https://decitre.github.io/divelet-ml/xeus/notebooks/index.html?path=02_preliminaries/02.01_ndarray.ipynb
179
+ [pyodide-2.1]: https://decitre.github.io/divelet-ml/pyodide/notebooks/index.html?path=02_preliminaries/02.01_ndarray.ipynb
180
+ [d2l-2.1]: https://d2l.ai/chapter_preliminaries/ndarray.html
181
+ [xeus-3.3]: https://decitre.github.io/divelet-ml/xeus/notebooks/index.html?path=03_linear-regression/03.03_synthetic-regression-data.ipynb
182
+ [pyodide-3.3]: https://decitre.github.io/divelet-ml/pyodide/notebooks/index.html?path=03_linear-regression/03.03_synthetic-regression-data.ipynb
183
+ [d2l-3.3]: https://d2l.ai/chapter_linear-regression/synthetic-regression-data.html
184
+ [transformers-js]: https://huggingface.co/docs/transformers.js/
185
+ [lmlite]: https://github.com/decitre/lmlite
186
+ [roadmap]: https://github.com/decitre/divelet-ml/blob/main/ROADMAP.md
187
+ [d2l-repo]: https://github.com/d2l-ai/d2l-en
188
+ [cc-by-sa]: https://creativecommons.org/licenses/by-sa/4.0/
189
+ [notice]: https://github.com/decitre/divelet-ml/blob/main/NOTICE.md
190
+ [issues]: https://github.com/decitre/divelet-ml/issues
191
+ [contributing]: https://github.com/decitre/divelet-ml/blob/main/CONTRIBUTING.md
192
+ [apache]: https://github.com/decitre/divelet-ml/blob/main/LICENSE
@@ -0,0 +1,45 @@
1
+ [build-system]
2
+ requires = ["hatchling>=1.27,<2"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "divelet-ml"
7
+ dynamic = ["version"]
8
+ description = "Dive into Deep Learning, adapted for JupyterLite"
9
+ readme = "README.md"
10
+ license = "Apache-2.0" # code; content adapted from Dive into Deep Learning is CC BY-SA 4.0 (CONTENT-LICENSE.md)
11
+ # Pyodide kernel 0.7.0 runs Python 3.13. The browser kernels get the divelet_ml package and [project.dependencies].
12
+ requires-python = ">=3.11"
13
+ authors = [{ name = "decitre" }]
14
+ dependencies = ["numpy"]
15
+
16
+ [project.urls]
17
+ Homepage = "https://github.com/decitre/divelet-ml"
18
+ Documentation = "https://decitre.github.io/divelet-ml/"
19
+ Issues = "https://github.com/decitre/divelet-ml/issues"
20
+
21
+ [dependency-groups]
22
+ dev = ["bumpver>=2024"]
23
+ browser = ["pytest>=8", "playwright>=1.50"]
24
+ test = ["pytest>=8", "pytest-cov>=5", "nbclient>=0.10", "ipykernel>=6"]
25
+
26
+ [tool.hatch.version]
27
+ path = "src/divelet_ml/__init__.py"
28
+
29
+ [tool.hatch.build.targets.sdist]
30
+ include = ["/src", "/tests", "/LICENSE", "/NOTICE.md", "/CONTENT-LICENSE.md"]
31
+
32
+ [tool.coverage.run]
33
+ source = ["src"]
34
+
35
+ [tool.bumpver]
36
+ current_version = "0.1.0"
37
+ version_pattern = "MAJOR.MINOR.PATCH[PYTAGNUM]"
38
+ commit_message = "bump version {old_version} -> {new_version}"
39
+ commit = true
40
+ tag = true
41
+ push = true
42
+
43
+ [tool.bumpver.file_patterns]
44
+ "pyproject.toml" = ['^current_version = "{version}"']
45
+ "src/divelet_ml/__init__.py" = ['^__version__ = "{version}"']
@@ -0,0 +1,70 @@
1
+ """divelet-ml's NumPy counterpart of the `d2l` library, imported as `import divelet_ml as d2l`.
2
+
3
+ `add_to_class`, `HyperParameters` and `DataModule` are adapted from `d2l/torch.py` in Dive into Deep Learning
4
+ (https://github.com/d2l-ai/d2l-en, commit 23d7a5aecceee57d1292c56e90cce307f183bb0a),
5
+ Copyright 2019 Amazon.com, Inc. or its affiliates, MIT-0 license (LICENSE-SAMPLECODE).
6
+ Only what adapted chapters use is here.
7
+ """
8
+
9
+ __version__ = "0.1.0"
10
+
11
+ import inspect
12
+
13
+ import numpy as np
14
+
15
+
16
+ def add_to_class(Class):
17
+ """Register functions as methods in created class."""
18
+ def wrapper(obj):
19
+ setattr(Class, obj.__name__, obj)
20
+ return wrapper
21
+
22
+
23
+ class HyperParameters:
24
+ """The base class of hyperparameters."""
25
+ def save_hyperparameters(self, ignore=[]):
26
+ """Save function arguments into class attributes."""
27
+ frame = inspect.currentframe().f_back
28
+ _, _, _, local_vars = inspect.getargvalues(frame)
29
+ self.hparams = {k: v for k, v in local_vars.items()
30
+ if k not in set(ignore + ['self']) and not k.startswith('_')}
31
+ for k, v in self.hparams.items():
32
+ setattr(self, k, v)
33
+
34
+
35
+ class DataModule(HyperParameters):
36
+ """The base class of data."""
37
+ def __init__(self, root='../data', num_workers=4):
38
+ self.save_hyperparameters()
39
+
40
+ def get_dataloader(self, train):
41
+ raise NotImplementedError
42
+
43
+ def train_dataloader(self):
44
+ return self.get_dataloader(train=True)
45
+
46
+ def val_dataloader(self):
47
+ return self.get_dataloader(train=False)
48
+
49
+
50
+ class ArrayLoader:
51
+ """Minibatches of NumPy arrays, in place of a framework's data loader such as PyTorch's `DataLoader`.
52
+
53
+ Iterating yields tuples of `batch_size` rows of each array. The last batch is smaller when
54
+ `batch_size` doesn't divide the number of rows, or left out with `drop_last=True`.
55
+ With `shuffle=True`, the rows are in a new random order on every pass.
56
+ """
57
+ def __init__(self, arrays, batch_size, shuffle=False, drop_last=False):
58
+ assert len({len(a) for a in arrays}) == 1, 'arrays must have the same number of rows'
59
+ self.arrays, self.batch_size, self.shuffle, self.drop_last = arrays, batch_size, shuffle, drop_last
60
+
61
+ def __len__(self):
62
+ n = len(self.arrays[0])
63
+ return n // self.batch_size if self.drop_last else -(-n // self.batch_size)
64
+
65
+ def __iter__(self):
66
+ n = len(self.arrays[0])
67
+ order = np.random.permutation(n) if self.shuffle else np.arange(n)
68
+ for i in range(0, len(self) * self.batch_size, self.batch_size):
69
+ batch = order[i:i + self.batch_size]
70
+ yield tuple(a[batch] for a in self.arrays)
@@ -0,0 +1,120 @@
1
+ """Browser smoke test: every notebook in content/ runs in each kernel and app, with no error output
2
+ and nothing fetched from other hosts.
3
+
4
+ Each case runs in desktop Google Chrome and in WebKit (Safari's engine, which every iPad browser uses)
5
+ emulating an iPad in portrait. The emulation covers the engine, screen size and touch, not a real
6
+ tablet's memory limits.
7
+
8
+ Run after build.sh, from the repository root (needs Google Chrome; install WebKit once with
9
+ `uv run --group browser playwright install webkit`):
10
+
11
+ uv run --group browser pytest tests/smoke_test.py -v
12
+
13
+ Adapted from jl-forge's examples/demo/smoke_test.py.
14
+ """
15
+
16
+ import re
17
+ import threading
18
+ from functools import partial
19
+ from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
20
+ from pathlib import Path
21
+ from urllib.parse import urlparse
22
+
23
+ import pytest
24
+ from playwright.sync_api import TimeoutError as PlaywrightTimeoutError
25
+ from playwright.sync_api import sync_playwright
26
+
27
+ ROOT = Path(__file__).parent.parent
28
+ PUBLIC = ROOT / "workspace" / "public"
29
+ FAILURES = ROOT / "workspace" / "smoke-failures" # screenshots of notebooks that did not finish
30
+ NOTEBOOKS = sorted(p.relative_to(ROOT / "content").as_posix() for p in (ROOT / "content").rglob("*.ipynb"))
31
+ KERNEL_START_MS = 240_000 # first load downloads the kernel and its packages
32
+ RUN_MS = 120_000
33
+ # The kernel name each site's toolbar shows: catches a site built with the other kernel.
34
+ KERNEL_NAMES = {"xeus": "(XPython)", "pyodide": "(Pyodide)"}
35
+ IPAD = "iPad (gen 11)"
36
+ # The toolbar's execution indicator reads "idle" before any kernel exists, then "initializing" or "unknown"
37
+ # while the kernel starts, then "idle" again once it is ready: wait for that second "idle".
38
+ KERNEL_READY_JS = """() => {
39
+ const status = document.querySelector('.jp-Notebook-ExecutionIndicator')?.dataset.status;
40
+ if (status && status !== 'idle') window.__kernelStarting = true;
41
+ return window.__kernelStarting === true && status === 'idle';
42
+ }"""
43
+ CASES = [(b, site, app, nb) for b in ("chrome", "ipad") for site in ("xeus", "pyodide")
44
+ for app in ("lab", "notebooks") for nb in NOTEBOOKS]
45
+
46
+
47
+ class _QuietHandler(SimpleHTTPRequestHandler):
48
+ def log_message(self, *args):
49
+ pass
50
+
51
+
52
+ @pytest.fixture(scope="module")
53
+ def base_url():
54
+ if not (PUBLIC / "index.html").is_file():
55
+ pytest.skip(f"{PUBLIC} not built; run ./build.sh first")
56
+ server = ThreadingHTTPServer(("127.0.0.1", 0), partial(_QuietHandler, directory=PUBLIC))
57
+ threading.Thread(target=server.serve_forever, daemon=True).start()
58
+ yield f"http://127.0.0.1:{server.server_port}"
59
+ server.shutdown()
60
+
61
+
62
+ @pytest.fixture(scope="module")
63
+ def new_context():
64
+ """new_context(name): a fresh browser context, launching each browser on first use."""
65
+ with sync_playwright() as p:
66
+ browsers = {}
67
+
68
+ def new_context(name):
69
+ if name not in browsers:
70
+ browsers[name] = p.webkit.launch() if name == "ipad" else p.chromium.launch(channel="chrome")
71
+ return browsers[name].new_context(**(p.devices[IPAD] if name == "ipad" else {}))
72
+
73
+ yield new_context
74
+ for browser in browsers.values():
75
+ browser.close()
76
+
77
+
78
+ @pytest.mark.parametrize(("browser", "site", "app", "notebook"), CASES,
79
+ ids=[f"{b}-{s}-{a}-{Path(n).stem}" for b, s, a, n in CASES])
80
+ def test_notebook(new_context, base_url, browser, site, app, notebook):
81
+ context = new_context(browser)
82
+ page = context.new_page()
83
+ responses, console_errors, page_errors = [], [], []
84
+ page.on("response", lambda r: responses.append((r.url, r.status)))
85
+ page.on("pageerror", lambda e: page_errors.append(str(e)))
86
+ # The kernel's own fetches run in a web worker; their failures show up here, not always as responses.
87
+ # The browser's own /favicon.ico request at the server root is outside the site.
88
+ page.on("console", lambda m: console_errors.append(m.text)
89
+ if m.type == "error" and not m.location.get("url", "").endswith("/favicon.ico") else None)
90
+ page.goto(f"{base_url}/{site}/{app}/index.html?path={notebook}")
91
+ page.wait_for_selector(".jp-CodeCell", timeout=KERNEL_START_MS)
92
+ # Run All while the kernel is still starting can lose the session in WebKit: nothing runs, or Lab
93
+ # shows a "Select Kernel" dialog. A tablet user who taps Run All right away sees the same.
94
+ page.wait_for_function(KERNEL_READY_JS, timeout=KERNEL_START_MS, polling=100)
95
+ page.click("li.lm-MenuBar-item:has-text('Run')")
96
+ page.click("li.lm-Menu-item:has-text('Run All Cells')")
97
+
98
+ # Run All stops at the first error, so the last code cell showing "[n]:" (not "[ ]:" or "[*]:")
99
+ # means every cell ran. Off-screen prompts can render empty, so don't check them all.
100
+ # Stop waiting at the first error output instead of timing out.
101
+ errors = page.locator(".jp-OutputArea-output[data-mime-type='application/vnd.jupyter.stderr']")
102
+ last_ran = page.locator(".jp-CodeCell .jp-InputPrompt").last.filter(has_text=re.compile(r"\[\d+\]"))
103
+ try:
104
+ last_ran.or_(errors.first).wait_for(timeout=RUN_MS)
105
+ except PlaywrightTimeoutError:
106
+ FAILURES.mkdir(parents=True, exist_ok=True)
107
+ shot = FAILURES / f"{browser}-{site}-{app}-{Path(notebook).stem}.png"
108
+ page.screenshot(path=shot)
109
+ prompts = page.locator(".jp-CodeCell .jp-InputPrompt").all_inner_texts()
110
+ status = page.locator(".jp-Notebook-ExecutionIndicator").first.get_attribute("data-status")
111
+ pytest.fail(f"cells did not finish: prompts {prompts}, kernel {status}, page errors {page_errors}, "
112
+ f"console errors {console_errors}, screenshot {shot}")
113
+ assert errors.count() == 0, errors.all_inner_texts()
114
+ assert KERNEL_NAMES[site] in page.locator(".jp-Toolbar-kernelName").first.inner_text()
115
+
116
+ # Self-hosted: no other host, and no file missing from the site (how a pruned Pyodide package shows up).
117
+ assert {urlparse(u).netloc for u, _ in responses if not u.startswith(base_url)} == set()
118
+ assert not [(u, s) for u, s in responses if u.startswith(base_url) and s >= 400]
119
+ assert not console_errors
120
+ context.close()
@@ -0,0 +1,53 @@
1
+ import numpy as np
2
+ import pytest
3
+
4
+ import divelet_ml as d2l
5
+
6
+
7
+ def test_array_loader_covers_every_row_once():
8
+ X, y = np.arange(10).reshape(5, 2), np.arange(5)
9
+ for shuffle in (False, True):
10
+ loader = d2l.ArrayLoader((X, y), batch_size=2, shuffle=shuffle)
11
+ batches = list(loader)
12
+ assert len(loader) == len(batches) == 3 and len(batches[-1][0]) == 1
13
+ rows = np.concatenate([b[1] for b in batches])
14
+ assert sorted(rows) == list(range(5))
15
+ assert all((bX[:, 0] // 2 == by).all() for bX, by in batches) # X and y rows stay paired
16
+ assert [b[1].tolist() for b in d2l.ArrayLoader((X, y), 2)] == [[0, 1], [2, 3], [4]]
17
+ dropped = d2l.ArrayLoader((X, y), 2, drop_last=True)
18
+ assert len(dropped) == 2 and [b[1].tolist() for b in dropped] == [[0, 1], [2, 3]]
19
+
20
+
21
+ def test_save_hyperparameters():
22
+ class Data(d2l.DataModule):
23
+ def __init__(self, a, b=2, _hidden=3):
24
+ super().__init__()
25
+ self.save_hyperparameters()
26
+
27
+ data = Data(1)
28
+ assert (data.a, data.b) == (1, 2) and not hasattr(data, '_hidden')
29
+ assert data.hparams == {'a': 1, 'b': 2}
30
+
31
+
32
+ def test_add_to_class():
33
+ class A:
34
+ pass
35
+
36
+ @d2l.add_to_class(A)
37
+ def double(self, x):
38
+ return 2 * x
39
+
40
+ assert A().double(3) == 6
41
+
42
+
43
+ def test_data_module_dataloaders():
44
+ data = d2l.DataModule()
45
+ assert data.hparams == {'root': '../data', 'num_workers': 4}
46
+ with pytest.raises(NotImplementedError):
47
+ data.train_dataloader()
48
+
49
+ class Data(d2l.DataModule):
50
+ def get_dataloader(self, train):
51
+ return 'train' if train else 'val'
52
+
53
+ assert (Data().train_dataloader(), Data().val_dataloader()) == ('train', 'val')
@@ -0,0 +1,22 @@
1
+ """Every notebook in content/ runs in CPython without errors, warnings or other stderr output.
2
+
3
+ uv run --group test pytest tests/test_notebooks.py
4
+
5
+ A fast check before the browser smoke test; it does not show that a notebook runs in JupyterLite.
6
+ """
7
+
8
+ from pathlib import Path
9
+
10
+ import nbformat
11
+ import pytest
12
+ from nbclient import NotebookClient
13
+
14
+ NOTEBOOKS = sorted((Path(__file__).parent.parent / "content").rglob("*.ipynb"))
15
+
16
+
17
+ @pytest.mark.parametrize("path", NOTEBOOKS, ids=lambda p: p.stem)
18
+ def test_notebook_runs(path):
19
+ nb = nbformat.read(path, as_version=4)
20
+ NotebookClient(nb, timeout=120, kernel_name="python3").execute()
21
+ stderr = [o["text"] for c in nb.cells if c.cell_type == "code" for o in c.outputs if o.get("name") == "stderr"]
22
+ assert not stderr