Skip to content

MiMo-V2: fused prefill and decode attention for the 192/128 global layers - #1927

Open
aidiffuser wants to merge 1 commit into
ml-explore:mainfrom
aidiffuser:mimo-v2-attention-kernels
Open

aidiffuser wants to merge 1 commit into
ml-explore:mainfrom
aidiffuser:mimo-v2-attention-kernels

Conversation

@aidiffuser

Copy link
Copy Markdown
Contributor

MiMo-V2's global-attention layers use 192-wide queries/keys and 128-wide values. mx.fast.scaled_dot_product_attention has no fused kernel for different key and value widths, so these layers take the unfused path:

  • prefill materializes the whole heads × queries × keys score matrix (35 GB for one 4096-token chunk against 32k keys on MiMo-V2.6-Pro shapes);
  • decode reads each KV head's keys and values once per query head, and 16 query heads share every KV head.

This PR adds two mx.fast.metal_kernel kernels in mlx_lm/models/mimo_v2_attention.py and uses them from the global layers in mimo_v2_flash.py:

  1. mimo_prefill_attention: causal flash attention at the native 192/128 widths. Scores stay in registers and meet the values in float32 (simdgroup matrix products, online softmax). Long contexts are split into launches of bounded length, so no single GPU command runs for seconds. The fragment layout follows MLX's steel attention kernel.
  2. mimo_decode_attention: split-context flash decoding. A KV head's 16 query heads are the rows of the matrix products, so its keys and values are read once; per-slice float32 partials are combined in a second pass. It reads the cache view in place through its strides and honours a boolean mask (a batch cache's left padding).

When they run: global layers without attention sinks, bfloat16, outside training (the kernels have no VJP), with a non-quantized cache. Prefill needs the "causal" mask string; array masks (batched prompts) keep the existing path. Decode takes no mask or a boolean mask. Sliding-window layers, sinks, quantized KV caches, other dtypes and training are unchanged.

Measured

M3 Ultra (512 GB), one machine, bfloat16.

Attention call alone (MiMo-V2.6-Pro shapes: 128 query heads, 8 KV heads):

stock this PR
prefill, 4096 queries × 32k keys 774 ms, peak 35.2 GB 514 ms, peak 0.8 GB
prefill, 4096 queries × 64k keys 1741 ms, peak 69.9 GB 1104 ms, peak 1.0 GB
decode, 32k keys 891 µs 423 µs (2.10×)
decode, 128k keys 3358 µs 1391 µs (2.42×)
decode, 256k keys 6729 µs 2646 µs (2.54×)

MiMo-V2.6-Flash shapes (64 / 4 heads) give similar ratios.

End to end, stream_generate on MiMo-V2.6-Flash (mxfp4 experts, 8-bit rest; 9 of its 48 layers are global), default prefill_step_size, greedy, 256 generated tokens:

context prefill tok/s decode tok/s peak memory (weights 167.3 GB)
32k 448.6 → 487.9 (+9%) 38.9 → 42.1 (+8%) 178.6 → 170.5 GB
100k 296.9 → 344.6 (+16%) 30.2 → 37.6 (+25%) 197.3 → 172.1 GB

The attention transient above the weights falls from 11.3 to 3.2 GB at 32k, and from 30.0 to 4.8 GB at 100k.

Greedy output: at 32k the first 64 tokens are identical. At 100k they split at token 38, mx.roll(self.keys, self._idx, ...) against mx.roll(self.keys, -self._idx, ...), and both continue with well-formed code. The kernels keep scores and probabilities in float32, where the existing path rounds them to bfloat16.

Two machines, tensor-parallel

The same kernel sources have served MiMo-V2.6-Pro and Flash in production on two M3 Ultra Mac Studios since 23–24 September, through exo. mlx-lm's mimo_v2_flash has no tensor-parallel sharding, so these numbers can't be reproduced with mlx-lm alone. Per machine, Pro has 64 query and 4 KV heads, the same attention shapes as Flash on one machine.

MiMo-V2.6-Pro:

before → with the kernels
prefill, cumulative over ~33k / ~66k / ~140k tokens +9% / +18% / +22%
prefill memory at 140k, per machine +87 GB → +18 GB
decode at 140.6k 14.1 → 19.0 tok/s (+34%)
decode at 276.7k 10.0 → 16.7 tok/s (+67%)

A 276,693-token prompt now prefills at +33 GB per machine, where the unfused path would need about +170 GB.

The decode baseline here was already faster than the unfused path. It zero-padded the global layers' cache to 256/256 so that MLX's fused vector kernel applies. Against that kernel, per layer on these shapes:

  • at 131k keys, 2098 → 760 µs;
  • at 262k keys, 4352 → 1385 µs;
  • effective bandwidth ~250 GB/s through the padded cache, against 440–485 GB/s through the native one.

Tests

tests/test_mimo_v2_attention.py:

  • prefill and decode against a float32 reference (several lengths, batch 2 for decode);
  • split prefill launches bit-identical to one launch;
  • the boolean decode mask (left padding);
  • a strided cache view bit-identical to a contiguous copy;
  • unsupported calls declined (dtype, widths, group size, non-boolean mask);
  • a tiny 192/128 mimo_v2_flash model: the kernels run (1 prefill, 3 decode steps) and match the existing path.

tests/test_models.py passes unchanged. On machines without Metal the kernels are never built and every call takes the existing path.

#1219 (MiMo-V2.5 / V2.6 as mimo_v2.py) has the same attention layout, so the same dispatch would apply there if it lands.

Co-authored with Claude Opus 5.5

…yers

MiMo-V2's global-attention layers use 192-wide queries/keys and 128-wide
values, a shape mx.fast.scaled_dot_product_attention has no fused kernel
for, so they took the unfused path: prefill materialized the whole
heads x queries x keys score matrix, and decode read each KV head's keys
and values once per query head (16 query heads share a KV head).

Adds two mx.fast.metal_kernel kernels in mimo_v2_attention.py:
- causal flash-attention prefill at the native widths (scores stay in
  registers, float32 probabilities meet the values; long contexts are
  split into launches of bounded length);
- split-context flash decoding with a KV head's 16 query heads as the rows
  of the matrix products, honoring a boolean mask (batch left padding).
The global layers use them for bfloat16 calls without sinks or a quantized
cache outside training; everything else keeps the existing path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant