[None][feat] Add V2 Mamba hybrid cache with snapshot reuse#8
[None][feat] Add V2 Mamba hybrid cache with snapshot reuse#8VALLIS-NERIA wants to merge 35 commits into
Conversation
VALLIS-NERIA
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
[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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
[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.
There was a problem hiding this comment.
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.
| 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: |
There was a problem hiding this comment.
[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.
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
[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.
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
[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.
There was a problem hiding this comment.
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": |
There was a problem hiding this comment.
[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.
There was a problem hiding this comment.
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.
9b6d32c to
bfc9660
Compare
…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>
bfc9660 to
94db6dc
Compare
…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>
94db6dc to
6ffdf55
Compare
…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>
@coderabbitai summary
Stack
agent/mamba-scheduling-disagg-lifecycleDescription
This PR adds V2 Mamba hybrid-cache snapshot reuse on top of the scheduler and
resource-manager contracts in the prerequisite PR.
The changes:
use_kv_cache_manager_v2: trueis explicitly configured. V2 disaggregatedserving and V1 additional fixed snapshot offsets are rejected up front.
mamba_state_config, withperiodic_snapshot_interval,additional_snapshot_offsets_from_start, andadditional_snapshot_offsets_from_end. The oldmamba_state_cache_intervalinput/property remains as a deprecatedcompatibility alias.
V2MambaHybridCacheManager, storing attention KV blocks and Mambaconvolution/SSM state in role-aware KVCMv2 pools while keeping generic
KVCMv2 policy-free.
expected_snapshot_pointsas the snapshot-boundary source and supportperiodic, fixed-from-start, and fixed-from-end boundaries, including the
final prompt boundary.
checkpoint bookkeeping, clean stale request-index mappings, and support
attention-only and Mamba-only PP ranks.
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.
commit_min_snapshotplus the corrected radix root-replacement order givessafe reuse; only the latest same-block partial snapshot is retained.
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
85 passed.151 passed.255 passed, 3 skipped; a preceding local attempt hitan unrelated MPI worker-start timeout before any Mamba test logic.
24 passed.13 passed.24 passed, 12 skipped.pre-commit hooks passed.
PR Checklist