-
Notifications
You must be signed in to change notification settings - Fork 2.6k
[TRTLLM-6452][feat]: Two-model engine KV cache reuse support #6133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ziyixiong-nv
merged 2 commits into
NVIDIA:main
from
ziyixiong-nv:dev-fxiong-kv-cache-reuse
Jul 19, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import os | ||
| import sys | ||
| import unittest | ||
|
|
||
| import pytest | ||
| import torch | ||
| from utils.llm_data import llm_models_root | ||
|
|
||
| from tensorrt_llm import LLM, SamplingParams | ||
| from tensorrt_llm.llmapi import (CudaGraphConfig, EagleDecodingConfig, | ||
| KvCacheConfig) | ||
|
|
||
| sys.path.append(os.path.join(os.path.dirname(__file__), '..')) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("use_cuda_graph,attn_backend", [ | ||
| [True, "TRTLLM"], | ||
| [False, "TRTLLM"], | ||
| ]) | ||
| @pytest.mark.high_cuda_memory | ||
| def test_kv_cache_reuse(use_cuda_graph: bool, attn_backend: str): | ||
| # Eagle3 one model works with overlap scheduler and block reuse. | ||
| total_mem_gb = torch.cuda.get_device_properties(0).total_memory / 1e9 | ||
| if total_mem_gb < 35: | ||
| pytest.skip("Not enough memory to load target + draft model") | ||
|
|
||
| models_path = llm_models_root() | ||
| eagle_model_dir = f"{models_path}/EAGLE3-LLaMA3.1-Instruct-8B" | ||
| target_model_dir = f"{models_path}/llama-3.1-model/Llama-3.1-8B-Instruct" | ||
|
|
||
| # bs > 1 gives non-deterministic when doing IFB. There are slight chances | ||
| # that ref and spec does not match 100% | ||
| max_batch_size = 1 | ||
| max_draft_len = 4 | ||
| kv_cache_config = KvCacheConfig(enable_block_reuse=True, | ||
| free_gpu_memory_fraction=0.5) | ||
| cuda_graph_config = CudaGraphConfig( | ||
| batch_sizes=[1]) if use_cuda_graph else None | ||
|
|
||
| llm_common_config = dict( | ||
| model=target_model_dir, | ||
| attn_backend=attn_backend, | ||
| disable_overlap_scheduler=True, | ||
| cuda_graph_config=cuda_graph_config, | ||
| max_batch_size=max_batch_size, | ||
| kv_cache_config=kv_cache_config, | ||
| # This max_seq_len is larger than the one specified | ||
| # in the llama 3 8B eagle's config. We want to make sure | ||
| # that the draft model won't go above its max in warmup | ||
| # in this test. | ||
| max_seq_len=8192, | ||
| ) | ||
|
|
||
| spec_config = EagleDecodingConfig( | ||
| max_draft_len=max_draft_len, | ||
| speculative_model_dir=eagle_model_dir, | ||
| eagle3_one_model=False, | ||
| ) | ||
|
|
||
| llm_spec = LLM(**llm_common_config, speculative_config=spec_config) | ||
|
|
||
| # Output tests | ||
| prompt = "The future of AI is" | ||
|
|
||
| sampling_params = SamplingParams(max_tokens=10, temperature=0) | ||
|
|
||
| # First run without KV cache | ||
| results = llm_spec.generate(prompt, sampling_params) | ||
| generated_text = results.outputs[0].text | ||
|
|
||
| # Second run with KV cache | ||
| results_kv_cache = llm_spec.generate(prompt, sampling_params) | ||
| generated_text_kv_cache = results_kv_cache.outputs[0].text | ||
|
|
||
| llm_spec.shutdown() | ||
|
|
||
| assert generated_text == generated_text_kv_cache | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.