Skip to main content

metal-linalg for Python

QR, symmetric eigendecomposition and SVD for batches of matrices on Apple GPUs, on MLX arrays. For PyTorch tensors, install metal-linalg-torch instead.

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)
R = ml.qr(a, mode="r")                   # R alone, as numpy's mode="r": Q never formed
Q, R = ml.qr(a, mode="complete")         # Q (1000, 64, 64), R (1000, 64, 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(32, 4096)             # 'ql': 4096 matrices of 32x32 go to the GPU
ml.eigh_backend(512, 64)              # 'cpu': the CPU's cores win 64 of 512x512
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; ml.cholesky(p, upper=False) factors symmetric positive definite matrices (since 2.18.0; a matrix that is not comes back all NaN), and ml.cholesky_ex(p) also returns LAPACK's info for each; ml.lu_factor, ml.solve and ml.inv work as mx.linalg's, on the GPU for large matrices, and their _ex forms return info too; ml.solve_triangular(a, b, upper=False) as mx.linalg.solve_triangular. 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.

Calibration

Routing comes from measurements made on each kind of Mac. Where there are none for yours, or they are stale or incomplete, importing the package raises a metal_linalg.CalibrationWarning once per decomposition, saying so and how to measure your Mac (about an hour and a half) and submit the results:

ml.calibration_status()     # {'qr': 'current', 'eigh': 'current', 'svd': 'stale'}

warnings.filterwarnings("ignore", category=ml.CalibrationWarning), or METAL_LINALG_NO_CALIBRATION_NOTICE=1, silences it. Which chips are measured: the measurements page.

Tests

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

Metadata

Release files for metal-linalg 2.19.0

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.19.0
File Size Uploaded
metal_linalg-2.19.0.tar.gz 2.0 MB Details

Built distributions (wheels)

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

Total release size: 12.0 MB

Release files / metal_linalg-2.19.0.tar.gz

Download URL metal_linalg-2.19.0.tar.gz
Size 2.0 MB
Tags Source
SHA-256 checksum
How to use checksums
4dfc3c04eb8e998f8115e78a234f355c39927e835564ae5c5b783fbddae752e6
BLAKE2b-256 checksum
How to use checksums
bbbe562bbaf1c57e44c4a29375a07132abe5088fab7c52e07d9b23b5714a688b
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 10, 2026.

Transparency log

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

Download URL metal_linalg-2.19.0-cp314-cp314-macosx_14_0_arm64.whl
Size 2.0 MB
Tags CPython 3.14 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
c13e96d06c08780ca0f9d7433d906626d5cd326bb2a5e131a6cb1f6fa9e3c8ab
BLAKE2b-256 checksum
How to use checksums
8a0ee08df82462da368af8f62ba0dd08e9860a9a4405852d9b9927af5a362636
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 10, 2026.

Transparency log

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

Download URL metal_linalg-2.19.0-cp313-cp313-macosx_14_0_arm64.whl
Size 2.0 MB
Tags CPython 3.13 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
30f1449f319f097c624490cd531dc1d30b107186955d70c61899e5158342cc0e
BLAKE2b-256 checksum
How to use checksums
148f3575033441cbca881fe6290a558e407580046f4895c35e2069d1d9625881
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 10, 2026.

Transparency log

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

Download URL metal_linalg-2.19.0-cp312-cp312-macosx_14_0_arm64.whl
Size 2.0 MB
Tags CPython 3.12 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
b36ee02db13a7ccab8d3bbfd60fe0d4365a8d9a515aa6a51549dd6ceb2bc674b
BLAKE2b-256 checksum
How to use checksums
7cf032c743dc186e8afa7f2300556b3ba12f5f562c1fc0f94f1c675aa7d9d647
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 10, 2026.

Transparency log

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

Download URL metal_linalg-2.19.0-cp311-cp311-macosx_14_0_arm64.whl
Size 2.0 MB
Tags CPython 3.11 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
8647bd170891532f17429533a1381e081c4942b89da90bbab780cccfb28f1fda
BLAKE2b-256 checksum
How to use checksums
5230c2aa95cef35d633913232b6138850a87e82068a4105197c0507bb78202a8
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 10, 2026.

Transparency log

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

Download URL metal_linalg-2.19.0-cp310-cp310-macosx_14_0_arm64.whl
Size 2.0 MB
Tags CPython 3.10 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
52e6075121f864ee099e195f779bb09fbfc687798ae89edad7d73c9555afa42e
BLAKE2b-256 checksum
How to use checksums
9706b84a4a62f8c7a5651f4c3ccac321fbcf9e921ad87815554526aeb625e9af
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 10, 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