Repository navigation
Description
mlxcel-server emits reserved <unused…> garbage tokens on Gemma 4 sliding-window models: chunked-prefill continuation drops the sliding-window attention mask
Summary
On mlxcel-server, a Gemma 4 sliding-window model can collapse into a short repeating cycle of reserved <unusedNNNN> vocabulary tokens instead of a real answer. Observed on gemma-4-e4b-it-qat-4bit (a dense Gemma 4, enable_moe_block=false, sliding_window=512) with a thinking budget active. The server logs a matching warning at the moment it happens:
WARN chunked_prefill_continue{seq_id=seq-6 offset=2048 total=2201}:
mlxcel::models::gemma4: trim_mask_to_keys: mask shorter than key length,
discarding caller mask mask_len=160 key_len=671
Sample corrupted output:
<unused2115><unused2102><unused2120><unused2115><unused2120><unused2102><unused2102>...
(<unused2115> etc. are reserved high-vocab tokens, e.g. vocab ids 258027 / 258014 / 258032; the model should never emit them.)
Root cause
During server-side chunked prefill continuation (long prompt split into --prefill-chunk-size chunks, default 512, so decode can be interleaved for other active sequences), the scheduler passes a chunk-local causal prefill mask into the model forward. That mask is sized to the current continuation chunk (padded/tile-aligned, e.g. mask_len=160 for a ~153-token chunk), built via create_padded_prefill_mask (src/server/batch/scheduler.rs:4863 / 5024 / 5186 / 5370).
Gemma 4's sliding-attention layers, however, read a RotatingKVCache whose returned key length is the sliding window plus the new chunk tokens (here key_len=671, roughly sliding_window(512) - 1 + chunk). The per-layer guard trim_mask_to_keys (src/models/gemma4.rs:2364-2387) can only crop a mask that is longer than the keys; when the caller mask is shorter than the keys it cannot be fixed, so it is discarded and None is returned:
} else {
// mask is smaller than key length ...
tracing::warn!(mask_len, key_len, "trim_mask_to_keys: mask shorter than key length, discarding caller mask");
None
}With the mask dropped, the sliding-attention layers run for a multi-token continuation chunk (l > 1) with no window/causal mask. The kernel's internal fallback cannot reconstruct the correct causal + windowed alignment for queries at absolute positions [offset, total) against a rotated key buffer, so attention is computed incorrectly. The corrupted hidden state drives the LM head into a degenerate region and the model emits reserved <unused…> tokens in a short repeating cycle.
Why the visible failure is intermittent/hard to hit: it requires (1) a prompt long enough to cross a prefill_chunk_size boundary, and (2) concurrent load so the scheduler actually chunks-and-interleaves (the reporter's seq_id=seq-6 indicates several concurrent sequences). A single idle request often prefills whole and does not trigger it. A thinking budget makes it more likely by extending sequences and keeping the batch busy.
Reproduction
- Model:
gemma-4-e4b-it-qat-4bit(dense,sliding_window=512). - Serve with default chunking (
--prefill-chunk-size 512) and--max-batch-size > 1. - Drive concurrent requests, at least one with a prompt longer than 2048 tokens (crossing multiple chunk boundaries) while other sequences decode, optionally with a thinking budget (
thinking_budget_tokens). - Watch the server log for
trim_mask_to_keys: mask shorter than key length, discarding caller mask; responses that coincide with it can contain<unused…>token cycles.
Note: the reporter reproduced this on a production server; a deterministic minimal repro tied to the exact concurrent chunked-continuation + rotating-cache state is still being reduced. The warning above is direct evidence of the mask-drop.
Expected
The sliding-attention layers must always receive a mask whose key axis matches the RotatingKVCache's returned key length for the current chunk (correct causal + window alignment for queries at [offset, total)), so trim_mask_to_keys never has to discard it. No reserved <unused…> tokens should ever appear in output.
Proposed fix direction
- For chunked-prefill continuation, build the per-layer sliding mask from the same live-window length the rotating cache returns (mirror the non-chunked
l > 1branch that usesfirst_cache_live_len/create_sliding_window_prefill_mask,src/models/gemma4.rs:3139-3158), instead of relying on a chunk-local caller mask that is shorter than the sliding key buffer. - Make the too-short case in
trim_mask_to_keysa hard error (or a correct rebuild) rather than a silent discard, at least for multi-token (l > 1) chunks where dropping the mask corrupts attention. Silently discarding is only safe for single-token decode where the kernel derives its own mask. - Add a regression test: chunked prefill of a >
prefill_chunk_sizeprompt on a sliding-window Gemma 4 model, asserting no<unused…>tokens and output parity against unchunked (--prefill-chunk-size 0) prefill.
Impact
- Affects Gemma 4 sliding-window models served with chunked prefill under concurrent load.
gemma-4-e4b-it-qat-4bitconfirmed (dense, window 512); other Gemma 4 sliding-window checkpoints (window 1024) are likely susceptible under the same chunked-continuation condition. - User-visible: garbage
<unused…>responses.
Notes / related
- This is independent of the separate Gemma 4 26B A4B (MoE) multi-turn corruption, which is the fused single-token-decode MoE kernel (
MLXCEL_FUSED_MOE=0avoids that one);gemma-4-e4b-it-qat-4bithere is dense, so that path is not involved. - Related but distinct from bug: Gemma 4 reasoning channel leaks into CLI output as raw tokens #884 (Gemma 4
<|channel>thoughtreasoning-channel leak in CLI output). This issue is the<unused…>garbage collapse from the chunked-prefill mask drop; bug: Gemma 4 reasoning channel leaks into CLI output as raw tokens #884 is the reasoning-channel text leak. Both are Gemma 4 output-quality issues but have different root causes. - Environment: Linux GB10 (CUDA, sm_121), HEAD of
main.trim_mask_to_keysatsrc/models/gemma4.rs:2364; scheduler prefill masks atsrc/server/batch/scheduler.rs.
Activity
fix(gemma4): discard too-short mask instead of panicking (avoid MTP v…
fix(gemma4): size chunked-prefill masks to returned keys per family (#…
fix(gemma4): size chunked-prefill masks to returned keys per family (l…