[Exp][Hybrid KV Cache] Evaluate input-end vs decode-end checkpoint retention#49574
Draft
qianlihuang wants to merge 1 commit into
Draft
[Exp][Hybrid KV Cache] Evaluate input-end vs decode-end checkpoint retention#49574qianlihuang wants to merge 1 commit into
qianlihuang wants to merge 1 commit into
Conversation
qianlihuang
force-pushed
the
feat/chat-cache-checkpoints
branch
from
July 23, 2026 10:23
75ccb29 to
a248ad5
Compare
Accept trusted frontend token boundaries and propagate them through the engine and scale-out request paths. Align recurrent prefill chunks so those boundaries can materialize under sparse retention. Add an opt-in rolling checkpoint for the latest committed decode prefix while preserving the prompt replay boundary and stable frontend checkpoints. Assisted-by: OpenAI Codex Signed-off-by: Yiliu Dong <91178480+qianlihuang@users.noreply.github.com>
qianlihuang
force-pushed
the
feat/chat-cache-checkpoints
branch
from
July 23, 2026 16:33
a248ad5 to
19d7b4a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This experimental PR evaluates semantic endpoint admission for recurrent-state prefix caching in
mamba_cache_mode=align.The production baseline is the current main-branch sparse-retention policy with:
Under this policy, main retains:
P_end;Relative to the current sparse-retention baseline, this experiment evaluates two proactive endpoint candidates:
U_end): the deepest materializable recurrent state at or before the end of the stable externally supplied conversation history, before generation-only prompt suffixes are appended.D_end): the deepest materializable recurrent state at or before the end of the committed generated token prefix.The three logical boundaries are:
A later request continues to use the existing chained token-prefix hashes to select the longest correct checkpoint.
Phase 1 retains both
U_endandD_end. Its purpose is to measure their production hit distribution, reusable-token benefit, recurrent-state storage cost, and eviction externality before introducing a policy that attempts to select only one.Existing reactive shared-prefix retention remains enabled in every experiment variant.
Motivation
Hybrid attention models combine paged attention KV cache with large recurrent states.
The existing fine-grained hybrid prefix-cache infrastructure already provides the mechanisms required to register and match recurrent states at
prefix_match_unitboundaries. The remaining question is which materializable states should be admitted under sparse retention.The current sparse main-branch policy retains the literal prompt or replay endpoint:
For reasoning models,
P_endmay contain generation-only tokens such as:<think>prefix;These tokens may not appear in the next canonical rendering after the generated assistant response becomes conversation history.
For example:
The next request may instead render:
In this case, the token chain represented by
P_endis not a prefix of the next request.The attention cache may still contain blocks before the divergence, but a hybrid cache can only resume at a position where the required attention and recurrent cache groups are jointly available.
If no earlier recurrent checkpoint has already been materialized, the effective joint prefix hit on the first continuation may therefore fall to zero.
Main already mitigates repeated branch misses through Marconi-style reactive shared-prefix checkpointing. Once a shared branch has been observed, main can materialize and retain the corresponding recurrent state.
This experiment evaluates whether proactive request-semantic endpoints can avoid the first miss before such a branch has necessarily been observed.
Checkpoint definitions
Literal prompt end (
P_end)P_endis the existing sparse-retention endpoint represented by the end of the literal tokenized prompt or replay sequence.It may include generation-only chat-template content after the stable externally supplied conversation history.
Examples include:
P_endis reusable only when its complete token chain remains an exact prefix of a later request.Input end (
U_end)U_endis the logical token position immediately after the stable externally supplied conversation history and before generation-only prompt suffixes.Typical external history includes user messages and tool results, but the definition is not tied to a specific role. It may also include system, developer, assistant-history, or application-inserted messages that are already part of the canonical request history.
The materialized
U_endcheckpoint is the deepest validprefix_match_unitboundary at or before that logical token position.U_endis intentionally conservative. A longer generation-prefix segment may also remain stable for some chat templates, but this experiment does not attempt to infer that longer boundary.Decode end (
D_end)D_endis the logical end of the committed generated token prefix.The materialized
D_endcheckpoint is the deepest validprefix_match_unitboundary at or before that position.It excludes:
D_endis reusable only when the generated sequence and its later serialized chat representation reproduce the same token prefix.Textual equivalence is insufficient: chat-template rendering, role tokens, parser output, escaping, and structured-output serialization must collectively preserve the token chain.
Examples
Tool-call continuation:
D_endis the expected longest hitDuring a tool loop, the generated assistant output may be included in the next request before the tool result.
The next request appends the tool result:
If parsing and chat rendering reproduce the generated prefix token-for-token,
D_endis the longest reusable checkpoint.If the serialized representation changes, chained token hashes reject
D_endand lookup falls back to an earlier matching checkpoint such asU_endor an existing shared-prefix checkpoint.Reasoning response:
U_endis the expected fallbackFor a normal reasoning response, the model may execute hidden reasoning that is omitted from later conversation history.
The next request may contain only the visible assistant response:
D_endcannot be reused because its recurrent state depends on hidden reasoning tokens that are absent from the new token prefix.The chained token hash rejects
D_end, and lookup falls back toU_end:The visible assistant answer and the new user message are then recomputed.
Reasoning disabled or preserved
If reasoning is disabled, or if generated reasoning remains part of the exact serialized history, the complete committed output may remain a prefix of the next request:
Phase 1 does not attempt to predict which behavior applies. Both checkpoints are retained, and token hashes select the longest correct match.
Relationship to existing main-branch behavior
The relevant baseline is main with sparse endpoint retention:
The baseline retains:
P_end, the literal prompt or replay endpoint;Historically, hybrid
alignmode also retained the latest materialized recurrent state, conceptually corresponding to Marconi's last-state checkpoint for chat continuation. PR #37898 added reactive shared-prefix admission on top of that pre-existing last-state behavior.Selective sparse retention later changed
retention_interval=0to retain only the latest prompt replay boundary. PR #47782 preserved reactive shared-prefix junctions under that sparse policy, but did not restore the decode-side last state.Therefore,
D_endis not a new checkpoint category relative to Marconi. In this experiment it explicitly restores Marconi-style last-state retention under the current sparse-retention baseline.U_endis the additional semantic candidate introduced to handle continuations where the literal prompt or generated execution suffix does not remain an exact prefix.This PR does not disable or replace reactive shared-prefix retention.
The experimental variable is the proactive endpoint policy:
All variants continue to use existing Marconi-style shared-prefix detection and retention.
Default dense retention is not a primary comparison target because retaining recurrent states at many aligned positions can be prohibitively expensive for models with large recurrent state.
Relationship to Marconi
Main already implements Marconi-style reactive shared-prefix checkpointing for hybrid prefix caching.
That mechanism discovers a useful branch after observing divergent requests and materializes the recurrent state required to make the shared prefix jointly reusable.
The relationship differs between the two evaluated endpoints:
D_endcorresponds to Marconi's last-state policy. The contribution here is to restore that checkpoint explicitly under selective sparse retention, where the currentretention_interval=0baseline otherwise retains the prompt replay boundary instead.U_endis complementary to Marconi. It proactively retains an earlier stable-history boundary for cases where generation-only prompt tokens, hidden reasoning, or output reserialization makeP_endandD_endunreachable from the next canonical request.Together, the experiment evaluates whether restoring last-state retention and adding a stable-history fallback can avoid the first continuation miss before a shared branch has necessarily been observed:
U_endtargets generation-only or non-canonical prompt suffixes that can makeP_endunreachable.D_endtargets exact continuation workloads in which committed model output round-trips token-for-token into the next request.This PR does not attempt to implement Marconi's complete admission, scoring, or eviction policy. It evaluates endpoint retention on top of the existing reactive shared-prefix mechanism.
Deriving
U_endPhase 1 does not derive
U_endthrough counterfactual prompt comparison.The logical
U_endtoken offset must be supplied by a structured request-rendering path that knows when stable externally supplied history ends and generation-only prompt content begins.Conceptually:
This experiment intentionally avoids:
Opaque rendering paths that cannot expose the boundary between stable history and generation-only suffixes are outside the initial scope.
Phase 1
Phase 1 retains both
U_endandD_end.The purpose is to establish an empirical upper bound for reuse from these two endpoint candidates and measure the cost of retaining the second recurrent state.
A
bothrun can measure which candidate is selected, but it is insufficient on its own because retaining an additional state changes:Separate policy runs are therefore required.
Experiment variants
Every policy retains existing reactive shared-prefix checkpoints.
main_sparseP_endinput_endU_enddecode_endD_endbothU_endandD_endThe initial implementation in this PR uses
both.The full experiment should evaluate the variants under two comparison modes.
Equal total cache capacity
Every variant receives the same total recurrent-state memory capacity.
This measures realistic production behavior, including the additional eviction pressure caused by retaining two endpoints.
Equal admitted checkpoint-byte budget
Every variant receives the same admitted recurrent-state byte budget.
Under this comparison,
bothmay retain fewer sessions or may need to evict one candidate earlier.This isolates the incremental utility of the second endpoint from the advantage of consuming more state memory.
Questions
This experiment is intended to answer:
D_end?D_endreuse come from tool-call continuation?D_enddoes not match, how often doesU_endprovide a useful fallback?U_endavoid a first-continuation miss caused by an unreachableP_end?U_endretain?Metrics
The experiment should record:
Checkpoint selection
prompt_end;input_end;decode_end;Prefix reuse
main_sparse;Checkpoint cost
End-to-end performance
Useful derived metrics include:
Correctness
Checkpoint placement does not determine correctness.
The existing chained token-prefix hashes continue to validate the exact token prefix represented by each cached recurrent state.
A checkpoint that includes hidden, rewritten, discarded, rejected, or otherwise non-canonical tokens simply fails to match a later request.
The request then falls back to the next longest checkpoint that is jointly available across all required cache groups.
No recurrent state may be reused beyond the validated token prefix it represents.
Relationship to fine-grained hybrid prefix caching
This PR does not introduce a new partial-prefix cache representation or lookup mechanism.
It builds on the existing hybrid prefix-cache infrastructure:
prefix_match_unit;The experimental variable is which request-boundary states are proactively retained.
Relationship to chat-aware checkpointing
This PR does not yet implement a chat-aware single-endpoint selection policy.
Phase 1 retains both candidates so that their actual hit distribution can be measured.
A later phase may use this data to choose one checkpoint:
A parser-aware extension may retain the end of the longest decode prefix that is serialized verbatim into conversation history.
More general prompt-internal or decode-internal checkpoint placement should only be considered if production measurements show that
U_endandD_endleave substantial reusable work uncaptured.Planned phases
Phase 1: retain both endpoints
Retain:
U_end;D_end.Continue retaining existing reactive shared-prefix checkpoints.
Measure:
Phase 2: select one endpoint
Evaluate whether a chat-aware or parser-aware policy can choose between
U_endandD_endwithout materially reducing reuse.Possible initial policy:
D_endfor active tool-call continuation or output expected to round-trip token-for-token;U_endwhen generated reasoning or generation-only suffixes are not expected to remain in canonical history.The policy should be evaluated under the same checkpoint-byte budget as
main_sparse.Phase 3: finer checkpoint placement
Only if endpoint selection is insufficient, evaluate more precise locations such as:
Canonical replay and arbitrary chat-template probing remain separate design options.
PD-disaggregated deployments
D_endis useful to a later prefill request only when both requests can access the same cache domain.This may be provided by:
In a strictly disaggregated deployment with no decode-to-prefill publication path,
D_endcannot benefit the next prefill request.Such deployments should disable decode-end retention or report the checkpoint as non-consumable.
U_endmay still be useful if it is produced and retained in the prefill cache domain.The experiment should distinguish: