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)
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. 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.11.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.11.0
File Size Uploaded
metal_linalg-2.11.0.tar.gz 321.3 kB Details

Built distributions (wheels)

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

Total release size: 1.8 MB

Release files / metal_linalg-2.11.0.tar.gz

Download URL metal_linalg-2.11.0.tar.gz
Size 321.3 kB
Tags Source
SHA-256 checksum
How to use checksums
13348ae3613071bd989f6e5c3bbed1b30a811aaf0c99f4711766b6a1fdfee0a6
BLAKE2b-256 checksum
How to use checksums
feb7a6a84ac2a96df53944166a20e67eeb0a1c3683c156b8f18d9e17cac1e22e
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 4, 2026.

Transparency log

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

Download URL metal_linalg-2.11.0-cp314-cp314-macosx_14_0_arm64.whl
Size 303.6 kB
Tags CPython 3.14 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
271d967790fd2f1277f41739010fc9fdea29d3135d754f5851996974e8390e97
BLAKE2b-256 checksum
How to use checksums
61ceb32ff6e0a3df724c1cad7356857a02a154b1dc2b343a1212b84829a37a24
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 4, 2026.

Transparency log

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

Download URL metal_linalg-2.11.0-cp313-cp313-macosx_14_0_arm64.whl
Size 303.6 kB
Tags CPython 3.13 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
775f0c11c394998524c37e655da52a143f4c71d8513295c933a6af4da04dbf3b
BLAKE2b-256 checksum
How to use checksums
04106c9aafcdb385351472c7b74451c6ac812259d097b5c56ff2a0b1d24d55cf
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 4, 2026.

Transparency log

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

Download URL metal_linalg-2.11.0-cp312-cp312-macosx_14_0_arm64.whl
Size 303.6 kB
Tags CPython 3.12 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
41c819f6eaddd3a0c952796e66e5c7d5674d467275414d19b302cb5403b98d7b
BLAKE2b-256 checksum
How to use checksums
07d9937da58db8dd8f548fd9a7d8c55390c3b9560721fa5a4ecff7925cc903c6
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 4, 2026.

Transparency log

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

Download URL metal_linalg-2.11.0-cp311-cp311-macosx_14_0_arm64.whl
Size 304.3 kB
Tags CPython 3.11 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
a1303b9bc25ea17ded7887ccdc439fd756fa6b227d5a0019c3e4801490abb13a
BLAKE2b-256 checksum
How to use checksums
987b334194a9a6e74637ee2d2db47d8bda8254000eb895aefb67198e573b3304
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 4, 2026.

Transparency log

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

Download URL metal_linalg-2.11.0-cp310-cp310-macosx_14_0_arm64.whl
Size 304.5 kB
Tags CPython 3.10 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
e3a565fddcf0634e2245da0c505f6826b1f83be5ac170f276ee859b965e858ad
BLAKE2b-256 checksum
How to use checksums
d776c92a3fab853c357afc8dc5354569e3c4bded80b51912631d1a3e58445019
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 4, 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