Repository navigation
perf(prefix-cache): exact Qwen snapshot cuts, generated-turn checkpoints, zero-copy restore - #792
Conversation
/v1/messages kept only text blocks. An assistant turn of tool_use blocks rendered empty, and so did a user turn of tool_result blocks, so the model never saw its own call or its result and Claude Code re-issued the same first tool call forever. - tool_use ids go through tool memory like OpenAI tool_calls; ids the server no longer remembers fall back to the Qwen template's <tool_call> rendering. - Each tool_result becomes a tool message (string or text-block content); the user's own text in that turn follows them. - Claude Code ends every request with a system note, so a request "ends with a tool result" when the last message other than a system note is one. The agent-turn prefix snapshot (#775) depends on it.
…nts, zero-copy restore - Qwen starts a prefill chunk at every restore point, as DeepSeek does, so a snapshot lands on the chat boundary it was requested at instead of the 512-token chunk start before it, and a restore still reproduces a cold prefill. Between restore points chunks keep to the 512 grid, and no chunk in the middle of a prompt is shorter than 16 tokens (a 2-token chunk gave NaN logits on the R9700). The rule is qwen35_prefill_chunk_tokens(). - Masked prefill attention pads its KV span to a multiple of 256 for chunks of at least 32 tokens. Other spans fall back to the generic tile kernel on RDNA4 at head size 256: 12.5% slower and 7.6x further from the CPU reference at 33K tokens. - After a tool-call turn the server keeps the state the generation left as a checkpoint of prompt plus generated tokens, as far as the next request renders that turn with the same tokens. The backend may keep it live (snapshot_save_deferred) and copy it out only before other work would overwrite the live state; restoring the snapshot the live state holds copies nothing. Qwen3.5 dense implements both; Qwen3.5-MoE opts out. - An agent turn continued from such a checkpoint takes its own snapshot only every 2048 tokens of growth: the next request continues it the same way. - A lookup skips entries whose snapshot is gone instead of prefilling the whole prompt, and a consumed deferred snapshot drops its entry. The empty spec-decode retry prefills the prompt when the snapshot was consumed.
There was a problem hiding this comment.
All reported issues were addressed across 14 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
- Qwen prefill raises LUCE_PREFILL_UBATCH to the 16-token chunk minimum. - The empty spec-decode retry that prefills the prompt reports no restored prefix. - Generated-turn checkpoints are always deferred; the disk cache's shutdown save copies a deferred snapshot out first (snapshot_flush_deferred()). - The throttle test covers a continued turn far from any other checkpoint. - Wording of the help text, a header comment and PREFIX_CACHE.md.
|
@cubic-dev-ai review |
@davide221 I have started the AI code review. It will take a few minutes to complete. |
There was a problem hiding this comment.
All reported issues were addressed across 14 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
- Qwen reports its 64-token minimum first chunk after a restore as the snapshot granularity, so the cache no longer reserves a cut the chunk planner cannot save exactly. - A restore that fails validation no longer consumes the live generated-turn checkpoint; it is consumed only once prefill or decode changes the live state. - Tests: the generated-turn save keeps the request's own inline snapshot at capacity; clearer comment in the consumed-snapshot lookup test.
|
@cubic-dev-ai review |
@davide221 I have started the AI code review. It will take a few minutes to complete. |
There was a problem hiding this comment.
All reported issues were addressed
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
…pshot copy - The prefill chunk constants (512 grid, 16-token minimum, 64-token lead after an off-grid restore) live in prefill_helpers.h, used by do_prefill, snapshot_granularity and the chunk-planning test alike. - snapshot_save into the slot a deferred snapshot holds keeps the copy materialization already made instead of copying twice. - PREFIX_CACHE.md: the 16-token minimum does not apply to the prompt's last chunk.
|
@cubic-dev-ai review |
@davide221 I have started the AI code review. It will take a few minutes to complete. |
Why
Measured on lucebox7 (R9700 behind a PCIe Gen3 x4 root port, Qwen3.8-27B UD-IQ4_XS + DFlash2, qualified launcher flags) with an engine-side trace of every request's prompt and generated tokens:
Change
qwen35_prefill_chunk_tokens(), unit tested. The generation prompt's own boundary starts a chunk only when the request snapshots there.test_fattn_mma256misses this because its mask puts the queries at the start of the span. This also moves the last prefill chunk of every prompt on main off the tile kernel.ModelBackend::snapshot_save_deferred(): Qwen keeps that state live and copies it out only before other work would overwrite it; restoring the snapshot the live state already holds copies nothing. Qwen3.5-MoE opts out (its own generation entry points).PREFIX_CACHE.md, the Agent Turn Cache section ofAPI.md;--agent-turn-cachenow only adds the idle-time replay for turns the next request renders differently.Results (lucebox7)
Limits