Skip to content

[None][fix] use prompt lookahead for MTP Eagle chunked prefill - #18295

Open
yizhang-nv wants to merge 5 commits into
NVIDIA:mainfrom
yizhang-nv:codex/fix-mtp-eagle-chunk-lookahead
Open

[None][fix] use prompt lookahead for MTP Eagle chunked prefill#18295
yizhang-nv wants to merge 5 commits into
NVIDIA:mainfrom
yizhang-nv:codex/fix-mtp-eagle-chunk-lookahead

Conversation

@yizhang-nv

@yizhang-nv yizhang-nv commented Aug 27, 2026

Copy link
Copy Markdown
Member

Description

MTP Eagle left-shifts each context chunk before the first draft-layer forward. For a non-final chunk, the shifted tail must be the immediate next prompt token. It previously used the target model's sampled token, so linear and dynamic-tree MTP Eagle could persist draft-layer KV that did not match canonical full-prompt prefill.

This change:

  • stages exactly one immediate prompt lookahead token per active context request in a fixed-address SpecMetadata tensor with shape [max_num_requests]; -1 marks final chunks with no valid lookahead;
  • derives the token from the request's context cursor, independently of KV cache manager V1 or V2;
  • uses the prompt token for non-final context rows in linear, dynamic-tree, and external shared-target-KV MTP Eagle, while final chunks fall back to the sampled token and generation rows remain unchanged; and
  • leaves vanilla MTP, cache keys, and V2 draft reuse unchanged for follow-up work.

The external shared-target-KV path does not persist independent draft KV, but it uses the same token/hidden-state/position contract so all MTP-Eagle rollout paths now start from the correctly aligned token.

Acceptance Measurements

Measured on one B200 with DeepSeek-V3-Lite/bf16, TP=1, MTP Eagle one-model, max draft length 1, chunked prefill enabled, prompt length 512, chunk size 128, and output length 128. The workload used three prompts sourced from the TensorRT-LLM documentation and two runs per configuration.

Configuration Accepted / drafted tokens Acceptance rate Acceptance length
Before 338 / 430 78.60% 1.7860
After 339 / 429 79.02% 1.7902

The aggregate delta is +0.42 percentage points in acceptance rate and +0.0042 in acceptance length. Per-run acceptance-rate ranges overlap (before: 76.96%-80.28%; after: 78.60%-79.44%), so this should be treated as neutral/small with no observed regression, rather than as a statistically meaningful improvement.

Test Coverage

  • pytest -s -q tests/unittest/_torch/executor/test_pytorch_model_engine.py tests/unittest/_torch/speculative/test_eagle3.py tests/unittest/_torch/speculative/hw_agnostic/test_mtp.py
    • 128 passed, 22 skipped, 26 subtests passed
  • pre-commit run --files on all seven changed files
    • all hooks passed

The focused tests cover prompt-boundary extraction, token ID 0, sentinel fallback and stale-value clearing, CUDA graph metadata allocation, linear and dynamic-tree context input, and external shared-target-KV context/generation alignment.

PR Checklist

  • The fix is independent of KV cache manager V1/V2.
  • Linear, dynamic-tree, and external shared-target-KV MTP-Eagle paths are covered.
  • Vanilla MTP and V2 draft reuse are intentionally unchanged.
  • No public API, dependency, ownership, documentation, or architecture-diagram change is introduced.

Dev Engineer Review

  • The implementation stores one immediate prompt lookahead token per active context request.
  • -1 marks final chunks and preserves sampled-token fallback behavior.
  • The implementation is independent of KV cache manager V1 and V2.
  • Linear, dynamic-tree, and shared-target-KV MTP Eagle paths use the lookahead token.
  • Vanilla MTP, cache keys, and V2 draft reuse remain unchanged.
  • The SpecMetadata API now uses a flat one-token-per-request buffer.
  • No configuration or test-list files changed.
  • Focused tests passed: 70. Pre-commit checks passed for all seven changed files.

QA Engineer Review

  • Modified test files:
    • test_pytorch_model_engine.py: updated prompt-boundary and overlap-mock coverage.
    • test_eagle3.py: updated flattened lookahead, sentinel, CUDA-graph, draft-input, and dynamic-tree coverage.
    • test_mtp.py: updated shared-KV draft-input coverage for multiple context requests.
  • The tests cover non-final lookahead tokens, final-chunk sentinel fallback, accepted tokens, draft IDs, recurrent states, and positions.
  • No corresponding test-db/ or qa/ test-list changes were provided.
  • Verdict: needs follow-up. CBTS coverage mapping is unavailable.

@coderabbitai

