Repository navigation
Closed
Labels
area:inferenceGeneration, sampling, decoding (incl. speculative, DRY)area:modelsModel architectures, weights, loading, metadatapriority:mediumMedium prioritystatus:doneCompletedtype:bugBug fixes, error corrections, or issue resolutions
Description
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(insrc/lib/mlxcel-core/src/utils.rs) CAPS the key axis towindowwhenseq_len > window, producing a[seq_len, window]mask.- A fresh
RotatingKVCacheprefill keeps ALL keys, so gemma4'strim_mask_to_keysdiscards the capped mask (it does not match the full key count). - With the mask gone,
attendfalls back tocausal_attention(insrc/lib/mlxcel-core/src/layers.rs). Itsneeds_window_maskpath slices K/V to the trailingwindowkeys (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:
- Add a defensive guard in
causal_attention(src/lib/mlxcel-core/src/layers.rs): whenq_len > 1 && k_len > windowand 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. - Audit the other sliding-window models in the tree that rely on the clamped
create_causal_mask_with_window(search for its callers andRotatingKVCacheusers beyond gemma3/gemma4) and confirm whether they can hit the same>windowsingle-pass prefill degeneration; fix or document each. Likely candidates include any model combining sliding-window attention with a rotating cache.
Acceptance Criteria
- A
>windowsingle-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_attentionis unchanged (existing decode parity tests still pass). - A regression test covers the multi-token
q_len > 1, k_len > windowcase at thecausal_attention/ mask layer. - All sliding-window callers of
create_causal_mask_with_window/RotatingKVCacheare audited; each is either fixed or has a documented guard.
Technical Considerations
- Generalized fix should reuse
create_causal_mask_with_window_full(added by fix(models): correct gemma sliding-window prefill beyond the window #405) rather than introduce a parallel mask builder. - The gate must follow the fix(models): correct gemma sliding-window prefill beyond the window #405 prior art:
seq_len > window && sliding_offset == 0(the over-windowsliding_offset > 0shape-mismatch case was the related HIGH fixed in fix(models): correct gemma sliding-window prefill beyond the window #405's review). - Decode (
q_len == 1) must keep the trailing-window K/V slice fast path; only the multi-token prefill case needs the corrected mask / loud failure.
Metadata
Metadata
Assignees
Labels
area:inferenceGeneration, sampling, decoding (incl. speculative, DRY)area:modelsModel architectures, weights, loading, metadatapriority:mediumMedium prioritystatus:doneCompletedtype:bugBug fixes, error corrections, or issue resolutions
Activity
fix(models): cover olmo3 in sliding-window >window prefill hardening
fix: harden sliding-window over-window prefill across models (#412)
refactor: hoist gemma3/gemma4 sliding_prefill_mask to shared helper (#…
fix(models): dense-cache sliding-window prefill uses full mask over a…
fix(test): reject NaN in iquestloop chunked prefill checks
fix(test): reject NaN in iquestloop chunked prefill checks
fix(test): bound iquestloop chunked prefill by measured bf16 noise (#…
fix: harden sliding-window over-window prefill across models (lablup#412
refactor: hoist gemma3/gemma4 sliding_prefill_mask to shared helper (l…
fix(models): dense-cache sliding-window prefill uses full mask over a…