Skip to content

[None][feat] Add V2 Mamba hybrid cache with snapshot reuse#8

Draft
VALLIS-NERIA wants to merge 35 commits into
agent/mamba-scheduling-disagg-lifecyclefrom
agent/v2-mamba-snapshot-reuse-core
Draft

[None][feat] Add V2 Mamba hybrid cache with snapshot reuse#8
VALLIS-NERIA wants to merge 35 commits into
agent/mamba-scheduling-disagg-lifecyclefrom
agent/v2-mamba-snapshot-reuse-core

Conversation

@VALLIS-NERIA

@VALLIS-NERIA VALLIS-NERIA commented Jul 16, 2026

Copy link
Copy Markdown
Owner

@coderabbitai summary

Stack

  • Base branch: agent/mamba-scheduling-disagg-lifecycle
  • Prerequisite: NVIDIA/TensorRT-LLM#16427
  • This PR contains exactly one signed-off commit relative to its base.

Description

This PR adds V2 Mamba hybrid-cache snapshot reuse on top of the scheduler and
resource-manager contracts in the prerequisite PR.

The changes:

  • Keep the V1 C++ Mamba manager as the default and select V2 only when
    use_kv_cache_manager_v2: true is explicitly configured. V2 disaggregated
    serving and V1 additional fixed snapshot offsets are rejected up front.
  • Move snapshot policy under mamba_state_config, with
    periodic_snapshot_interval, additional_snapshot_offsets_from_start, and
    additional_snapshot_offsets_from_end. The old
    mamba_state_cache_interval input/property remains as a deprecated
    compatibility alias.
  • Add V2MambaHybridCacheManager, storing attention KV blocks and Mamba
    convolution/SSM state in role-aware KVCMv2 pools while keeping generic
    KVCMv2 policy-free.
  • Use expected_snapshot_points as the snapshot-boundary source and support
    periodic, fixed-from-start, and fixed-from-end boundaries, including the
    final prompt boundary.
  • Keep CUDA state-index addresses stable across dummy insertion, mirror replay
    checkpoint bookkeeping, clean stale request-index mappings, and support
    attention-only and Mamba-only PP ranks.
  • Match model PP semantics under MTP: partition base layers first, append spec
    attention layers to the last PP rank, and use the same exact masks for cache
    construction and affine memory sizing. Separate attention-only draft caches
    no longer receive phantom Mamba fixed cost.
  • Remove the PR-specific prefix-sibling mechanism. Upstream
    commit_min_snapshot plus the corrected radix root-replacement order gives
    safe reuse; only the latest same-block partial snapshot is retained.
  • Size partial-snapshot attention storage to at most one copied page per live
    request and scan every physical attention/state buffer in invalid-value
    diagnostics.

The detailed design decisions, experiment results, and verification record are
in PR3_MAMBA_V2_REVIEW.md.

Test Coverage

  • Full Mamba cache-manager suite: 85 passed.
  • Combined Mamba, cache-budget split, dual-pool, and executor-creator suites:
    151 passed.
  • Full LLM-args suite: 255 passed, 3 skipped; a preceding local attempt hit
    an unrelated MPI worker-start timeout before any Mamba test logic.
  • Generic KVCMv2 and executor-creator focused suites: 24 passed.
  • Runtime KVCMv2 SSM support: 13 passed.
  • Runtime KVCMv2 no-batching coverage: 24 passed, 12 skipped.
  • Golden manifest check, Python syntax checks, diff checks, and all changed-file
    pre-commit hooks passed.

PR Checklist

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

@VALLIS-NERIA VALLIS-NERIA left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Focused cleanup review of the PR3-only diff against the latest PR1 base.

if self.kv_cache_type != CacheTypeCpp.SELFKONLY:
buffer_type.append(Role.VALUE_BLOCK_SCALE)

scratch_reuse_config = None

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] This forwards enable_swa_scratch_reuse into a cache containing SsmLayerConfigs, but KVCacheManagerV2._prepare_page_table_tensor() still hard-codes Role.KEY / Role.KEY_BLOCK_SCALE for every layer in its scratch-reuse branch. Mamba layers register only SSM_STATE and CONV_STATE, so enabling SWA scratch reuse reaches get_mem_pool_base_address(mamba_layer, Role.KEY, PER_LAYER) and fails during construction. Please either reject/disable this combination explicitly for V2 Mamba, or generalize the per-layer scratch page-table branch with the same role hooks used by the non-scratch branch, and add a construction test for this configuration.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Generalized SWA scratch pointer and converter construction through the pool role hooks, preserved the SSM placeholder row, and made state views request the explicit per-layer base with PageIndexMode.SHARED. Added a constructor plus copy_batch_block_offsets GPU regression test. Addressed in 9b6d32c.

