Apple MLX Backend ("mlx")
macOS Apple Silicon GPU backend powered by Apple's MLX framework, supporting both compiled static shapes and native dynamic shapes.
Importing & Prerequisites
- Included by default in
backends/default? Yes — automatically included on macOS (darwin, with CGo enabled) when importing_ "github.com/gomlx/gomlx/backends/default"(exclude at build time with--tags=nomlx). - Standalone blank import (links only the Apple MLX backend):
import _ "github.com/gomlx/go-darwinml/compute/mlx" - Runtime prerequisite: Requires MLX installed on macOS:(See the MLX installation guide for alternative installation methods. Building Go programs succeeds even if
brew install mlxmlxis not installed, andlibmlx.dylibis loaded dynamically at runtime when the"mlx"backend is selected.)
Overview
The "mlx" backend (github.com/gomlx/go-darwinml/compute/mlx), part of the github.com/gomlx/go-darwinml repository alongside the "mps" backend, executes GoMLX computation graphs on Apple Silicon GPUs using Apple’s MLX C++ framework (mlx::core).
Configuration Options & Environment Variables
Configure via GOMLX_BACKEND="mlx[:<options>]" or compute.NewWithConfig(...):
mlx(default): Compiled static-shape mode (DynamicShapesNone). Compiles the recorded op tape withmlx::core::compile(tape_fn, /*shapeless=*/false)for automatic kernel fusion, and materializes outputs as row-major contiguous arrays (eager_contiguous).mlx:dynamic: Native dynamic-shape mode (DynamicShapesNative,DynamicAxes: true). Replays the lazy MLX op tape directly on runtime input arrays withoutmlx::core::compile, supporting variable batch sizes and sequence lengths (shapes.DynamicDim) with zero recompilation overhead.mlx:dynamic,lazy_contiguous: In dynamic mode, skips materializing row-major contiguity on view outputs (Transpose,BroadcastInDim,Slice) at the end ofExecute(), deferringmlx::core::contiguousuntil the buffer is accessed on the CPU viaBuffer.Data()orBuffer.ToFlatData().
Environment Variables
MLX_LIBRARY_PATH: Optional explicit path tolibmlx.dylib(if installed in a non-standard location outside/opt/homebrew/lib/libmlx.dylibor/usr/local/lib/libmlx.dylib).
Architecture & Features
- Zero-Copy Unified Memory Shared Buffers (
mlx::core::array): Allocates unified-memory buffers directly frommlx::core::allocator::malloc, exposing shared CPU/GPU pointers as typed Go slices with atomic handle management andruntime.AddCleanup. - Native In-Place Buffer Donation: Releases external
mlx::core::arrayreferences for donated inputs (donate[i] == true) beforemlx::core::eval(outputs)souse_count() == 1, enabling MLX’s native in-place buffer reuse (move_shared_buffer) with only a single copy of model variables in memory. - Broad Operation & DType Coverage: Supports
Complex64,Bitcast,BitCount,Clz, bitwise reductions (ReduceBitwiseAnd/Or/Xor,ReduceLogicalXor), cumulative ops (CumSum,CumProd,CumMax,CumMin), 1D/2D/3DConvGeneral, general N-DReduceWindowandSelectAndScatterMax/Min(viamlx::core::as_strided),BatchNormForInference/Training/Gradient,Call(via tape inlining), pattern-matchedSort, and fused ops (FusedSoftmax,FusedLayerNormviamlx::core::fast::layer_norm,FusedActivation,FusedDense,FusedAttentionQKVProjection, andFusedScaledDotProductAttention).
Limitations
- Unsupported DTypes: Does not support
Float64,Complex128, or sub-byte integer types (Int2,Int4,Uint2,Uint4) on Apple Silicon Metal GPUs. - Unsupported Operations: Data-dependent control flow (
IfandWhile;Callis supported via tape inlining andSortis supported for standard ascending/descending comparators),SelectAndScatterSum,FFT, multi-device collective operations (AllReduce), and quantized fused ops (FusedQuantizedDense,QuantizedEmbeddingLookup).
Apple Silicon Performance Note
"go" vs "mlx" / "mps" on Apple Silicon: With the addition of ARM64 NEON and Apple SME (FEAT_SME) SIMD assembly microkernels and the 3-level parallel scheduler in the "go" backend, the "go" backend is often faster than "mlx" and "mps" on Apple Silicon Macs—particularly on small-to-medium models or workloads where Metal command-buffer dispatch and graph compilation overhead dominate (you can test "go" with or without SME via GOMLX_GO_SIMD_SME=0). We recommend benchmarking GOMLX_BACKEND=go (with GOEXPERIMENT=simd), GOMLX_BACKEND=mlx, and GOMLX_BACKEND=mps for your specific model.Last updated December 1, 2026