Skip to content

Validate top_k and top_p before the greedy branch in sample_logits - #2012

Merged
jlarson4 merged 1 commit into
TransformerLensOrg:devfrom
abhishekcpp007:fix/sample-logits-greedy-filter-validation
Nov 10, 2026
Merged

jlarson4 merged 1 commit into
TransformerLensOrg:devfrom
abhishekcpp007:fix/sample-logits-greedy-filter-validation

Conversation

@abhishekcpp007

Copy link
Copy Markdown
Contributor

Description

sample_logits checks freq_penalty, temperature and repetition_penalty before it branches,
so they raise at any temperature. top_k and top_p were checked inside the sampling branch
instead, which greedy sampling never enters. A call with temperature=0.0 therefore accepted any
filter value without complaint.

Measured on dev before this change, with logits = [[1.0, 3.0, 2.0, 0.5]]:

call before
temperature=1.0, top_k=0 AssertionError: top_k has to be greater than 0
temperature=1.0, top_k=-5 AssertionError: top_k has to be greater than 0
temperature=1.0, top_p=0.0 AssertionError: top_p has to be in (0, 1]
temperature=1.0, top_p=1.5 AssertionError: top_p has to be in (0, 1]
temperature=0.0, top_k=0 accepted, returns [1]
temperature=0.0, top_k=-5 accepted, returns [1]
temperature=0.0, top_p=0.0 accepted, returns [1]
temperature=0.0, top_p=1.5 accepted, returns [1]

freq_penalty=-1 raises 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.0 branch, next to the other parameter checks.
They still raise AssertionError with the same messages, so callers and existing tests see the
same exception type; the checks simply also fire for greedy sampling now. Clamping top_k to the
vocabulary size stays where it was, since it needs the logits' shape.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have not rewritten tests relating to key interfaces which would affect backward compatibility

test_top_k_rejects_non_positive and test_top_p_rejects_out_of_range now run over
temperature of 1.0 and 0.0, matching how test_non_positive_penalty_raises and
test_negative_freq_penalty_raises already cover both. They also assert the message rather than
just the type. Both fail on dev for the 0.0 rows.

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.
@jlarson4

Copy link
Copy Markdown
Collaborator

Looks good @abhishekcpp007, merging

@jlarson4
jlarson4 merged commit 14dac2a into TransformerLensOrg:dev Nov 10, 2026
31 checks passed
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