Skip to content

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

Closed

Description

@inureyes

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__ in mlx_lm/models/llama4.py rebuilds the chunk mask on every forward, including decode. It computes block_pos from the key and query chunk indices (// attention_chunk_size) and uses chunk_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 though ChunkedKVCache.maybe_trim_front keeps a sliding window of the last chunk_size keys 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.md against the corrected reference. Decode tok/s is recorded before and after.

Refs #3100, #3104.

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.

Activity

  1. added
    status:ready
    type:bug
    priority:high
    area:models
    area:inference
    status:in-progress
    status:review
    status:done
    and removed
    status:ready
    status:in-progress
    status:review
    on Nov 16, 2026
  2. added a commit that references this issue on Nov 16, 2026
    314ee10
  3. added this to the 0.9 milestone on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:inferencearea:modelspriority:highstatus:donetype:bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions