Skip to content

[TRTLLM-17204][feat] Add First-N/Last-N mixed-precision KV caching using native SWA in KVCM V2 - #20452

Merged
Hudayday merged 90 commits into
NVIDIA:mainfrom
Hudayday:kvcm-mixed-precision-native-swa-20261014
Oct 23, 2026
Merged

Hudayday merged 90 commits into
NVIDIA:mainfrom
Hudayday:kvcm-mixed-precision-native-swa-20261014

Conversation

@Hudayday

@Hudayday Hudayday commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

Description

This PR adds optional mixed-precision KV caching using native KVCM V2 pools, retaining first/last tokens at the configured KV dtype while storing full history at lower precision.

A full-attention layer keeps its whole KV history in a low-precision pool, and the first mixed_precision_first_n and last mixed_precision_last_n tokens of every request at the configured KV dtype in a second pool. The second pool is a native sliding-window + sink layer group, so V2's existing window logic allocates, reuses and evicts it. Attention kernels are unchanged: before each layer the batch is gathered into a staging copy in the KV dtype, attention runs on it, and only the tokens written in this step go back to the two pools. NVFP4 is the first low-precision format.

Enable with kv_cache_config.dtype = "mixed_nvfp4_{fp8|float16|bfloat16}" plus mixed_precision_first_n / mixed_precision_last_n (default 256); pool_ratio optionally splits the KV budget between the two pools. Prototype.

Mixed-Precision KV Cache using SWA KV pool

image

Mixed-Precision KV Cache Read/Write

image

Dev Engineer Review

  • Mixed precision is folded into KVCacheManagerV2; no new manager class. A full-attention layer keeps all tokens in a low-precision pool and its mixed_precision_first_n / mixed_precision_last_n tokens in a native sliding-window + sink layer group at the KV dtype.
  • Attention backends are unchanged at the kernel level. The attention metadata owns one staging copy per rank; each layer is gathered into it before attention and only the new tokens are written back. TRTLLM, FlashInfer, GQA and MLA share this path.
  • One CuTe DSL kernel does the gather and the write-back. NVFP4 is the first low-precision format; the dtype string mixed_<low>_<high> leaves room for others.
  • Configuration is three fields on KvCacheConfig (dtype, mixed_precision_first_n, mixed_precision_last_n) plus the existing pool_ratio; serve schemas and the usage manifest are regenerated.

QA Engineer Review

  • test_mixed_precision_kv_cache.py (l0_b200): pool layout and sizing, retention, reuse, pool_ratio, disaggregated mapping.
  • test_mixed_precision_backends.py (l0_b200 / l0_b300 via the unittest/_torch/attention entry): TRTLLM and FlashInfer prefill + decode, MLA, conversion kernel, staging geometry under TP / attention DP, option validation.
  • test_trtllm_serve_config_schemas.py (l0_cpu): new dtype strings and retention counts.
  • Manual E2E on B200 with CUDA graph and reuse: Qwen3-8B BF16 dense vs mixed identical outputs on both backends; GLM-4.5-Air-FP4 (FP8 KV) within the dense run-to-run band; GSM8K Qwen3-8B 87.2 dense vs 88.5 mixed (±0.9).

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]>
@Hudayday
Hudayday requested review from a team as code owners October 16, 2026 03:20
@Hudayday Hudayday changed the title [None][feat] Add First/Last-N mixed-precision KV caching using native SWA in KVCM V2 [None][feat] Add First-N/Last-N mixed-precision KV caching using native SWA in KVCM V2 Oct 16, 2026
@Hudayday
Hudayday marked this pull request as draft October 16, 2026 03:20
…s unset

Without a typical step the backend splits the pools for one 2048-token
request. The retained-token group needs a constant few blocks per request
while the low-precision group grows with the context, so that split
handed the retained pool 38% of the quota under 100K-token traces and
left the low-precision pool only 8% larger than a dense cache. When
mixed precision is on and avg_seq_len is not set, size for max_seq_len /
2 as the hybrid manager does, and warn once. Dense caches and other
managers are unchanged.

Signed-off-by: tianruih <[email protected]>
@Hudayday

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #79831 [ run ] triggered by Bot. Commit: d7c6e08 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #79779 [ run ] completed with state ABORTED. Commit: 44ebe57

Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #79831 [ run ] completed with state SUCCESS. Commit: d7c6e08
/LLM/main/L0_MergeRequest_PR pipeline #65717 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

@Hudayday

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #80031 [ run ] triggered by Bot. Commit: d7c6e08 Link to invocation

Comment thread tensorrt_llm/llmapi/llm_args.py Resolved
Comment thread tensorrt_llm/llmapi/llm_args.py Resolved
Comment thread tensorrt_llm/llmapi/llm_args.py Resolved
@github-actions

Copy link
Copy Markdown

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.

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #80031 [ run ] completed with state SUCCESS. Commit: d7c6e08
/LLM/main/L0_MergeRequest_PR pipeline #65866 completed with status: 'SUCCESS'

CI Report

Link to invocation

…cision validator

get_attention_backend upper-cases the name, so attn_backend="flashinfer" is valid; compare .upper() and raise ValueError so the rejection surfaces as a ValidationError like the other TorchLlmArgs checks.

Signed-off-by: Tianrui Hu <[email protected]>
…on time

The connector layout maps every cache layer to one model layer, which the retained-token layers break; build_kv_cache_layout_v2 would otherwise fail at executor startup, after the model has loaded.

Signed-off-by: Tianrui Hu <[email protected]>
…type before loading weights

mixed_nvfp4_float16/bfloat16 leave the KV cache unquantized, so the retained dtype has to be the model dtype; check it in validate_and_set_kv_cache_quant instead of letting KVCacheManagerV2 fail after the weights are loaded. Also trim the new tests to one case each.

Signed-off-by: Tianrui Hu <[email protected]>
…n and mixed_precision_last_n

The names only apply when kv_cache_config.dtype is mixed_*, and the cold-page compression config is getting first_n/last_n fields with different defaults. Schemas and the golden manifest regenerated.

Signed-off-by: Tianrui Hu <[email protected]>
Fill the TRTLLM block-offset table on a pinned host tensor with numpy instead of several small torch CPU ops per step; each torch CPU op opens an OpenMP parallel region, which stalls on a busy host.

Signed-off-by: Tianrui Hu <[email protected]>
@Hudayday

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #80119 [ run ] triggered by Bot. Commit: a6ceeac Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #80119 [ run ] completed with state SUCCESS. Commit: a6ceeac
/LLM/main/L0_MergeRequest_PR pipeline #65937 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

@Hudayday

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #80190 [ run ] triggered by Bot. Commit: a6ceeac Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #80190 [ run ] completed with state SUCCESS. Commit: a6ceeac
/LLM/main/L0_MergeRequest_PR pipeline #65999 completed with status: 'SUCCESS'

CI Report

Link to invocation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api-compatible Accepted LLM API contract change that is backwards-compatible ci: full pre-merge approved

Projects

None yet

Development

Successfully merging this pull request may close these issues.