Wink Pings

Running KMeans on Apple GPUs: up to 7x faster, e-means up to 15x faster

mlx-learn 0.1.3 brings KMeans and Meta's e-means to Apple Silicon GPUs while retaining a scikit-learn-style API. In single-seed tests with 10M vectors and 65,536 centers, Metal KMeans is 1.96 to 6.19x faster than scikit-learn, and Metal e-means is 3.5 to 15.6x faster than CPU Faiss.

mlx-learn 0.1.3 has been released, adding two clustering implementations: KMeans and EMeans, both running on Apple Silicon GPUs. Installation is straightforward:

pip install mlx-learn==0.1.3

mlx-learn is positioned as accelerating classical machine learning with MLX, with an API close to scikit-learn. It has two usage layers: one is importing mlxlearn.cluster.KMeans or EMeans directly; the other is patch_sklearn(), which points scikit-learn names to accelerated implementations, requiring few changes to existing code. Note that KMeans and EMeans are direct estimators and do not participate in patch_sklearn(), so sklearn.cluster.KMeans in existing code will not be silently replaced.

KMeans uses the familiar Lloyd clustering: full-batch, dense FP32 training, supports explicit centers, random centers, k-means++ initialization, and multiple restarts, finally returning nearest-center labels and inertia. The number in the original post is up to 7x speedup on Apple GPUs; in the 10M vector benchmark, across five datasets, 65,536 centers, FP32, seed 10, same initial centers and pass budget, Metal KMeans is 1.96 to 6.19x faster than scikit-learn KMeans. This is a single-seed measurement, with no error bars and no claim of statistical significance.

EMeans is worth a closer look. It comes from Meta's new paper e-means: outperforming k-means (in one-tenth the epochs). The idea is to update centers with mini-batches, maintain centers with an exponential moving average, and have batch size and decay given directly by a scaling law rather than hand tuning. The paper claims it converges to lower loss with fewer epochs, and in common cases is much faster than Lloyd k-means. mlx-learn's implementation supports the paper recipe, Faiss recipe, last-epoch means, slow EMA, spherical clustering, and native BF16 Metal assignment with precision="bfloat16".

The EMeans number in the original post is up to 15x speedup versus the original CPU implementation. Another reply gives a more detailed range: across five 10M-vector datasets, Metal e-means is 3.5 to 15.6x faster than CPU Faiss e-means on the same M4 Max; when matching pass budgets, held-out clustering error is 1.4% to 4.5% lower than Metal KMeans. These numbers also come from single-seed FP32 runs; they are suitable for gauging magnitude, not as universal promises.

Runtime, memory, and test MSE of KMeans and e-means on five 10M datasets

The usage requirements are also clear: macOS on Apple silicon arm64, Python 3.10 to 3.13, scikit-learn ≥ 1.7, MLX ≥ 0.29. Version 0.1.3 is still marked alpha, and the API may change. Small data and small queries may not be worth it; GPU startup and preparation overhead can eat the gains. In the original post's comments, someone asked about the minimum dataset threshold, and the author did not answer directly; what can be confirmed from the docs is that other patched solvers fall back to scikit-learn below a crossover, while these two clustering direct estimators do not go through the patch, so benchmark on your own data before switching.

Someone in the comments also posted o-means, claiming to reach the same error in 22% of e-means' time. That is another Faiss contrib implementation, benchmarked on an RTX 5090, and is not the same thing as this Apple Silicon update. It can be used as a comparison, but don't confuse it with the same release.

Overall, this update fills in a piece of the puzzle for classical machine learning on Apple Silicon: clustering can run on the GPU while the API still keeps a scikit-learn style. For people doing embedding clustering, vector indexing, or codebook training, 10M-scale data is where it gets interesting; on small samples, run benchmarks before switching.

发布时间: 2026-11-17 01:27