Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions python/sglang/srt/managers/controller/infer_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,18 +668,16 @@ def sample(self, logits: torch.Tensor):

max_top_k_round, batch_size = 32, probs.shape[0]
uniform_samples = torch.rand((max_top_k_round, batch_size), device=probs.device)
batch_next_token_ids, _ = top_k_top_p_sampling_from_probs(
batch_next_token_ids, success = top_k_top_p_sampling_from_probs(
probs, uniform_samples, self.top_ks, self.top_ps
)

# FIXME: this is a temporary fix for the illegal token ids
illegal_mask = torch.logical_or(
batch_next_token_ids < 0, batch_next_token_ids >= probs.shape[-1]
)
if torch.any(illegal_mask):
warnings.warn("Illegal sampled token ids")
probs = probs.masked_fill(torch.isnan(probs), 0.0)
batch_next_token_ids = torch.argmax(probs, dim=-1)
if torch.any(~success):
warnings.warn("Sampling failed, fallback to top_k=1 strategy")
argmax_ids = torch.argmax(probs, dim=-1)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to make sure probs is not NaN before calling argmax?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we should make sure it.

batch_next_token_ids = torch.where(
success, batch_next_token_ids, argmax_ids
)

if has_regex:
batch_next_token_ids_cpu = batch_next_token_ids.cpu().numpy()
Expand Down