Skip to content

perf(models): compute Llama 4 chunked attention without a dense mask - #3104

Merged
inureyes merged 4 commits into
mainfrom
perf/issue-3100-llama4-chunked-attention
Nov 16, 2026
Merged

inureyes merged 4 commits into
mainfrom
perf/issue-3100-llama4-chunked-attention

Conversation

@inureyes

Copy link
Copy Markdown
Member

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::attend splits the step at chunk boundaries and runs MLX's causal-mode SDPA per piece over that piece's own keys (chunked_causal_attention in llama4_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's start_position, whichever is later.

The dense mask stays where prefill_uses_causal_mode says 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 keep ChunkedKVCache::attend. The legacy Llama4CxxModel::forward (plain KVCache) does not use the chunk mask and is unchanged.

Measurements (M5 Max, llama-4-scout-17b-16e-instruct-4bit, 19717 tokens, two alternated repetitions)

Path Peak, main to branch Prefill, main to branch (rep 1, rep 2)
single pass 72.88 to 68.41 GB 37.92 / 27.31 s to 38.50 / 27.73 s
2048-token chunks 65.86 to 65.70 GB 22.25 / 27.48 s to 21.29 / 17.84 s

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 in docs/benchmark_results/llama4-chunked-attention-m5max-2026-11-15.md.

Test plan

  • New unit tests compare the maskless path with the dense mask at offset 0 (inside one chunk and across chunks), mid-chunk, mid-chunk across a boundary, behind a trimmed front, on a chunk boundary, and in f16; a synthetic-weight model test checks a 5 + 15 split prefill against the single pass (models::llama4, 18 passed).
  • create_causal_mask_roster_count_matches_repo, cargo fmt --check, clippy for mlxcel lib 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 last chunk_size keys and so can include the previous chunk's keys once a context passes 8192 tokens; mlx-lm's chunk_mask excludes them. That predates this PR and is left for a separate issue.

Closes #3100

@inureyes inureyes added status:done Completed type:performance Performance improvements priority:medium Medium priority area:models Model architectures, weights, loading, metadata area:inference Generation, sampling, decoding (incl. speculative, DRY) labels 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
inureyes force-pushed the perf/issue-3100-llama4-chunked-attention branch from 17d5f77 to 3b417a2 Compare November 16, 2026 00:23
@inureyes
inureyes marked this pull request as ready for review November 16, 2026 00:23
@inureyes
inureyes merged commit ab2f1d0 into main Nov 16, 2026
@inureyes
inureyes deleted the perf/issue-3100-llama4-chunked-attention branch November 16, 2026 00:24
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:inference Generation, sampling, decoding (incl. speculative, DRY) area:models Model architectures, weights, loading, metadata priority:medium Medium priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(models): compute Llama 4 chunked attention without a dense mask

1 participant