Skip to content

[https://nvbugs/6463967][fix] DeepSeek-V4 one-model MTP separate draft kv cache (TEP) - #16887

Merged
QiJune merged 1 commit into
NVIDIA:mainfrom
nvxuanyuc:dsv4aggfix
Aug 20, 2026
Merged

[https://nvbugs/6463967][fix] DeepSeek-V4 one-model MTP separate draft kv cache (TEP)#16887
QiJune merged 1 commit into
NVIDIA:mainfrom
nvxuanyuc:dsv4aggfix

Conversation

@nvxuanyuc

@nvxuanyuc nvxuanyuc commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

Summary

Adds separate draft KV-cache support for DeepSeek-V4 sparse attention during one-model MTP under TEP.

  • Adds draft cache managers, sliding block tables, and pool base pointers for SWA-only draft layers.
  • Adds target-state and draft-state handling for eager execution and CUDA graph replay.
  • Adds generic draft execution lifecycle hooks to attention metadata.
  • Disables separate draft KV-cache allocation when PP > 1.
  • Adds GSM8K accuracy coverage and registers it in l0_dgx_b200.yml.
  • Scope is limited to SWA-only draft layers, PP=1, and beam_width=1.
  • Addresses NVBug 6463967.

Dev Engineer Review

  • Target and draft KV-cache state uses backend lifecycle hooks for preparation and restoration.
  • Unsupported configurations raise NotImplementedError.
  • The DeepSeek-V4 PP > 1 fallback prevents unsupported cache allocation.
  • The GSM8K reference includes the FP8 KV-cache and MTP qualifiers.
  • CI succeeded on the third run for 797c4a5 and the fourth run for e56cedb.

Verdict: sufficient

QA Engineer Review

Added test function:

  • TestDeepSeekV4Flash::test_tep_mtp_separate_draft_kv_cache
    • Uses GSM8K.
    • Covers TEP-style attention, MTP, CUDA graph padding, and FP8 KV cache.
    • Skips MPI world sizes below 4.

Coverage:

  • Registered in tests/integration/test_lists/test-db/l0_dgx_b200.yml.
  • The test-list entry uses a 60-second timeout.

Verdict: sufficient

Description

Wires DeepSeek-V4's sparse attention forward to the existing separate draft KV cache mechanism for one-model MTP under TEP.
Fix issue #17024

Design

  • A dedicated DeepseekV4CacheManager holds the MTP draft layers; the metadata carries
    draft-sized sliding block tables + draft pool base pointers, allocated separately.
  • The draft's _num_tables and precomputed sliding tables are populated in prepare() via
    draft_mgr.compute_sliding_block_tables(...), mirroring the target manager.
  • Target/draft state is swapped via save/apply/restore_target_sparse_state at both the
    eager draft_kv_cache_context and the CUDA-graph prepare_attn_metadata_for_draft_replay,
    gated by isinstance(attn_metadata, DeepseekV4TrtllmAttentionMetadata) (mirrors the DSA
    branch). The swap is a pointer rebind plus per-batch copy.
  • Scope: SWA-only draft layers (compress_ratio 1, asserted); PP=1 only (folds otherwise);
    beam_width=1 (pre-existing DSv4 + MTP constraint).

Validation Results

[DeepSeek-V4-Flash | TEP4 | fp8kv]

Separate vs unified draft KV

max_draft_len AL (separate) AL (unified) GSM8K (separate) GSM8K (unified)
1 1.9600 1.9591 95.41 94.84
2 2.6418 2.6499 95.03 94.88

Perf [nextn=1 | ISL/OSL 2k/2k | max conc 64]

metric separate unified overhead%
BS1 ITL (ms) 3.556 3.561 -0.15%
Throughput (tok/s) 5787.4 5692.9 -1.66%
AL-normalized throughput (tok/s / avg AL) 2919.5 2890.9 -0.99%

Test Coverage

TestDeepSeekV4Flash::test_tep_mtp_separate_draft_kv_cache (GSM8K), registered in
l0_dgx_b200.yml. Local run: 95.53.

GitHub Bot Help

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

@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

DeepSeek-V4 sparse attention now supports separate draft KV-cache buffers for one-model MTP. Backend hooks manage draft forward and replay state. Cache selection and integration coverage are updated.

Changes

DeepSeek-V4 draft KV-cache support

Layer / File(s) Summary
Draft replay hook contract
tensorrt_llm/_torch/attention_backend/trtllm.py, tensorrt_llm/_torch/attention_backend/sparse/dsa.py
Attention metadata defines draft replay and forward hooks. DSA metadata saves, updates, and restores indexer offsets and slot mappings.
Draft sparse buffer setup
tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/deepseek_v4.py
DeepSeek-V4 metadata builds shared target and draft pointer mappings, allocates draft buffers, validates draft layers, and prepares draft sliding-window tables.
Draft state replay and restoration
tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/deepseek_v4.py, tensorrt_llm/_torch/speculative/interface.py
Speculative replay and draft-forward contexts apply backend-specific draft state and restore target state after completion or failure.
Cache policy and integration coverage
tensorrt_llm/_torch/pyexecutor/_util.py, tests/integration/defs/accuracy/..., tests/integration/test_lists/test-db/l0_dgx_b200.yml, tests/unittest/_torch/attention/sparse/dsa/test_dsa_indexer.py
Separate draft cache creation is disabled for DeepSeek-V4 with pipeline parallelism greater than one. TEP MTP FP8 accuracy coverage and hook assertions are added.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant SpeculativeInterface
  participant DeepseekV4TrtllmAttentionMetadata
  participant DSAtrtllmAttentionMetadata
  SpeculativeInterface->>DeepseekV4TrtllmAttentionMetadata: prepare draft forward or replay
  DeepseekV4TrtllmAttentionMetadata->>DSAtrtllmAttentionMetadata: prepare DSA replay state
  SpeculativeInterface->>DeepseekV4TrtllmAttentionMetadata: restore backend state
  DeepseekV4TrtllmAttentionMetadata->>DSAtrtllmAttentionMetadata: restore DSA replay state
Loading

Suggested reviewers: qijune, schetlur-nv, bowenfu

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 69.57% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title follows the required format and clearly identifies the DeepSeek-V4 one-model MTP separate draft KV-cache fix for TEP.
Description check ✅ Passed The description explains the problem, design, scope, validation results, and relevant test coverage; the checklist remains uncompleted.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/cache_manager.py`:
- Around line 603-606: Modernize the annotations at all three sites: in
tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/cache_manager.py:603-606,
change the per_layer_compress_ratios property return type to list[int]; in
tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/deepseek_v4.py:653-761,
annotate helper parameters and returns, using None for mutating helpers and the
precise snapshot type for snapshot-returning helpers; in
tests/integration/defs/accuracy/test_llm_api_pytorch.py:3880, add -> None to the
test method.

In `@tensorrt_llm/_torch/speculative/interface.py`:
- Around line 196-204: Make the DeepseekV4TrtllmAttentionMetadata mutation in
prepare_attn_metadata_for_draft_replay exception-safe by wrapping
apply_draft_sparse_state() with restoration of the saved sparse state when it
raises. Restore the saved metadata before re-raising the original exception,
while preserving the existing saved-state behavior on success.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 78af6d9d-93bc-409f-919e-60efd609be8d

📥 Commits

Reviewing files that changed from the base of the PR and between 49e16c9 and 1c9fb3a.

📒 Files selected for processing (7)
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/cache_manager.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/deepseek_v4.py
  • tensorrt_llm/_torch/pyexecutor/_util.py
  • tensorrt_llm/_torch/speculative/interface.py
  • tests/integration/defs/accuracy/references/gsm8k.yaml
  • tests/integration/defs/accuracy/test_llm_api_pytorch.py
  • tests/integration/test_lists/test-db/l0_dgx_b200.yml

Comment thread tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/cache_manager.py Outdated
Comment thread tensorrt_llm/_torch/speculative/interface.py Outdated
@nvxuanyuc

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #62069 [ run ] triggered by Bot. Commit: 797c4a5 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #62069 [ run ] completed with state FAILURE. Commit: 797c4a5
/LLM/main/L0_MergeRequest_PR pipeline #50254 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

@nvxuanyuc

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #62256 [ run ] triggered by Bot. Commit: 797c4a5 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #62256 [ run ] completed with state FAILURE. Commit: 797c4a5
/LLM/main/L0_MergeRequest_PR pipeline #50425 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

@nvxuanyuc

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@nvxuanyuc

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #66876 [ run ] triggered by Bot. Commit: 9c32de2 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #66876 [ run ] completed with state FAILURE. Commit: 9c32de2
/LLM/main/L0_MergeRequest_PR pipeline #54432 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

@nvxuanyuc

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #66933 [ run ] triggered by Bot. Commit: 9c32de2 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #66933 [ run ] completed with state SUCCESS. Commit: 9c32de2
/LLM/main/L0_MergeRequest_PR pipeline #54485 completed with status: 'SUCCESS'

CI Report

Link to invocation

…t kv cache (TEP)

Signed-off-by: Xuanyu Chen <xuanyuc@nvidia.com>
@nvxuanyuc

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #67134 [ run ] triggered by Bot. Commit: 0ce195a Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #67134 [ run ] completed with state FAILURE. Commit: 0ce195a
/LLM/main/L0_MergeRequest_PR pipeline #54670 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

@nvxuanyuc

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #67183 [ run ] triggered by Bot. Commit: 0ce195a Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #67183 [ run ] completed with state FAILURE. Commit: 0ce195a
/LLM/main/L0_MergeRequest_PR pipeline #54711 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

@nvxuanyuc

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #67265 [ run ] triggered by Bot. Commit: 0ce195a Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #67265 [ run ] completed with state FAILURE. Commit: 0ce195a
/LLM/main/L0_MergeRequest_PR pipeline #54790 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

@nvxuanyuc

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #67346 [ run ] triggered by Bot. Commit: 0ce195a Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #67346 [ run ] completed with state SUCCESS. Commit: 0ce195a
/LLM/main/L0_MergeRequest_PR pipeline #54860 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

@nvxuanyuc

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #67504 [ run ] triggered by Bot. Commit: 0ce195a Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #67504 [ run ] completed with state SUCCESS. Commit: 0ce195a
/LLM/main/L0_MergeRequest_PR pipeline #54999 completed with status: 'SUCCESS'

CI Report

Link to invocation

@QiJune
QiJune enabled auto-merge (squash) August 20, 2026 01:02
@QiJune
QiJune merged commit 1a95c93 into NVIDIA:main Aug 20, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants