Skip to content

Allow top_k greater than vocab_size - #2486

Open
Hrishith Thadicherla (hthadicherla) wants to merge 1 commit into
microsoft:mainfrom
hthadicherla:hthadicherla/allow-top-k-above-vocab-size
Open

Allow top_k greater than vocab_size#2486
Hrishith Thadicherla (hthadicherla) wants to merge 1 commit into
microsoft:mainfrom
hthadicherla:hthadicherla/allow-top-k-above-vocab-size

Conversation

@hthadicherla

Copy link
Copy Markdown
Contributor

Allow top_k greater than vocab_size

Generator::InitializeSamplingMethod rejected any top_k above the model's
vocab_size.
This caused failure in these two test cases whose parameters violate this condition by default.

SamplingTests.BatchedSamplingTopPNvTensorRtRtx
SamplingTests.RandomizedSamplingTopPNvTensorRtRtx
C++ exception with description "top_k (50) must be less than or equal to vocab_size (5)"   // BatchedSamplingTopP
C++ exception with description "top_k (50) must be less than or equal to vocab_size (21)"  // RandomizedSamplingTopP

That is not an error condition: a top_k larger than the vocabulary
simply 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 k to vocab_size
before use, so nothing can read out of bounds:

  • CPU: ComputeSampledCategorical - src/sampling_distribution.h:245
const int m = std::min(top_k, vocab_size);
  • CUDA: cuda::GetSample - src/cuda/cuda_sampling.cu:369
if (k <= 0 || k > vocab_size) { k = vocab_size; }
  • MTP device top-k: MtpGenerator::TopKScoresRows - src/mtp_generator.cpp:419
topk_k_ = std::min(top_k_, vocab_size_);

The top_k < 0 check is unchanged.

Signed-off-by: Hrishith Thadicherla <hthadicherla@nvidia.com>
Copilot AI lite review requested due to automatic review settings August 28, 2026 08:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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_size runtime error in Generator::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.

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