Repository navigation
MiMo-V2: fused prefill and decode attention for the 192/128 global layers - #1927
Open
aidiffuser wants to merge 1 commit into
Open
aidiffuser wants to merge 1 commit into
aidiffuser wants to merge 1 commit into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
MiMo-V2's global-attention layers use 192-wide queries/keys and 128-wide values.
mx.fast.scaled_dot_product_attentionhas no fused kernel for different key and value widths, so these layers take the unfused path:This PR adds two
mx.fast.metal_kernelkernels inmlx_lm/models/mimo_v2_attention.pyand uses them from the global layers inmimo_v2_flash.py: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.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):
MiMo-V2.6-Flash shapes (64 / 4 heads) give similar ratios.
End to end,
stream_generateon MiMo-V2.6-Flash (mxfp4 experts, 8-bit rest; 9 of its 48 layers are global), defaultprefill_step_size, greedy, 256 generated tokens: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, ...)againstmx.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_flashhas 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:
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:
Tests
tests/test_mimo_v2_attention.py:mimo_v2_flashmodel: the kernels run (1 prefill, 3 decode steps) and match the existing path.tests/test_models.pypasses 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