Skip to content

[Bugfix][Sampler] Keep tied top-k tokens in the PyTorch sampling fallback - #33539

Closed
liuxiang-sudo wants to merge 1 commit into
sgl-project:mainfrom
liuxiang-sudo:fix/topk-tie-pytorch-fallback
Closed

liuxiang-sudo wants to merge 1 commit into
sgl-project:mainfrom
liuxiang-sudo:fix/topk-tie-pytorch-fallback

Conversation

@liuxiang-sudo

@liuxiang-sudo liuxiang-sudo commented Aug 4, 2026 •

Copy link
Copy Markdown

Motivation

top_k_top_p_min_p_sampling_from_probs_torch (the --sampling-backend pytorch fallback) truncated the top-k set by rank, keeping exactly top_k entries. The CUDA kernels keep every token tied at the k-th largest probability:

  • sgl_kernel.top_k_renorm_prob (used when need_min_p_sampling is on)
  • flashinfer.top_k_top_p_sampling_from_probs (default path)

So for the same request with tied logits at the k-th boundary, the PyTorch fallback could sample from a strictly smaller candidate set than the kernel backends.

Modifications

Filter by probability instead of rank: zero out every token strictly below the k-th largest probability (probs_sort < k-th largest -> 0), matching the kernel semantics. top_k >= vocab / top_k = TOP_K_ALL still keep the whole vocabulary (index clamped to vocab - 1).

Accuracy Tests

Verified on tied distributions (2-way and 3-way ties at the k-th boundary): the fixed fallback's retained set matches sgl_kernel.top_k_renorm_prob exactly. Regression unit test added:

test_pytorch_sampling_fallback.py: all 6 tests pass (CPU)
test/registered/unit/sampling/: 191 tests pass

Speed Tests and Profiling

No impact on the default (flashinfer) backend. The fallback change is one gather + one comparison instead of one arange comparison.

Checklist

  • Format your code according to the pre-commit (ruff F401/F821/UP037, isort, black 26.1.0, codespell all pass)
  • Add unit tests (new test/registered/unit/sampling/test_pytorch_sampling_fallback.py, registered for CPU CI)
  • Update documentation
  • Provide accuracy and speed benchmark results
  • Follow the SGLang code style

CI States

Latest PR Test (Base): ❌ Run #30903068680
Latest PR Test (Extra): ❌ Run #30903068516

…back

top_k_top_p_min_p_sampling_from_probs_torch truncated the top-k set by
rank, keeping exactly top_k entries. The CUDA kernels (sgl_kernel
top_k_renorm_prob, flashinfer top_k_top_p_sampling_from_probs) instead
keep every token tied at the k-th largest probability, so the PyTorch
fallback could sample from a strictly smaller candidate set than the
kernel backends for the same request.

Filter by probability instead of rank: zero out every token strictly
below the k-th largest probability, matching the kernel semantics. Add
a CPU unit test that captures the filtered distribution and asserts the
tied tokens are retained.

Signed-off-by: liuxiang-sudo <[email protected]>
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@HaiShaw

HaiShaw commented Oct 10, 2026

Copy link
Copy Markdown
Collaborator

@liuxiang-sudo any insight on the likelihood of any tied top-k probability?

@HaiShaw

HaiShaw commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

@liuxiang-sudo can you ping @jiaqiang-dot-liu and rebase this from #43346 merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants