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.3

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.3
File Size Uploaded
metal_linalg-2.2.3.tar.gz 205.0 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for metal-linalg 2.2.3
File
metal_linalg-2.2.3-cp314-cp314-macosx_14_0_arm64.whl CPython 3.14 CPython 3.14 macOS 14.0+ ARM64 Details
metal_linalg-2.2.3-cp313-cp313-macosx_14_0_arm64.whl CPython 3.13 CPython 3.13 macOS 14.0+ ARM64 Details
metal_linalg-2.2.3-cp312-cp312-macosx_14_0_arm64.whl CPython 3.12 CPython 3.12 macOS 14.0+ ARM64 Details
metal_linalg-2.2.3-cp311-cp311-macosx_14_0_arm64.whl CPython 3.11 CPython 3.11 macOS 14.0+ ARM64 Details
metal_linalg-2.2.3-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.3.tar.gz

Download URL metal_linalg-2.2.3.tar.gz
Size 205.0 kB
Tags Source
SHA-256 checksum
How to use checksums
ba4342d9cf308d522f2040906bf99bc8a5d5ab74efecb0a297929c48edfd064d
BLAKE2b-256 checksum
How to use checksums
7b88562c67c8afe3b2d657af9243adf36a3219907cdc23991139652bbd4e3790
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.3-cp314-cp314-macosx_14_0_arm64.whl

Download URL metal_linalg-2.2.3-cp314-cp314-macosx_14_0_arm64.whl
Size 199.5 kB
Tags CPython 3.14 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
8993ecdee15df71f1229765bb3b56929c7d94e512591f7c62818742e7a9e60f7
BLAKE2b-256 checksum
How to use checksums
3a28f3e3ed92ab6ca4b591e2e819b01c958595622bf2006224bc44947b294cce
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.3-cp313-cp313-macosx_14_0_arm64.whl

Download URL metal_linalg-2.2.3-cp313-cp313-macosx_14_0_arm64.whl
Size 199.5 kB
Tags CPython 3.13 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
13676609cc12d72af1a91cfb9d8403c9a23196c58de8f85a9e2f7c45dcd72c31
BLAKE2b-256 checksum
How to use checksums
bf3754064fb61fa0fd7a1490e21828a1508e3ec6c7ce5d266744a6c7bc108048
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.3-cp312-cp312-macosx_14_0_arm64.whl

Download URL metal_linalg-2.2.3-cp312-cp312-macosx_14_0_arm64.whl
Size 199.5 kB
Tags CPython 3.12 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
f7a85a566fd7ee4e3657852f5b3095394d02869f97dfca05bf4c40c58dda7e0d
BLAKE2b-256 checksum
How to use checksums
9e8b62b52b371dbc7c162d32914942031255fca06e5874eeaeac9148dad3fe68
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.3-cp311-cp311-macosx_14_0_arm64.whl

Download URL metal_linalg-2.2.3-cp311-cp311-macosx_14_0_arm64.whl
Size 200.2 kB
Tags CPython 3.11 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
f63d795bb8261ad359a5182601efbdda5d1032e762c763f15aecf78ca256521f
BLAKE2b-256 checksum
How to use checksums
f8da6a2315ff520d51830c50e86790217aee3ab287f5f05e43c7d13bc50e3fa1
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.3-cp310-cp310-macosx_14_0_arm64.whl

Download URL metal_linalg-2.2.3-cp310-cp310-macosx_14_0_arm64.whl
Size 200.3 kB
Tags CPython 3.10 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
d646014830d088402d925751240438a0001685e8a20b23198aa05b7769c4925a
BLAKE2b-256 checksum
How to use checksums
7e3d966210ab5231f44e01f42a8fa5ec8ad1a7053a6dedc9f10b817f1037b6d6
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