Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/transformers/generation/logits_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,7 @@ def _validate_arguments(self):
def all_token_bias_pairs_are_valid(sequence):
return (
isinstance(sequence[0], list)
and len(sequence[0]) > 0
and all(isinstance(token_id, (int, np.integer)) and token_id > 0 for token_id in sequence[0])
and isinstance(sequence[1], float)
)
Expand Down Expand Up @@ -1477,6 +1478,8 @@ def _validate_arguments(self):
raise ValueError(f"`bad_words_ids` has to be a non-empty list, but is {bad_words_ids}.")
if any(not isinstance(bad_word_ids, list) for bad_word_ids in bad_words_ids):
raise ValueError(f"`bad_words_ids` has to be a list of lists, but is {bad_words_ids}.")
if any(len(bad_word_ids) == 0 for bad_word_ids in bad_words_ids):
raise ValueError(f"`bad_words_ids` has to be a list of non-empty lists, but is {bad_words_ids}.")
if any(
any((not isinstance(token_id, (int, np.integer)) or token_id < 0) for token_id in bad_word_ids)
for bad_word_ids in bad_words_ids
Expand Down
11 changes: 11 additions & 0 deletions tests/generation/test_logits_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,17 @@ def test_no_bad_words_dist_processor(self):
filtered_scores = no_bad_words_dist_proc(input_ids, scores)
torch.testing.assert_close(scores, filtered_scores, rtol=1e-3, atol=1e-3)

def test_bad_words_and_sequence_bias_reject_empty_token_list(self):
# An empty token list (e.g. from `tokenizer("", add_special_tokens=False).input_ids`) must be rejected at
# construction with a clear `ValueError`, rather than crashing later with an `IndexError` during generation.
eos_token_id = 4
with self.assertRaises(ValueError):
NoBadWordsLogitsProcessor(bad_words_ids=[[]], eos_token_id=eos_token_id)
with self.assertRaises(ValueError):
NoBadWordsLogitsProcessor(bad_words_ids=[[], [3]], eos_token_id=eos_token_id)
with self.assertRaises(ValueError):
SequenceBiasLogitsProcessor(sequence_bias=[[[], 1.0]])

def test_bias_dist_processor(self):
vocab_size = 5
batch_size = 2
Expand Down