Skip to content

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

Closed

Description

@inureyes

Problem

Llama 4's MoE (local) layers use chunked attention: a query attends causally only to keys in its own attention_chunk_size block (8192 for llama-4-scout). The prefill builds this as one dense create_chunked_attention_mask(seq_len, start_position, offset, chunk) array (src/models/llama4.rs around 1192 and 1266) and hands it to every MoE layer. After #3097, this mask is what sets Llama 4's peak:

llama-4-scout-17b-16e-instruct-4bit, 19,717 tokens main after #3097
single pass 73.00 GB
2048 chunks 66.79 GB

The mask is block-diagonal and causal, so it can be computed without the dense array: run causal-mode SDPA per chunk over that chunk's keys only, or use a window equal to the chunk where that is equivalent.

Acceptance Criteria

  • Llama 4 MoE-layer prefill no longer builds a dense chunked mask. Each query chunk attends to its own block in causal mode, including a prefill piece that starts mid-chunk behind a cache. The array path stays for padded, batched or caller-masked cases.
  • Unit tests on tiny random weights compare the new path with the dense-mask path for offsets 0, mid-chunk and across a chunk boundary.
  • Record llama-4-scout peak, prefill and greedy output at single pass and 2048 chunks, main against branch. If outputs differ, report the teacher-forced logit trace.

Refs #3097.

Implemented in #3104.

Activity

  1. added
    status:ready
    type:performance
    priority:medium
    area:models
    area:inference
    status:in-progress
    status:done
    and removed
    status:ready
    status:in-progress
    on Nov 15, 2026
  2. added a commit that references this issue on Nov 16, 2026
    ab2f1d0
  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:mediumstatus:donetype:performance

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions