Skip to content

mlx-exact 0.13.0

Choose a tag to compare

@audenvenn audenvenn released this 21 Nov 10:58

Raw matmuls are rerouted in exact mode, and Gemma 2 is bit-exact.

  • Raw matmuls (a @ b, mx.matmul) used to stay schedule-dependent. MLX picks their kernel from the shape (gemv, gemv_wide, accelerator kernels, split-K for some unbatched shapes), so they rounded differently in decode and prefill. While exact mode is on:
    • a 2-D right operand (a weight applied as x @ w.T, such as MoE router gates and tied output layers) takes mlx-exact's dense Linear route;
    • batched products run on MLX's accelerator kernels with at least 16 rows and columns.
    • This applies to every @ in the process while exact mode is on.
  • Gemma 2 computes attention with raw matmuls (logit softcapping). It now also uses mlx-exact's softmax, because MLX's switches kernels above 4,096 keys.
    • With mlx-community/gemma-2-2b-it-4bit, decode after a 1,400-token prefill matches the full prefill at 100 of 100 positions (0.12.x and stock MLX: 0).
    • Speed: prefill 0.99×, decode 0.81× of stock.
    • Batching Gemma 2 fails in mlx-lm 0.32.2 itself (mask broadcasting).
  • The other real checkpoints (Llama-3.2-1B, Gemma-3-1B, Gemma-3-4B) are unchanged and bit-exact in every check.
  • Not covered: DeepSeek-V3's MLA, whose decode uses a different formulation than its prefill.
  • New tests: tests/test_raw_matmul.py, four tests that all fail without the change. 270 tests in all.

Install: pip install "mlx-exact[lm] @ git+https://github.com/audenvenn/mlx-exact@v0.13.0". Validated on Apple M6 with MLX 0.32.4 and mlx-lm 0.32.2.

Developed with AI assistance (Anthropic's Claude).