Repository navigation
Description
Summary
After a MULTI-CALL (chunked) prefill on the gemma family, decode produces a few correct tokens and then collapses to endless <pad> (argmax over NaN logits) once the sliding-attention window slides onto cache slots left corrupted by the continuation append. This affects BOTH the new CLI cache-level chunked prefill (issue #674 validation battery) and the PRODUCTION server path, whose default prefill_chunk_size is 512: any gemma-4 server request with a prompt longer than 512 tokens degenerates once enough tokens are decoded.
Reproduction
GB10 CUDA, release build, gemma-4-e2b-it-4bit, sliding_window 512. Build a varied ~700-token prompt (80 short numbered "Chapter i" sentences), then:
- (a) CLI single-pass prefill (
MLXCEL_PREFILL_CHUNK=0): coherent continuation. - (b) CLI chunked prefill (chunk 512 or 1024 or 2048): 0 to 4 correct tokens then
<pad>forever. - (c) mlxcel-server (default chunked-512) with the same prompt and max_tokens 48: 10 correct tokens then
<pad>.
Evidence
Longer prompts move the collapse point later (a 6100-token prompt collapses at ~8 CLI tokens, and looked healthy in a 24-token server smoke, which is why this was never caught).
The attention-level chunk gate is not involved (MLXCEL_ATTENTION_CHUNK_BUDGET_MB=0 reproduces identically).
The server binary predates the #672/#676 changes, so this is a latent pre-existing bug, not a regression from them.
Root-cause lineage
Issue #401 fixed the single-pass over-window prefill pad degeneration. Issue #413 fixed the rolled-over over-window prefill for dense-KVCache sliding models (cohere2/gemma3n/olmo3) and explicitly assumed the RotatingKVCache-backed gemma3/gemma4 path handles offset>0 continuation correctly, leaving it out of scope. This issue is the missing case: a multi-token continuation append into an already-populated rotating sliding cache (trim + wraparound bookkeeping) leaves stale or misplaced physical slots, and decode's trailing-window reads hit them N steps later (N shrinks as the prompt/chunk geometry changes).
A 1-layer synthetic CPU gemma4 (window 8, chunk 8, including chunk greater than window with window 4) does NOT reproduce, so the corruption involves real-model geometry (60-layer interleave, window 512/1024, f16, proportional rope full-attn layers) or scale.
Scope
Family scope from the #674 parity battery (chunk 2048 vs single-pass, greedy, ~6k varied prompt):
- gemma-4-31b-it-4bit and gemma-4-e2b-it-4bit emit
<pad>. - gemma3-4b-4bit generates 0 tokens (immediate EOS).
- gemma3n-e2b-4bit degenerates into repeated code fences.
- llama-3.1-8b, qwen2.5-7b, qwen3.5-2b are token-identical; qwen3-30b-a3b differs by one near-tie token.
- gemma-2-9b-8bit and deepseek-v2-lite-4bit are coherent chunked (their single-pass side has separate pre-existing long-prompt degenerations worth noting).
Acceptance Criteria
- Chunked prefill (CLI
MLXCEL_PREFILL_CHUNKand serverprefill_chunk_size) followed by long decode is coherent and token-consistent with single-pass prefill on gemma-4 (e2b and a large variant), gemma3, and gemma3n. - A regression test covers multi-call prefill plus at least 2x window of decode at the cache or model layer.
- The feat: default the single-sequence CLI/bench prefill to chunked (2048) with a per-model opt-out policy #674 default chunked prefill can then remove any gemma-family opt-out.
References
- Blocked by relationship: feat: default the single-sequence CLI/bench prefill to chunked (2048) with a per-model opt-out policy #674 depends on this fix for the gemma family.
- Lineage: fix(models): gemma4 sliding-window single-pass prefill degenerates to <pad> beyond the window #401, fix(models): dense-cache sliding-window models still degenerate on rolled-over over-window prefill (offset>0 && seq>window) #413.
- Context (not the cause): fix: gemma-4 long-prompt prefill OOMs on CUDA because attention materializes O(heads*L^2) scores instead of using flash SDPA #672, fix(cuda/attention): bound long-prompt prefill memory for flash-ineligible models #676.
Activity
fix(cache): unbreak rotating-cache chunked prefill and default the CL…
fix(cache): bound the turbo4 sliding append to visible_len (#2330)
fix(cache): unbreak rotating-cache chunked prefill and default the CL…