Repository navigation
[TRTLLM-17229][feat] Extend the mixed-precision KV cache to speculative decoding and the sparse MLA backends - #21021
Conversation
Keep full history and First/Last storage in REQUIRED lifecycle groups. Reuse native matching, commits, offload and eviction without new C++ state or a separate replay policy. Stage a standard paged cache for TRTLLM and FlashInfer, and write new KV to both pools. Leave high windows to the raw-copy cold codec. Signed-off-by: tianruih <[email protected]>
Reuse existing dtype layout accounting and native SWA policy. Leave recurrent and existing window state unchanged. Keep shared Attention input preparation beside the backends; remove the redundant view type, cold-page extension and draft documentation. Signed-off-by: tianruih <[email protected]>
Reuse existing dtype layout accounting and native SWA policy. Leave recurrent and existing window state unchanged. Keep shared Attention input preparation beside the backends; remove the redundant view type, cold-page extension and draft documentation. Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
…ation Signed-off-by: tianruih <[email protected]>
…ction Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Signed-off-by: tianruih <[email protected]>
Materialize native KV directly from the paged high or low/scales pools and quantize all new tokens directly back into low/scales and retained high pages. Reuse FlashInfer block quantizers and CUTLASS conversion/copy primitives in the kernel directory. Remove four copy kernels and six codec workspaces. Attention dtype, quantization configuration, fusion, and FMHA dispatch stay on the existing native path. Validate 24 dtype/layout/stride/KV-factor cases with strict whole-pool byte checks and CUDA Graph replays changing page tables and lengths. Six focused source tests also pass through standalone kernel loading; the installed native binding lacks PoolRebalanceConfig, so full backend tests are pending. The FP8 codec matches FlashInfer's public CuTe quantizer. Compared with the native CUDA quantizer, all observed differences are exact FP4 midpoints with equal minimum quantization error caused by reciprocal evaluation order. Keep the existing codec rather than introduce another rounding implementation. FlashInfer PR 3387 is related precision work, not a confirmed fix for this case. Signed-off-by: tianruih <[email protected]>
- An explicit max_tokens is converted to a quota that also pays for the mixed-precision high layers, in the base manager (dense, hybrid, DSA) and in DeepSeek-V4's own conversion; the low pool no longer ends up far below max_tokens. - The target/draft split, when it weighs per-request spend, prices a mixed full-attention layer as its NVFP4 history plus a first/last-token window the fixed cost already funds, as the per-token cost does. - pool_ratio counts the mixed-precision high group only when it is a group of its own: with no sink and the retained window of a sliding-window layer, the high layers join that group. - Reject mixed-precision KV on DSA and DeepSeek-V4 where NVFP4 sparse MLA is unavailable (SM107), instead of falling back to FP8 with an unread high pool. - Tests: the draft staging lengths inside a CUDA graph, staging that covers the allocated capacity, and the DSA mixed-precision high page table. - The DSA generation store test checks where each row lands and that k_pe is rotated, instead of comparing against a RoPE reference. Signed-off-by: tianruih <[email protected]>
…lass hook and trim the PR - Disaggregated transfer and KV cache sharing name every layer through get_disagg_global_layer_ids, which KVCacheManagerV2 sets whenever mixed-precision KV is on, also on a pipeline rank without mixed-precision layers: each layer gets twice the id it has without mixed-precision KV, and the mixed-precision high layer of model layer L gets 2L + 1. The disaggregation and KV cache sharing modules and their tests are back to main. - Remove the pool-sizing changes of the previous commit (max_tokens quota, per-request pricing of the target/draft split, pool_ratio group counting) and the peer setting check; keep the fixes for crashes and silently wrong results, and the startup rejections (sleep_config, qwen4_exp, SM107). - Simplify without changing behavior: one mixed-precision high row store for context and generation, one lookup for a layer's mixed-precision high pool and page table, the compressed-row mask launched inline, a class attribute for mixed-precision support, no wrapper for the mixed-precision high page table, and no guards for staging states that cannot occur. - Follow the coding guidelines: Google-style docstrings for helpers called from other files, single backticks, plain-English comments, `X | None`. - Tests stay functional and cover MTP (in the target manager, a separate draft manager, CUDA graph, overlap-scheduler corrections), Eagle3, DSA, DeepSeek-V4, the hybrid managers, the disaggregated layer ids and the startup rejections. Drop the tests that compared kernel output values (high row stores, overlay, compressed-row mask) and the checks of the removed pool sizing. Signed-off-by: tianruih <[email protected]>
|
/bot run --disable-fail-fast |
|
PR_Github #82026 [ run ] triggered by Bot. Commit: |
|
PR_Github #82026 [ run ] completed with state
|
…layer record - KVCacheManagerV2 already records which of its layers belong to a drafter; under a layer_mask it now finds them too, from the model config's layer count. Mixed-precision KV keeps the layers that a drafter other than MTP adds to the manager on a plain cache through this record, instead of a separate first-drafter-layer check. The error for a missing pretrained_config goes away: every caller that can enable mixed-precision KV passes it. - Rename _mixed_precision_native_pool to _own_pool_is_mixed_precision_low. Signed-off-by: tianruih <[email protected]>
|
/bot run --disable-fail-fast |
|
PR_Github #82174 [ run ] triggered by Bot. Commit: |
- Find the plain draft layers, and so read the speculative decoding mode, only when mixed-precision KV is on, as before; managers without it never needed the mode and their test stand-ins do not provide it. - Comments and test names follow the draft-layer record and the renamed flag; the custom-manager test says a subclass must opt in. Signed-off-by: tianruih <[email protected]>
|
/bot run --disable-fail-fast |
|
Automatically added "ci: full pre-merge approved" because this PR has satisfied the required GitHub review approvals. Unresolved review conversations and other required checks remain independent merge requirements. |
|
PR_Github #82190 [ run ] triggered by Bot. Commit: |
|
PR_Github #82174 [ run ] completed with state |
|
PR_Github #82190 [ run ] completed with state
|
|
/bot run --disable-fail-fast |
|
PR_Github #82302 [ run ] triggered by Bot. Commit: |
|
PR_Github #82302 [ run ] completed with state |
Description
PR #20452 added the First-N/Last-N mixed-precision KV cache for the standard
KVCacheManagerV2: every full-attention layer stores its history in NVFP4,the first
mixed_precision_first_nand lastmixed_precision_last_ntokensstay in the attention dtype in a retained-token twin layer, and attention runs
on a staging copy. It rejected speculative decoding, the hybrid Mamba manager
and every sparse-attention backend. This PR lifts those three limits.
Speculative decoding (MTP, NGram, Eagle one-model).
We modified it so that the draft KV dtype depends on the kind of drafter:
Hybrid Mamba manager (Qwen3.5, Kimi-K3). A manager class opts in with
_supports_mixed_precision_kv; the hybrid manager appends the retained-tokenlayer group after its recurrent-state and attention layers and keeps the new
layer ids out of its Mamba layer mask. No kernel changes: these models use the
existing staging path.
Sparse MLA backends (GLM-5.2 DSA, DeepSeek-V4). These backends keep their
history in a native NVFP4 pool and already dequantize the rows their top-k
selected into an FP8 scratch before attention, so they get no staging copy.
The manager keeps the NVFP4 pool as the low-precision pool and appends the
retained-token twin in the high-precision dtype next to it (one twin per
compressed-history layer on DeepSeek-V4, keyed by compression ratio).
the twin through a small Triton row-store kernel driven by the twin's page
table; the DeepSeek-V4 compressor writes the twin with a second call of the
native
compressor_postprocess_scatter, with the twin's page table and arow mask that excludes pages the twin does not hold.
overlay_retained_rowscopies thetwin's rows over the dequantized scratch rows of the retained tokens, so the
attention kernels and the NVFP4 path are unchanged.
pool format is natively NVFP4.
Public options are unchanged (
kv_cache_config.dtype=mixed_nvfp4_<dtype>,mixed_precision_first_n,mixed_precision_last_n). The sparse-attention andspeculative-decoding rejections in
validate_mixed_precision_kvare removed,the KV cache doc lists the supported managers, and the retained-dtype unit
test's model-config stand-in gains the
sparse_attention_configattribute theloader now reads.
Follow-up (not in this PR): fold the overlay into the native NVFP4 gather
kernels so the read side needs no extra kernel.
Test Coverage
Unit tests:
tests/unittest/_torch/attention/test_mixed_precision_backends.py(speculative configs accepted except dynamic tree, one-model draft manager
keeps a plain dtype, staging lengths follow the device-side KV-length
correction on both backends, DeepSeek-V4 retained-row mask),
tests/unittest/_torch/executor/kv_cache/test_mixed_precision_kv_cache.py(appended draft layers stay native),
tests/unittest/_torch/executor/kv_cache/test_mamba_cache_manager.py(hybridmanager appends the retained group; page tables).
Dev Engineer Review
The change extends mixed-precision KV caching to supported speculative decoding, hybrid Mamba managers, and sparse MLA backends. Sparse backends retain selected rows in higher precision and overlay them on gathered NVFP4 history. Dynamic-tree drafting and DeepSeek-V4.1 (CSA2) remain unsupported.
Verify retained-row indexing, page-table mappings, dtype scaling, and sparse context and generation overlays. Also verify staging-length corrections, draft-layer dtype, and hybrid-manager layer placement. Test execution results were not supplied.
QA Engineer Review
Three test files changed:
test_mixed_precision_backends.pyadds coverage for speculative configurations, draft-cache configuration, staging-length corrections, and retained-row behavior during sparse draft forwards.test_mixed_precision_kv_cache.pyaddstest_appended_draft_layers_stay_native, which checks that appended draft layers retain their native dtype.test_mamba_cache_manager.pyaddstest_v2_hybrid_mixed_precision_appends_retained_group, which checks retained-layer mapping, buffers, page-table entries, and cache validity.test_mixed_precision_kv_cache.pyis listed intests/integration/test_lists/test-db/l0_b200.yml. The CI list includes a different test fromtest_mamba_cache_manager.py, not the added mixed-precision test. The backend test file is not listed in the inspected CI or manual-QA lists.test_mixed_precision_retained_dtype_must_match_model_dtypeis markedSKIPintests/integration/test_lists/waives.txt.Coverage verdict: needs follow-up. The supplied test summary does not establish dedicated numerical coverage of sparse retained-row writes and overlays. Test execution results are unavailable.
Per-File QA Perspective
Source files
docs/source/features/kvcache.md— Verify documented manager and backend requirements and compatibility limits against runtime behavior.tensorrt_llm/_torch/attention/backends/flashinfer.py— Verify speculative-decoding offsets update staging lengths only for the active cache manager.tensorrt_llm/_torch/attention/backends/interface.py— Verify staging views are hidden when uninitialized, missing the requested layer, or associated with another manager.tensorrt_llm/_torch/attention/backends/sparse/deepseek_v4/backend.py— Verify retained rows overlay gathered rows on generation and context/mixed-input paths.tensorrt_llm/_torch/attention/backends/sparse/deepseek_v4/cache_manager.py— Verify retained twin-layer page size, window, sink count, reuse policy, and speculative-layer cutoff.tensorrt_llm/_torch/attention/backends/sparse/deepseek_v4/compressor.py— Verify eligible compressed rows are stored and indexer compressors remain unchanged.tensorrt_llm/_torch/attention/backends/sparse/deepseek_v4/metadata.py— Verify retained page tables map to the correct high-precision layers and are cleared for draft preparation.tensorrt_llm/_torch/attention/backends/sparse/dsa/backend.py— Verify context and generation overlays use the correct request and index inputs.tensorrt_llm/_torch/attention/backends/sparse/dsa/cache_manager.py— Verify mixed-precision opt-in preserves the native NVFP4 pool.tensorrt_llm/_torch/attention/backends/sparse/dsa/metadata.py— Verify retained page tables and context positions are prepared only when high-precision layers exist.tensorrt_llm/_torch/attention/backends/sparse/dsa/module.py— Verify context appends store retained rows after the paged cache append.tensorrt_llm/_torch/attention/backends/sparse/mixed_precision_retained_rows.py— Verify retained-row writes, overlays, scaling, and masking when pools or retained pages are unavailable.tensorrt_llm/_torch/attention/backends/trtllm.py— Verify staging lengths refresh from corrected KV lengths only for the active manager.tensorrt_llm/_torch/attention/mla.py— Verify generation stores retained rows after the fused RoPE/cache-update path.tensorrt_llm/_torch/pyexecutor/_util.py— Verify one-model draft-cache configurations selectfp8for FP8 targets andautootherwise.tensorrt_llm/_torch/pyexecutor/kv_cache/kv_cache_manager_v2.py— Verify manager opt-in, native NVFP4 storage, draft-layer exclusion, retained-pool initialization, and page-table filling.tensorrt_llm/_torch/pyexecutor/kv_cache/mamba_cache_manager.py— Verify hybrid-manager opt-in preserves Mamba layer masking and forwards buffer options.tensorrt_llm/_torch/pyexecutor/model_loader.py— Verify mixed cache requests map to NVFP4 for sparse DSA and DeepSeek-V4 without changing other dtype mappings.tensorrt_llm/llmapi/llm_args.py— Verify supported speculative and sparse configurations are accepted while dynamic-tree speculation and other documented restrictions remain rejected.Test files
tests/unittest/_torch/attention/test_mixed_precision_backends.py— Covers speculative configuration, draft-cache behavior, retained-row draft masking, and staging-length updates. It is absent from the inspected CI and manual-QA lists; one test is marked skipped inwaives.txt.tests/unittest/_torch/executor/kv_cache/test_mixed_precision_kv_cache.py— Checks appended draft-layer dtype and mixed-precision cache behavior. The file is listed intests/integration/test_lists/test-db/l0_b200.yml.tests/unittest/_torch/executor/kv_cache/test_mamba_cache_manager.py— Checks hybrid-manager retained-layer mapping, buffers, page table, and cache validity. The inspected CI list includes a different test from this file, not the added mixed-precision test.