Skip to content

[None][fix] Fix one-model MTP KV cache accounting - #17264

Open
2ez4bz wants to merge 1 commit into
NVIDIA:mainfrom
2ez4bz:dev-flashinfer-mtp-ima
Open

[None][fix] Fix one-model MTP KV cache accounting#17264
2ez4bz wants to merge 1 commit into
NVIDIA:mainfrom
2ez4bz:dev-flashinfer-mtp-ima

Conversation

@2ez4bz

@2ez4bz 2ez4bz commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

Dev Engineer Review

  • One-model MTP cache accounting now synchronizes Python-side and C++ draft-token state.
  • The first speculative-generation step uses the prompt KV boundary when the backend lacks kv_lens_cuda.
  • This prevents overstated cached lengths, invalid FlashInfer page access, and incorrect KV scheduler capacity planning.
  • Dynamic KV-length backends retain the existing calculation.
  • No public API, configuration, or test-list files changed.
  • Regression tests cover generation, disaggregated generation, context requests, and generation after a context batch.

QA Engineer Review

  • Added test_first_speculative_generation_uses_prompt_kv_boundary.
  • Updated one-model MTP scheduling tests in test_py_executor.py.
  • The modified test functions are not referenced in tests/integration/test_lists/ based on the available repository coverage entries.
  • Verdict: needs follow-up.

Description

  • Why?

The first overlapped MTP generation step could overstate its cached length and access an invalid FlashInfer page, causing a CUDA illegal memory access. The KV scheduler also lacked the Python-side draft-token count needed for correct capacity planning.

  • What?

Keep the Python and C++ draft-token state synchronized, and use the prompt KV boundary when generation immediately follows context for backends without dynamic KV-length correction.

Test Coverage

PR Checklist

Please review the following before submitting your PR:

  • PR description clearly explains what and why. If using CodeRabbit's summary, please make sure it makes sense.

  • PR Follows TRT-LLM CODING GUIDELINES to the best of your knowledge.

  • Test cases are provided for new code paths (see test instructions)

  • If PR introduces API changes, an appropriate PR label is added - either api-compatible or api-breaking. For api-breaking, include BREAKING in the PR title.

  • Any new dependencies have been scanned for license and vulnerabilities

  • CODEOWNERS updated if ownership changes

  • Documentation updated as needed

  • Update tava architecture diagram if there is a significant design change in PR.

  • The reviewers assigned automatically/manually are appropriate for the PR.

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

GitHub Bot Help

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

@2ez4bz

2ez4bz commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run

@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: aeab924d-6fef-4288-be5f-3c325a47eab0

📥 Commits

Reviewing files that changed from the base of the PR and between 1dfb7b1 and 9ff8de8.

📒 Files selected for processing (4)
  • tensorrt_llm/_torch/pyexecutor/model_engine.py
  • tensorrt_llm/_torch/pyexecutor/py_executor.py
  • tests/unittest/_torch/executor/test_py_executor.py
  • tests/unittest/_torch/executor/test_pytorch_model_engine.py
🚧 Files skipped from review as they are similar to previous changes (4)
  • tensorrt_llm/_torch/pyexecutor/py_executor.py
  • tensorrt_llm/_torch/pyexecutor/model_engine.py
  • tests/unittest/_torch/executor/test_py_executor.py
  • tests/unittest/_torch/executor/test_pytorch_model_engine.py

Walkthrough

One-model speculative decoding preserves Python draft tokens across iterations. First-iteration cache accounting uses the prompt boundary when dynamic KV lengths are unavailable. Tests cover draft-token state, cached-token accounting, and position IDs.

Changes

Speculative decoding updates

Layer / File(s) Summary
Draft-token state initialization
tensorrt_llm/_torch/pyexecutor/py_executor.py, tests/unittest/_torch/executor/test_py_executor.py
One-model speculative decoding preserves populated Python draft tokens and normalizes scheduler-visible draft-token buffers. Tests cover generation, disaggregated-generation, and context requests.
First-iteration cache boundary
tensorrt_llm/_torch/pyexecutor/model_engine.py, tests/unittest/_torch/executor/test_pytorch_model_engine.py
The first decoding iteration uses request.max_beam_num_tokens as the cache boundary when kv_lens_cuda is unavailable. Tests verify cached-token accounting and position IDs.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Suggested reviewers: chienchunhung, tabrizian, allisonlim-nv

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely identifies the one-model MTP KV cache accounting fix.
Description check ✅ Passed The description explains the problem and solution, but it does not list the relevant tests in the Test Coverage section.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ 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: 1

🤖 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 `@tests/unittest/_torch/executor/test_py_executor.py`:
- Around line 2010-2013: Update the draft-token assertions in the affected
executor test to verify values, not only lengths: compare both
gen.py_draft_tokens and disagg_gen.py_draft_tokens against [0] *
self.MAX_TOTAL_DRAFT_TOKENS. Keep the existing num_draft_tokens assertions
unchanged.
🪄 Autofix

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: cfdc40e2-408d-41fd-8732-7561ca155193

📥 Commits

Reviewing files that changed from the base of the PR and between be93500 and 0baecbe.

📒 Files selected for processing (4)
  • tensorrt_llm/_torch/pyexecutor/model_engine.py
  • tensorrt_llm/_torch/pyexecutor/py_executor.py
  • tests/unittest/_torch/executor/test_py_executor.py
  • tests/unittest/_torch/executor/test_pytorch_model_engine.py