class KVCacheDesc:
capacity: int
history_length: int
ssm_snapshots: int | None = None

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] This field is consumed as the total SSM slot count, not the number of reusable snapshots: the V2 builder adds max_batch_size live-state slots to snapshot slots before populating it, and None maps to one live slot in _compute_slots_for_batch(). The name therefore invites callers to omit the live slot, and the current >= 0 validation even permits an SSM request to plan zero slots. Please rename it to e.g. num_ssm_slots (default 1, require > 0), or keep snapshot semantics and have the storage planner add one live slot per request. Update the stub and tests with the same invariant.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Renamed the field to num_ssm_slots, made its default explicitly 1, and reject non-positive values. Runtime config, stub, storage sizing, and tests now use the same total-slot semantics. Addressed in 9b6d32c.

Comment thread tensorrt_llm/_torch/pyexecutor/_util.py Outdated
f"Unrecognized value for TLLM_MAMBA_MANAGER_PREFERENCE: {env_override}. "
f"Expected 'CPP', 'MIXED', or 'V2'. Using default {default_cls.__name__}."
)
if kv_cache_config.enable_block_reuse:

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[cleanup] The routing matrix still contains several leftovers. The earlier if is_disagg: returns on every path, so and not is_disagg at line 136 is redundant; default_cls is already V2MambaHybridCacheManager, so this block-reuse branch returns exactly the same class as the final return; and the V2 preference warning says it overrides a default that is already V2. Please collapse these branches. While doing so, remove the two test-only TRTLLM_USE_CPP_MAMBA delenv calls (production has no reader left) and update use_py_mamba_cache_manager() / MambaHybridCacheManager / Cpp-manager docstrings that still describe Cpp as the default or list only Mixed/Cpp.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Removed the redundant routing branches and stale TRTLLM_USE_CPP_MAMBA test cleanup, stopped warning when V2 is explicitly selected because it is already the default, and refreshed the manager-selection documentation. Addressed in 9b6d32c.

commit_min_snapshot=True,
)

def _build_pool_mapping_tensors(self):

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[cleanup] _build_pool_mapping_tensors() is dead code: a repository-wide call-site search finds only this definition (plus an unrelated sparse-manager definition), while KVCacheManagerV2.__init__() calls _prepare_page_table_tensor() directly. The base implementation was already generalized in this PR through _get_pool_page_index_role() / _get_pool_paired_role(), so this 70-line duplicate is stale and bypasses the base implementation's exact_div checks. Please remove the whole method.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. _build_pool_mapping_tensors had no callers after the base V2 page-table path became authoritative, so the entire dead implementation was removed. Addressed in 9b6d32c.

)
self.mamba_ssm_rand_seed[ctx_slots] = seed_tensor

def flush_state_transfers(self) -> None:

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[cleanup] These two overrides add no behavior: flush_state_transfers() is an unconditional no-op and its only caller already uses getattr, while update_resources() forwards the identical arguments to the inherited KVCacheManagerV2 method. Please remove both. The same initialization pass can also drop _full_attention_layer_mask / _combined_layer_mask (write-only) and the duplicate self.kv_cache_config assignment already performed by the base class.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Removed the no-op flush_state_transfers override, the pass-through update_resources override, write-only mask/config state, and the two one-use request helpers. The commit/snapshot decisions now live at their KVCacheManagerV2 call sites. Addressed in 9b6d32c.

nheads = nheads // tp_size
ng_ds_local = self._n_groups_per_rank * mamba_d_state
d_inner_local = mamba_head_dim * nheads
if model_type == "qwen3_next":

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[cleanup/scope] model_type is used here only to retain conv_section_dims. The sole production consumer of that field is the disaggregated kv_extractor, which currently accesses the legacy facade's manager._impl; this PR's routing explicitly never selects V2 for disaggregated serving. Since V2 disagg is a later PR, please move this future-only field/model-specific branching there and keep the aggregate V2 core independent of transfer-layout metadata.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Removed model_type and conv_section_dims from V2 because they only serve the later disaggregated transport work. The factory now supplies model_type only to the legacy Mixed/Cpp implementations that consume it. Addressed in 9b6d32c.

