Skip to main content

IO-aware batched K-Means for Apple Silicon, ported from Flash-KMeans (Triton/CUDA) to pure MLX.

Project description

flash-kmeans-mlx

IO-aware batched K-Means for Apple Silicon, ported from Flash-KMeans (Triton/CUDA) to pure MLX.

Installation

uv pip install flash-kmeans-mlx

From source:

git clone https://github.com/hanxiao/flash-kmeans-mlx.git
cd flash-kmeans-mlx
uv pip install .

Usage

Functional API

import mlx.core as mx
from flash_kmeans_mlx import batch_kmeans_Euclid

x = mx.random.normal((32, 75600, 128))
cluster_ids, centers, n_iters = batch_kmeans_Euclid(
    x, n_clusters=1000, tol=1e-4, verbose=True
)

Input shape is (B, N, D) where B is batch size, N is number of points, D is dimensionality. All batches are clustered independently in a single vectorized pass.

Three distance metrics are available: batch_kmeans_Euclid, batch_kmeans_Cosine, and batch_kmeans_Dot.

Class API

from flash_kmeans_mlx import FlashKMeans

model = FlashKMeans(d=128, k=1000, niter=25, tol=1e-6)
model.fit(x)
labels = model.predict(x_new)

# or in one step
labels = model.fit_predict(x)

The FlashKMeans class accepts both (N, D) and (B, N, D) inputs. Set metric="cosine" or metric="dot" to switch distance functions.

Benchmark

All timings on M3 Ultra, float32, single batch. MLX uses mx.compile; sklearn uses Lloyd's algorithm on CPU (n_init=1).

N D K Iters MLX sklearn Speedup
5K 64 50 10 5ms 34ms 6.7x
50K 128 256 20 22ms 1.28s 58x
100K 128 1000 20 149ms 9.8s 66x
500K 128 1000 10 425ms 39.8s 94x

Run the benchmark yourself:

uv pip install 'flash-kmeans-mlx[benchmark]'
python -m flash_kmeans_mlx.benchmark --n 100000 --d 128 --k 1000 --max-iters 20

Correctness

Verified against sklearn with identical initial centroids over 20 iterations. Cluster assignment agreement is 92-99.8% depending on configuration, with inertia difference below 0.01%. The remaining discrepancy comes from float32 vs sklearn's float64 accumulation - boundary points near equidistant cluster borders get assigned differently due to rounding.

Distance metrics

Euclidean (squared L2), Cosine (dot product on L2-normalized vectors), and Dot-product (raw inner product).

Credits

This is an independent MLX port of Flash-KMeans and is not affiliated with the original authors.

Papers:

License

Apache 2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flash_kmeans_mlx-0.1.0.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flash_kmeans_mlx-0.1.0-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file flash_kmeans_mlx-0.1.0.tar.gz.

File metadata

  • Download URL: flash_kmeans_mlx-0.1.0.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.8

File hashes

Hashes for flash_kmeans_mlx-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5dd5699a5167524578269bf1243b6311444296f6f2b340ce3355090b7298f39f
MD5 c4f387a1dea916f68f6abea86567164e
BLAKE2b-256 68ee261c375b7878f25f1dd530f1ef03903cad7d00287f9f1e08072430af9772

See more details on using hashes here.

File details

Details for the file flash_kmeans_mlx-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flash_kmeans_mlx-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2535dab1f23acdd6355d6fb6f5ccea0969dba210d244ec1bb2f463e4aea9ce9d
MD5 71b072812cace5935e899f45dde42bf0
BLAKE2b-256 88b0105499b477a38c62cc1717f878ad29ded7dbfbe4bfd9236d10211d856d21

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page