Comment thread tests/unittest/_torch/executor/test_py_executor.py Outdated
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #63826 [ run ] triggered by Bot. Commit: 0baecbe Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

@BowenFu

BowenFu commented Aug 5, 2026

Copy link
Copy Markdown

Could you clarify how these two hunks relate?

  • On the one-model path, py_draft_tokens now contains dummy values when _prepare_tp_inputs first reads it. Please explain why setting only the C++ draft_tokens count would not satisfy scheduler accounting.
  • Is the py_decoding_iter == 0 cached_token_num correction required because that list is now non-empty? If the changes are coupled, please document the invariant and add a focused test. If they fix independent bugs, separate commits would make them easier to validate and revert.

The kv_lens_cuda backend guard looks appropriately scoped.

@2ez4bz
2ez4bz force-pushed the dev-flashinfer-mtp-ima branch from 0baecbe to 990299f Compare August 5, 2026 16:53
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@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: 1

🤖 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 `@tests/unittest/_torch/executor/test_py_executor.py`:
- Around line 2036-2037: Update the sampler draft setup in the relevant executor
test to use a nonempty list shorter than self.MAX_TOTAL_DRAFT_TOKENS, while
retaining the assertion that request.draft_tokens uses the full scheduler budget
and request.py_draft_tokens preserves the supplied shorter list.
🪄 Autofix

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: 7989f080-973b-451a-9155-656f54711e18

📥 Commits

Reviewing files that changed from the base of the PR and between 1dfb7b1 and 990299f.

📒 Files selected for processing (4)
  • tensorrt_llm/_torch/pyexecutor/model_engine.py
  • tensorrt_llm/_torch/pyexecutor/py_executor.py
  • tests/unittest/_torch/executor/test_py_executor.py
  • tests/unittest/_torch/executor/test_pytorch_model_engine.py
🚧 Files skipped from review as they are similar to previous changes (3)
  • tests/unittest/_torch/executor/test_pytorch_model_engine.py
  • tensorrt_llm/_torch/pyexecutor/py_executor.py
  • tensorrt_llm/_torch/pyexecutor/model_engine.py

Comment thread tests/unittest/_torch/executor/test_py_executor.py
@2ez4bz

2ez4bz commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator Author

@BowenFu

These changes address two parts of the same MTP + FlashInfer + block-reuse failure path.

The python and C++ draft-token representations must both be populated because they have different consumers: the C++ micro-batch scheduler uses draft_tokens, while python scheduling and model-input preparation use py_draft_tokens.

The first-generation cached_token_num correction handles the same context-to-generation transition: a previous overlap tensor exists, but no speculative target forward has populated draft-token KV entries yet. Without using the prompt boundary, FlashInfer can access an invalid reused page. The focused tests cover the two state assumptions independently.

I’m keeping them together because both are needed for this MTP + FlashInfer + block-reuse fix.

* Why?

The first overlapped MTP generation step could overstate its cached length
and access an invalid FlashInfer page, causing a CUDA illegal memory access.
The KV scheduler also lacked the Python-side draft-token count needed for
correct capacity planning.

* What?

Keep the Python and C++ draft-token state synchronized, and use the prompt
KV boundary when generation immediately follows context for backends
without dynamic KV-length correction.

Signed-off-by: William Zhang <133824995+2ez4bz@users.noreply.github.com>
@2ez4bz
2ez4bz force-pushed the dev-flashinfer-mtp-ima branch from 990299f to 9ff8de8 Compare August 5, 2026 17:26
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@2ez4bz

2ez4bz commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #64088 [ run ] triggered by Bot. Commit: 9ff8de8 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #64088 [ run ] completed with state SUCCESS. Commit: 9ff8de8
/LLM/main/L0_MergeRequest_PR pipeline #52015 completed with status: 'FAILURE'

CI Report

⚠️ Multi-GPU Label Required:
Multi-GPU tests require the ci: full pre-merge approved label on this PR. Ask a member of NVIDIA/trt-llm-ci-approvers to add the label, then re-trigger CI with the same bot command (no rebase needed).

⚠️ 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

@2ez4bz

2ez4bz commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #64146 [ run ] triggered by Bot. Commit: 9ff8de8 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #64146 [ run ] completed with state FAILURE. Commit: 9ff8de8
/LLM/main/L0_MergeRequest_PR pipeline #52064 completed with status: 'FAILURE'

CI Report

⚠️ Multi-GPU Label Required:
Multi-GPU tests require the ci: full pre-merge approved label on this PR. Ask a member of NVIDIA/trt-llm-ci-approvers to add the label, then re-trigger CI with the same bot command (no rebase needed).

⚠️ 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

@2ez4bz

2ez4bz commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #64208 [ run ] triggered by Bot. Commit: 9ff8de8 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #64208 [ run ] completed with state FAILURE. Commit: 9ff8de8
/LLM/main/L0_MergeRequest_PR pipeline #52120 completed with status: 'FAILURE'

CI Report

⚠️ Multi-GPU Label Required:
Multi-GPU tests require the ci: full pre-merge approved label on this PR. Ask a member of NVIDIA/trt-llm-ci-approvers to add the label, then re-trigger CI with the same bot command (no rebase needed).

⚠️ 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

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.

3 participants