onnxruntime-mlx
PyPI package:
onnxruntime-ep-mlx—pip install onnxruntime-ep-mlx,import onnxruntime_ep_mlx. (Formerly published asonnxruntime-mlx, now renamed.)
An MLX-native execution provider for ONNX Runtime on Apple Silicon, built as an out-of-tree
plugin EP (ORT plugin-EP C ABI, ORT 1.27 / ORT_API_VERSION 27). It ships as a standalone
libonnxruntime_mlx_ep.dylib loaded by a stock prebuilt libonnxruntime.dylib via
RegisterExecutionProviderLibrary — no ONNX Runtime fork required.
The EP translates fused ONNX subgraphs into MLX graphs for encoders, LLM prefill, and token-at-a-time decode.
How it works
ONNX fused subgraph → MLX graph → mlx_compile → mlx_eval → ORT outputs
The EP translates supported ONNX regions into MLX, compiles reusable closures, and leaves unsupported
ops on ORT CPU. It covers common encoder and decoder operators, including quantized matmul, GQA/MHA,
PagedAttention, RoPE, normalization, convolution, pooling, reductions, and shape operations. See
docs/OP_ARCHITECTURE.md for the coverage table.
Large, fully claimed regions are fastest. Dynamic shapes are compiled per shape key; autoregressive
decode uses a shapeless path so growing KV length does not retrace. Use
ONNXRUNTIME_EP_MLX_VERBOSE=1 or ONNXRUNTIME_EP_MLX_CLAIM_DEBUG=1 to diagnose fallback and
fragmentation.
Requirements
- macOS on Apple Silicon, ORT 1.27 prebuilt (
ORT_API_VERSION >= 27) mlx-c(andmlx) — a HARD build dependency:brew install mlx-c- A Rust toolchain (
rustup) to build the EP from source
Versioning (ORT compatibility)
A plugin EP targets one ORT C-ABI version. Package versions use
0.<ORT_API_VERSION>.<patch>:
| onnxruntime-ep-mlx | ONNX Runtime | ORT_API_VERSION |
|---|---|---|
0.27.x |
1.27.x | 27 |
For example, ORT 1.28 moves the EP to 0.28.x.
Build
The EP is a Rust cdylib crate under rust/. Point it at an ONNX Runtime C-API
include directory and cargo build:
brew install mlx-c # HARD dependency (mlx-c + mlx)
cd rust
# Either point ORT_INCLUDE_DIR at the ORT headers directly, or set ORT_HOME to an
# ONNX Runtime release root (build.rs will look in $ORT_HOME/include):
export ORT_INCLUDE_DIR=/path/to/onnxruntime/include # or: export ORT_HOME=/path/to/onnxruntime-osx-arm64-1.27.0
cargo build --release
# => rust/target/release/libonnxruntime_mlx_ep.dylib (registers the EP as "MLXExecutionProvider")
Set MLX_PREFIX and MLXC_PREFIX to link and bundle a custom MLX runtime
instead of the Homebrew installation. Each prefix must contain include/ and
lib/; the wheel builder also bundles mlx.metallib and an optional
libjaccl.dylib from MLX_PREFIX/lib.
The crate binds the ORT plugin-EP C ABI and mlx-c directly via bindgen; it does not link
libonnxruntime (ORT is reached through the OrtApi function-pointer table passed to
CreateEpFactories).
Install & use
Python (recommended)
pip install -U onnxruntime-ep-mlx # macOS/Apple-Silicon wheel; bundles the mlx runtime
import onnxruntime as ort
import onnxruntime_ep_mlx
# Register the plugin EP once, then select it (with CPU fallback) like any provider.
onnxruntime_ep_mlx.register_execution_provider_library() # name: "MLXExecutionProvider"
sess = ort.InferenceSession(
"model.onnx",
providers=["MLXExecutionProvider", "CPUExecutionProvider"],
)
out = sess.run(None, feeds)
onnxruntime_ep_mlx also exposes library_path(), ep_name(), version(), and
append_to_session_options(so).
C / C++ (or any onnxruntime binding)
Point onnxruntime at the built dylib and select the provider by name:
// 1. Register the plugin library with the environment (once).
RegisterExecutionProviderLibrary(env, "MLXExecutionProvider",
"/abs/path/libonnxruntime_mlx_ep.dylib");
// 2. Append it to a session's options (falls back to CPU for unclaimed ops).
const char* ep = "MLXExecutionProvider";
SessionOptionsAppendExecutionProvider_V2(options, env, &ep, /*count*/ 1, ...);
From Rust via onnx-genai: ONNX_GENAI_EP=metal +
ONNX_GENAI_METAL_EP_LIB=/abs/path/libonnxruntime_mlx_ep.dylib.
Large-decoder partition metadata
The EP automatically infers residual layer boundaries from graph topology for decoders of about 24
layers or larger. Exporters can make the boundaries explicit with the
ONNX custom metadata key onnxruntime_ep_mlx.layer_boundary_outputs. Its value is a JSON array of
residual output tensor names, one per transformer layer:
import json
import onnx
model = onnx.load("decoder/model.onnx", load_external_data=False)
entry = model.metadata_props.add()
entry.key = "onnxruntime_ep_mlx.layer_boundary_outputs"
entry.value = json.dumps(["layer.0.output", "layer.1.output", "layer.2.output"])
onnx.save(model, "decoder/model.onnx")
Metadata takes precedence over inference and does not rely on node names. The EP chooses a dynamic
group size of 4-8 layers, targeting about seven partitions. Override it with
ONNXRUNTIME_EP_MLX_LAYER_PARTITIONS=<layers>; use 0 or off to disable partitioning.
Performance (M1 Max, warm)
Real end-to-end models, median of 10 runs, MLX EP vs the ORT CPU EP on the same machine — top-1 identical and max abs diff ≤ 6e-5 in every case:
| Model | Workload | CPU EP | MLX EP | Speedup |
|---|---|---|---|---|
| Perch v2 | audio encoder (with DFT front-end) | 64.0 ms | 12.0 ms | 5.3× |
| Perch v2 (no DFT) | audio encoder | 56.5 ms | 12.0 ms | 4.7× |
| BirdNET | audio classifier (CNN) | 14.9 ms | 7.3 ms | 2.0× |
| gemma-4-E2B | vision encoder (fp16 ViT) | 267 ms | 47 ms | 5.7× |
Feed-forward encoders (audio / CNN / vision) are the EP's sweet spot: the whole graph fuses into a
single MLX closure that is traced + mlx_compiled once and replayed, so a static-shape model runs
end-to-end on the GPU with one dispatch (e.g. Perch: 725/725 nodes claimed, 1 fused subgraph).
Eligible BF16 INT4/INT8 prefill matmuls (block size 32/64/128, at least 32 rows) explicitly use stock
MLX's FP16 compute path by default. Set ONNXRUNTIME_EP_MLX_BF16_QMM_FP16=0 to disable it.
The Foundry Local q4f16 decoders below run on the same M1 Max, warm, MLX EP vs the ORT CPU EP (decode = 1 token with 128 past; prefill = 128-token step):
| Model | Arch | Prefill | Decode |
|---|---|---|---|
| Qwen2.5-0.5B | GQA, external rotary | 5.2× | dispatch-bound (CPU-favored) |
| Phi-3.5-mini | Phi3, GQA | 5.29× | 1.19× |
| Phi-4-mini | Phi4, long-context RoPE | 5.78× | 1.10× |
| Mistral-7B-Instruct | GQA, growing KV | 11.89× | 3.30× |
| gemma-4-E2B | Gemma3n, 15-layer | 3.3× | 3.3× |
Muse-Glimmer-30B INT4, with the optimized bundled MLX runtime and automatic 8-layer groups, reaches 138.67 prefill tok/s at 512 tokens and 14.79 decode tok/s over 200 generated tokens. The same-quantization llama.cpp baseline reaches 137.84 / 13.50 tok/s.
Decode is weight-bandwidth-bound: small models can favor CPU, while larger q4 decoders benefit from MLX. Unclaimed ops fall back to ORT CPU.
Profiling & tracing (Perfetto)
The EP ships a built-in tracer (compiled in by default, near-zero cost when off). Recording is gated entirely by environment variables — set one, run your model, and inspect the result.
Get a Perfetto/Chrome trace. Point ONNXRUNTIME_EP_MLX_TRACE at an output path; the JSON trace is
written when the inference session is torn down:
ONNXRUNTIME_EP_MLX_TRACE=/tmp/mlx_trace.json python your_script.py
# then open https://ui.perfetto.dev (or chrome://tracing) and load /tmp/mlx_trace.json
The timeline shows one span per fused subgraph (mlx.subgraph), a nested span around the synchronous
mlx_eval (mlx.eval — its CPU wall time is the GPU-inclusive time of the whole fused subgraph),
per-op build spans with shapes/dtype/bytes, and counter tracks for GPU memory / utilisation. Ops that
fell back to a slower composed path (despite a fused kernel existing) are coloured distinctly with a
reason=…, and a top-10 slowest-ops summary is emitted at teardown.
Lighter options (no JSON file):
| Env var | Effect |
|---|---|
ONNXRUNTIME_EP_MLX_VERBOSE=1 |
Print the end-of-run session summary (claim rate, compute-path breakdown, time attribution) to stderr. |
ONNXRUNTIME_EP_MLX_CLAIM_DEBUG=1 |
Print each unclaimed node + the actionable reason (why the graph fragmented). |
ONNXRUNTIME_EP_MLX_SIGNPOST=1 |
Emit os_signpost intervals so an Instruments Metal System Trace correlates. |
ONNXRUNTIME_EP_MLX_NO_STABLE_CROSS_CACHE=1 |
Disable per-generation MLX reuse of immutable MHA cross-attention K/V inputs for performance A/B. |
Per-kernel GPU detail (Xcode). MLX hides its Metal command buffers inside one fused mlx_eval, so
the JSON trace times the fused eval as a whole. To see inside it, capture a boundary eval to a
.gputrace bundle (full per-kernel timing / occupancy / bandwidth) and open it in Xcode:
MTL_CAPTURE_ENABLED=1 \
ONNXRUNTIME_EP_MLX_GPU_CAPTURE=/tmp/mlx.gputrace \
ONNXRUNTIME_EP_MLX_GPU_CAPTURE_EVAL=5 \
python your_script.py
MTL_CAPTURE_ENABLED=1 must be set before process start. …_GPU_CAPTURE_EVAL picks which eval to
capture (0-based, default 0); for decode, eval 0 is prefill/warmup, so pick a steady-state token.
Concurrency
MLX evaluation is thread-affine. Use one InferenceSession per thread; do not call Run() on one
shared session from multiple threads.
Numerical accuracy
Outputs are tolerance-matched against ORT CPU but are not bit-identical. Long greedy generations can diverge after near-tied logits because MLX and CPU use different floating-point reduction orders.
Layout
docs/ design docs (DESIGN, OP_ARCHITECTURE, COMPILED_CAPTURE, MLX_EVALUATION)
rust/ the Rust EP: plugin-EP C-ABI vtables (factory/ep) + the modular ONNX->MLX
translator (engine, registry, ops/*.rs) over a mlx-c RAII layer (mlx.rs)
python/ pure-Python pip package (onnxruntime-ep-mlx): a locator that bundles + registers
the cargo-built dylib (hatchling build hook, hatch_build.py)
tests/ MLX op-correctness (tests/ops, pytest) + ONNX-standard conformance (tests/conformance)
.github/ CI (cargo build + op tests) and PyPI trusted-publishing workflows
Testing
Build the EP (above), then run the pytest op-correctness suite (MLX vs ORT CPU reference):
export ONNXRUNTIME_MLX_EP_LIB=$PWD/rust/target/release/libonnxruntime_mlx_ep.dylib
export DYLD_LIBRARY_PATH=<ort-prebuilt/lib>
python -m pytest tests/ops -q
tests/ops— each translated decoder op via MLX vs. ORT CPU reference (tolerance-gated, pytest)tests/conformance— opt-in fuzz-conformance of the MLX EP against the ONNX standard (cbourjau/onnx-tests); seetests/conformance/README.md
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file onnxruntime_ep_mlx-0.27.6.tar.gz.
File metadata
- Download URL: onnxruntime_ep_mlx-0.27.6.tar.gz
- Upload date:
- Size: 258.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
225ef7b899126a5d977d24c02ab476b160a024e55d2520c958ed3571c2df69de
|
|
| MD5 |
2862109c8e822a108d50dc189ad6f142
|
|
| BLAKE2b-256 |
5fd3dc41545bd3948e0feb25814cc517efa397f06a49847bf3434791452fa795
|
Provenance
The following attestation bundles were made for onnxruntime_ep_mlx-0.27.6.tar.gz:
Publisher:
publish.yml on justinchuby/onnxruntime-mlx
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
onnxruntime_ep_mlx-0.27.6.tar.gz -
Subject digest:
225ef7b899126a5d977d24c02ab476b160a024e55d2520c958ed3571c2df69de - Sigstore transparency entry: 2514608863
- Sigstore integration time:
-
Permalink:
justinchuby/onnxruntime-mlx@dcfb56e78aaf40e1591731aa335e1b827fc831c5 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/justinchuby
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@dcfb56e78aaf40e1591731aa335e1b827fc831c5 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file onnxruntime_ep_mlx-0.27.6-py3-none-macosx_14_0_universal2.whl.
File metadata
- Download URL: onnxruntime_ep_mlx-0.27.6-py3-none-macosx_14_0_universal2.whl
- Upload date:
- Size: 38.0 MB
- Tags: Python 3, macOS 14.0+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7749cf9f5d4658fae5b4bd493bb309b413a434d46c6945a36923bac9a02a3ffa
|
|
| MD5 |
917a00b6425f180e4c2d1601a05a220f
|
|
| BLAKE2b-256 |
dcd37fac9bc105290afb970372af65ab93a2e06cbc7cc7b2e9032d4589cf435f
|
Provenance
The following attestation bundles were made for onnxruntime_ep_mlx-0.27.6-py3-none-macosx_14_0_universal2.whl:
Publisher:
publish.yml on justinchuby/onnxruntime-mlx
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
onnxruntime_ep_mlx-0.27.6-py3-none-macosx_14_0_universal2.whl -
Subject digest:
7749cf9f5d4658fae5b4bd493bb309b413a434d46c6945a36923bac9a02a3ffa - Sigstore transparency entry: 2514608899
- Sigstore integration time:
-
Permalink:
justinchuby/onnxruntime-mlx@dcfb56e78aaf40e1591731aa335e1b827fc831c5 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/justinchuby
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@dcfb56e78aaf40e1591731aa335e1b827fc831c5 -
Trigger Event:
workflow_dispatch
-
Statement type: