Allow top_k greater than vocab_size - #2486
Open
Hrishith Thadicherla (hthadicherla) wants to merge 1 commit into
Open
Allow top_k greater than vocab_size#2486Hrishith Thadicherla (hthadicherla) wants to merge 1 commit into
Hrishith Thadicherla (hthadicherla) wants to merge 1 commit into
Conversation
Signed-off-by: Hrishith Thadicherla <hthadicherla@nvidia.com>
Hrishith Thadicherla (hthadicherla)
requested a review
from a team
as a code owner
August 28, 2026 08:27
Copilot started reviewing on behalf of
Hrishith Thadicherla (hthadicherla)
August 28, 2026 08:27
View session
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adjusts generator sampling initialization to allow top_k values greater than a model’s vocab_size, aligning generator creation behavior with existing sampling implementations that already clamp k to vocab_size at use sites.
Changes:
- Removed the
top_k > vocab_sizeruntime error inGenerator::InitializeSamplingMethod. - Preserved existing validation for invalid
top_k(negative values) and existing sampling-method selection logic.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
Allow top_k greater than vocab_size
Generator::InitializeSamplingMethodrejected anytop_kabove the model'svocab_size.This caused failure in these two test cases whose parameters violate this condition by default.
That is not an error condition: a
top_klarger than the vocabularysimply means "consider all tokens", and it is what a common default (e.g. 50) does
on a small-vocab model, which failed generator creation outright.
Current PR removes the throw. All three sampling paths already clamp
ktovocab_sizebefore use, so nothing can read out of bounds:
ComputeSampledCategorical-src/sampling_distribution.h:245cuda::GetSample-src/cuda/cuda_sampling.cu:369MtpGenerator::TopKScoresRows-src/mtp_generator.cpp:419The
top_k < 0check is unchanged.