Skip to content

fix(gemma): rotating-cache chunked prefill corrupts sliding-window state; decode degenerates to pad after the window slides #678

Closed

Description

@inureyes

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

References

Activity

  1. added
    type:bug
    priority:high
    area:inference
    status:ready
    on Jul 6, 2026
  2. added
    status:done
    and removed
    status:ready
    on Jul 6, 2026
  3. self-assigned this
    on Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:inferencepriority:highstatus:donetype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions