Skip to content

mlx-learn 0.1.6

Choose a tag to compare

@tabulai tabulai released this 19 Nov 19:39
· 72 commits to main since this release
6636399

Version 0.1.6 moves the k-means++ initialization of mlxlearn.cluster.KMeans to the GPU. For a given random_state, it selects the same starting centers as sklearn.cluster.kmeans_plusplus on the same machine, so fits keep the centers, iterations and inertia they had before, only faster.

python -m pip install --upgrade mlx-learn==0.1.6
from mlxlearn.cluster import KMeans

model = KMeans(n_clusters=4096, random_state=10).fit(X_train)  # init="k-means++" is the default

K-means++ initialization on Metal

Before 0.1.6, KMeans called scikit-learn's helper on the CPU for k-means++, and with thousands of centers that took most of the fit. Now a Metal kernel proves in float32, with a rigorous error bound, which (candidate, row) pairs a new candidate center cannot change. For inputs with at least 256 features it first compares 64-dimensional images of the rows. The host evaluates the remaining pairs in float64, using the helper's own expressions for sampling and potentials. Every step is certified with interval bounds; if a seeding cannot be certified, the helper itself runs.

MNIST, 60,000 × 784 float32, random_state=10, MacBook Pro M4 Max. Seeding times are medians of three interleaved rounds against one run of the helper. Fit times compare the 0.1.6 default with the same fit routed to the helper, as in 0.1.5.

Centers Seeding, scikit-learn helper Seeding, 0.1.6 Default KMeans fit, 0.1.5 path Default KMeans fit, 0.1.6
1,024 26.9 s 0.90 s 27.5 s 1.4 s
2,048 54.2 s 1.62 s 54.9 s 2.4 s
4,096 108.6 s 2.82 s 110.4 s 4.4 s
8,192 217.1 s 5.41 s 218.7 s 7.0 s

At every size the 0.1.6 seeding selected the helper's rows, and the fits reached the same iterations and inertia. The default k-means++ fit is now also faster than init="random" (6.7–13.6 s here, ten restarts) and reaches lower inertia. The kernels compile in Metal's safe math mode. The products used for certification run in float64 on the host, so M5's TF32 matrix multiply does not affect them. Timings were measured on M4 Max; the tests also pass on M5 Ultra. K-means++ initialization · Measurements and scripts · PR #29

O-means: short fits keep the full search where it is faster

OMeans(assignment="auto") now keeps the full search for fits shorter than two epochs on M1 Ultra and M5 Ultra unless the probes are expected to cover at most 36% of the centroids, instead of one half. On M5 Ultra, one-epoch fits at 4,096 centroids on the MNIST family were 7–11% slower than EMeans with the two-level search; with the full search they run at 0.97× of EMeans' speed, the cost of OMeans' own loop. With the default cells and probes this affects 2,176 to 4,223 centroids at 512 features or more. M4 Max, fits of two epochs or more (the default is three), and assignment="two_level" are unchanged. The M1 Ultra limit was not measured in this regime and takes the stricter value. O-means guide · PR #30

MNIST-family benchmarks

The O-means results on MNIST, Fashion-MNIST and Kannada-MNIST from PR #28 are now part of a release. On M5 Ultra, OMeans.fit is 675–1,854× faster than scikit-learn's KMeans at 1,024 to 8,192 centroids, with held-out error between −0.89% and +0.44% of it. Results

The release provides Apple silicon wheels for Python 3.10–3.13 and a source distribution. Changelog · PyPI