Skip to content

Add MLXVSModel: approximate nearest neighbours on Apple silicon GPUs with mlxvs - #763

Open
mikepapadim wants to merge 1 commit into
benfred:mainfrom
mikepapadim:mlxvs-ann
Open

mikepapadim wants to merge 1 commit into
benfred:mainfrom
mikepapadim:mlxvs-ann

Conversation

@mikepapadim

Copy link
Copy Markdown

Closes #762

Adds implicit.ann.mlxvs.MLXVSModel and an MLXVSAlternatingLeastSquares factory. With them, recommend and similar_items run on the GPU of Apple silicon Macs through mlxvs, a CAGRA graph index on Apple's MLX. The wrapper follows FaissModel:

  • recommend: an inner product index over the item factors. It adds the norm-equalising extra dimension from augment_inner_product_matrix, as the annoy and nmslib models do.
  • similar_items: an index over the normalized item factors.
  • Fallback to exact search: on small item sets (no larger than intermediate_graph_degree) and on result counts above 1024. Exact search runs on the GPU up to 256 results and in NumPy beyond that.
  • Not supported: save/load/similar_users raise NotImplementedError, like FaissModel.

Tests and CI

  • tests/approximate_als_test.py: an MLXVSALSTest inside try: import mlxvs. It runs the shared RecommenderBaseTestMixin, plus test_large_recommend (falling back past 1024 results) and test_graph_index (graph results match the exact model).
  • build.yml: installs mlxvs on the macOS test runners, which are Apple silicon, for Python 3.10 and later. That way the tests run for real in CI. Linux and Windows are unaffected.
  • Local results: on an M3 Pro, all 24 MLXVS tests pass. The rest of the file gives the same results as main: the 5 AnnoyALSTest failures there happen without this change too.
  • Lint: ruff (v0.15.12, as in pre-commit) and codespell are clean.

Benchmark

lastfm-360k (292,385 items), ALS with 64 factors, N=10. Recall@10 is measured against the exact results, on an M3 Pro.

recommend recall@10 similar_items recall@10
exact (CPU) 4,300 users/s 1.000
FaissModel (nlist 400, nprobe 20) 33,100 users/s 0.613 39,000 items/s 0.989
MLXVSModel (itopk 64) 244,300 users/s 0.973 140,900 items/s 0.986
MLXVSModel (itopk 128) 129,500 users/s 0.989 85,800 items/s 0.990

Building the index takes about 5 s for the two indices, against 0.5 s for faiss IVF.

Usage

from implicit.approximate_als import MLXVSAlternatingLeastSquares

model = MLXVSAlternatingLeastSquares(factors=64, itopk_size=64)
model.fit(user_items)
ids, scores = model.recommend(userid, user_items[userid])

Install: pip install mlxvs (macOS 14+, Apple silicon).

Disclosure: I maintain mlxvs.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Approximate nearest neighbours on the GPU of Apple silicon Macs (mlxvs)

1 participant