@VALLIS-NERIA
VALLIS-NERIA force-pushed the agent/v2-mamba-snapshot-reuse-core branch from 9b6d32c to bfc9660 Compare July 19, 2026 16:44
JunyiXu-nv and others added 24 commits July 20, 2026 10:57
…is dead (NVIDIA#16312)

Signed-off-by: JunyiXu-nv <219237550+JunyiXu-nv@users.noreply.github.com>
Signed-off-by: trtllm-agent <296075020+trtllm-agent@users.noreply.github.com>
Signed-off-by: TensorRT LLM <90828364+tensorrt-cicd@users.noreply.github.com>
Signed-off-by: trtllm-agent <296075020+trtllm-agent@users.noreply.github.com>
…IDIA#16226)

Signed-off-by: Shuyi Xiong <219646547+shuyixiong@users.noreply.github.com>
…A#16587)

Signed-off-by: trtllm-agent <296075020+trtllm-agent@users.noreply.github.com>
…choices (NVIDIA#16245)

Signed-off-by: lonexreb <reach2shubhankar@gmail.com>
…dtype (NVIDIA#16534)

Signed-off-by: yihwang-nv <yihwang@nvidia.com>
…out cases (NVIDIA#16588)

Signed-off-by: Jie Li <lijie@nvidia.com>
Signed-off-by: xinhe-nv <200704525+xinhe-nv@users.noreply.github.com>
Co-authored-by: xinhe-nv <200704525+xinhe-nv@users.noreply.github.com>
Signed-off-by: trtllm-agent <296075020+trtllm-agent@users.noreply.github.com>
Signed-off-by: trtllm-agent <296075020+trtllm-agent@users.noreply.github.com>
Co-authored-by: xinhe-nv <200704525+xinhe-nv@users.noreply.github.com>
Signed-off-by: trtllm-agent <296075020+trtllm-agent@users.noreply.github.com>
…y tests (NVIDIA#16521)

Signed-off-by: Xianjie <5410381+qiaoxj07@users.noreply.github.com>
Signed-off-by: trtllm-agent <296075020+trtllm-agent@users.noreply.github.com>
NVIDIA#16481)

Signed-off-by: Lizhi Zhou <1432185+reasonsolo@users.noreply.github.com>
…equest_PR (NVIDIA#16480)

Signed-off-by: Weimin Wang <301118019+weiminwang-nv@users.noreply.github.com>
Signed-off-by: trtllm-agent <296075020+trtllm-agent@users.noreply.github.com>
Signed-off-by: trtllm-agent <296075020+trtllm-agent@users.noreply.github.com>
Co-authored-by: xinhe-nv <200704525+xinhe-nv@users.noreply.github.com>
Signed-off-by: trtllm-agent <296075020+trtllm-agent@users.noreply.github.com>
Signed-off-by: trtllm-agent <296075020+trtllm-agent@users.noreply.github.com>
Signed-off-by: jiant <107457950+JadoTu@users.noreply.github.com>
Signed-off-by: Jian Tu <107457950+JadoTu@users.noreply.github.com>
Signed-off-by: trtllm-agent <296075020+trtllm-agent@users.noreply.github.com>
Co-authored-by: xinhe-nv <200704525+xinhe-nv@users.noreply.github.com>
…ssion (NVIDIA#15697)

Signed-off-by: tianruih <tianruih@nvidia.com>
Signed-off-by: TensorRT LLM <90828364+tensorrt-cicd@users.noreply.github.com>
@VALLIS-NERIA
VALLIS-NERIA force-pushed the agent/v2-mamba-snapshot-reuse-core branch from bfc9660 to 94db6dc Compare July 20, 2026 10:10
trtllm-agent and others added 2 commits July 20, 2026 18:16
…A#16615)

Signed-off-by: trtllm-agent <296075020+trtllm-agent@users.noreply.github.com>
Signed-off-by: Xiwen Yu <13230610+VALLIS-NERIA@users.noreply.github.com>
@VALLIS-NERIA
VALLIS-NERIA force-pushed the agent/v2-mamba-snapshot-reuse-core branch from 94db6dc to 6ffdf55 Compare July 20, 2026 10:19
pengbowang-nv and others added 8 commits July 20, 2026 18:27
…ests (NVIDIA#16591)

Signed-off-by: Pengbo Wang <221450789+pengbowang-nv@users.noreply.github.com>
Signed-off-by: trtllm-agent <296075020+trtllm-agent@users.noreply.github.com>
Signed-off-by: Xiwen Yu <13230610+VALLIS-NERIA@users.noreply.github.com>
Signed-off-by: Xiwen Yu <13230610+VALLIS-NERIA@users.noreply.github.com>
Signed-off-by: Xiwen Yu <13230610+VALLIS-NERIA@users.noreply.github.com>
…hreshold to prevent OOM (NVIDIA#16627)

Signed-off-by: Dom Brown <3886319+DomBrown@users.noreply.github.com>
Signed-off-by: Xiwen Yu <13230610+VALLIS-NERIA@users.noreply.github.com>
Signed-off-by: Xiwen Yu <13230610+VALLIS-NERIA@users.noreply.github.com>
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.