Raise a clear error for empty token lists in bad_words_ids / sequence_bias#46994
Open
Sunt-ing wants to merge 1 commit into
Open
Raise a clear error for empty token lists in bad_words_ids / sequence_bias#46994Sunt-ing wants to merge 1 commit into
Sunt-ing wants to merge 1 commit into
Conversation
…_bias
NoBadWordsLogitsProcessor and the list form of SequenceBiasLogitsProcessor accept
an empty inner token list and only fail later, during generation, with an opaque
IndexError instead of a clear validation error at construction time. The empty
list is reachable from the documented usage, since some words tokenize to an empty
list (e.g. tokenizer("", add_special_tokens=False).input_ids == []).
The dict form of sequence_bias already rejects empty sequences with a clear
ValueError; this adds the same check to the two paths that missed it (the vacuous
all/any over an empty list let it slip through), so an empty token list now raises
a clear ValueError at construction.
Signed-off-by: Ting Sun <suntcrick@gmail.com>
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.
What does this PR do?
NoBadWordsLogitsProcessorand the list form ofSequenceBiasLogitsProcessorcurrently accept an empty token sequence and then fail later withIndexError: tuple index out of rangewhen generation tries to read the last token of that sequence. This is reachable through the documentedbad_words_idspattern because tokenizing an empty string withadd_special_tokens=Falsereturns[].This PR rejects empty token sequences at construction time for both entry points. The dict form of
sequence_biasalready rejects empty sequences; this makesbad_words_idsand the list form ofsequence_biasfollow the same contract, while non-empty entries keep their existing behavior.Reproduction and before/after output (CPU)
Run from the repo root:
Before:
After:
A focused regression test is added in
tests/generation/test_logits_process.pyforbad_words_ids=[[]], mixed empty and non-emptybad_words_ids, andsequence_bias=[[[], 1.0]]. It fails on main because noValueErroris raised at construction time, and passes with this change.Code Agent Policy
Before submitting
Who can review?
@Cyrilvallez