coderabbitai Bot commented Aug 27, 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: b521a106-3193-4ca6-a517-e79bf523e6ff

📥 Commits

Reviewing files that changed from the base of the PR and between 390f92d and 52a4c92.

📒 Files selected for processing (7)
  • tensorrt_llm/_torch/pyexecutor/model_engine.py
  • tensorrt_llm/_torch/speculative/eagle3.py
  • tensorrt_llm/_torch/speculative/interface.py
  • tensorrt_llm/_torch/speculative/mtp_dynamic_tree.py
  • tests/unittest/_torch/executor/test_pytorch_model_engine.py
  • tests/unittest/_torch/speculative/hw_agnostic/test_mtp.py
  • tests/unittest/_torch/speculative/test_eagle3.py
🚧 Files skipped from review as they are similar to previous changes (7)
  • tests/unittest/_torch/speculative/hw_agnostic/test_mtp.py
  • tests/unittest/_torch/speculative/test_eagle3.py
  • tests/unittest/_torch/executor/test_pytorch_model_engine.py
  • tensorrt_llm/_torch/speculative/mtp_dynamic_tree.py
  • tensorrt_llm/_torch/speculative/eagle3.py
  • tensorrt_llm/_torch/pyexecutor/model_engine.py
  • tensorrt_llm/_torch/speculative/interface.py

Included review availability: Your plan provides up to 12 included reviews per hour; 11 remain after this review.


Walkthrough

The change replaces padded prompt lookahead rows with one immediate token per context request. Boundary cases use INVALID_PROMPT_LOOKAHEAD_TOKEN. MTP Eagle and dynamic-tree preparation consume the flattened representation.

Changes

Context prompt lookahead

Layer / File(s) Summary
Prompt lookahead metadata and input selection
tensorrt_llm/_torch/speculative/interface.py
SpecMetadata stores one sentinel-filled CUDA token per request. Context input preparation uses the lookahead token when valid and the accepted token when the sentinel marks the final chunk.
Context lookahead collection
tensorrt_llm/_torch/pyexecutor/model_engine.py, tests/unittest/_torch/executor/test_pytorch_model_engine.py
The model engine extracts one token after each context chunk. Prompt-boundary chunks receive one invalid-token sentinel. Tests cover valid tokens, boundaries, and metadata setup.
Eagle draft input integration and validation
tensorrt_llm/_torch/speculative/eagle3.py, tensorrt_llm/_torch/speculative/mtp_dynamic_tree.py, tests/unittest/_torch/speculative/test_eagle3.py, tests/unittest/_torch/speculative/hw_agnostic/test_mtp.py
MTP Eagle and dynamic-tree step-0 preparation forward flattened prompt lookahead tokens. Shared-KV preparation preserves accepted-token fallback for sentinel entries. Tests cover initialization, fallback behavior, dynamic-tree inputs, and updated draft outputs.

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

Merge Risk: ⚪ Minimal · up to 52a4c

This localized change aligns MTP Eagle chunked-prefill inputs with the immediate prompt token while preserving final-chunk and generation behavior; no actionable merge-blocking risk remains after normal checks and review.

Sequence Diagram(s)

sequenceDiagram
  participant ModelEngine
  participant SpecMetadata
  participant MTPEagleWorker
  participant DynamicTree
  participant ContextInputPreparation

  ModelEngine->>ModelEngine: Extract one token after each context chunk
  ModelEngine->>SpecMetadata: Store token or INVALID_PROMPT_LOOKAHEAD_TOKEN
  MTPEagleWorker->>SpecMetadata: Read flat lookahead buffer
  MTPEagleWorker->>ContextInputPreparation: Pass MTP lookahead tokens
  DynamicTree->>SpecMetadata: Read flat lookahead buffer
  DynamicTree->>ContextInputPreparation: Pass step-0 lookahead tokens
  ContextInputPreparation->>ContextInputPreparation: Select lookahead or accepted-token fallback
Loading

Suggested reviewers: mikeiovine, nv-xtf, lori-ren

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 56.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 25 functions across 7 files. 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 [None][fix] format and clearly describes the MTP Eagle chunked-prefill fix.
Description check ✅ Passed The description includes a clear problem statement, solution details, acceptance measurements, focused test coverage, and a completed checklist. It explains the affected MTP Eagle paths and the intent…
Full details: Description check

Explanation

The description includes a clear problem statement, solution details, acceptance measurements, focused test coverage, and a completed checklist. It explains the affected MTP Eagle paths and the intentionally unchanged behavior.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

@yizhang-nv

Copy link
Copy Markdown
Member Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #69655 [ run ] triggered by Bot. Commit: 345f71d Link to invocation

@yizhang-nv

