Skip to content

fix: harden sliding-window >window prefill across non-Gemma models; guard causal_attention windowed K/V slice #408

Closed

Description

@inureyes

Problem / Background

PR #405 (issue #401) fixed Gemma 3/4 single-pass prefill degenerating to <pad> when seq_len > window. The root cause chain:

  • create_causal_mask_with_window (in src/lib/mlxcel-core/src/utils.rs) CAPS the key axis to window when seq_len > window, producing a [seq_len, window] mask.
  • A fresh RotatingKVCache prefill keeps ALL keys, so gemma4's trim_mask_to_keys discards the capped mask (it does not match the full key count).
  • With the mask gone, attend falls back to causal_attention (in src/lib/mlxcel-core/src/layers.rs). Its needs_window_mask path slices K/V to the trailing window keys (start = k_len - window_size).
  • That trailing-window slice is only correct for single-query decode. For a multi-token prefill (q_len > 1, k_len > window) it strands the earliest query rows (logical position < seq_len - window) with no visible key, producing an all-masked row that softmaxes to NaN.

The #405 fix introduced create_causal_mask_with_window_full and a per-model sliding_prefill_mask helper, but wired it into gemma3.rs and gemma4.rs ONLY. The underlying causal_attention trailing-window K/V slice remains a latent footgun for ANY other caller that passes q_len > 1, k_len > window, and a discarded/short mask.

PR #405's review also found and fixed a related HIGH severity bug (gemma3 over-window with sliding_offset > 0 shape mismatch), so the corrected gate is seq_len > window && sliding_offset == 0. Use that as prior art for any generalized fix.

Proposed Solution

Two concrete asks:

  1. Add a defensive guard in causal_attention (src/lib/mlxcel-core/src/layers.rs): when q_len > 1 && k_len > window and the explicit mask was discarded, either build the correct windowed mask over the full key set (generalizing the fix(models): gemma4 sliding-window single-pass prefill degenerates to <pad> beyond the window #401 fix so it protects all models) or fail loudly instead of silently stranding query rows. The single-query decode path must stay unchanged.
  2. Audit the other sliding-window models in the tree that rely on the clamped create_causal_mask_with_window (search for its callers and RotatingKVCache users beyond gemma3/gemma4) and confirm whether they can hit the same >window single-pass prefill degeneration; fix or document each. Likely candidates include any model combining sliding-window attention with a rotating cache.

Acceptance Criteria

  • A >window single-pass prefill on every sliding-window model in the tree produces coherent output (no <pad> / NaN), OR is explicitly documented as unsupported with a guard that fails loudly rather than silently degenerating.
  • The single-query decode path through causal_attention is unchanged (existing decode parity tests still pass).
  • A regression test covers the multi-token q_len > 1, k_len > window case at the causal_attention / mask layer.
  • All sliding-window callers of create_causal_mask_with_window / RotatingKVCache are audited; each is either fixed or has a documented guard.

Technical Considerations

Activity

  1. added
    type:bug
    area:inference
    area:models
    priority:medium
    status:backlog
    status:in-progress
    status:review
    and removed
    status:backlog
    status:in-progress
    on Jun 23, 2026
  2. added
    status:done
    and removed
    status:review
    on Jun 23, 2026
  3. self-assigned this
    on Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:inferencearea:modelspriority:mediumstatus:donetype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions