|
| 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 | +from utils.util import similar |
| 9 | + |
| 10 | +from tensorrt_llm import LLM, SamplingParams |
| 11 | +from tensorrt_llm._torch.speculative.speculation_gate import SpeculationGate |
| 12 | +from tensorrt_llm.llmapi import (CudaGraphConfig, EagleDecodingConfig, |
| 13 | + KvCacheConfig) |
| 14 | + |
| 15 | +sys.path.append(os.path.join(os.path.dirname(__file__), '..')) |
| 16 | + |
| 17 | + |
| 18 | +# It tests the end-to-end functionality of the SpeculationGate, |
| 19 | +# which will turn off spec decode when the average acceptance length is below the threshold. |
| 20 | +# It is set with acceptance window and acceptance threshold in spec_config. |
| 21 | +# This test set the max_concurrency to a large value to prevent spec decode turned off due to number of effective requests > max_concurrency, |
| 22 | +# So that we can only focus on the turning off effect from the SpeculationGate. |
| 23 | +@pytest.mark.high_cuda_memory |
| 24 | +def test_spec_gate_e2e(): |
| 25 | + total_mem_gb = torch.cuda.get_device_properties(0).total_memory / 1e9 |
| 26 | + if total_mem_gb < 35: |
| 27 | + pytest.skip("Not enough memory to load target + draft model") |
| 28 | + models_path = llm_models_root() |
| 29 | + eagle_model_dir = f"{models_path}/EAGLE3-LLaMA3.1-Instruct-8B" |
| 30 | + target_model_dir = f"{models_path}/llama-3.1-model/Llama-3.1-8B-Instruct" |
| 31 | + |
| 32 | + max_batch_size = 2 |
| 33 | + max_draft_len = 4 |
| 34 | + kv_cache_config = KvCacheConfig(enable_block_reuse=True, max_tokens=8192) |
| 35 | + cuda_graph_config = CudaGraphConfig(batch_sizes=[1]) |
| 36 | + |
| 37 | + llm_common_config = dict( |
| 38 | + model=target_model_dir, |
| 39 | + attn_backend="TRTLLM", |
| 40 | + disable_overlap_scheduler=True, |
| 41 | + cuda_graph_config=cuda_graph_config, |
| 42 | + max_batch_size=max_batch_size, |
| 43 | + kv_cache_config=kv_cache_config, |
| 44 | + max_seq_len=4096, |
| 45 | + ) |
| 46 | + |
| 47 | + spec_config = EagleDecodingConfig( |
| 48 | + max_draft_len=max_draft_len, |
| 49 | + speculative_model_dir=eagle_model_dir, |
| 50 | + # Llama 3 does not support one model eagle. |
| 51 | + eagle3_one_model=False, |
| 52 | + max_concurrency=10000, |
| 53 | + acceptance_window=5, |
| 54 | + acceptance_length_threshold=0.6, |
| 55 | + ) |
| 56 | + |
| 57 | + llm_spec = LLM(**llm_common_config, speculative_config=spec_config) |
| 58 | + # Output tests |
| 59 | + prompts = [ |
| 60 | + "The capital of France is", |
| 61 | + "The president of the United States is", |
| 62 | + "What is the capital of Australia?", |
| 63 | + "Explain in one sentence why the sky is blue.", |
| 64 | + "Who wrote the book 'Pride and Prejudice'?", |
| 65 | + "List three U.S. national holidays in the year 2025.", |
| 66 | + "What is the currency of Japan?", |
| 67 | + "How many players are on a basketball court for one team?", |
| 68 | + "List three primary colors.", |
| 69 | + ] |
| 70 | + sampling_params = SamplingParams(max_tokens=32, temperature=0) |
| 71 | + |
| 72 | + results_spec = llm_spec.generate(prompts, sampling_params) |
| 73 | + generated_text_spec = [result.outputs[0].text for result in results_spec] |
| 74 | + llm_spec.shutdown() |
| 75 | + |
| 76 | + llm_ref = LLM(**llm_common_config) |
| 77 | + results_ref = llm_ref.generate(prompts, sampling_params) |
| 78 | + generated_text_ref = [result.outputs[0].text for result in results_ref] |
| 79 | + llm_ref.shutdown() |
| 80 | + |
| 81 | + for text_spec, text_ref in zip(generated_text_spec, generated_text_ref): |
| 82 | + assert similar(text_spec, text_ref) |
| 83 | + |
| 84 | + |
| 85 | +def test_returns_none_until_window_and_enabled_when_above_threshold(): |
| 86 | + gate = SpeculationGate(window=3, threshold=0.5) |
| 87 | + |
| 88 | + disabled, avg = gate.record_avg_decoded(2.0, request_id=1) |
| 89 | + assert disabled is False and avg is None |
| 90 | + assert gate.disabled is False |
| 91 | + |
| 92 | + disabled, avg = gate.record_avg_decoded(2.0, request_id=2) |
| 93 | + assert disabled is False and avg is None |
| 94 | + assert gate.disabled is False |
| 95 | + |
| 96 | + disabled, avg = gate.record_avg_decoded(2.0, request_id=3) |
| 97 | + assert disabled is False |
| 98 | + assert avg == pytest.approx(1.0, rel=1e-6) |
| 99 | + assert gate.disabled is False |
| 100 | + |
| 101 | + |
| 102 | +def test_disables_when_avg_below_threshold_and_stays_disabled(): |
| 103 | + gate = SpeculationGate(window=3, threshold=0.7) |
| 104 | + |
| 105 | + gate.record_avg_decoded(1.1) |
| 106 | + gate.record_avg_decoded(1.2) |
| 107 | + |
| 108 | + disabled, avg = gate.record_avg_decoded(1.3) |
| 109 | + assert disabled is True |
| 110 | + assert avg == pytest.approx(0.2, rel=1e-6) |
| 111 | + assert gate.disabled is True |
| 112 | + |
| 113 | + # Once disabled, subsequent calls do nothing and return (False, None) |
| 114 | + disabled, avg = gate.record_avg_decoded(100.0) |
| 115 | + assert disabled is False and avg is None |
| 116 | + assert gate.disabled is True |
| 117 | + |
| 118 | + disabled, avg = gate.record_avg_decoded(200.0) |
| 119 | + assert disabled is False and avg is None |
| 120 | + assert gate.disabled is True |
| 121 | + |
| 122 | + |
| 123 | +def test_rolling_window_and_disable_on_drop(): |
| 124 | + gate = SpeculationGate(window=3, threshold=0.8) |
| 125 | + |
| 126 | + # First three high-acceptance requests keep it enabled |
| 127 | + gate.record_avg_decoded(2.0) |
| 128 | + gate.record_avg_decoded(2.0) |
| 129 | + disabled, avg = gate.record_avg_decoded(2.0) |
| 130 | + assert disabled is False |
| 131 | + assert avg == pytest.approx(1.0, rel=1e-6) |
| 132 | + assert gate.disabled is False |
| 133 | + |
| 134 | + # Fourth lower value enters window -> average drops below threshold -> disable |
| 135 | + disabled, avg = gate.record_avg_decoded(1.2) |
| 136 | + assert disabled is True |
| 137 | + assert avg == pytest.approx((1.0 + 1.0 + 0.2) / 3.0, rel=1e-6) |
| 138 | + assert gate.disabled is True |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == "__main__": |
| 142 | + unittest.main() |
0 commit comments