[Bugfix][Model Runner V2][Spec Decode] Decouple the draft's gumbel noise stream from the target's (#54282)
Signed-off-by: Giancarlo Delfin <gdelfin@inferact.ai>
This commit is contained in:
@@ -6,6 +6,7 @@ import math
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
from vllm.v1.worker.gpu.sample.gumbel import gumbel_sample
|
||||
from vllm.v1.worker.gpu.spec_decode.rejection_sampler_utils import (
|
||||
rejection_sample,
|
||||
)
|
||||
@@ -195,6 +196,101 @@ def test_stochastic_rejection_sample(
|
||||
)
|
||||
|
||||
|
||||
# The test above spreads its samples too thin to resolve a small distributional
|
||||
# bias: VOCAB_SIZE bins over 10 * VOCAB_SIZE trials is ~10 samples per bin.
|
||||
# Sixteen bins over 200K trials is ~12K per bin, which resolves a few percent.
|
||||
NARROW_VOCAB_SIZE = 16
|
||||
NARROW_NUM_TRIALS = 200_000
|
||||
|
||||
|
||||
def _gumbel_drafted_tokens(
|
||||
inputs: dict,
|
||||
draft_logits_1d: torch.Tensor,
|
||||
num_trials: int,
|
||||
num_speculative_steps: int,
|
||||
) -> torch.Tensor:
|
||||
"""Proposals drawn with gumbel_sample, shaped like inputs["draft_sampled"].
|
||||
|
||||
_build_rejection_sample_inputs draws them with torch.multinomial, which is
|
||||
independent of the resample noise by construction. Production drafts come
|
||||
from gumbel_sample keyed by pos[t * (K + 1) + i] for step i of trial t --
|
||||
the same entry _rejection_kernel and _resample_kernel read for that token --
|
||||
so the draft and the residual compete for one noise stream.
|
||||
"""
|
||||
k = num_speculative_steps
|
||||
vocab_size = draft_logits_1d.shape[0]
|
||||
device = draft_logits_1d.device
|
||||
draft_tokens = gumbel_sample(
|
||||
draft_logits_1d.unsqueeze(0).expand(num_trials * k, vocab_size).float(),
|
||||
inputs["expanded_idx_mapping"]
|
||||
.view(num_trials, k + 1)[:, :k]
|
||||
.reshape(-1)
|
||||
.contiguous(),
|
||||
inputs["temperature"],
|
||||
inputs["seed"],
|
||||
inputs["pos"].view(num_trials, k + 1)[:, :k].reshape(-1).contiguous(),
|
||||
apply_temperature=True,
|
||||
is_drafting=True,
|
||||
)
|
||||
draft_sampled = torch.zeros(num_trials * (k + 1), dtype=torch.int64, device=device)
|
||||
draft_sampled.view(num_trials, k + 1)[:, 1:] = draft_tokens.view(num_trials, k)
|
||||
return draft_sampled
|
||||
|
||||
|
||||
@pytest.mark.parametrize("num_speculative_steps", [1, 3])
|
||||
def test_gumbel_drafted_rejection_sample_is_unbiased(num_speculative_steps: int):
|
||||
"""The proposal and the residual resample must not share a noise vector.
|
||||
|
||||
Draws proposals on the same (seed, pos) stream the sampler verifies and
|
||||
resamples with, then checks the output still follows the target. Conditioned
|
||||
on a proposal winning the argmax, every other token's Gumbel is truncated
|
||||
below that max -- most tightly for the tokens the draft ranked highest -- so
|
||||
a shared stream makes the residual under-weight exactly those tokens.
|
||||
|
||||
Runs narrow because the wide-vocab test above cannot resolve this: dropping
|
||||
`is_drafting=True` in _gumbel_drafted_tokens takes position 0 from chi2 ~12 to
|
||||
~1500 against a threshold of ~70 here, while leaving that test passing.
|
||||
"""
|
||||
torch.manual_seed(42)
|
||||
device = "cuda"
|
||||
|
||||
# A draft that ranks tokens in exactly the opposite order rejects ~73% of
|
||||
# proposals, so most trials reach the residual resample where the bias
|
||||
# lives. The disagreement has to be constructed rather than sampled: two
|
||||
# independent randn draws land close together often enough that the signal
|
||||
# swings between chi2 ~14 and ~1900 depending on the seed. At temperature
|
||||
# 1.0 the target needs no scaling before being passed in.
|
||||
target_logits_1d = torch.randn(
|
||||
NARROW_VOCAB_SIZE, device=device, dtype=torch.float32
|
||||
)
|
||||
draft_logits_1d = -target_logits_1d
|
||||
|
||||
inputs = _build_rejection_sample_inputs(
|
||||
target_logits_1d,
|
||||
draft_logits_1d,
|
||||
num_speculative_steps,
|
||||
temperature=1.0,
|
||||
num_trials=NARROW_NUM_TRIALS,
|
||||
)
|
||||
inputs["draft_sampled"] = _gumbel_drafted_tokens(
|
||||
inputs, draft_logits_1d, NARROW_NUM_TRIALS, num_speculative_steps
|
||||
)
|
||||
|
||||
sampled, num_sampled = rejection_sample(
|
||||
**inputs, num_speculative_steps=num_speculative_steps
|
||||
)
|
||||
|
||||
# Position 0 carries the power: every trial reaches it, while later
|
||||
# positions are only reached on acceptance, which is rare by construction.
|
||||
assert (num_sampled >= 1).all()
|
||||
target_probs = torch.softmax(target_logits_1d, dim=0)
|
||||
for pos in range(num_speculative_steps + 1):
|
||||
accepted_mask = num_sampled >= pos + 1
|
||||
_assert_distribution_match(
|
||||
sampled[accepted_mask, pos], target_probs, device, label=f"position {pos}"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("num_speculative_steps", [1, 3])
|
||||
def test_greedy_rejection_sample(num_speculative_steps: int):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user