Repository navigation
fix(models): keep Llama 4 chunked decode inside the query's chunk - #3117
Merged
Merged
Conversation
3 tasks done
Past attention_chunk_size tokens the front-trimmed window of a Llama 4 chunked (MoE) layer still holds keys of the previous chunk, and a maskless decode step attended over all of them. mlx-lm rebuilds its chunk mask on every forward, decode included, so a query sees only keys of its own chunk. ChunkedKVCache::attend now runs the chunk-local attention for every maskless step: a decode step slices the window to the chunk start and runs the same unmasked SDPA, and a multi-token step splits per chunk in causal mode (the #3100 prefill split, moved here from llama4_helpers so the single-row, batched-rows and batched-decode routes all share it). A caller mask still runs as given. Tests: a core test against a host-built mlx-lm chunk mask across three boundaries, and a tiny-model decode across chunk boundaries against the one-pass prefill. Both fail on main. Refs #3105
Teacher-forced trace across position 8192 on llama-4-scout (width-1 scalar and B=2 batched decode, against mlxcel's one-pass prefill and against mlx-lm with mlxcel's RoPE), plus generate A/B for a 554-token and an 8077-token prompt. Also notes the separate Llama 4 RoPE mismatch with mlx-lm the trace exposed. Refs #3105
inureyes
force-pushed
the
fix/issue-3105-llama4-chunk-decode
branch
from
November 16, 2026 06:17
cb36e1f to
4918783
Compare
inureyes
marked this pull request as ready for review
November 16, 2026 06:17
This was referenced Nov 16, 2026
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
Past
attention_chunk_sizetokens (8192 for llama-4-scout) a Llama 4 chunked layer's front-trimmed window still holds keys of the previous chunk, and the maskless decode step attended over all of them. mlx-lm rebuilds its chunk mask on every forward, so a query sees only its own chunk.ChunkedKVCache::attendnow runs chunk-local attention for every maskless step: a decode step slices the window to the query's chunk start and runs the same unmasked SDPA; a multi-token step keeps the perf(models): compute Llama 4 chunked attention without a dense mask #3100 per-chunk causal split, moved here fromllama4_helpers.rs. The single-row, batched-rows (attend_batched_rows) and batched-decode routes all go through it. A caller mask still runs as given.Validation (llama-4-scout-17b-16e-instruct-4bit, M5 Max)
Teacher-forced trace (
--decided 2), 448 width-1 decode positions behind 8001 tokens, boundary at query position 8192. Reference: mlxcel's one-pass prefill, which matches mlx-lm with mlxcel's RoPE on 0 / 941 decided disagreements.Decode tok/s, two repetitions with cooldowns and the arm order reversed: 554-token prompt + 128 tokens: 45.55 / 44.94 on main and 45.56 / 44.77 on the branch, output byte-identical. 8077-token prompt + 400 tokens: 27.04 / 27.12 on main and 28.09 / 28.37 on the branch, output diverges once the context passes 8192. Peak memory is unchanged. Details:
docs/benchmark_results/llama4-chunk-boundary-decode-m5max-2026-11-15.md.The new tests fail on main and pass on the branch. One is a core test against a host-built mlx-lm chunk mask over three boundaries. The other runs a tiny-model decode across chunk boundaries against the one-pass prefill. The narrow llama4 and attend test modules, clippy for both crates, fmt, env-var docs, raw-log and kernel-key checks also pass.
Not fixed here: stock mlx-lm still disagrees with mlxcel on about 46% of decided positions at any context, because mlxcel's Llama 4 RoPE ignores
traditional=Trueand thellama3rope_scaling. That needs its own issue.Closes #3105