|
| 1 | +"""This docstring details important information on the testing methodology. |
| 2 | +
|
| 3 | +Most of the tests rely on "greedy equality", where we expect the output of |
| 4 | +speculative decoding on a sequence to exactly match the output of normal non- |
| 5 | +speculative decoding. |
| 6 | +
|
| 7 | +Since speculative decoding with rejection sampling guarantees that the output |
| 8 | +distribution matches the target model's output distribution (up to hardware |
| 9 | +numerics, see https://arxiv.org/pdf/2302.01318.pdf), we can expect greedy |
| 10 | +equality. |
| 11 | +
|
| 12 | +For ngram lookup, its idea comes from https://github.com/apoorvumang/prompt-lookup-decoding, |
| 13 | +and is merged into transform code base: https://github.com/huggingface/transformers/pull/27775. |
| 14 | +Since there is no model is needed for generate the proposal, we could make |
| 15 | +the testcase much simpler than drafter multi-step one. |
| 16 | +
|
| 17 | +However, we still need to verify below scenario could be passed: |
| 18 | + * Batch size 1 greedy equality |
| 19 | + * Batch size >1 greedy equality |
| 20 | + * Test greedy equality under preemption |
| 21 | + * Test greedy equality under various ngram sizes / speculative sizes |
| 22 | +
|
| 23 | +With those tests, we can say at least, ngram spec would not break the correctess |
| 24 | +for the target model outputs. |
| 25 | +""" |
| 26 | + |
| 27 | +import pytest |
| 28 | + |
| 29 | +from .conftest import run_greedy_equality_correctness_test |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.parametrize( |
| 33 | + "common_llm_kwargs", |
| 34 | + [{ |
| 35 | + # Skip cuda graph recording for fast test. |
| 36 | + "enforce_eager": True, |
| 37 | +
|
| 38 | + # Required for spec decode. |
| 39 | + "use_v2_block_manager": True, |
| 40 | +
|
| 41 | + # Print spec metrics. |
| 42 | + "disable_log_stats": False, |
| 43 | + }]) |
| 44 | +@pytest.mark.parametrize("per_test_common_llm_kwargs", [ |
| 45 | + { |
| 46 | + "model": "JackFram/llama-68m", |
| 47 | + }, |
| 48 | +]) |
| 49 | +@pytest.mark.parametrize("baseline_llm_kwargs", [{}]) |
| 50 | +@pytest.mark.parametrize("test_llm_kwargs", [ |
| 51 | + { |
| 52 | + "speculative_model": "[ngram]", |
| 53 | + "num_speculative_tokens": 5, |
| 54 | + "ngram_prompt_lookup_max": 3, |
| 55 | + }, |
| 56 | +]) |
| 57 | +@pytest.mark.parametrize("output_len", [ |
| 58 | + 256, |
| 59 | +]) |
| 60 | +@pytest.mark.parametrize("batch_size", [1, 64]) |
| 61 | +@pytest.mark.parametrize("seed", [1]) |
| 62 | +def test_ngram_e2e_greedy_correctness(baseline_llm_generator, |
| 63 | + test_llm_generator, batch_size: int, |
| 64 | + output_len: int): |
| 65 | + """Verify greedy equality on a tiny model with different batch size.""" |
| 66 | + run_greedy_equality_correctness_test(baseline_llm_generator, |
| 67 | + test_llm_generator, |
| 68 | + batch_size, |
| 69 | + max_output_len=output_len, |
| 70 | + force_output_len=True) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.parametrize( |
| 74 | + "common_llm_kwargs", |
| 75 | + [{ |
| 76 | + "block_size": 8, |
| 77 | + # 2 for small prompt, 256//8 for generated. |
| 78 | + "num_gpu_blocks_override": 2 + 256 // 8, |
| 79 | + "max_model_len": (2 + 256 // 8) * 8, |
| 80 | +
|
| 81 | + # Skip cuda graph recording for fast test. |
| 82 | + "enforce_eager": True, |
| 83 | +
|
| 84 | + # Required for spec decode. |
| 85 | + "use_v2_block_manager": True |
| 86 | + }]) |
| 87 | +@pytest.mark.parametrize("per_test_common_llm_kwargs", [ |
| 88 | + { |
| 89 | + "model": "JackFram/llama-160m", |
| 90 | + }, |
| 91 | +]) |
| 92 | +@pytest.mark.parametrize("baseline_llm_kwargs", [{}]) |
| 93 | +@pytest.mark.parametrize("test_llm_kwargs", [ |
| 94 | + { |
| 95 | + "speculative_model": "[ngram]", |
| 96 | + "num_speculative_tokens": 5, |
| 97 | + "ngram_prompt_lookup_max": 3, |
| 98 | + }, |
| 99 | +]) |
| 100 | +@pytest.mark.parametrize( |
| 101 | + "output_len", |
| 102 | + [ |
| 103 | + # Use small output len for fast test. |
| 104 | + 256, |
| 105 | + ]) |
| 106 | +@pytest.mark.parametrize("batch_size", [4]) |
| 107 | +@pytest.mark.parametrize("seed", [1]) |
| 108 | +def test_ngram_e2e_greedy_correctness_with_preemption(baseline_llm_generator, |
| 109 | + test_llm_generator, |
| 110 | + batch_size: int, |
| 111 | + output_len: int): |
| 112 | + """Verify greedy equality, even when some sequences are preempted mid- |
| 113 | + generation. |
| 114 | + """ |
| 115 | + run_greedy_equality_correctness_test(baseline_llm_generator, |
| 116 | + test_llm_generator, |
| 117 | + batch_size, |
| 118 | + max_output_len=output_len, |
| 119 | + force_output_len=True) |
| 120 | + |
| 121 | + |
| 122 | +@pytest.mark.parametrize( |
| 123 | + "common_llm_kwargs", |
| 124 | + [{ |
| 125 | + "model": "JackFram/llama-68m", |
| 126 | +
|
| 127 | + # Skip cuda graph recording for fast test. |
| 128 | + "enforce_eager": True, |
| 129 | +
|
| 130 | + # Required for spec decode. |
| 131 | + "use_v2_block_manager": True |
| 132 | + }]) |
| 133 | +@pytest.mark.parametrize("per_test_common_llm_kwargs", [{}]) |
| 134 | +@pytest.mark.parametrize("baseline_llm_kwargs", [{}]) |
| 135 | +@pytest.mark.parametrize( |
| 136 | + "test_llm_kwargs", |
| 137 | + [ |
| 138 | + { |
| 139 | + "speculative_model": "[ngram]", |
| 140 | + "num_speculative_tokens": k, |
| 141 | + "ngram_prompt_lookup_max": 3, |
| 142 | + } |
| 143 | + # Try a range of common k, as well as large speculation. |
| 144 | + for k in [1, 3, 5] |
| 145 | + ] + [ |
| 146 | + { |
| 147 | + "speculative_model": "[ngram]", |
| 148 | + "num_speculative_tokens": k, |
| 149 | + "ngram_prompt_lookup_max": 1, |
| 150 | + } |
| 151 | + # Try a range of common k, as well as large speculation. |
| 152 | + for k in [1, 3, 5] |
| 153 | + ]) |
| 154 | +@pytest.mark.parametrize("batch_size", [2]) |
| 155 | +@pytest.mark.parametrize( |
| 156 | + "output_len", |
| 157 | + [ |
| 158 | + # Use smaller output len for fast test. |
| 159 | + 32, |
| 160 | + ]) |
| 161 | +@pytest.mark.parametrize("seed", [1]) |
| 162 | +def test_ngram_different_k(baseline_llm_generator, test_llm_generator, |
| 163 | + batch_size: int, output_len: int): |
| 164 | + """Verify that ngram speculative decoding produces exact equality |
| 165 | + to without spec decode with many different values of k and |
| 166 | + different ngram_prompt_lookup_max. |
| 167 | + """ |
| 168 | + run_greedy_equality_correctness_test(baseline_llm_generator, |
| 169 | + test_llm_generator, |
| 170 | + batch_size, |
| 171 | + max_output_len=output_len, |
| 172 | + force_output_len=True) |
0 commit comments