Skip to content

Conversation

@ziyixiong-nv
Copy link
Collaborator

@ziyixiong-nv ziyixiong-nv commented Jul 17, 2025

Description

As mentioned in #5448 , there's an issue when enabling KV cache reuse for two-model engine.

If there are cached blocks, https://github.com/NVIDIA/TensorRT-LLM/blob/main/tensorrt_llm/_torch/pyexecutor/resource_manager.py#L349 would return false because context_current_position > 0.
As a result, when we try to allocate KV cache pages for the draft model, no pages are allocated.

To fix this issue, the checking in isFirstContextChunk() can be replaced with mContextCurrentPosition == mPrepopulatedPromptLen where mPrepopulatedPromptLen is the number of tokens already in KV cache.

Test Coverage

tests/unittest/_torch/speculative/test_eagle3.py::test_llama_eagle3
tests/unittest/_torch/speculative/test_kv_cache_reuse.py::test_kv_cache_reuse

GitHub Bot Help

/bot [-h] ['run', 'kill', 'skip', 'reuse-pipeline'] ...

Provide a user friendly way for developers to interact with a Jenkins server.

Run /bot [-h|--help] to print this help message.

See details below for each supported subcommand.

Details

run [--disable-fail-fast --skip-test --stage-list "A10-1, xxx" --gpu-type "A30, H100_PCIe" --add-multi-gpu-test --only-multi-gpu-test --disable-multi-gpu-test --post-merge --extra-stage "H100_PCIe-[Post-Merge]-1, xxx"]

Launch build/test pipelines. All previously running jobs will be killed.

--disable-fail-fast (OPTIONAL) : Disable fail fast on build/tests/infra failures.

--skip-test (OPTIONAL) : Skip all test stages, but still run build stages, package stages and sanity check stages. Note: Does NOT update GitHub check status.

--stage-list "A10-1, xxx" (OPTIONAL) : Only run the specified test stages. Examples: "A10-1, xxx". Note: Does NOT update GitHub check status.

--gpu-type "A30, H100_PCIe" (OPTIONAL) : Only run the test stages on the specified GPU types. Examples: "A30, H100_PCIe". Note: Does NOT update GitHub check status.

--only-multi-gpu-test (OPTIONAL) : Only run the multi-GPU tests. Note: Does NOT update GitHub check status.

--disable-multi-gpu-test (OPTIONAL) : Disable the multi-GPU tests. Note: Does NOT update GitHub check status.

--add-multi-gpu-test (OPTIONAL) : Force run the multi-GPU tests. Will also run L0 pre-merge pipeline.

--post-merge (OPTIONAL) : Run the L0 post-merge pipeline instead of the ordinary L0 pre-merge pipeline.

--extra-stage "H100_PCIe-[Post-Merge]-1, xxx" (OPTIONAL) : Run the ordinary L0 pre-merge pipeline and specified test stages. Examples: --extra-stage "H100_PCIe-[Post-Merge]-1, xxx".

For guidance on mapping tests to stage names, see docs/source/reference/ci-overview.md.

kill

kill

Kill all running builds associated with pull request.

skip

skip --comment COMMENT

Skip testing for latest commit on pull request. --comment "Reason for skipping build/test" is required. IMPORTANT NOTE: This is dangerous since lack of user care and validation can cause top of tree to break.

reuse-pipeline

reuse-pipeline

Reuse a previous pipeline to validate current commit. This action will also kill all currently running builds associated with the pull request. IMPORTANT NOTE: This is dangerous since lack of user care and validation can cause top of tree to break.

Summary by CodeRabbit

  • Bug Fixes

    • Improved detection of the first context chunk in LLM requests for more accurate processing.
    • Removed restrictions on using draft model engines with KV cache reuse, enabling broader compatibility.
  • Tests

    • Added new tests for speculative decoding scenarios, including KV cache reuse.
    • Introduced a dedicated test verifying KV cache reuse functionality and output consistency.
    • Expanded test coverage with additional parameter combinations for speculative decoding.

