apple-mlx
Rust bindings for Apple MLX through the official mlx-c C API.
This crate currently provides:
apple_mlx::raw: full generated raw bindings formlx-c- a thin safe layer for
Device,Stream,Array, andComplex32 - a working complex matrix multiplication example validated against a CPU reference
Status
This crate is now library-usable and packageable for crates.io.
The important build constraint is explicit:
mlx-cis vendored in this crate- MLX itself is not fetched at build time
- you must provide an installed MLX package and point CMake at it with
CMAKE_PREFIX_PATHorMLX_DIR
That keeps the crate build reproducible and avoids hidden network fetches during cargo build.
Project Layout
src/lib.rs: generated raw bindings export plus thin safe wrapperssrc/main.rs: small binary using the libraryexamples/complex_matmul.rs: example entrypoint for the same demobuild.rs: generates bindings and builds vendoredmlx-cagainst an installed MLXvendor/mlx-c: vendored upstreammlx-csource
Library Surface
Raw bindings:
use raw;
Thin safe API:
use ;
Demo entrypoint:
demo_complex_matmul?;
How the Build Works
build.rs does three things:
- Generates Rust bindings from
vendor/mlx-c/mlx/c/mlx.husingbindgen. - Builds vendored
mlx-cwith CMake. - Links it against an already-installed MLX package discovered through:
CMAKE_PREFIX_PATH- or
MLX_DIR
Metal support is enabled only if this succeeds:
If that command fails, the build falls back to CPU-only MLX usage.
Requirements
- macOS on Apple silicon
- Rust toolchain
- Xcode command line tools
- CMake
- an installed MLX package
Install the basic tools if needed:
Installing MLX for This Crate
This crate expects MLX to be installed somewhere CMake can find it.
Two common options:
- Install MLX into a prefix and export
CMAKE_PREFIX_PATH. - Point
MLX_DIRdirectly at.../share/cmake/MLX.
Example with a local install prefix:
Example with a direct MLX config path:
Build and Run
All commands below assume you are in the crate root:
Build:
CMAKE_PREFIX_PATH="/../apple-mlx-prefix"
Run the binary:
CMAKE_PREFIX_PATH="/../apple-mlx-prefix"
Run the example:
CMAKE_PREFIX_PATH="/../apple-mlx-prefix"
Run tests:
CMAKE_PREFIX_PATH="/../apple-mlx-prefix"
Verified CPU Run
This environment was verified successfully in CPU mode. The Metal toolchain was not installed, so the crate built and ran with CPU fallback.
Observed output:
Using Apple MLX on CPU device 0 (Apple M2 Pro)
Output shape: [2, 2]
Left matrix:
1.000+2.000i 3.000-1.000i
-2.000+0.500i 0.000+4.000i
Right matrix:
0.500-1.000i 2.000+0.000i
-3.000+1.500i 1.000-2.000i
MLX product:
-5.000+7.500i 3.000-3.000i
-6.500-9.750i 4.000+5.000i
Max absolute error vs CPU reference: 0.000000
GPU Reproduction
To run on GPU, install the Metal toolchain first:
Then rebuild and run with the same MLX prefix:
CMAKE_PREFIX_PATH="/../apple-mlx-prefix"
If GPU support is available, the program should print:
Using Apple MLX on GPU device 0 (...)
Packaging Notes
- package name:
apple-mlx - crate docs target:
docs.rs/apple-mlx - docs.rs uses the
docs-onlyfeature to skip native library compilation during documentation builds - no build-time network fetches are required by this crate
Current Limits
- the safe wrapper only covers a small subset of MLX so far
- the raw
mlx-cbinding surface is available, but ergonomic Rust wrappers still need to be expanded module by module - GPU execution was designed for and wired in, but only CPU execution was verified in this environment