Repository navigation
perf(models): compute Llama 4 chunked attention without a dense mask - #3104
Merged
Merged
Conversation
This was referenced Nov 16, 2026
Llama 4's MoE layers built one dense f32 chunk mask per prefill forward and ran the masked SDPA over every visible key. The chunked layers now take no mask: `Llama4Cache::attend` splits an unmasked multi-token step at chunk boundaries and runs MLX's causal-mode SDPA per piece over that piece's own keys (`chunked_causal_attention`), so a piece starting mid-chunk behind a cache attends to the cached keys of its chunk plus its own. A decode row or a caller mask keeps `ChunkedKVCache::attend`. Unit tests compare the maskless path against the dense mask at offset 0, mid-chunk, across a boundary, behind a trimmed front and in f16, and a synthetic-weight model test checks a mid-chunk split prefill against the single pass. Refs #3100
…apply Batched rows, speculative verify blocks and MLXCEL_PREFILL_CAUSAL_MODE=0 keep the dense chunk mask, the same gate prefill_causal_mask uses, so the kill switch restores main's dispatch for an A/B. Refs #3100
Peak, prefill and output identity for llama-4-scout at single pass and 2048-token chunks, main against branch, plus the teacher-forced logit trace across a chunk boundary. Refs #3100
inureyes
force-pushed
the
perf/issue-3100-llama4-chunked-attention
branch
from
November 16, 2026 00:23
17d5f77 to
3b417a2
Compare
inureyes
marked this pull request as ready for review
November 16, 2026 00:23
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.
Summary
Llama 4's MoE (chunked-attention) layers built one dense f32
[L, S]chunk mask per prefill forward and ran the masked SDPA over every visible key, which set Llama 4's single-pass peak after #3097. A one-row prefill now passes no mask to those layers:Llama4Cache::attendsplits the step at chunk boundaries and runs MLX's causal-mode SDPA per piece over that piece's own keys (chunked_causal_attentioninllama4_helpers.rs). A piece that starts mid-chunk behind a cache attends to the cached keys of its chunk plus its own, through causal mode's bottom-right alignment; the key range starts at the chunk start or the cache'sstart_position, whichever is later.The dense mask stays where
prefill_uses_causal_modesays no (batched rows, speculative verify blocks,MLXCEL_PREFILL_CAUSAL_MODE=0), so the kill switch restores main's dispatch. A decode row and a caller mask keepChunkedKVCache::attend. The legacyLlama4CxxModel::forward(plainKVCache) does not use the chunk mask and is unchanged.Measurements (M5 Max, llama-4-scout-17b-16e-instruct-4bit, 19717 tokens, two alternated repetitions)
Greedy output is byte-identical in both cells and both repetitions. Prefill timing is within the shared host's load noise (load 1.6 to 30), so no prefill change is claimed. Teacher-forced logit trace, dense mask (
MLXCEL_PREFILL_CAUSAL_MODE=0) against the branch default, three 2048-token forwards behind 7168 tokens of context (each crosses the 8192 boundary mid-forward): 0 / 6144 top-1 disagreement, 0 / 756 on decided positions, max logit delta 0.0000, perplexity 11.4503 in both. A width-10000 single-pass trace ran out of memory in both arms (full-vocabulary logits per row), so the single pass is covered by the CLI identity and the unit tests. Details indocs/benchmark_results/llama4-chunked-attention-m5max-2026-11-15.md.Test plan
models::llama4, 18 passed).create_causal_mask_roster_count_matches_repo,cargo fmt --check, clippy formlxcellib and tests,check_env_var_docs.py,check_benchmark_raw_logs.py,make verify-kernel-dtype-keys.Not changed
Decode (
q_len == 1) still attends unmasked over the cache's visible window, which after the front trim holds the lastchunk_sizekeys and so can include the previous chunk's keys once a context passes 8192 tokens; mlx-lm'schunk_maskexcludes them. That predates this PR and is left for a separate issue.Closes #3100