Skip to content

feat: Donate a history-boundary snapshot during prefill for snapshot-only families #1143

Closed

Description

@inureyes

Part of epic #1148.

Problem / Background

For snapshot-only families (every model reporting supports_snapshot_reuse(): bailing_moe_linear, falcon_h1, gemma4, granitemoehybrid, jamba, lfm2, mamba, mamba2, muse_glimmer, nemotron_h, plamo2, qwen3_5, qwen3_next), the only prompt-cache reuse path is the exact-prefix snapshot lookup in PromptCacheStore::lookup_snapshot_prefix (src/server/prompt_cache/store.rs ~line 901). The entry donated at end of generation (donate_finished_sequence_cache, src/server/batch/scheduler.rs ~line 2195) stores the whole token vector: previous prompt plus generated tokens. That vector must be an exact prefix of the next request's tokens for a hit.

In practice this never happens, for three independent reasons verified live on 2026-08-14 (main at 9c154ff):

  • (a) Generation-prompt-only tokens. gemma-4-31b-it-4bit: the template appends an empty thought scaffold <|channel>thought\n<channel|> only to the generation prompt, never in history re-render. Token-level proof: stored entry 139 tokens (94 prompt + 45 generated), turn-2 prompt shares only 90, divergence tokens [100, 45518, 107, 101] detokenize to exactly the scaffold. qwen3.5-4b-4bit with enable_thinking=false has the same shape via its empty <think>\n\n</think>\n\n injection (turn 2 cached_tokens = 0 of 177).
  • (b) Thinking stripped from history. qwen3.5-4b-4bit default mode primes <think>\n in the generation prompt, but the template renders assistant history before the last user query without the <think> block, so the vectors diverge right after <|im_start|>assistant\n (turn 2 cached_tokens = 0 of 123). Gemma 4 with enable_thinking=true hits the same class via strip_thinking.
  • (c) Retokenization drift. falcon-h1-tiny-90m-instruct-4bit has a plain ChatML template with no thinking scaffold and still misses (turn 2 cached_tokens = 0 of 250): the donated vector stores 120 sampled completion tokens while the same reply text re-tokenizes to 118 tokens. Sampled sequences are not canonical tokenizations.

The insight: all three divergence classes happen after the history boundary. The history portion of the prompt (everything rendered with add_generation_prompt=false) is prefix-stable across turns regardless of template quirks.

Proposed Solution

Capture a second snapshot at the history boundary during prefill, in addition to the existing end-of-generation donate:

  1. The route layer already renders the chat template. Render it a second time with add_generation_prompt=false, tokenize, and carry that boundary token count in PromptCacheRequestContext (src/server/config.rs).
  2. The scheduler aligns a prefill chunk boundary at that index (chunked prefill already exists via --prefill-chunk-size) and captures an additional model-state snapshot there, keyed by the boundary token prefix.
  3. Insert the boundary snapshot into the store alongside the existing end-of-generation donate.

Because history rendering is prefix-stable, the boundary snapshot is guaranteed to be a prefix of every follow-up turn's prompt. This neutralizes classes (a), (b), and (c) by construction: the donated vector is produced by tokenizing the re-rendered history form itself.

Acceptance Criteria

  • Turn N+1 against gemma-4-31b-it-4bit restores the boundary snapshot and reports cached_tokens equal to the boundary length through the production /v1/chat/completions path (deferred to the epic-level verification run: only small checkpoints were exercised on this branch)
  • Turn N+1 against qwen3.5-4b-4bit (default thinking mode) does the same, covering the GatedDeltaNet side of the class where partial restore is impossible and this path is the only route to reuse
  • Verification is counter-based via /v1/cache/stats (snapshot_hits advances), not wall-clock only
  • The existing end-of-generation snapshot donate stays intact
  • Integrated end to end: route context field, scheduler capture, store insert, adopt path; not a standalone helper
  • No regression for dense-KV families (llama control scenario stays at parity)

Technical Considerations

  • Measured baseline (2026-08-14, Apple Silicon dev box, local main at 9c154ff, cargo build --release --features metal,accelerate): gemma-4-31b two-turn chat, turn 2 usage.prompt_tokens_details.cached_tokens = 0 of 169; qwen3.5-4b, 0 of 123 (thinking on) and 0 of 177 (thinking off); falcon-h1-tiny, 0 of 250. Control on the same binary: llama-3.2-1b-instruct, identical scenario, turn 2 cached_tokens = 256 of 308, hits = 1. The store, key composition, and adopt paths work; the failure is specific to the snapshot-only path.
  • Gemma 4 uses sliding_window = 1024 with 60 layers in a 5:1 sliding:full pattern, which bounds where rotating-layer truncation is mechanically possible (relevant to the follow-up partial-restore work). Recurrent-state families (GatedDeltaNet, SSM) cannot truncate at all, so this boundary snapshot is their load-bearing fix.

Activity

  1. added
    status:ready
    type:enhancement
    priority:high
    area:inference
    status:in-progress
    status:review
    and removed
    status:ready
    status:in-progress
    on Aug 14, 2026
  2. added
    status:done
    and removed
    status:review
    on Aug 14, 2026
  3. 3 remaining items

  4. added a commit that references this issue on Aug 15, 2026
  5. 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:enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions