Repository navigation
[Bugfix][Sampler] Keep tied top-k tokens in the PyTorch sampling fallback - #33539
Closed
liuxiang-sudo wants to merge 1 commit into
Closed
liuxiang-sudo wants to merge 1 commit into
liuxiang-sudo wants to merge 1 commit into
Conversation
…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]>
liuxiang-sudo
requested review from
BBuf,
Edwardf0t1,
Fridge003,
HaiShaw,
Ying1123,
ch-wan,
ispobock and
merrymercy
as code owners
August 4, 2026 11:01
Contributor
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
4 of 5 tasks
Collaborator
|
@liuxiang-sudo any insight on the likelihood of any tied top-k probability? |
Collaborator
|
@liuxiang-sudo can you ping @jiaqiang-dot-liu and rebase this from #43346 merged? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
top_k_top_p_min_p_sampling_from_probs_torch(the--sampling-backend pytorchfallback) truncated the top-k set by rank, keeping exactlytop_kentries. The CUDA kernels keep every token tied at the k-th largest probability:sgl_kernel.top_k_renorm_prob(used whenneed_min_p_samplingis 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_ALLstill keep the whole vocabulary (index clamped tovocab - 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_probexactly. Regression unit test added:Speed Tests and Profiling
No impact on the default (flashinfer) backend. The fallback change is one
gather+ one comparison instead of onearangecomparison.Checklist
test/registered/unit/sampling/test_pytorch_sampling_fallback.py, registered for CPU CI)CI States
Latest PR Test (Base): ❌ Run #30903068680
Latest PR Test (Extra): ❌ Run #30903068516