Copy link
Copy Markdown
Member Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #69663 [ run ] triggered by Bot. Commit: 32a287f Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #69655 [ run ] completed with state ABORTED. Commit: 345f71d

Link to invocation

@zhaoyangwang-nvidia zhaoyangwang-nvidia left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could you add measured acceptance-rate / acceptance-length numbers, before and after this change, to the description — along with the prompt length and chunk size used?

if spec_metadata.context_prompt_lookahead_tokens is not None:
# No-cache context inputs contain the complete prompt, so
# there is never a valid token beyond the current chunk.
spec_metadata.populate_context_prompt_lookahead(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

_set_up_spec_metadata's no_cache branch constructs a fresh SpecMetadata on every call rather than reusing self.spec_metadata, so the buffer here was just filled with INVALID_PROMPT_LOOKAHEAD_TOKEN by allocate_context_prompt_lookahead and this call always writes -1 over -1. Is there a path where a stale value could actually reach here? If not, I'd drop the block rather than leave a no-op that implies one exists.

Comment thread tensorrt_llm/_torch/speculative/interface.py Outdated
Comment thread tensorrt_llm/_torch/speculative/eagle3.py Outdated
Comment thread tensorrt_llm/_torch/speculative/eagle3.py Outdated
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #69663 [ run ] completed with state SUCCESS. Commit: 32a287f
/LLM/main/L0_MergeRequest_PR pipeline #56967 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

Signed-off-by: Yi Zhang <187001205+yizhang-nv@users.noreply.github.com>
Signed-off-by: Yi Zhang <187001205+yizhang-nv@users.noreply.github.com>
Signed-off-by: Yi Zhang <187001205+yizhang-nv@users.noreply.github.com>
Signed-off-by: Yi Zhang <187001205+yizhang-nv@users.noreply.github.com>
Signed-off-by: Yi Zhang <187001205+yizhang-nv@users.noreply.github.com>
@yizhang-nv
yizhang-nv force-pushed the codex/fix-mtp-eagle-chunk-lookahead branch from 32a287f to 52a4c92 Compare August 28, 2026 10:03
@coderabbitai

coderabbitai Bot commented Aug 28, 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.

@yizhang-nv

Copy link
Copy Markdown
Member Author

@zhaoyangwang-nvidia Thanks for the review. I added the measured acceptance-rate and acceptance-length results, including prompt length and chunk size, to the PR description.

On one B200 with DeepSeek-V3-Lite BF16, TP=1, MTP Eagle one-model, max draft length 1, prompt length 512, chunk size 128, and output length 128, using 3 documentation prompts and 2 runs per configuration:

  • before: 338/430 accepted draft tokens, AR 78.60%, acceptance length 1.7860
  • after: 339/429 accepted draft tokens, AR 79.02%, acceptance length 1.7902

That is +0.42 pp AR and +0.0042 acceptance length. The per-run AR ranges overlap (76.96%-80.28% before and 78.60%-79.44% after), so I described the impact as neutral/small with no observed regression rather than claiming a statistically meaningful improvement.

I also addressed the inline cleanup suggestions, rebased onto the latest origin/main, and reran the related suites: 128 passed, 22 skipped, and 26 subtests passed.

@yizhang-nv

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #69963 [ run ] triggered by Bot. Commit: 52a4c92 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #69963 [ run ] completed with state SUCCESS. Commit: 52a4c92
/LLM/main/L0_MergeRequest_PR pipeline #57244 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

@yizhang-nv

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #70131 [ run ] triggered by Bot. Commit: 52a4c92 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #70131 [ run ] completed with state SUCCESS. Commit: 52a4c92
/LLM/main/L0_MergeRequest_PR pipeline #57395 completed with status: 'UNSTABLE'

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

Link to invocation

@yizhang-nv

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #70249 [ run ] triggered by Bot. Commit: 52a4c92 Link to invocation

@yizhang-nv
yizhang-nv enabled auto-merge (squash) August 31, 2026 06:17
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #70249 [ run ] completed with state FAILURE. Commit: 52a4c92
/LLM/main/L0_MergeRequest_PR pipeline #57498 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

@yizhang-nv

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #70324 [ run ] triggered by Bot. Commit: 52a4c92 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #70324 [ run ] completed with state SUCCESS. Commit: 52a4c92
/LLM/main/L0_MergeRequest_PR pipeline #57562 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

@yizhang-nv

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #70378 [ run ] triggered by Bot. Commit: 52a4c92 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #70378 [ run ] completed with state SUCCESS. Commit: 52a4c92
/LLM/main/L0_MergeRequest_PR pipeline #57611 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

@yizhang-nv

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

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.

6 participants