@ziyixiong-nv ziyixiong-nv requested a review from a team as a code owner July 17, 2025 07:13
@ziyixiong-nv ziyixiong-nv requested a review from Naveassaf July 17, 2025 07:13
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 17, 2025

Walkthrough

The changes update logic for context chunk detection in the LLM request class, remove restrictions and workarounds for KV cache block reuse with speculative decoding in the PyExecutor and its config mangling, and add new and expanded tests to validate KV cache reuse with speculative decoding, including a new test file and additional test cases.

Changes

File(s) Change Summary
cpp/include/tensorrt_llm/batch_manager/llmRequest.h Modified pause() to reset prepopulated prompt length; updated isFirstContextChunk() to use prepopulated prompt length for context chunk detection.
tensorrt_llm/_torch/pyexecutor/py_executor.py Removed error-raising check preventing draft model engine with KV cache block reuse.
tensorrt_llm/_torch/pyexecutor/py_executor_creator.py Deleted workaround disabling KV cache block reuse when using speculative decoding with a draft model.
tests/integration/test_lists/test-db/l0_b200.yml Added two new test entries for speculative decoding and KV cache reuse under PyTorch backend.
tests/unittest/_torch/speculative/test_eagle3.py Added two new parameterized test cases with block reuse enabled.
tests/unittest/_torch/speculative/test_kv_cache_reuse.py Introduced new test file for verifying KV cache reuse with speculative decoding.

Sequence Diagram(s)

sequenceDiagram
    participant Tester
    participant LLM
    participant KVCacheManager
    participant DraftModel
    participant TargetModel

    Tester->>LLM: Initialize with KV cache block reuse enabled
    LLM->>KVCacheManager: Configure block reuse
    Tester->>LLM: Generate text with speculative decoding (first run, no KV cache)
    LLM->>DraftModel: Generate draft tokens
    LLM->>TargetModel: Generate target tokens
    LLM->>KVCacheManager: Store results in KV cache
    Tester->>LLM: Generate text with speculative decoding (second run, with KV cache)
    LLM->>KVCacheManager: Reuse cached blocks
    LLM->>DraftModel: Generate draft tokens
    LLM->>TargetModel: Generate target tokens
    Tester->>Tester: Compare outputs for consistency
Loading

Suggested reviewers

  • mikeiovine
  • HuiGao-NV
  • yilin-void

Poem

🐇 In code's quiet burrow, changes take flight,
KV cache reuse now shines bright.
Speculative leaps with models aligned,
Draft and target in harmony combined.
Tests hop forward, ensuring no glitch,
In this warren of logic, every byte fits!
🌿✨


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8dfc142 and faed2b1.

📒 Files selected for processing (6)
  • cpp/include/tensorrt_llm/batch_manager/llmRequest.h (2 hunks)
  • tensorrt_llm/_torch/pyexecutor/py_executor.py (0 hunks)
  • tensorrt_llm/_torch/pyexecutor/py_executor_creator.py (0 hunks)
  • tests/integration/test_lists/test-db/l0_b200.yml (1 hunks)
  • tests/unittest/_torch/speculative/test_eagle3.py (1 hunks)
  • tests/unittest/_torch/speculative/test_kv_cache_reuse.py (1 hunks)
💤 Files with no reviewable changes (2)
  • tensorrt_llm/_torch/pyexecutor/py_executor_creator.py
  • tensorrt_llm/_torch/pyexecutor/py_executor.py
🚧 Files skipped from review as they are similar to previous changes (4)
  • tests/unittest/_torch/speculative/test_eagle3.py
  • tests/integration/test_lists/test-db/l0_b200.yml
  • tests/unittest/_torch/speculative/test_kv_cache_reuse.py
  • cpp/include/tensorrt_llm/batch_manager/llmRequest.h
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@ziyixiong-nv
Copy link
Collaborator Author

/bot run

@tensorrt-cicd
Copy link
Collaborator

PR_Github #12174 [ run ] triggered by Bot

