Expand description
Safe ownership boundary around the official MLX C API.
This crate owns exactly one responsibility: the MLX-C C ABI translated
into idiomatic Rust. It holds the bindgen-generated raw declarations, the
captured-error machinery, the owned handle types (MlxArray, MlxDtype,
MlxArrayVector, MlxStream), the per-worker bindings context
(MlxBindingsContext: GPU stream and linked version), and every operation
wrapper family: creation, shape, padding, elementwise math, activation,
normalization, convolution, random, rope, quantized operations,
quantization construction, NVFP4, attention, the compiled-graph family,
the Metal kernels and capture, the general operation set, and the gap
shim family — this repository’s own mlxrs_* C ABI (compiled from
native_gap_shim/ by the native build engine) for MLX functions the
vendored MLX-C pin has not wrapped yet, so an MLX capability is
reachable the day the pin moves. It contains
no embedding policy: memory limits, the metallib path selection and
verification, safetensors vtable policies, and process I/O accounting
belong to the embedding application, which links this crate against an
MLX-C library and converts MlxCError values into its own typed runtime
errors at its boundary.
§API-surface completeness
The bridge is complete, not selective: every public function and type the
vendored MLX-C headers declare is carried here. The build script
translates the vendored tree in full and emits a bridged-symbol inventory
alongside the declarations, so any upstream pin bump that adds, removes,
or renames surface shows up as an inventory change the coverage contract
can fail on. The recorded state for the current pin lives in this crate’s
COVERAGE_INVENTORY.md.
Source-reference audits must never drive removals here: a wrapper with no caller yet is the expected steady state of a complete bridge — the crate exists precisely so that every MLX-C capability is reachable without waiting for a wrapper to be written on demand.
§Bindings-only mode
Consumers who provide their own native MLX build — for example a pinned,
patched fork produced by their own native orchestration — can take this
crate as the bridge alone: depend with default-features = false and
the build script emits no link directives at all. The vendored-header
declarations, the captured-error machinery, the owned handle types, and
every operation wrapper family are identical; the consumer’s own build
supplies and links the native libraries. The default link feature
carries the whole discovery ladder for everyone else.
§Test placement
Tests that never touch MLX-C symbols (the error-channel machinery) live
in this crate’s test tree. Anything exercising the bindings themselves
needs an MLX-C library at link time, discovered through the
MLX_C_RUST_MLX_C_PREFIX environment variable or pkg-config mlx-c
(see the build script and README); without one, library compilation and
documentation builds still succeed and only test binaries fail to link.
Re-exports§
pub use error::MlxCError;pub use error::check_status;pub use error::clear_captured_mlx_error;pub use error::install_non_terminating_error_handler;pub use error::take_captured_mlx_error;
Modules§
- error
- Captured MLX-C error translation.
- raw
- Bindgen-generated declarations for the complete official MLX C boundary.
Structs§
- Graph
Output File - A rendered-graph destination backed by a C
FILE*the caller owns for the duration of one rendering call. - MlxArray
- Owned MLX array handle released exactly once through the official C API.
- MlxArray
Vector - Temporary MLX vector that retains array handles for one aggregate operation.
- MlxBindings
Context - Process-global official MLX C bindings context configured for one worker.
- MlxClosure
- Owned MLX closure handle released exactly once through the C API.
- MlxClosure
Custom - Owned MLX custom-transform (VJP) closure, released exactly once.
- MlxClosure
Custom Jvp - Owned MLX custom JVP closure, released exactly once.
- MlxClosure
Custom Vmap - Owned MLX custom VMAP closure, released exactly once.
- MlxClosure
Kwargs - Owned MLX closure with keyword arguments, released exactly once.
- MlxClosure
Value AndGrad - Owned MLX value-and-gradient closure, released exactly once.
- MlxCompile
Cache - Owned MLX compile cache handle released exactly once.
- MlxCompiled
Dual Head Norm - Owns the compiled dual-output query/key head-normalization graph.
- MlxCompiled
Elementwise Graphs - Retained shapeless compilations for elementwise model graph composites.
- MlxCompiled
Gated Delta Rate Activation - Owns the compiled dual-output rate-activation graph.
- MlxCompiled
Multi Output Graph - Owns one compiled MLX graph that produces an ordered vector of outputs.
- MlxCompiled
SwiGlu - Owned shapeless MLX compilation of
silu(gate) * input. - MlxCuda
Kernel - Owned MLX-C CUDA kernel handle.
- MlxCuda
Kernel Config - Owned MLX-C CUDA kernel configuration handle.
- MlxDevice
Handle - Owned MLX device handle released exactly once through the official C API.
- MlxDevice
Info - Owned MLX device information table released exactly once.
- MlxDistributed
Group - Owned MLX distributed group handle released exactly once.
- MlxFunction
Exporter - Owned MLX function exporter released exactly once.
- MlxImported
Function - Owned MLX imported function released exactly once.
- MlxIo
Gguf - Owned MLX GGUF container handle released exactly once.
- MlxIo
Reader - Owned MLX reader transport released exactly once.
- MlxIo
Writer - Owned MLX writer transport released exactly once.
- MlxMap
String ToArray - Owned MLX map from strings to arrays, released exactly once.
- MlxMap
String ToArray Iterator - Owned iterator over an MLX string-to-array map.
- MlxMap
String ToString - Owned MLX map from strings to strings, released exactly once.
- MlxMap
String ToString Iterator - Owned iterator over an MLX string-to-string map.
- MlxMetal
Kernel - Owned MLX custom Metal kernel function compiled from a source string.
- MlxMetal
Kernel Config - MlxMetal
Kernel Output - Output shape and dtype requested from one custom Metal kernel launch.
- MlxNode
Namer - Owned MLX node namer released exactly once.
- MlxStream
- Owned MLX stream handle used to preserve runtime thread affinity.
- MlxStream
Thread Local - Per-thread MLX stream slot for one device.
- MlxString
- Owned MLX string handle released exactly once through the official C API.
- MlxVector
Int - Owned MLX integer vector handle released exactly once through the C API.
- MlxVector
Stream - Owned MLX vector of streams; elements are copied out as owned stream handles.
- MlxVector
String - Owned MLX vector of strings; elements are borrowed from the vector for each read and copied into Rust memory.
- MlxVector
Vector Array - Owned MLX vector of array vectors.
Enums§
- MlxDtype
- MLX array element types exposed without leaking generated C declarations.
- MlxMetal
Kernel Template Argument - Compile-time template argument passed to an MLX custom Metal kernel launch.
Functions§
- active_
memory_ bytes - Samples process-local MLX active-memory bytes.
- apply_
metal_ kernel_ in_ graph - Applies one custom Metal kernel inside a compiled-graph trace.
- array_
from_ vector - Reads one array out of a closure’s input vector inside a graph builder.
- cache_
memory_ bytes - Samples process-local MLX allocator-cache bytes.
- clear_
allocator_ cache - Releases reclaimable MLX allocator-cache allocations.
- clear_
streams - Frees every stream created in this process except the defaults in use.
- cuda_
is_ available - Whether the CUDA backend reports availability in this process.
- disable_
compile - Disables MLX’s compilation transform globally.
- distributed_
is_ available - Whether MLX’s distributed backend is initialized in this process.
- enable_
compile - Re-enables MLX’s compilation transform globally.
- export_
function - Exports a closure and example arguments to an MLX function file.
- export_
function_ with_ keywords - Exports a kwargs closure and example arguments to an MLX function file.
- export_
graph_ ⚠to_ dot - Writes the graph behind the outputs to a Graphviz DOT file.
- graph_
output_ array - Runs one raw MLX operation inside a graph builder and owns its lazy output.
- initialize_
distributed - Initializes MLX’s distributed backend.
- jvp_
of_ closure - Computes the Jacobian-vector product of a closure.
- load_
array_ from_ reader - Loads one array from an open reader transport.
- load_
safetensors - Loads the string-to-array and string-to-string safetensors maps from a file.
- load_
safetensors_ from_ reader - Loads the safetensors maps from an open reader transport.
- memory_
limit_ bytes - Reads the allocator limit currently enforced by MLX.
- metal_
is_ available - Whether the MLX-Metal backend is available in this process.
- metallib_
path - The linked MLX-Metal backend’s metallib path.
- peak_
memory_ bytes - Samples process-local MLX peak active-memory bytes.
- print_
graph ⚠ - Prints the graph behind the outputs as text.
- reset_
peak_ memory - Resets MLX’s process-local peak active-memory counter.
- save_
array_ to_ writer - Saves one array through an open writer transport.
- save_
safetensors - Saves the parameter and metadata maps to a safetensors file.
- save_
safetensors_ to_ writer - Saves the parameter and metadata maps through an open writer transport.
- set_
cache_ limit - Applies the allocator-cache retention limit and returns the previous limit in bytes.
- set_
compile_ mode - Selects the global compilation mode.
- set_
default_ stream - Replaces the process’s default stream.
- set_
graph_ ⚠output - Publishes the single output of a single-output graph builder.
- set_
graph_ ⚠output_ vector - Appends one lazy output to a multi-output graph’s result vector.
- set_
memory_ limit - Applies the graph-evaluation active-memory guidance and returns the previous limit in bytes.
- set_
metallib_ path - Overrides the metallib MLX loads.
- set_
wired_ limit - Raises the wired-memory ceiling for MLX allocations.
- synchronize
- Waits for submitted work on the given stream to complete.
- synchronize_
all_ defaults - Synchronizes work across all devices’ default streams.
- value_
and_ grad_ of_ closure - Builds the value-and-gradient closure for the given closure.
- vjp_
of_ closure - Computes the vector-Jacobian product of a closure.
- vmap_
replace - Replaces one traced array in a vector-of-arrays graph, the building block of the VMAP transform.
- vmap_
trace - Traces a closure for the VMAP transform, producing traced inputs and outputs plus the resolved axes.
Type Aliases§
- MlxGraph
Builder - The MLX C closure ABI every compiled-graph builder follows: it receives the output vector to populate and the input vector to read, and returns zero on success.