Repository navigation
Closed
Labels
area:inferenceGeneration, sampling, decoding (incl. speculative, DRY)area:modelsModel architectures, weights, loading, metadatapriority:highHigh prioritystatus:doneCompletedtype:bugBug fixes, error corrections, or issue resolutions
Milestone
Description
Problem
Llama 4's chunked (local) attention layers attend across the chunk boundary during decode once the context passes attention_chunk_size (8192 for llama-4-scout).
- mlx-lm:
LlamaModel.__call__inmlx_lm/models/llama4.pyrebuilds the chunk mask on every forward, including decode. It computesblock_posfrom the key and query chunk indices (// attention_chunk_size) and useschunk_mask = (block_pos == 0) & token_pos. A query at absolute position p with 8192 <= p < 16384 therefore attends only to keys in [8192, p], even thoughChunkedKVCache.maybe_trim_frontkeeps a sliding window of the lastchunk_sizekeys that can still hold keys from the previous chunk. - mlxcel: a decode step (
q_len == 1) on the chunked layers attends without a mask over the cache's visible window. After the front trim, that window holds the last 8192 keys, so past position 8192 it includes keys from the previous chunk.
The result is decode output that diverges from the reference after 8K tokens of context. This predates #3104, which fixed the prefill path. It was found during #3100.
Acceptance Criteria
- At decode, and for any multi-token step that is not already chunk-split, the chunked layers attend only to keys in the query's own chunk. Either build the chunk restriction per step, or slice the window to keys at or after
floor(p / chunk) * chunk. - A unit test on tiny random weights decodes across a chunk boundary and matches a reference that applies the mlx-lm chunk mask. The test fails on the current code.
- On llama-4-scout, a greedy decode that crosses position 8192 matches mlx-lm's behavior, checked with a teacher-forced logit trace across the boundary per
docs/benchmarks.mdagainst the corrected reference. Decode tok/s is recorded before and after.
PR #3117. The mlx-lm comparison uses mlx-lm with its RoPE set to mlxcel's: stock mlx-lm differs from mlxcel at every context length because mlxcel's Llama 4 RoPE ignores traditional=True and the llama3 rope_scaling, a separate defect.
Metadata
Metadata
Assignees
Labels
area:inferenceGeneration, sampling, decoding (incl. speculative, DRY)area:modelsModel architectures, weights, loading, metadatapriority:highHigh prioritystatus:doneCompletedtype:bugBug fixes, error corrections, or issue resolutions
Activity
fix(models): keep Llama 4 chunked decode inside the query's chunk
docs(bench): record Llama 4 chunk-boundary decode measurements
fix(models): keep Llama 4 chunked decode inside the query's chunk (la…