Repository navigation
Validate top_k and top_p before the greedy branch in sample_logits - #2012
Merged
jlarson4 merged 1 commit intoNov 10, 2026
Conversation
Both filters were only checked inside the sampling branch, so a call with temperature=0.0 accepted top_k=0, a negative top_k, or a top_p outside (0, 1] without complaint. freq_penalty, temperature and repetition_penalty are all checked up front and raise at either temperature. The checks keep raising AssertionError, so existing callers and tests see the same exception type; they just now fire for greedy sampling too.
Collaborator
|
Looks good @abhishekcpp007, merging |
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.
Description
sample_logitschecksfreq_penalty,temperatureandrepetition_penaltybefore it branches,so they raise at any temperature.
top_kandtop_pwere checked inside the sampling branchinstead, which greedy sampling never enters. A call with
temperature=0.0therefore accepted anyfilter value without complaint.
Measured on
devbefore this change, withlogits = [[1.0, 3.0, 2.0, 0.5]]:temperature=1.0, top_k=0AssertionError: top_k has to be greater than 0temperature=1.0, top_k=-5AssertionError: top_k has to be greater than 0temperature=1.0, top_p=0.0AssertionError: top_p has to be in (0, 1]temperature=1.0, top_p=1.5AssertionError: top_p has to be in (0, 1]temperature=0.0, top_k=0[1]temperature=0.0, top_k=-5[1]temperature=0.0, top_p=0.0[1]temperature=0.0, top_p=1.5[1]freq_penalty=-1raises at both temperatures, which is the behaviour the filters should match.Someone sweeping a sampling config over temperatures gets a silent pass on the greedy rows and a
failure everywhere else, from the same arguments.
What changed
The two checks move above the
temperature == 0.0branch, next to the other parameter checks.They still raise
AssertionErrorwith the same messages, so callers and existing tests see thesame exception type; the checks simply also fire for greedy sampling now. Clamping
top_kto thevocabulary size stays where it was, since it needs the logits' shape.
Type of change
Checklist:
test_top_k_rejects_non_positiveandtest_top_p_rejects_out_of_rangenow run overtemperatureof1.0and0.0, matching howtest_non_positive_penalty_raisesandtest_negative_freq_penalty_raisesalready cover both. They also assert the message rather thanjust the type. Both fail on
devfor the0.0rows.