Skip to content

[None][fix] KV cache manager V2: keep the KDA replay state across a suspend; refuse a pool rebalance with it - #19846

Closed
vsabavat wants to merge 6 commits into
NVIDIA:mainfrom
vsabavat:k3-up/p12-kda-replay-suspend
Closed

vsabavat wants to merge 6 commits into
NVIDIA:mainfrom
vsabavat:k3-up/p12-kda-replay-suspend

Conversation

@vsabavat

@vsabavat vsabavat commented Oct 4, 2026 •

Copy link
Copy Markdown
Collaborator

Description

MambaHybridCacheManagerV2 keeps the KDA replay state per SSM slot, outside the V2 pools. That is
prev_num_accepted_tokens and the six replay caches (kda_conv_q / kda_conv_k / kda_conv_v, kda_qkg_cache,
kda_v_cache, kda_beta_cache), allocated once from the SSM pool's slot count at construction.

With the fused KDA verify (trtllm::kda_mtp_decode):

  • the V2 SSM state holds the state after the last golden token;
  • the drafts the last verify accepted are pending in the replay caches;
  • the conv window lives only in the replay caches; the verify does not write the V2 conv state.

The manager remembers each request's last slot in _request_id_to_state_index until free_resources, and
_setup_state_indices relocates a request's replay state when its slot changes. Two paths break the assumption that a
request's slot holds its state.

1. A suspended request resumes with another request's replay state

A V2 suspend does not keep that state. The V2 scheduler suspends generation requests under page pressure (eviction
and self-eviction), and the pool rebalance suspends every request. A suspend only unpins the pages; with the host tier
KVCacheManagerV2 provisions by default, they move to host when another cache needs the memory, and the request's
SSM slot goes to another request. The suspended request then resumes in whichever slot is free:

  • in a new slot, _setup_state_indices relocated the old slot's contents, by then the other request's replay state;
  • back in its old slot (after the other request finished), nothing was relocated or reset.

Either way the next verify replayed another request's drafts on this request's state: wrong tokens, no error.

The fix: a suspend leaves the request's whole KDA state in its V2 states, catching the full state up as suggested in
review. The first commit kept a host copy of the replay caches instead; commits 3 and 4 replace it.

  • suspend_request (override in MambaHybridCacheManagerV2, so every suspend path goes through it) commits the
    slot's pending accepted drafts into its V2 SSM state and its conv window into its V2 conv state. It then clears the
    draft count and forgets the slot. Per KDA layer, commit_kda_pending_drafts does the commit:
    • The drafts are replayed by the fused verify kernel's own replay steps. kda_decode_mtp_kernel gains a
      REPLAY_ONLY compile-time mode that runs only those steps and commits the state after them
      (kda_mtp_commit_pending_drafts). The state is therefore bit-identical to what the next verify reaches before its
      golden token. The default variants are unchanged (identical PTX).
    • With [None][feat] KDA verify: optional per-token states in the V2 hybrid cache manager #19817's per-token verify states (kda_state_tok), the Kimi K3 verify kernels ([None][feat] modeling_v2 catalog: Kimi K3 KDA and MLA decode kernels as stateful entries (sm_100) #19832) keep per-draft
      records there: the row innovations, beta * k and the decay. The accepted drafts are replayed from those records
      with the kernels' arithmetic (replay_kda_token_records), so the state is again the one their next verify starts
      from.
    • store_kda_replay_conv_window stores the conv window after the accepted drafts into the V2 conv state. It holds
      raw bf16 inputs, so the bf16 pool stores it exactly.
  • _setup_state_indices: a request that resumes generating has the replay caches of the slot it resumes in seeded
    from that slot's V2 conv state, with nothing pending, as a disaggregated generation request's are. A request that
    comes back as a context request starts over.
  • Ordering: both run on the KV caches' stream.
    • The commit runs after the current stream's work, and before the event kv_cache.suspend() records there, which
      later page migrations wait for.
    • The seeding runs after the pages the resume brings back.
  • Host drafter (NGram): update_resources records a batch's acceptance, which the commit needs.
    • The executors record it before they can suspend the request: the host drafter runs without the overlap scheduler,
      and the PP loop does not evict requests in flight.
    • A suspend between a batch's prepare_resources and its update_resources raises instead of committing a stale
      count.