@ziyixiong-nv ziyixiong-nv force-pushed the dev-fxiong-kv-cache-reuse branch from fa72ae2 to 5e17c7d Compare July 17, 2025 07:27
@ziyixiong-nv ziyixiong-nv changed the title [Draft] [TRTLLM-6452][feat]: Two-model engine KV cache reuse support [TRTLLM-6452][feat]: Two-model engine KV cache reuse support Jul 17, 2025
@ziyixiong-nv
Copy link
Collaborator Author

/bot run

@tensorrt-cicd
Copy link
Collaborator

PR_Github #12179 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #12174 [ run ] completed with state ABORTED

@ziyixiong-nv ziyixiong-nv requested a review from mikeiovine July 17, 2025 08:00
@tensorrt-cicd
Copy link
Collaborator

PR_Github #12179 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #9045 completed with status: 'FAILURE'

@ziyixiong-nv ziyixiong-nv marked this pull request as draft July 17, 2025 11:54
@ziyixiong-nv ziyixiong-nv force-pushed the dev-fxiong-kv-cache-reuse branch from 5e17c7d to 034bebe Compare July 17, 2025 13:36
@ziyixiong-nv ziyixiong-nv force-pushed the dev-fxiong-kv-cache-reuse branch from 034bebe to 135f7b2 Compare July 18, 2025 08:04
@ziyixiong-nv
Copy link
Collaborator Author

/bot run

@ziyixiong-nv ziyixiong-nv marked this pull request as ready for review July 18, 2025 08:04
@tensorrt-cicd
Copy link
Collaborator

PR_Github #12298 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #12298 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #9134 completed with status: 'FAILURE'

@mikeiovine
Copy link
Collaborator

Ok, I think I've convinced myself that this is safe. The scenario I proposed in the previous comment is not possible because context_current_position will always be greater than prepopulated_prompt_length when the request is moved to the next context chunk.

The change in behavior is like this:

  • Before this PR, is_first_context_chunk will become false immediately after calling kv_cache_manager.add_sequence.
  • Now, it won't be false until the request is moved to the next context chunk, which is much more intuitive.

The amount of mutation we have is still very confusing though. Can you add some comments to isFirstContextChunk?

@ziyixiong-nv ziyixiong-nv force-pushed the dev-fxiong-kv-cache-reuse branch from 135f7b2 to 8dfc142 Compare July 19, 2025 00:08
@ziyixiong-nv
Copy link
Collaborator Author

/bot run

@ziyixiong-nv ziyixiong-nv enabled auto-merge (squash) July 19, 2025 00:14
@tensorrt-cicd
Copy link
Collaborator

PR_Github #12357 [ run ] triggered by Bot

@ziyixiong-nv
Copy link
Collaborator Author

/bot run

@tensorrt-cicd
Copy link
Collaborator

PR_Github #12359 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #12357 [ run ] completed with state ABORTED

@tensorrt-cicd
Copy link
Collaborator

PR_Github #12359 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #9181 completed with status: 'SUCCESS'
Pipeline passed with automatic retried tests. Check the rerun report for details.

@ziyixiong-nv ziyixiong-nv force-pushed the dev-fxiong-kv-cache-reuse branch from 8dfc142 to faed2b1 Compare July 19, 2025 05:01
@ziyixiong-nv
Copy link
Collaborator Author

/bot reuse-pipeline

@tensorrt-cicd
Copy link
Collaborator

PR_Github #12361 [ reuse-pipeline ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #12361 [ reuse-pipeline ] completed with state SUCCESS
Reusing PR_Github #12359 for commit faed2b1

@ziyixiong-nv ziyixiong-nv merged commit 66030ef into NVIDIA:main Jul 19, 2025
3 checks passed
reasonsolo pushed a commit to reasonsolo/TensorRT-LLM that referenced this pull request Jul 21, 2025
timlee0212 pushed a commit to timlee0212/TensorRT-LLM that referenced this pull request Jul 21, 2025
NVShreyas pushed a commit to NVShreyas/TensorRT-LLM that referenced this pull request Jul 28, 2025
…6133)

Signed-off-by: ziyixiong-nv <[email protected]>
Signed-off-by: ziyixiong-nv <[email protected]>
Signed-off-by: Shreyas Misra <[email protected]>
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