[BUG] Fix FP64 Gumbel precision coverage (#43150)

Signed-off-by: tianyu-z <zhangtianyupro@gmail.com>
Signed-off-by: Tianyu Zhang <53099276+tianyu-z@users.noreply.github.com>
Co-authored-by: Nick Hill <nickhill123@gmail.com>
Co-authored-by: OpenAI Codex <codex@openai.com>
This commit is contained in:
Tianyu Zhang
2026-06-05 19:04:14 +08:00
committed by GitHub
co-authored by Nick Hill OpenAI Codex
parent 8a83e6f2d7
commit 7fe7800fa4
11 changed files with 391 additions and 21 deletions
+4 -3
View File
@@ -235,9 +235,10 @@ class ModelConfig:
temperature and top_k/top_p.
"""
use_fp64_gumbel: bool = False
"""Whether to use FP64 (instead of FP32) for the Gumbel noise used by the
sampler. FP64 reduces the chance of ties in Gumbel-max sampling at the cost
of significantly lower kernel throughput on most GPUs."""
"""Whether to use FP64 (instead of FP32) random noise for Gumbel-max and
equivalent exponential-race sampling. FP64 preserves lower-tail sampling
events that fp32 uniform/exponential draws can truncate, at the cost of
significantly lower throughput on most GPUs."""
disable_sliding_window: bool = False
"""Whether to disable sliding window. If True, we will disable the sliding
window functionality of the model, capping to sliding window size. If the
+36 -7
View File
@@ -75,9 +75,14 @@ class TopKTopPSampler(nn.Module):
Implementations may update the logits tensor in-place.
"""
def __init__(self, logprobs_mode: LogprobsMode = "raw_logprobs") -> None:
def __init__(
self,
logprobs_mode: LogprobsMode = "raw_logprobs",
use_fp64_gumbel: bool = False,
) -> None:
super().__init__()
self.logprobs_mode = logprobs_mode
self.use_fp64_gumbel = use_fp64_gumbel
if current_platform.is_cuda():
# FlashInfer doesn't expose post-top-k/top-p logits/logprobs,
# so it can't be used when the configured mode requires them.
@@ -142,7 +147,10 @@ class TopKTopPSampler(nn.Module):
elif self.logprobs_mode == "processed_logprobs":
logits_to_return = logits.log_softmax(dim=-1, dtype=torch.float32)
probs = logits.softmax(dim=-1, dtype=torch.float32)
return random_sample(probs, generators), logits_to_return
return (
random_sample(probs, generators, self.use_fp64_gumbel),
logits_to_return,
)
def forward_cuda(
self,
@@ -163,6 +171,8 @@ class TopKTopPSampler(nn.Module):
"PyTorch-native implementation."
)
return self.forward_native(logits, generators, k, p)
if self.use_fp64_gumbel:
return self.forward_native(logits, generators, k, p)
assert self.logprobs_mode not in ("processed_logits", "processed_logprobs"), (
"FlashInfer does not support returning logits/logprobs"
)
@@ -190,16 +200,16 @@ class TopKTopPSampler(nn.Module):
elif self.logprobs_mode == "processed_logprobs":
logits_to_return = logits.log_softmax(dim=-1, dtype=torch.float32)
if len(generators) != logits.shape[0]:
if len(generators) != logits.shape[0] and not self.use_fp64_gumbel:
return compiled_random_sample(logits), logits_to_return
probs = logits.softmax(dim=-1, dtype=torch.float32)
q = torch.empty_like(probs)
q = empty_exponential_noise_like(probs, self.use_fp64_gumbel)
q.exponential_()
for i, generator in generators.items():
q[i].exponential_(generator=generator)
return probs.div_(q).argmax(dim=-1).view(-1), logits_to_return
return sample_with_exponential_noise(probs, q), logits_to_return
def forward_hip(
self,
@@ -216,6 +226,8 @@ class TopKTopPSampler(nn.Module):
"falling back to PyTorch-native."
)
return self.forward_native(logits, generators, k, p)
if self.use_fp64_gumbel:
return self.forward_native(logits, generators, k, p)
assert self.logprobs_mode not in (
"processed_logits",
"processed_logprobs",
@@ -404,16 +416,33 @@ def apply_top_k_only(logits: torch.Tensor, k: torch.Tensor) -> torch.Tensor:
return logits.masked_fill_(logits < top_k_mask, -float("inf"))
def empty_exponential_noise_like(
probs: torch.Tensor, use_fp64_gumbel: bool
) -> torch.Tensor:
dtype = torch.float64 if use_fp64_gumbel else probs.dtype
return torch.empty(probs.shape, dtype=dtype, device=probs.device)
def sample_with_exponential_noise(probs: torch.Tensor, q: torch.Tensor) -> torch.Tensor:
if q.dtype == probs.dtype:
scores = probs.div_(q)
else:
scores = q.reciprocal_()
scores.mul_(probs)
return scores.argmax(dim=-1).view(-1)
def random_sample(
probs: torch.Tensor,
generators: dict[int, torch.Generator],
use_fp64_gumbel: bool = False,
) -> torch.Tensor:
"""Randomly sample from the probabilities.
We use this function instead of torch.multinomial because torch.multinomial
causes CPU-GPU synchronization.
"""
q = torch.empty_like(probs)
q = empty_exponential_noise_like(probs, use_fp64_gumbel)
# NOTE(woosuk): To batch-process the requests without their own seeds,
# which is the common case, we first assume that every request does
# not have its own seed. Then, we overwrite the values for the requests
@@ -425,7 +454,7 @@ def random_sample(
# one by one. Optimize this.
for i, generator in generators.items():
q[i].exponential_(generator=generator)
return probs.div_(q).argmax(dim=-1).view(-1)
return sample_with_exponential_noise(probs, q)
def flashinfer_sample(
+13 -2
View File
@@ -65,6 +65,7 @@ class RejectionSampler(nn.Module):
):
super().__init__()
self.sampler = sampler
self.use_fp64_gumbel = getattr(sampler, "use_fp64_gumbel", False)
logprobs_mode = self.sampler.logprobs_mode
self.is_processed_logprobs_mode = logprobs_mode.startswith("processed")
self.is_logits_logprobs_mode = logprobs_mode.endswith("logits")
@@ -176,6 +177,7 @@ class RejectionSampler(nn.Module):
sampling_metadata,
synthetic_mode=self.synthetic_mode,
synthetic_conditional_rates=self.synthetic_conditional_rates,
use_fp64_gumbel=self.use_fp64_gumbel,
)
logprobs_tensors = None
@@ -406,6 +408,7 @@ def rejection_sample(
sampling_metadata: SamplingMetadata,
synthetic_mode: bool = False,
synthetic_conditional_rates: torch.Tensor | None = None,
use_fp64_gumbel: bool = False,
) -> torch.Tensor:
assert draft_token_ids.ndim == 1
assert draft_probs is None or draft_probs.ndim == 2
@@ -480,6 +483,7 @@ def rejection_sample(
target_probs,
sampling_metadata,
device,
use_fp64_gumbel,
)
# Rejection sampling for random sampling requests.
@@ -669,13 +673,15 @@ def sample_recovered_tokens(
target_probs: torch.Tensor,
sampling_metadata: SamplingMetadata,
device: torch.device,
use_fp64_gumbel: bool = False,
) -> torch.Tensor:
# NOTE(woosuk): Create only one distribution for each request.
batch_size = len(num_draft_tokens)
vocab_size = target_probs.shape[-1]
q_dtype = torch.float64 if use_fp64_gumbel else torch.float32
q = torch.empty(
(batch_size, vocab_size),
dtype=torch.float32,
dtype=q_dtype,
device=device,
)
q.exponential_()
@@ -699,6 +705,7 @@ def sample_recovered_tokens(
vocab_size,
BLOCK_SIZE,
NO_DRAFT_PROBS=draft_probs is None,
USE_FP64_GUMBEL=use_fp64_gumbel,
)
return recovered_token_ids
@@ -861,6 +868,7 @@ def sample_recovered_tokens_kernel(
vocab_size,
BLOCK_SIZE: tl.constexpr,
NO_DRAFT_PROBS: tl.constexpr,
USE_FP64_GUMBEL: tl.constexpr,
):
req_idx = tl.program_id(0)
start_idx = 0 if req_idx == 0 else tl.load(cu_num_draft_tokens_ptr + req_idx - 1)
@@ -877,7 +885,10 @@ def sample_recovered_tokens_kernel(
if NO_DRAFT_PROBS:
draft_token_id = tl.load(draft_token_ids_ptr + token_idx)
max_val = float("-inf")
if USE_FP64_GUMBEL:
max_val = tl.full((), float("-inf"), tl.float64)
else:
max_val = tl.full((), float("-inf"), tl.float32)
recovered_id = 0
for v in range(0, vocab_size, BLOCK_SIZE):
vocab_offset = v + tl.arange(0, BLOCK_SIZE)
+7 -2
View File
@@ -58,11 +58,16 @@ class Sampler(nn.Module):
9. Return the final `SamplerOutput`.
"""
def __init__(self, logprobs_mode: LogprobsMode = "raw_logprobs"):
def __init__(
self,
logprobs_mode: LogprobsMode = "raw_logprobs",
use_fp64_gumbel: bool = False,
):
super().__init__()
self.topk_topp_sampler = TopKTopPSampler(logprobs_mode)
self.topk_topp_sampler = TopKTopPSampler(logprobs_mode, use_fp64_gumbel)
self.pin_memory = is_pin_memory_available()
self.logprobs_mode = logprobs_mode
self.use_fp64_gumbel = use_fp64_gumbel
def forward(
self,
+11 -3
View File
@@ -32,6 +32,10 @@ from vllm.v1.attention.backends.triton_attn import TritonAttentionMetadata
from vllm.v1.cudagraph_dispatcher import CudagraphDispatcher
from vllm.v1.kv_cache_interface import KVCacheConfig, UniformTypeKVCacheSpecs
from vllm.v1.sample.metadata import SamplingMetadata
from vllm.v1.sample.ops.topk_topp_sampler import (
empty_exponential_noise_like,
sample_with_exponential_noise,
)
from vllm.v1.sample.sampler import _SAMPLING_EPS
from vllm.v1.spec_decode.metadata import SpecDecodeMetadata
from vllm.v1.spec_decode.utils import (
@@ -113,6 +117,7 @@ class SpecDecodeBaseProposer:
self.use_local_argmax_reduction: bool = (
self.speculative_config.use_local_argmax_reduction
)
self.use_fp64_gumbel = vllm_config.model_config.use_fp64_gumbel
self.max_batch_size = vllm_config.scheduler_config.max_num_seqs
self.max_num_tokens = vllm_config.scheduler_config.max_num_batched_tokens
@@ -409,7 +414,9 @@ class SpecDecodeBaseProposer:
return logits.argmax(dim=-1), None
if sampling_metadata.all_greedy:
return logits.argmax(dim=-1), None
return compute_probs_and_sample_next_token(logits, sampling_metadata)
return compute_probs_and_sample_next_token(
logits, sampling_metadata, self.use_fp64_gumbel
)
def _sample_draft_tokens(
self,
@@ -1656,6 +1663,7 @@ class SpecDecodeBaseProposer:
def compute_probs_and_sample_next_token(
logits: torch.Tensor,
sampling_metadata: SamplingMetadata,
use_fp64_gumbel: bool = False,
) -> tuple[torch.Tensor, torch.Tensor]:
if sampling_metadata.all_greedy:
# For greedy requests, draft_probs is not used in rejection sampling.
@@ -1682,11 +1690,11 @@ def compute_probs_and_sample_next_token(
# of the generated tokens after rejection sampling.
# TODO(woosuk): Consider seeds.
q = torch.empty_like(probs)
q = empty_exponential_noise_like(probs, use_fp64_gumbel)
q.exponential_()
# NOTE(woosuk): We shouldn't use `probs.div_(q)` because the draft_probs
# will be used later for rejection sampling.
next_token_ids = probs.div(q).argmax(dim=-1).view(-1)
next_token_ids = sample_with_exponential_noise(probs.clone(), q)
if not sampling_metadata.all_random:
greedy_token_ids = probs.argmax(dim=-1)
next_token_ids = torch.where(is_greedy, greedy_token_ids, next_token_ids)
+4 -1
View File
@@ -504,7 +504,10 @@ class GPUModelRunner(
self.use_async_scheduling = self.scheduler_config.async_scheduling
# Sampler
self.sampler = Sampler(logprobs_mode=self.model_config.logprobs_mode)
self.sampler = Sampler(
logprobs_mode=self.model_config.logprobs_mode,
use_fp64_gumbel=self.model_config.use_fp64_gumbel,
)
self.eplb_state: EplbState | None = None
self._moe_model: MixtureOfExperts | None = None