apple-mlx
Rust bindings for Apple MLX via the official mlx-c C API.
This crate provides:
apple_mlx::raw: generated raw bindings for the fullmlx-csurface- a thin safe layer for
Device,Stream,Array, andComplex32 - runnable examples, including complex matrix multiplication and graph export
What This Repo Does
This repository vendors mlx-c, generates Rust bindings in build.rs, and links them against an installed MLX build.
The build model is explicit:
mlx-cis vendored in this crate- MLX itself is installed locally by the repo
Makefile - no hidden MLX fetch happens during
cargo build
Clone
All commands below assume you are at the repo root:
Requirements
- macOS on Apple silicon
- Rust toolchain
- Xcode command line tools
- CMake
Install the basics if needed:
CPU Build From Scratch
This is the shortest fully reproducible path.
- Build and install MLX into the repo-local prefix:
- Build the Rust crate:
- Run tests:
- Run the main demo:
- Run a specific example:
The local MLX install prefix used by the repo is:
The Makefile exports these automatically:
CMAKE_PREFIX_PATH="/.local/apple-mlx"
MLX_DIR="/.local/apple-mlx/share/cmake/MLX"
GPU Build With Metal
If the Metal compiler is available, the repo will build MLX with Metal enabled and the GPU examples will run against the GPU backend.
Install the Metal toolchain:
Verify it:
Then rebuild with Metal enabled:
Run the Metal example:
The Makefile records whether MLX was built with MLX_BUILD_METAL=ON or OFF. If that mode changes, make build, make run, and make run-example will rebuild the local MLX install automatically.
Examples
Run the core examples:
Run the GPU/Metal example:
Check all examples compile:
How The FFI Build Works
build.rs does three jobs:
- runs
bindgenonvendor/mlx-c/mlx/c/mlx.h - builds vendored
mlx-cwith CMake - links it against the installed MLX package exposed through
CMAKE_PREFIX_PATHandMLX_DIR
Key repo files:
build.rssrc/lib.rssrc/main.rsexamples/vendor/mlx-c/Makefile
Integrating Into Another Rust Project
There are two parts:
- add the Rust crate
- make sure your project can find an installed MLX prefix at build time
From crates.io
Add this to your Cargo.toml:
[]
= "0.1"
Use it from Rust:
use raw;
use ;
From a local checkout
If you want to work against this repo directly:
[]
= { = "../apple-mlx" }
Build Environment In The Consumer Project
Your consuming project must expose the MLX install location when Cargo builds apple-mlx.
If you installed MLX with this repo’s Makefile, export:
Then build your own project normally:
Consumer Project Makefile Example
If you want the same workflow in another repo:
APPLE_MLX_PREFIX ?=
export CMAKE_PREFIX_PATH :=
export MLX_DIR :=
:
:
Minimal Consumer Example
use demo_complex_matmul;
Or use the raw bindings directly:
use raw;
Verified Runs
Verified CPU flow:
Verified GPU flow:
Packaging Notes
- crate name:
apple-mlx - docs:
https://docs.rs/apple-mlx - repo:
https://github.com/ms3c/apple-mlx - docs.rs uses the
docs-onlyfeature to avoid native compilation during documentation builds
Current Limits
- the raw binding surface is broad, but the safe Rust wrapper is still thin
- MLX must be installed on the machine building the crate
- this is currently a macOS Apple-silicon-focused crate