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:
    brew install mlx
    
    (See the MLX installation guide for alternative installation methods. Building Go programs succeeds even if mlx is not installed, and libmlx.dylib is 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 with mlx::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 without mlx::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 of Execute(), deferring mlx::core::contiguous until the buffer is accessed on the CPU via Buffer.Data() or Buffer.ToFlatData().

Environment Variables

  • MLX_LIBRARY_PATH: Optional explicit path to libmlx.dylib (if installed in a non-standard location outside /opt/homebrew/lib/libmlx.dylib or /usr/local/lib/libmlx.dylib).

Architecture & Features

  • Zero-Copy Unified Memory Shared Buffers (mlx::core::array): Allocates unified-memory buffers directly from mlx::core::allocator::malloc, exposing shared CPU/GPU pointers as typed Go slices with atomic handle management and runtime.AddCleanup.
  • Native In-Place Buffer Donation: Releases external mlx::core::array references for donated inputs (donate[i] == true) before mlx::core::eval(outputs) so use_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/3D ConvGeneral, general N-D ReduceWindow and SelectAndScatterMax/Min (via mlx::core::as_strided), BatchNormForInference/Training/Gradient, Call (via tape inlining), pattern-matched Sort, and fused ops (FusedSoftmax, FusedLayerNorm via mlx::core::fast::layer_norm, FusedActivation, FusedDense, FusedAttentionQKVProjection, and FusedScaledDotProductAttention).

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 (If and While; Call is supported via tape inlining and Sort is 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