|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import unittest |
| 4 | + |
| 5 | +import pytest |
| 6 | +import torch |
| 7 | +from utils.llm_data import llm_models_root |
| 8 | + |
| 9 | +from tensorrt_llm import LLM, SamplingParams |
| 10 | +from tensorrt_llm.llmapi import (CudaGraphConfig, EagleDecodingConfig, |
| 11 | + KvCacheConfig) |
| 12 | + |
| 13 | +sys.path.append(os.path.join(os.path.dirname(__file__), '..')) |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.parametrize("use_cuda_graph,attn_backend", [ |
| 17 | + [True, "TRTLLM"], |
| 18 | + [False, "TRTLLM"], |
| 19 | +]) |
| 20 | +@pytest.mark.high_cuda_memory |
| 21 | +def test_kv_cache_reuse(use_cuda_graph: bool, attn_backend: str): |
| 22 | + # Eagle3 one model works with overlap scheduler and block reuse. |
| 23 | + total_mem_gb = torch.cuda.get_device_properties(0).total_memory / 1e9 |
| 24 | + if total_mem_gb < 35: |
| 25 | + pytest.skip("Not enough memory to load target + draft model") |
| 26 | + |
| 27 | + models_path = llm_models_root() |
| 28 | + eagle_model_dir = f"{models_path}/EAGLE3-LLaMA3.1-Instruct-8B" |
| 29 | + target_model_dir = f"{models_path}/llama-3.1-model/Llama-3.1-8B-Instruct" |
| 30 | + |
| 31 | + # bs > 1 gives non-deterministic when doing IFB. There are slight chances |
| 32 | + # that ref and spec does not match 100% |
| 33 | + max_batch_size = 1 |
| 34 | + max_draft_len = 4 |
| 35 | + kv_cache_config = KvCacheConfig(enable_block_reuse=True, |
| 36 | + free_gpu_memory_fraction=0.5) |
| 37 | + cuda_graph_config = CudaGraphConfig( |
| 38 | + batch_sizes=[1]) if use_cuda_graph else None |
| 39 | + |
| 40 | + llm_common_config = dict( |
| 41 | + model=target_model_dir, |
| 42 | + attn_backend=attn_backend, |
| 43 | + disable_overlap_scheduler=True, |
| 44 | + cuda_graph_config=cuda_graph_config, |
| 45 | + max_batch_size=max_batch_size, |
| 46 | + kv_cache_config=kv_cache_config, |
| 47 | + # This max_seq_len is larger than the one specified |
| 48 | + # in the llama 3 8B eagle's config. We want to make sure |
| 49 | + # that the draft model won't go above its max in warmup |
| 50 | + # in this test. |
| 51 | + max_seq_len=8192, |
| 52 | + ) |
| 53 | + |
| 54 | + spec_config = EagleDecodingConfig( |
| 55 | + max_draft_len=max_draft_len, |
| 56 | + speculative_model_dir=eagle_model_dir, |
| 57 | + eagle3_one_model=False, |
| 58 | + ) |
| 59 | + |
| 60 | + llm_spec = LLM(**llm_common_config, speculative_config=spec_config) |
| 61 | + |
| 62 | + # Output tests |
| 63 | + prompt = "The future of AI is" |
| 64 | + |
| 65 | + sampling_params = SamplingParams(max_tokens=10, temperature=0) |
| 66 | + |
| 67 | + # First run without KV cache |
| 68 | + results = llm_spec.generate(prompt, sampling_params) |
| 69 | + generated_text = results.outputs[0].text |
| 70 | + |
| 71 | + # Second run with KV cache |
| 72 | + results_kv_cache = llm_spec.generate(prompt, sampling_params) |
| 73 | + generated_text_kv_cache = results_kv_cache.outputs[0].text |
| 74 | + |
| 75 | + llm_spec.shutdown() |
| 76 | + |
| 77 | + assert generated_text == generated_text_kv_cache |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + unittest.main() |
0 commit comments