| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | import pytest |
| 4 | |
| 5 | from vllm.config.speculative import SpeculativeConfig |
| 6 | from vllm.v1.spec_decode.utils import unconditional_to_conditional_rates |
| 7 | |
| 8 | |
| 9 | def test_unconditional_to_conditional_rates_basic(): |
| 10 | # c_0 = p_0; c_i = p_i / p_{i-1} |
| 11 | assert unconditional_to_conditional_rates([0.9, 0.5, 0.2]) == pytest.approx( |
| 12 | [0.9, 0.5 / 0.9, 0.2 / 0.5] |
| 13 | ) |
| 14 | |
| 15 | |
| 16 | def test_unconditional_to_conditional_rates_handles_zero(): |
| 17 | # After a zero, subsequent conditional rates are clamped to 0 (the chain |
| 18 | # has already terminated in the kernel, so these values are unused). |
| 19 | assert unconditional_to_conditional_rates([1.0, 0.6, 0.0, 0.0]) == pytest.approx( |
| 20 | [1.0, 0.6, 0.0, 0.0] |
| 21 | ) |
| 22 | |
| 23 | |
| 24 | def test_unconditional_to_conditional_rates_all_ones(): |
| 25 | assert unconditional_to_conditional_rates([1.0, 1.0, 1.0]) == pytest.approx( |
| 26 | [1.0, 1.0, 1.0] |
| 27 | ) |
| 28 | |
| 29 | |
| 30 | @pytest.mark.parametrize( |
| 31 | "length,n,expected", |
| 32 | [ |
| 33 | (2.6, 3, [1.0, 0.6, 0.0]), |
| 34 | (1.0, 3, [0.0, 0.0, 0.0]), |
| 35 | (4.0, 3, [1.0, 1.0, 1.0]), |
| 36 | (2.0, 3, [1.0, 0.0, 0.0]), |
| 37 | (3.5, 4, [1.0, 1.0, 0.5, 0.0]), |
| 38 | ], |
| 39 | ) |
| 40 | def test_acceptance_length_to_rates(length, n, expected): |
| 41 | assert SpeculativeConfig._acceptance_length_to_rates(length, n) == pytest.approx( |
| 42 | expected |
| 43 | ) |
| 44 | |
| 45 | |
| 46 | def test_resolve_length_produces_minvariance_schedule(): |
| 47 | assert SpeculativeConfig._resolve_synthetic_acceptance_rates( |
| 48 | 3, None, 2.6 |
| 49 | ) == pytest.approx([1.0, 0.6, 0.0]) |