Cost: only when a KDA replay request is suspended. Per local KDA layer there is one replay-only kernel (or one records
replay, with the draft records of #19817) and one small copy at the suspend, and a seeding copy at the resume. Nothing
is held while the request is suspended: V2 alone holds its state, so its page can change tiers.

2. A KV pool rebalance can grow the SSM pool past the replay buffers

With kv_cache_config.enable_kv_pool_rebalance, the executor's rebalance calls adjust(), which resizes the pool
groups to the auto-tuner's target ratio and can give the SSM pool more slots than it had at construction. A request in
a slot past the buffers makes the prev_num_accepted_tokens[slot] = 0 reset fail, and the KDA verify kernels index
the replay entries by slot without a bound. Nothing refused the combination: _util.py refuses a rebalance only with
block reuse, and the executor's rebalance gate skips only two-model drafters, so one-model speculative decoding on
KDA layers reaches it.

The fix (commit 2): _build_cache_config raises ValueError for the KDA replay path with
enable_kv_pool_rebalance, as KvCacheConfig does for fp8_ds_mla's fixed pool views. The rebalance stays allowed
without the replay caches.

Note for #19817

#19817 adds kda_state_tok, the Kimi K3 verify kernels' per-draft records, another per-slot buffer of this scheme,
and with it does not allocate the q/k/g, v and beta replay caches.

  • The suspend commit replays the records when the manager keeps them, and passes no replay caches then.
  • _kda_replay_state_buffers moves kda_state_tok when it exists and skips absent replay caches, so a live slot
    move relocates the records with the rest in either merge order. ([None][feat] KDA verify: optional per-token states in the V2 hybrid cache manager #19817 appends the records in its own
    _relocate_kda_replay_slots; whichever of the two merges second keeps this helper.)
  • The rebalance refusal keys on the replay path, so it already covers them.

Test Coverage

tests/unittest/_torch/executor/kv_cache/test_mamba_cache_manager.py:

  • test_v2_kda_replay_suspend_resume_matches_an_unsuspended_run[slot_taken|slot_kept] (one GPU, sm_100 /
    sm_103):
    • A request on the V2 runtime runs at Kimi K3's per-rank KDA shapes and verifies with the fused kernel. It is
      suspended with two accepted drafts pending, then resumes.
    • With slot_taken, new caches take its slot through the host tier meanwhile and overwrite it.
    • Every later verify gives the outputs and leaves the states of the run never suspended, bit for bit.
    • slot_taken fails before the fix, at the first verify after the resume.
  • test_v2_kda_replay_suspended_request_resumes_from_its_v2_states[new_slot|old_slot] (CPU):
    • The suspend commits and forgets the slot, and another request takes the slot.
    • The request resumes in a new slot or in its old one, its replay caches seeded from the V2 conv state with nothing
      pending. The other request's replay state is left alone.
    • Fails before the fix.
  • test_v2_kda_replay_refuses_a_suspend_before_the_host_drafter_acceptance[update_resources|revert_allocate_generation]
    (CPU): a suspend between a batch's prepare_resources and its update_resources raises. Once the acceptance is
    recorded, or the batch is reverted, it commits. Fails before the fix.
  • test_v2_kda_replay_host_drafter_records_active_requests: also checks that update_resources clears only its
    batch's pending acceptances.
  • test_v2_kda_replay_refuses_kv_pool_rebalance[kda_replay|no_replay] (one GPU, commit 2): building the manager
    with the replay caches and enable_kv_pool_rebalance raises; without the replay caches the rebalance stays allowed.
    The first case fails before the fix (the manager builds).

tests/unittest/_torch/modules/kimi_kda/test_kda_mtp_decode_cute_parity.py:

  • test_commit_pending_drafts_matches_the_next_verify[32-12-2|32-6-2|4-6-7]: the commit runs before a verify;
    the conv window is stored in a bf16 pool and the caches are seeded from it. That verify gives the outputs, state,
    caches and conv windows of the verify that replays the drafts itself, bit for bit.
  • test_commit_pending_drafts_replays_the_per_token_records: with per-token states, a slot's accepted drafts are
    replayed in order from its per-draft records ([slots, 3, num_spec, H, K]), and the window after them is stored;
    other slots are left alone. The values are exactly representable, so the torch reference is exact.
  • test_v2_kda_replay_commit_and_relocation_without_the_replay_caches (one GPU, test_mamba_cache_manager.py):
    a manager with the records and without the replay caches. The suspend commit equals the records' replay and the
    conv store run on their own, bit for bit, and relocation moves a slot's conv caches, records and draft count.
    Fails before 003f8d088d (TypeError: 'NoneType' object is not subscriptable).

Repro of the rebalance overrun on the V2 runtime (the manager at Kimi K3's shapes, no fix needed to show it): with the
auto-tuner's target giving the SSM pool group 8x its share, adjust() grows the SSM pool from 10 to 11 slots over
10-slot replay buffers.

Results on one GB200 GPU, against main at ace058d:

  • The new and changed tests (15): on main's code 11 fail and 4 pass. The passes are slot_kept, which survives
    on main because its slot does not change, the rebalance no_replay case and the two disaggregated seed tests. With
    this PR all 15 pass.
  • test_kda_mtp_decode_cute_parity.py whole: 34 passed.
  • test_mamba_cache_manager.py whole: main 34 failed / 151 passed, this PR 34 failed / 159 passed.
    • The same 34 fail on both, and main waives all of them (nvbugs/6818710): get_kv_cache_manager_cls reads
      model_config.mapping, which their config stubs lack.
    • None of them runs code this PR changes. Nothing fails only with this PR.
  • compute-sanitizer on the commit paths (the four op tests and the two runtime tests): racecheck 0 hazards,
    synccheck 0 errors, initcheck 0 errors, memcheck 0 kernel errors. Memcheck's host API reports come from OpenMPI / UCX
    initialization; main's run of the same test gives the same ones.
  • PTX: the default kda_mtp_decode variants (five configurations) compile to byte-identical PTX on main and with
    this PR.
  • Kimi K3's per-token path (the K3 verify kernels with [None][feat] KDA verify: optional per-token states in the V2 hybrid cache manager #19817's draft records, 7 drafts, on a tree with all
    three PRs): after a suspend with drafts pending and the slot taken meanwhile, every verify's outputs and the final
    states equal the run never suspended, bit for bit.
  • The records follow-ups (c95eac12a6, 003f8d088d): both files give the same 34 failing tests as before
    (the main-waived stubs above) and one more passing test; the records test and the new manager test fail on the
    previous code. On Kimi K3 (16 GB200, sessions 7777282 and 7779408, with [None][feat] KDA verify: optional per-token states in the V2 hybrid cache manager #19817's and [None][feat] modeling_v2 catalog: Kimi K3 KDA and MLA decode kernels as stateful entries (sm_100) #19832's follow-ups) the
    fixed-prompt outputs and all 1319 GSM8K responses are identical, and TPOT is within noise.
  • Kimi K3 on 16 GB200, the full Kimi K3 stack with and without this PR:
    • the 8 fixed-prompt outputs are unchanged, with speculative decoding (DSpark, forced and natural acceptance) and
      without;
    • decode TPOT at batch 1 and 8 is unchanged within run-to-run noise (-1.1 % to +0.5 %).

PR Checklist

Please review the following before submitting your PR:

  • PR description clearly explains what and why. If using CodeRabbit's summary, please make sure it makes sense.

  • PR Follows TRT-LLM CODING GUIDELINES to the best of your knowledge.

  • Test cases are provided for new code paths (see test instructions)

  • If PR introduces API changes, an appropriate PR label is added - either api-compatible or api-breaking. For api-breaking, include BREAKING in the PR title. (No API change.)

  • Any new dependencies have been scanned for license and vulnerabilities (No new dependencies.)

  • CODEOWNERS updated if ownership changes (No ownership change.)

  • Documentation updated as needed (No user-facing change.)

  • Update tava architecture diagram if there is a significant design change in PR. (No design change.)

  • The reviewers assigned automatically/manually are appropriate for the PR. (To check once the PR is open.)

  • Please check this after reviewing the above items as appropriate for this PR.

GitHub Bot Help

To see a list of available CI bot commands, please comment /bot help.

…y state

MambaHybridCacheManagerV2 keeps the KDA replay state
(prev_num_accepted_tokens and the six replay caches) per SSM slot,
outside the V2 pools, and remembers each request's last slot until
free_resources. A V2 suspend (the scheduler's eviction and
self-eviction, the pool rebalance) only suspends the pages. Under
pressure the host tier takes them, the slot goes to another request,
and the suspended request resumes in whichever slot is free. Its next
_setup_state_indices then relocated the old slot's contents, by then
the other request's replay state, or, back in its old slot, kept them:
the next verify replayed another request's drafts.

suspend_request now copies the slot's replay state to host memory and
forgets the slot; _setup_state_indices writes it into the slot the
request resumes in. update_resources gives the host drafter's
acceptance of a request suspended after its batch was scheduled to the
saved state instead of the old slot. Relocation and the copy share one
list of the per-slot buffers.

The new tests resume a request in a new slot and in its old one and
record a late acceptance on a manager stub, and on the V2 runtime let
new caches take a suspended request's slot through the host tier before
it resumes.

Signed-off-by: Vasanth Sabavat <[email protected]>
…KDA replay caches

The KDA replay caches and prev_num_accepted_tokens are allocated once,
one entry per slot the SSM pool keeps at construction
(get_page_index_upper_bound). With enable_kv_pool_rebalance, adjust()
resizes the pool groups to the auto-tuner's target ratio, which gives
the SSM pool one slot per typical request, so a pool built smaller
than that grows past the buffers. A request in a slot past them makes
the prev_num_accepted_tokens[slot] = 0 reset fail, and the KDA verify
kernels index the replay entries by slot without a bound. Nothing
refused the combination: _util.py refuses a rebalance only with block
reuse, and the executor's rebalance gate skips only two-model
drafters, so one-model speculative decoding on KDA layers reaches it.

_build_cache_config now raises ValueError for the KDA replay path with
enable_kv_pool_rebalance, as KvCacheConfig does for fp8_ds_mla's fixed
pool views. test_v2_kda_replay_refuses_kv_pool_rebalance covers the
refusal and the control without the replay caches.

Signed-off-by: Vasanth Sabavat <[email protected]>
@svc-trtllm-gh-bot svc-trtllm-gh-bot added the Community want to contribute PRs initiated from Community label Oct 4, 2026
@VALLIS-NERIA

Copy link
Copy Markdown
Collaborator

I think a short term fix is dropping replay states, either rewinding the sequence to the previous full state, or update the full state to catch up the tokens.

The fused KDA verify leaves the state after the last golden token in the
SSM pool, the drafts the sampler accepted pending in per-slot replay
caches, and the conv window only in those caches. commit_kda_pending_drafts
moves the drafts and the window into the live pools of one layer, so that
the pools hold exactly what the next verify would start from:

- kda_decode_mtp_kernel gains a REPLAY_ONLY compile-time mode that runs
  only the replay steps of a verify and writes the state after them. The
  replay is the verify's own code, so the result is bit-identical to the
  state the next verify reaches before its golden token. The mode reads no
  token inputs or weights; kda_mtp_commit_pending_drafts launches it.
- With per-token verify states (kda_state_tok), the accepted drafts are
  replayed from the per-draft records the Kimi K3 verify kernels keep
  there (row innovations, beta * k and the decay), with the kernels'
  arithmetic (replay_kda_token_records).
- store_kda_replay_conv_window stores the W - 1 conv columns starting at
  the accepted count into the conv pool. They are raw bf16 inputs, so the
  bf16 pool holds them exactly.

The default kernel variant is unchanged: the new code is behind
const_expr gates and the new parameter is the last one.

The new tests check that committing the drafts, storing the window in a
bf16 pool and seeding the replay caches from it before a verify gives
that verify's outputs, state, caches and windows bit for bit, and the
replay of the per-token records.

Signed-off-by: Vasanth Sabavat <[email protected]>
…lay state into V2

A suspend now leaves the request's whole KDA state in its V2 SSM and conv
states instead of a host copy of the replay caches. Before the pages are
released, suspend_request commits the slot's pending accepted drafts and
its conv window into them (commit_kda_pending_drafts) and clears the
draft count; the replay caches hold nothing the request needs any more,
and no extra memory is held while it is suspended. The request's next
generation step seeds the replay caches of whichever slot it resumes in
from the V2 conv state, as a disaggregated generation request's are.

Both run on the KV caches' stream: the commit after the current stream's
work and before the event the suspend records there, which later page
migrations wait for; the seeding after the pages a resume brings back.

With a host drafter, update_resources records a batch's acceptance, which
the commit needs. Executors record it before they can suspend the
request: the host drafter runs without the overlap scheduler, and the PP
loop does not evict requests in flight. A suspend between a batch's
prepare_resources and its update_resources raises instead of committing
a stale count.

The host copies and the late acceptance hand-off are removed. The tests
resume a request in a new slot and in its old one, check the host
drafter guard, and on the V2 runtime with the fused verify kernel compare
every verify after a suspend and resume with the run that is never
suspended, bit for bit, with the slot taken by another cache meanwhile
and without.

Signed-off-by: Vasanth Sabavat <[email protected]>
@karljang karljang removed the Community want to contribute PRs initiated from Community label Oct 6, 2026
@vsabavat

vsabavat commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks. Done as you suggested, catching the full state up (ce1ae633f1, f9d32b4c4d):

  • At suspend, the slot's pending accepted drafts are committed into its V2 SSM state, and its conv window into its
    V2 conv state.
    • The replay runs the fused verify kernel's own replay steps, a replay-only compile-time mode of
      kda_decode_mtp_kernel. The state is therefore bit-identical to what the next verify would reach before its golden
      token.
    • With [None][feat] KDA verify: optional per-token states in the V2 hybrid cache manager #19817's per-token states, the drafts are replayed from the per-draft records the Kimi K3 verify kernels
      keep there, with the kernels' arithmetic.
  • On resume, the replay caches are seeded from the V2 conv state with nothing pending, as for a disaggregated
    generation request.
  • The host copies are gone. While a request is suspended, V2 alone holds its state, so its page can change tiers.
  • Host drafter: update_resources records the acceptance, and every executor loop does that before it can suspend
    the request. A suspend before it now raises instead of committing a stale count.
  • New test: a request is suspended with drafts pending, and another cache takes its slot through the host tier.
    After it resumes, every verify gives the same outputs and states as a run that never suspends, bit for bit. It fails
    on main.

vsabavat added a commit to vsabavat/TensorRT-LLM that referenced this pull request Oct 7, 2026
Remakes 5fbf086203, the same merge onto 4c9e23d, on top of e253695
(P15, NVIDIA#19903). The tree is 5fbf086203's plus P15's three files, which P12
does not touch.

Conflicts resolved for U2's per-token KDA states: shutdown keeps
kda_state_tok = None; the test file keeps U2's import, builds the
suspend/resume runtime test's manager without per-token states, and skips
the absent kda_state_tok buffer when it overwrites a slot.

# Conflicts:
#	tensorrt_llm/_torch/pyexecutor/kv_cache/mamba_cache_manager.py
#	tests/unittest/_torch/executor/kv_cache/test_mamba_cache_manager.py
…pec, H, K]

The Kimi K3 verify kernels' kda_state_tok now holds only the drafts'
records, [slots, 3, num_spec, H, K] fp32 per layer: the row innovations
vn, then beta * k and the decay, in the order they have always been
laid out. It no longer has room for a full state per draft.

replay_kda_token_records takes num_spec, H and K from that shape and V
from the state pool. It raises ValueError for any other layout instead
of misreading it. The records' offsets within a slot are unchanged.

The suspend commit test builds its records in the new shape.

Signed-off-by: Vasanth Sabavat <[email protected]>
With the Kimi K3 verify kernels' draft records (kda_state_tok, NVIDIA#19817),
the per-draft replay caches kda_qkg_cache, kda_v_cache and
kda_beta_cache are not allocated. The suspend-time commit indexed them
per layer and the slot relocation asserted them, so both would fail on
such a manager. The commit now passes None for absent caches (the
records' replay never reads them), and the relocation moves the buffers
that exist, the drafts' records included. With the replay caches
nothing changes.

The new test builds a manager with the records and without the caches.
It compares the suspend-time commit bit for bit with the records' replay
and the conv store run on their own, and checks that relocation moves
a slot's conv caches, records and draft count.

Signed-off-by: Vasanth Sabavat <[email protected]>
@vsabavat

vsabavat commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #76599 [ run ] triggered by Bot. Commit: 003f8d0 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #76599 [ run ] completed with state SUCCESS. Commit: 003f8d0
/LLM/main/L0_MergeRequest_PR pipeline #63169 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@vsabavat

vsabavat commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator Author

Superseded by the consolidated draft PRs: #19988, #19989. Source commits and follow-ups are retained; prior reviews remain historical evidence, not approval of the replacement heads.

@vsabavat vsabavat closed this Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants