Skip to main content

metal-linalg for Python

QR, symmetric eigendecomposition and SVD for batches of matrices on Apple GPUs, on MLX arrays.

import mlx.core as mx
import metal_linalg as ml

a = mx.random.normal((1000, 64, 32))     # a batch of 1000 matrices

Q, R = ml.qr(a)                          # Q (1000, 64, 32), R (1000, 32, 32)
U, S, Vt = ml.svd(a)                     # thin: U (1000, 64, 32), S (1000, 32), Vt (1000, 32, 32)
w, V = ml.eigh(a.swapaxes(-1, -2) @ a)   # w ascending (1000, 32), V (1000, 32, 32)

Each call is routed to the fastest Metal kernel for its shape and batch, or to LAPACK on the CPU, by a policy measured on the Mac it runs on:

ml.device_name()                      # 'Apple M5 Pro'
ml.eigh_policy_source()               # 'tuned:Apple M5 Pro'
ml.eigh_backend(512, 64)              # 'block': 64 matrices of 512x512 go to the GPU
ml.eigh_backend(512, 1)               # 'cpu': one matrix is faster on the CPU
ml.set_eigh_policy(gpu_min_batch=1)   # override the measured policy

Inputs may be mx.array, NumPy arrays or nested lists; outputs are float32 mx.array. ml.eigvalsh and ml.svdvals return the values alone, for less work. The kernels and the routing are described in the main README.

Installing

pip install metal-linalg

On an Apple Silicon Mac with macOS 14 or later and Python 3.10 to 3.14, this installs a prebuilt wheel and the mlx it was built against; nothing is compiled.

MLX is pinned to one release. The package shares MLX's own library and passes mx.array objects through MLX's internals, so it works only with the MLX it was built against, and asks pip for exactly that one (mlx==0.32.3 for this release). Installing it may therefore move your mlx to that version, and upgrading mlx past it on its own breaks the import with an ImportError that says so; upgrade both together, pip install -U metal-linalg mlx, once a release of this package for the newer MLX is out.

From source

On a Mac with no matching wheel, pip builds the package from source, which needs Xcode's command line tools (xcode-select --install) and fetches CMake and the rest itself. To build a clone, or the main branch:

pip install .                                                  # in a clone
pip install git+https://github.com/c0rmac/metal-linalg.git     # main branch

Against another MLX

To build against an MLX other than the pinned one, a source build of MLX or Homebrew's, turn off pip's build isolation so the build sees your MLX, and --no-deps so pip leaves it alone. The build must also use the nanobind your MLX was built with: the pip MLX 0.32.3 uses nanobind 3.0.1, Homebrew's MLX 0.32.1 uses 2.15.0. The build checks this before compiling and stops with a message naming the mismatch.

With Homebrew's Python and MLX (brew install mlx includes the mlx Python package), in a virtual environment that can see Homebrew's packages:

python3 -m venv --system-site-packages ~/.venvs/metal-linalg
source ~/.venvs/metal-linalg/bin/activate
pip install scikit-build-core "nanobind==2.15.0"
pip install --no-build-isolation --no-deps git+https://github.com/c0rmac/metal-linalg.git

After upgrading that MLX, reinstall the package the same way.

Tests

python -m unittest discover -s python/tests -v

Metadata

Release files for metal-linalg 2.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for metal-linalg 2.2.2
File Size Uploaded
metal_linalg-2.2.2.tar.gz 204.6 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for metal-linalg 2.2.2
File
metal_linalg-2.2.2-cp314-cp314-macosx_14_0_arm64.whl CPython 3.14 CPython 3.14 macOS 14.0+ ARM64 Details
metal_linalg-2.2.2-cp313-cp313-macosx_14_0_arm64.whl CPython 3.13 CPython 3.13 macOS 14.0+ ARM64 Details
metal_linalg-2.2.2-cp312-cp312-macosx_14_0_arm64.whl CPython 3.12 CPython 3.12 macOS 14.0+ ARM64 Details
metal_linalg-2.2.2-cp311-cp311-macosx_14_0_arm64.whl CPython 3.11 CPython 3.11 macOS 14.0+ ARM64 Details
metal_linalg-2.2.2-cp310-cp310-macosx_14_0_arm64.whl CPython 3.10 CPython 3.10 macOS 14.0+ ARM64 Details

Total release size: 1.2 MB

Release files / metal_linalg-2.2.2.tar.gz

Download URL metal_linalg-2.2.2.tar.gz
Size 204.6 kB
Tags Source
SHA-256 checksum
How to use checksums
5bb94bdf5927034a115dac84531d4dcf7d359bcec6a043ab52be204d5102bcd4
BLAKE2b-256 checksum
How to use checksums
4aec7c31c187a28f27c55795468b3e921f6b5dd007c583ec1f36fb46301d88cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 2, 2026.

Transparency log

Release files / metal_linalg-2.2.2-cp314-cp314-macosx_14_0_arm64.whl

Download URL metal_linalg-2.2.2-cp314-cp314-macosx_14_0_arm64.whl
Size 199.4 kB
Tags CPython 3.14 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
bcc312e3c0533be78ee9f8d18f976b0150a05527d3e7091e1e02c0faf718ca64
BLAKE2b-256 checksum
How to use checksums
ebd0c4e996859c00756a669bda9abd28706c70638aacf91579e2aea34de8e3fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 2, 2026.

Transparency log

Release files / metal_linalg-2.2.2-cp313-cp313-macosx_14_0_arm64.whl

Download URL metal_linalg-2.2.2-cp313-cp313-macosx_14_0_arm64.whl
Size 199.4 kB
Tags CPython 3.13 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
33c2b17b5cafb2ca807520e320ad08d9e61ed4c163388ba851208d529f63fa3e
BLAKE2b-256 checksum
How to use checksums
8326d4a2283e77fb4a15a62651fa1f4019cc89724ccc12bfae7a5a42ca50db91
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 2, 2026.

Transparency log

Release files / metal_linalg-2.2.2-cp312-cp312-macosx_14_0_arm64.whl

Download URL metal_linalg-2.2.2-cp312-cp312-macosx_14_0_arm64.whl
Size 199.4 kB
Tags CPython 3.12 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
034c0140e1ca1b38f0d62eb0921d5581efe0c4886ad2eb8c6e070e59c3e7f318
BLAKE2b-256 checksum
How to use checksums
ff015528768cc1292fdc72d4ad31819ca6d9f04c384e72abd243d69f449e40b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 2, 2026.

Transparency log

Release files / metal_linalg-2.2.2-cp311-cp311-macosx_14_0_arm64.whl

Download URL metal_linalg-2.2.2-cp311-cp311-macosx_14_0_arm64.whl
Size 200.1 kB
Tags CPython 3.11 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
7b6c2fcbd6e8b5d432af0cd4b335f92c39faca818e3c1221e78fc68e45ae3f41
BLAKE2b-256 checksum
How to use checksums
cc91cbc9474ef14494b626cbeab829f9896c87c52b8e1a751e3a834af5ab43b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 2, 2026.

Transparency log

Release files / metal_linalg-2.2.2-cp310-cp310-macosx_14_0_arm64.whl

Download URL metal_linalg-2.2.2-cp310-cp310-macosx_14_0_arm64.whl
Size 200.2 kB
Tags CPython 3.10 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
6ac805727a7f5623d64fd70e2a33eb526e3dfadb0c4d11e751f3ce677d18c0bb
BLAKE2b-256 checksum
How to use checksums
8c5742808585fdec6e4203aea0ff5febd0ec8d39f6270dcd2410cabb4015293a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 2, 2026.

Transparency log
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page