Skip to content

fix(models): keep Llama 4 chunked decode inside the query's chunk - #3117

Merged
inureyes merged 3 commits into
mainfrom
fix/issue-3105-llama4-chunk-decode
Nov 16, 2026
Merged

inureyes merged 3 commits into
mainfrom
fix/issue-3105-llama4-chunk-decode

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

Past attention_chunk_size tokens (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::attend now 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 from llama4_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.
  • Before the boundary the ops are unchanged, so trace rows and short-context output are bit-identical to main.

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.

Arm 8001-8191 decided disagree 8192-8448 decided disagree Perplexity 8192-8448 (ref 5.86)
main, scalar decode 0 / 4 20 / 75 27.87
branch, scalar decode 0 / 4 0 / 75 5.86
main, B=2 batched 0 / 4 20 / 75 27.88
branch, B=2 batched 0 / 4 0 / 75 5.86

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=True and the llama3 rope_scaling. That needs its own issue.

Closes #3105

@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions priority:high High priority area:models Model architectures, weights, loading, metadata area:inference Generation, sampling, decoding (incl. speculative, DRY) status:review Under review labels Nov 16, 2026
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 16, 2026
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
inureyes force-pushed the fix/issue-3105-llama4-chunk-decode branch from cb36e1f to 4918783 Compare November 16, 2026 06:17
@inureyes
inureyes marked this pull request as ready for review November 16, 2026 06:17
@inureyes
inureyes merged commit 314ee10 into main Nov 16, 2026
@inureyes
inureyes deleted the fix/issue-3105-llama4-chunk-decode branch November 16, 2026 06:17
@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:high High priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(models): Llama 4 chunked layers attend across the chunk boundary at decode

1 participant