[Feat] Unified Synthetic Acceptance Rate for V1 and V2 (#40662)
Signed-off-by: Benjamin Chislett <bchislett@nvidia.com> Signed-off-by: Benjamin Chislett <chislett.ben@gmail.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
parent
fe85a92e86
commit
626daa2076
@@ -2,33 +2,48 @@
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
import pytest
|
||||
|
||||
from vllm.v1.worker.gpu.spec_decode.synthetic_rejection_sampler_utils import (
|
||||
compute_synthetic_rejection_sampler_params,
|
||||
from vllm.config.speculative import SpeculativeConfig
|
||||
from vllm.v1.spec_decode.utils import unconditional_to_conditional_rates
|
||||
|
||||
|
||||
def test_unconditional_to_conditional_rates_basic():
|
||||
# c_0 = p_0; c_i = p_i / p_{i-1}
|
||||
assert unconditional_to_conditional_rates([0.9, 0.5, 0.2]) == pytest.approx(
|
||||
[0.9, 0.5 / 0.9, 0.2 / 0.5]
|
||||
)
|
||||
|
||||
|
||||
def test_unconditional_to_conditional_rates_handles_zero():
|
||||
# After a zero, subsequent conditional rates are clamped to 0 (the chain
|
||||
# has already terminated in the kernel, so these values are unused).
|
||||
assert unconditional_to_conditional_rates([1.0, 0.6, 0.0, 0.0]) == pytest.approx(
|
||||
[1.0, 0.6, 0.0, 0.0]
|
||||
)
|
||||
|
||||
|
||||
def test_unconditional_to_conditional_rates_all_ones():
|
||||
assert unconditional_to_conditional_rates([1.0, 1.0, 1.0]) == pytest.approx(
|
||||
[1.0, 1.0, 1.0]
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"length,n,expected",
|
||||
[
|
||||
(2.6, 3, [1.0, 0.6, 0.0]),
|
||||
(1.0, 3, [0.0, 0.0, 0.0]),
|
||||
(4.0, 3, [1.0, 1.0, 1.0]),
|
||||
(2.0, 3, [1.0, 0.0, 0.0]),
|
||||
(3.5, 4, [1.0, 1.0, 0.5, 0.0]),
|
||||
],
|
||||
)
|
||||
|
||||
NUM_SPECULATIVE_STEPS = [1, 2, 3, 4, 5, 7, 10]
|
||||
ACCEPTANCE_RATES = [i / 100 for i in range(0, 100)]
|
||||
def test_acceptance_length_to_rates(length, n, expected):
|
||||
assert SpeculativeConfig._acceptance_length_to_rates(length, n) == pytest.approx(
|
||||
expected
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("num_speculative_steps", NUM_SPECULATIVE_STEPS)
|
||||
def test_compute_synthetic_rejection_sampler_params(num_speculative_steps: int):
|
||||
"""Test that the base acceptance rate and decay factor generated for
|
||||
synthetic rejection sampling have a mean joint acceptance probability
|
||||
that matches the desired acceptance rate."""
|
||||
tol = 1e-9
|
||||
for desired_acceptance_rate in ACCEPTANCE_RATES:
|
||||
base_rate, decay_factor = compute_synthetic_rejection_sampler_params(
|
||||
desired_acceptance_rate, num_speculative_steps, tol=tol
|
||||
)
|
||||
|
||||
# Compute the mean of joint acceptance probabilities across
|
||||
# all speculative positions.
|
||||
joint_prob = 1.0
|
||||
mean_joint = 0.0
|
||||
for i in range(num_speculative_steps):
|
||||
joint_prob *= base_rate * decay_factor**i
|
||||
mean_joint += joint_prob
|
||||
mean_joint /= num_speculative_steps
|
||||
|
||||
assert abs(desired_acceptance_rate - mean_joint) < 10 * tol
|
||||
assert base_rate <= 1.0
|
||||
def test_resolve_length_produces_minvariance_schedule():
|
||||
assert SpeculativeConfig._resolve_synthetic_acceptance_rates(
|
||||
3, None, 2.6
|
||||
) == pytest.approx([1.0, 0.6, 0.0])
|
||||
|
||||
Reference in New Issue
Block a user