Skip to content

Commit

Permalink
remove floats == 0 comparison (vllm-project#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuXiaoxuanPKU committed Jun 28, 2023
1 parent 4338cc4 commit 425040d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
11 changes: 6 additions & 5 deletions vllm/model_executor/layers/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from vllm.sampling_params import SamplingParams
from vllm.sequence import SequenceOutputs

_SAMPLING_EPS = 1e-5

class Sampler(nn.Module):
"""Samples the next tokens from the model's outputs.
Expand Down Expand Up @@ -74,7 +75,7 @@ def forward(
# Apply top-p and top-k truncation.
top_ps, top_ks = _get_top_p_top_k(input_metadata, self.vocab_size)
assert len(top_ps) == len(top_ks) == probs.shape[0]
if any(p < 1.0 for p in top_ps) or any(k != self.vocab_size for k in top_ks):
if any(p < 1.0 - _SAMPLING_EPS for p in top_ps) or any(k != self.vocab_size for k in top_ks):
probs = _apply_top_p_top_k(probs, top_ps, top_ks)

# Sample the next tokens.
Expand Down Expand Up @@ -152,7 +153,7 @@ def _apply_penalties(
continue
p = presence_penalties[i]
f = frequency_penalties[i]
if p == 0.0 and f == 0.0:
if p < _SAMPLING_EPS and f < _SAMPLING_EPS:
continue
indices.append(i)

Expand Down Expand Up @@ -190,7 +191,7 @@ def _get_temperatures(
for i, seq_group in enumerate(input_metadata.seq_groups):
seq_ids, sampling_params = seq_group
temperature = sampling_params.temperature
if temperature == 0.0:
if temperature < _SAMPLING_EPS:
# NOTE: Zero temperature means deterministic sampling
# (i.e., greedy sampling or beam search).
# Set the temperature to 1 to avoid division by zero.
Expand Down Expand Up @@ -286,7 +287,7 @@ def _sample_from_prompt(
beam_width = sampling_params.best_of
_, next_token_ids = torch.topk(prob, beam_width)
next_token_ids = next_token_ids.tolist()
elif sampling_params.temperature == 0.0:
elif sampling_params.temperature < _SAMPLING_EPS:
# Greedy sampling.
assert sampling_params.best_of == 1
next_token_id = torch.argmax(prob)
Expand Down Expand Up @@ -343,7 +344,7 @@ def _sample_from_generation_tokens(

parent_seq_ids = [beam_outputs[seq_id][0] for seq_id in seq_ids]
next_token_ids = [beam_outputs[seq_id][1] for seq_id in seq_ids]
elif sampling_params.temperature == 0.0:
elif sampling_params.temperature < _SAMPLING_EPS:
# Greedy sampling.
assert len(seq_ids) == 1
next_token_id = torch.argmax(probs, dim=-1)
Expand Down
9 changes: 5 additions & 4 deletions vllm/sampling_params.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Sampling parameters for text generation."""
from typing import List, Optional, Union

_SAMPLING_EPS = 1e-5

class SamplingParams:
"""Sampling parameters for text generation.
Expand Down Expand Up @@ -71,7 +72,7 @@ def __init__(
self._verify_args()
if self.use_beam_search:
self._verity_beam_search()
elif self.temperature == 0.0:
elif self.temperature < _SAMPLING_EPS:
# Zero temperature means greedy sampling.
self._verify_greedy_sampling()

Expand Down Expand Up @@ -106,9 +107,9 @@ def _verity_beam_search(self) -> None:
if self.best_of == 1:
raise ValueError("best_of must be greater than 1 when using beam "
f"search. Got {self.best_of}.")
if self.temperature > 0.0:
if self.temperature > _SAMPLING_EPS:
raise ValueError("temperature must be 0 when using beam search.")
if self.top_p < 1.0:
if self.top_p < 1.0 - _SAMPLING_EPS:
raise ValueError("top_p must be 1 when using beam search.")
if self.top_k != -1:
raise ValueError("top_k must be -1 when using beam search.")
Expand All @@ -117,7 +118,7 @@ def _verify_greedy_sampling(self) -> None:
if self.best_of > 1:
raise ValueError("best_of must be 1 when using greedy sampling."
f"Got {self.best_of}.")
if self.top_p < 1.0:
if self.top_p < 1.0 - _SAMPLING_EPS:
raise ValueError("top_p must be 1 when using greedy sampling.")
if self.top_k != -1:
raise ValueError("top_k must be -1 when using greedy sampling.")
Expand Down

0 comments on commit 425040d

Please sign in to comment.