mlx-learn 0.1.0a1__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.
Files changed (44) hide show
  1. mlx_learn-0.1.0a1/LICENSE +202 -0
  2. mlx_learn-0.1.0a1/PKG-INFO +226 -0
  3. mlx_learn-0.1.0a1/README.md +182 -0
  4. mlx_learn-0.1.0a1/THIRD_PARTY_NOTICES.md +167 -0
  5. mlx_learn-0.1.0a1/pyproject.toml +81 -0
  6. mlx_learn-0.1.0a1/setup.cfg +4 -0
  7. mlx_learn-0.1.0a1/src/mlx_learn.egg-info/PKG-INFO +226 -0
  8. mlx_learn-0.1.0a1/src/mlx_learn.egg-info/SOURCES.txt +42 -0
  9. mlx_learn-0.1.0a1/src/mlx_learn.egg-info/dependency_links.txt +1 -0
  10. mlx_learn-0.1.0a1/src/mlx_learn.egg-info/not-zip-safe +1 -0
  11. mlx_learn-0.1.0a1/src/mlx_learn.egg-info/requires.txt +19 -0
  12. mlx_learn-0.1.0a1/src/mlx_learn.egg-info/top_level.txt +1 -0
  13. mlx_learn-0.1.0a1/src/mlxlearn/__init__.py +99 -0
  14. mlx_learn-0.1.0a1/src/mlxlearn/_common/__init__.py +66 -0
  15. mlx_learn-0.1.0a1/src/mlxlearn/_common/arrays.py +167 -0
  16. mlx_learn-0.1.0a1/src/mlxlearn/_common/base.py +328 -0
  17. mlx_learn-0.1.0a1/src/mlxlearn/_common/config.py +324 -0
  18. mlx_learn-0.1.0a1/src/mlxlearn/_common/device.py +180 -0
  19. mlx_learn-0.1.0a1/src/mlxlearn/_common/diagnostics.py +210 -0
  20. mlx_learn-0.1.0a1/src/mlxlearn/_common/rng.py +97 -0
  21. mlx_learn-0.1.0a1/src/mlxlearn/_common/validation.py +249 -0
  22. mlx_learn-0.1.0a1/src/mlxlearn/_kernels/__init__.py +11 -0
  23. mlx_learn-0.1.0a1/src/mlxlearn/_kernels/distance.py +417 -0
  24. mlx_learn-0.1.0a1/src/mlxlearn/_kernels/kernels.py +497 -0
  25. mlx_learn-0.1.0a1/src/mlxlearn/_solvers/__init__.py +12 -0
  26. mlx_learn-0.1.0a1/src/mlxlearn/_solvers/lbfgs.py +497 -0
  27. mlx_learn-0.1.0a1/src/mlxlearn/_solvers/logistic.py +502 -0
  28. mlx_learn-0.1.0a1/src/mlxlearn/_solvers/smo.py +422 -0
  29. mlx_learn-0.1.0a1/src/mlxlearn/exceptions.py +168 -0
  30. mlx_learn-0.1.0a1/src/mlxlearn/linear_model/__init__.py +15 -0
  31. mlx_learn-0.1.0a1/src/mlxlearn/linear_model/_logistic.py +509 -0
  32. mlx_learn-0.1.0a1/src/mlxlearn/neighbors/__init__.py +15 -0
  33. mlx_learn-0.1.0a1/src/mlxlearn/neighbors/_base.py +366 -0
  34. mlx_learn-0.1.0a1/src/mlxlearn/neighbors/_classification.py +130 -0
  35. mlx_learn-0.1.0a1/src/mlxlearn/neighbors/_regression.py +91 -0
  36. mlx_learn-0.1.0a1/src/mlxlearn/neighbors/_unsupervised.py +81 -0
  37. mlx_learn-0.1.0a1/src/mlxlearn/patching/__init__.py +33 -0
  38. mlx_learn-0.1.0a1/src/mlxlearn/patching/_estimators.py +91 -0
  39. mlx_learn-0.1.0a1/src/mlxlearn/patching/_patch.py +251 -0
  40. mlx_learn-0.1.0a1/src/mlxlearn/patching/_registry.py +133 -0
  41. mlx_learn-0.1.0a1/src/mlxlearn/py.typed +0 -0
  42. mlx_learn-0.1.0a1/src/mlxlearn/svm/__init__.py +14 -0
  43. mlx_learn-0.1.0a1/src/mlxlearn/svm/_classes.py +820 -0
  44. mlx_learn-0.1.0a1/tests/test_sklearn_compliance.py +164 -0
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 Bojan Tunguz
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,226 @@
1
+ Metadata-Version: 2.4
2
+ Name: mlx-learn
3
+ Version: 0.1.0a1
4
+ Summary: MLX-accelerated classical machine learning with a scikit-learn compatible API for Apple silicon
5
+ Author: Bojan Tunguz
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/tabulai/mlxlearn
8
+ Project-URL: Repository, https://github.com/tabulai/mlxlearn
9
+ Project-URL: Changelog, https://github.com/tabulai/mlxlearn/blob/main/CHANGELOG.md
10
+ Project-URL: Issues, https://github.com/tabulai/mlxlearn/issues
11
+ Keywords: machine-learning,mlx,apple-silicon,scikit-learn,gpu
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Operating System :: MacOS :: MacOS X
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
22
+ Classifier: Typing :: Typed
23
+ Requires-Python: >=3.10
24
+ Description-Content-Type: text/markdown
25
+ License-File: LICENSE
26
+ License-File: THIRD_PARTY_NOTICES.md
27
+ Requires-Dist: numpy>=1.23.5
28
+ Requires-Dist: scikit-learn<1.10,>=1.7
29
+ Requires-Dist: mlx<1.0,>=0.29
30
+ Provides-Extra: test
31
+ Requires-Dist: pytest>=8.0; extra == "test"
32
+ Requires-Dist: scipy>=1.10; extra == "test"
33
+ Requires-Dist: packaging>=23.0; extra == "test"
34
+ Provides-Extra: bench
35
+ Requires-Dist: pytest>=8.0; extra == "bench"
36
+ Requires-Dist: scipy>=1.10; extra == "bench"
37
+ Provides-Extra: dev
38
+ Requires-Dist: pytest>=8.0; extra == "dev"
39
+ Requires-Dist: scipy>=1.10; extra == "dev"
40
+ Requires-Dist: ruff>=0.6; extra == "dev"
41
+ Requires-Dist: build>=1.2; extra == "dev"
42
+ Requires-Dist: twine>=5.0; extra == "dev"
43
+ Dynamic: license-file
44
+
45
+ # mlxlearn
46
+
47
+ **Classical machine learning on Apple silicon, accelerated with [MLX](https://github.com/ml-explore/mlx), with a scikit-learn compatible API.**
48
+
49
+ > mlxlearn is not officially associated with scikit-learn or PROBABL, nor with Apple.
50
+
51
+ `mlxlearn` has two layers.
52
+
53
+ **Layer 1 — the library.** Explicit imports, typed solver configuration and state, strict
54
+ errors, eager `fit`, no mutation of scikit-learn, no data-keyed caches:
55
+
56
+ ```python
57
+ from mlxlearn.neighbors import KNeighborsClassifier
58
+
59
+ clf = KNeighborsClassifier(n_neighbors=5).fit(X_train, y_train)
60
+ y_pred = clf.predict(X_test)
61
+ ```
62
+
63
+ **Layer 2 — the patching shell.** A thin adapter that points scikit-learn's own names at
64
+ Layer 1 estimators, so existing code — including code an LLM generated against the
65
+ scikit-learn API — runs accelerated without being edited:
66
+
67
+ ```python
68
+ from mlxlearn import patch_sklearn
69
+ patch_sklearn()
70
+
71
+ from sklearn.svm import SVC # now resolves to the accelerated class
72
+ ```
73
+
74
+ The numerical library is fully usable and fully tested with the patching mechanism absent.
75
+
76
+ ## Status
77
+
78
+ **`0.1.0a1` — alpha.** The API may change. See [CHANGELOG.md](CHANGELOG.md).
79
+
80
+ | Estimator | Status |
81
+ |---|---|
82
+ | `mlxlearn.neighbors.NearestNeighbors` | 0.1.0 |
83
+ | `mlxlearn.neighbors.KNeighborsClassifier` | 0.1.0 |
84
+ | `mlxlearn.neighbors.KNeighborsRegressor` | 0.1.0 |
85
+ | `mlxlearn.linear_model.LogisticRegression` | 0.1.0 |
86
+ | `mlxlearn.svm.SVC` (exact) | 0.1.0 |
87
+ | `LinearRegression`, `Ridge`, `PCA`, `KMeans`, `DBSCAN`, `TSNE` | planned, 0.2.x |
88
+ | `SVR`, `NuSVR`, `NuSVC` | deferred — will not ship until they implement the true objective |
89
+ | tree ensembles | out of scope |
90
+
91
+ ## Requirements
92
+
93
+ - macOS on Apple silicon (arm64)
94
+ - Python 3.10 – 3.13
95
+ - scikit-learn ≥ 1.7, MLX ≥ 0.29
96
+
97
+ Python 3.10 is supported against scikit-learn ≤ 1.8; scikit-learn 1.9 requires Python ≥ 3.11.
98
+ CI tests only valid pairs — see [`docs/support_matrix.md`](docs/support_matrix.md).
99
+
100
+ ## Install
101
+
102
+ ```bash
103
+ pip install mlx-learn
104
+ ```
105
+
106
+ `0.1.0a1` ships as a pure-Python wheel — no compiler, no build step, no ABI matrix.
107
+
108
+ ## Import-order semantics
109
+
110
+ Patching replaces attributes **on scikit-learn modules**. Attribute access
111
+ (`sklearn.svm.SVC`) therefore resolves to the mlxlearn class whether `import sklearn`
112
+ happened before or after `patch_sklearn()`.
113
+
114
+ Symbols captured *before* patching are local bindings and cannot be rebound:
115
+
116
+ ```python
117
+ from sklearn.svm import SVC # binds the stock class into your namespace
118
+ patch_sklearn() # cannot reach back and change SVC
119
+ SVC() # still the stock class
120
+ ```
121
+
122
+ That is Python name binding, not a bug in mlxlearn. **The supported pattern is patch-first.**
123
+ `patch_sklearn()` is idempotent, and `unpatch_sklearn()` fully restores scikit-learn.
124
+
125
+ ## When mlxlearn falls back
126
+
127
+ Layer 1 raises a precise error when it cannot honor a request. Layer 2 falls back to stock
128
+ scikit-learn instead — but only for *capability* mismatches (sparse input, an unsupported
129
+ parameter, an unsupported dtype), never to hide a bug:
130
+
131
+ | Situation | Direct import | Patched |
132
+ |---|---|---|
133
+ | Capability mismatch | precise `MLXLearnError` | falls back, warns once per class, records diagnostics |
134
+ | Problem below the measured crossover | internal CPU path | dispatches to scikit-learn |
135
+ | Invalid user input | sklearn-equivalent validation error | scikit-learn's own exception, unmasked |
136
+ | Unexpected MLX runtime failure | raises | **raises** — never silently rerun on scikit-learn |
137
+
138
+ Set the policy with `fallback_policy="warn" | "raise" | "silent"`. Diagnostics are recorded
139
+ in every mode:
140
+
141
+ ```python
142
+ import mlxlearn
143
+
144
+ with mlxlearn.config_context(fallback_policy="raise"):
145
+ ... # any capability fallback becomes an error
146
+
147
+ mlxlearn.get_last_backend_event() # what the last fit/predict actually did
148
+ mlxlearn.get_backend_diagnostics() # everything recorded this session
149
+ ```
150
+
151
+ **Sticky backend.** The backend is chosen during `fit` and recorded as
152
+ `estimator._execution_backend_` (`"mlx"`, `"cpu"`, or `"sklearn"`). Every subsequent
153
+ `predict` / `predict_proba` / `transform` / `kneighbors` uses the backend the model was
154
+ fitted with. A model fitted on MLX can never wander into scikit-learn inference with
155
+ incompatible state. A new `fit` clears state and may pick a different backend.
156
+
157
+ ## Configuration
158
+
159
+ The public surface is deliberately small:
160
+
161
+ ```python
162
+ mlxlearn.set_config(
163
+ device="auto", # "auto" | "gpu" | "cpu"
164
+ fallback_policy="warn", # "warn" | "raise" | "silent"
165
+ output_type="numpy", # 0.1.0: NumPy in, NumPy out
166
+ deterministic=True,
167
+ random_state=0,
168
+ diagnostics=True,
169
+ )
170
+ ```
171
+
172
+ Crossover thresholds, block sizes, and solver tuning are private, typed, and overridable
173
+ only through `MLXLEARN_*` environment variables for CI and debugging. They are not part of
174
+ the public API.
175
+
176
+ ## Performance
177
+
178
+ "Never slower than scikit-learn" is the design goal. The shipped *gate* is measurable: on
179
+ each benchmarked workload class, patched dispatch shows no statistically significant
180
+ regression against stock scikit-learn. Crossover points are measured per algorithm and per
181
+ operation, published in [`docs/benchmarks.md`](docs/benchmarks.md), and wired into the
182
+ dispatch thresholds — small problems are handed to scikit-learn on purpose.
183
+
184
+ What that means in practice for 0.1.0a1, on an M4 Max:
185
+
186
+ | | |
187
+ |---|---|
188
+ | **Neighbor queries** | **2.9×–17×** from ~250 samples up. This is the reason to use mlxlearn. |
189
+ | Neighbor `fit` | 0.17×–0.36×. mlxlearn uploads to the device; scikit-learn stores a reference. The first query repays it several times over. |
190
+ | `SVC` | **2.93× on wide data** (4 000 × 256, rbf), but slower than LIBSVM on narrow data — 0.47× at 4 000 × 32, worse below. `kernel="linear"` never uses MLX. Width decides, and patched dispatch routes accordingly. |
191
+ | `LogisticRegression` | **Slower than scikit-learn below 1 024 features**, by a lot. The crossover is set high so patched dispatch hands those to scikit-learn; see [`docs/benchmarks.md`](docs/benchmarks.md) for why and what would change it. |
192
+
193
+ Reproduce them:
194
+
195
+ ```bash
196
+ python -m benchmarks.run --profile smoke
197
+ ```
198
+
199
+ ## Precision
200
+
201
+ MLX computes in float32 on the GPU. mlxlearn's parity tests are written to float32
202
+ tolerances, and the cases where float32 makes strict scikit-learn equivalence impossible
203
+ are enumerated — not waved away — in [`docs/fp32_policy.md`](docs/fp32_policy.md).
204
+
205
+ ## Development
206
+
207
+ ```bash
208
+ pip install -e ".[dev]"
209
+ pytest
210
+ ```
211
+
212
+ Contributor rules, the estimator gate checklist, and the compliance checks are in
213
+ [`docs/development.md`](docs/development.md).
214
+
215
+ ## Provenance
216
+
217
+ mlxlearn succeeds a private research fork of an Intel-maintained scikit-learn accelerator.
218
+ That history is not carried here: this repository was bootstrapped from an audited
219
+ allowlist, and no source file was copied. The audit, the recorded behavioral baseline of
220
+ the ancestor, and the authorship attestation are in [`phase0/`](phase0/). Full lineage,
221
+ disclaimers, and third-party notices are in [`ACKNOWLEDGMENTS.md`](ACKNOWLEDGMENTS.md) and
222
+ [`THIRD_PARTY_NOTICES.md`](THIRD_PARTY_NOTICES.md).
223
+
224
+ ## License
225
+
226
+ Apache-2.0. See [LICENSE](LICENSE).
@@ -0,0 +1,182 @@
1
+ # mlxlearn
2
+
3
+ **Classical machine learning on Apple silicon, accelerated with [MLX](https://github.com/ml-explore/mlx), with a scikit-learn compatible API.**
4
+
5
+ > mlxlearn is not officially associated with scikit-learn or PROBABL, nor with Apple.
6
+
7
+ `mlxlearn` has two layers.
8
+
9
+ **Layer 1 — the library.** Explicit imports, typed solver configuration and state, strict
10
+ errors, eager `fit`, no mutation of scikit-learn, no data-keyed caches:
11
+
12
+ ```python
13
+ from mlxlearn.neighbors import KNeighborsClassifier
14
+
15
+ clf = KNeighborsClassifier(n_neighbors=5).fit(X_train, y_train)
16
+ y_pred = clf.predict(X_test)
17
+ ```
18
+
19
+ **Layer 2 — the patching shell.** A thin adapter that points scikit-learn's own names at
20
+ Layer 1 estimators, so existing code — including code an LLM generated against the
21
+ scikit-learn API — runs accelerated without being edited:
22
+
23
+ ```python
24
+ from mlxlearn import patch_sklearn
25
+ patch_sklearn()
26
+
27
+ from sklearn.svm import SVC # now resolves to the accelerated class
28
+ ```
29
+
30
+ The numerical library is fully usable and fully tested with the patching mechanism absent.
31
+
32
+ ## Status
33
+
34
+ **`0.1.0a1` — alpha.** The API may change. See [CHANGELOG.md](CHANGELOG.md).
35
+
36
+ | Estimator | Status |
37
+ |---|---|
38
+ | `mlxlearn.neighbors.NearestNeighbors` | 0.1.0 |
39
+ | `mlxlearn.neighbors.KNeighborsClassifier` | 0.1.0 |
40
+ | `mlxlearn.neighbors.KNeighborsRegressor` | 0.1.0 |
41
+ | `mlxlearn.linear_model.LogisticRegression` | 0.1.0 |
42
+ | `mlxlearn.svm.SVC` (exact) | 0.1.0 |
43
+ | `LinearRegression`, `Ridge`, `PCA`, `KMeans`, `DBSCAN`, `TSNE` | planned, 0.2.x |
44
+ | `SVR`, `NuSVR`, `NuSVC` | deferred — will not ship until they implement the true objective |
45
+ | tree ensembles | out of scope |
46
+
47
+ ## Requirements
48
+
49
+ - macOS on Apple silicon (arm64)
50
+ - Python 3.10 – 3.13
51
+ - scikit-learn ≥ 1.7, MLX ≥ 0.29
52
+
53
+ Python 3.10 is supported against scikit-learn ≤ 1.8; scikit-learn 1.9 requires Python ≥ 3.11.
54
+ CI tests only valid pairs — see [`docs/support_matrix.md`](docs/support_matrix.md).
55
+
56
+ ## Install
57
+
58
+ ```bash
59
+ pip install mlx-learn
60
+ ```
61
+
62
+ `0.1.0a1` ships as a pure-Python wheel — no compiler, no build step, no ABI matrix.
63
+
64
+ ## Import-order semantics
65
+
66
+ Patching replaces attributes **on scikit-learn modules**. Attribute access
67
+ (`sklearn.svm.SVC`) therefore resolves to the mlxlearn class whether `import sklearn`
68
+ happened before or after `patch_sklearn()`.
69
+
70
+ Symbols captured *before* patching are local bindings and cannot be rebound:
71
+
72
+ ```python
73
+ from sklearn.svm import SVC # binds the stock class into your namespace
74
+ patch_sklearn() # cannot reach back and change SVC
75
+ SVC() # still the stock class
76
+ ```
77
+
78
+ That is Python name binding, not a bug in mlxlearn. **The supported pattern is patch-first.**
79
+ `patch_sklearn()` is idempotent, and `unpatch_sklearn()` fully restores scikit-learn.
80
+
81
+ ## When mlxlearn falls back
82
+
83
+ Layer 1 raises a precise error when it cannot honor a request. Layer 2 falls back to stock
84
+ scikit-learn instead — but only for *capability* mismatches (sparse input, an unsupported
85
+ parameter, an unsupported dtype), never to hide a bug:
86
+
87
+ | Situation | Direct import | Patched |
88
+ |---|---|---|
89
+ | Capability mismatch | precise `MLXLearnError` | falls back, warns once per class, records diagnostics |
90
+ | Problem below the measured crossover | internal CPU path | dispatches to scikit-learn |
91
+ | Invalid user input | sklearn-equivalent validation error | scikit-learn's own exception, unmasked |
92
+ | Unexpected MLX runtime failure | raises | **raises** — never silently rerun on scikit-learn |
93
+
94
+ Set the policy with `fallback_policy="warn" | "raise" | "silent"`. Diagnostics are recorded
95
+ in every mode:
96
+
97
+ ```python
98
+ import mlxlearn
99
+
100
+ with mlxlearn.config_context(fallback_policy="raise"):
101
+ ... # any capability fallback becomes an error
102
+
103
+ mlxlearn.get_last_backend_event() # what the last fit/predict actually did
104
+ mlxlearn.get_backend_diagnostics() # everything recorded this session
105
+ ```
106
+
107
+ **Sticky backend.** The backend is chosen during `fit` and recorded as
108
+ `estimator._execution_backend_` (`"mlx"`, `"cpu"`, or `"sklearn"`). Every subsequent
109
+ `predict` / `predict_proba` / `transform` / `kneighbors` uses the backend the model was
110
+ fitted with. A model fitted on MLX can never wander into scikit-learn inference with
111
+ incompatible state. A new `fit` clears state and may pick a different backend.
112
+
113
+ ## Configuration
114
+
115
+ The public surface is deliberately small:
116
+
117
+ ```python
118
+ mlxlearn.set_config(
119
+ device="auto", # "auto" | "gpu" | "cpu"
120
+ fallback_policy="warn", # "warn" | "raise" | "silent"
121
+ output_type="numpy", # 0.1.0: NumPy in, NumPy out
122
+ deterministic=True,
123
+ random_state=0,
124
+ diagnostics=True,
125
+ )
126
+ ```
127
+
128
+ Crossover thresholds, block sizes, and solver tuning are private, typed, and overridable
129
+ only through `MLXLEARN_*` environment variables for CI and debugging. They are not part of
130
+ the public API.
131
+
132
+ ## Performance
133
+
134
+ "Never slower than scikit-learn" is the design goal. The shipped *gate* is measurable: on
135
+ each benchmarked workload class, patched dispatch shows no statistically significant
136
+ regression against stock scikit-learn. Crossover points are measured per algorithm and per
137
+ operation, published in [`docs/benchmarks.md`](docs/benchmarks.md), and wired into the
138
+ dispatch thresholds — small problems are handed to scikit-learn on purpose.
139
+
140
+ What that means in practice for 0.1.0a1, on an M4 Max:
141
+
142
+ | | |
143
+ |---|---|
144
+ | **Neighbor queries** | **2.9×–17×** from ~250 samples up. This is the reason to use mlxlearn. |
145
+ | Neighbor `fit` | 0.17×–0.36×. mlxlearn uploads to the device; scikit-learn stores a reference. The first query repays it several times over. |
146
+ | `SVC` | **2.93× on wide data** (4 000 × 256, rbf), but slower than LIBSVM on narrow data — 0.47× at 4 000 × 32, worse below. `kernel="linear"` never uses MLX. Width decides, and patched dispatch routes accordingly. |
147
+ | `LogisticRegression` | **Slower than scikit-learn below 1 024 features**, by a lot. The crossover is set high so patched dispatch hands those to scikit-learn; see [`docs/benchmarks.md`](docs/benchmarks.md) for why and what would change it. |
148
+
149
+ Reproduce them:
150
+
151
+ ```bash
152
+ python -m benchmarks.run --profile smoke
153
+ ```
154
+
155
+ ## Precision
156
+
157
+ MLX computes in float32 on the GPU. mlxlearn's parity tests are written to float32
158
+ tolerances, and the cases where float32 makes strict scikit-learn equivalence impossible
159
+ are enumerated — not waved away — in [`docs/fp32_policy.md`](docs/fp32_policy.md).
160
+
161
+ ## Development
162
+
163
+ ```bash
164
+ pip install -e ".[dev]"
165
+ pytest
166
+ ```
167
+
168
+ Contributor rules, the estimator gate checklist, and the compliance checks are in
169
+ [`docs/development.md`](docs/development.md).
170
+
171
+ ## Provenance
172
+
173
+ mlxlearn succeeds a private research fork of an Intel-maintained scikit-learn accelerator.
174
+ That history is not carried here: this repository was bootstrapped from an audited
175
+ allowlist, and no source file was copied. The audit, the recorded behavioral baseline of
176
+ the ancestor, and the authorship attestation are in [`phase0/`](phase0/). Full lineage,
177
+ disclaimers, and third-party notices are in [`ACKNOWLEDGMENTS.md`](ACKNOWLEDGMENTS.md) and
178
+ [`THIRD_PARTY_NOTICES.md`](THIRD_PARTY_NOTICES.md).
179
+
180
+ ## License
181
+
182
+ Apache-2.0. See [LICENSE](LICENSE).