Skip to content

mlx-exact 0.12.0

Choose a tag to compare

@audenvenn audenvenn released this 21 Nov 05:45

Correction (2026-11-21): on the real mlx-community/gemma-3-4b-it-qat-4bit, batched sliding windows and decode after the window has slid are not bit-exact for head dims 192/256 with several KV heads.

  • MLX's unfused attention softmax depends on the keys' offset modulo 512 or more, while mlx-exact aligns them modulo 32.
  • The long-prompt crash fix and chunked prefill are fine.
  • See the README's Limitations; a fix is in progress.

Head dims 192/256 with several KV heads (Gemma 3 4B and larger): two fixes, and batched sliding windows.

  • Fix (since 0.1.0): in exact mode, prompt chunks of 1,024 or more tokens with an array mask raised NotImplementedError.
    • This hit a single prompt over 1,024 tokens on a sliding-window layer (mlx-lm prefills in 2,048-token steps), and long left-padded batches.
    • Such chunks are now split into blocks of at most 1,008 rows on MLX's unfused attention path, which decode and shorter chunks use.
  • Fix (since 0.6.0): when a causal chunk of 1,024 or more tokens on a sliding-window layer was split, each block re-derived its key alignment from the cache and could shift its keys by up to 31 positions. Values were correct, but the bits could differ from other schedules.
  • Batched sliding windows are bit-exact for these head dims too: each sequence is placed at its absolute positions modulo 32. This was the last batched case the README listed as not covered.
  • New tests:
    • tests/test_long_d256.py: a 1,100-token prompt prefilled whole, in chunks of 300, or token by token gives bit-identical logits; a left-padded batch of 1,100- and 1,040-token prompts matches each sequence alone.
    • A Gemma 3 4B-like case in tests/test_batch_sliding.py, which fails with 0.11.0.
  • README: a related-work section (batch-invariant kernels on CUDA and MLX; LLM-42, CoRun, Vosti).
  • 264 tests.

Install: pip install "mlx-exact[lm] @ git+https://github.com/audenvenn/mlx-exact@v0.12.0". Validated on Apple M6 with MLX 0.32.4 and mlx-lm 0.32.2 (tiny random-weight models for these cases).

Developed with AI assistance (Anthropic's Claude).