Fix single letter stop strings (#31448)
* Fix single letter stop strings * Change the 0 to a 1 to avoid potential empty vector headaches later * Restructure for clarity * Update tests/generation/test_stopping_criteria.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Add the unsqueeze --------- Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com>
This commit is contained in:
@@ -208,6 +208,24 @@ class StoppingCriteriaTestCase(unittest.TestCase):
|
||||
token_lengths = embedding_vec[:, 2].tolist()
|
||||
self.assertEqual(token_lengths, [len(token) for token in token_list])
|
||||
|
||||
def test_single_letter_stop_string(self):
|
||||
true_strings = ["a", "baa", "abc"] # "abc" is a single token
|
||||
false_strings = ["abbbbbbb", "b"] # "abbbbbbb" is split into multiple tokens
|
||||
stop_strings = ["a"]
|
||||
tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
|
||||
tokenizer.pad_token_id = tokenizer.eos_token_id
|
||||
tokenizer.padding_side = "left"
|
||||
|
||||
true_input_ids = tokenizer(true_strings, return_tensors="pt", padding="longest", add_special_tokens=False)
|
||||
false_input_ids = tokenizer(false_strings, return_tensors="pt", padding="longest", add_special_tokens=False)
|
||||
|
||||
scores = None
|
||||
criteria = StopStringCriteria(tokenizer=tokenizer, stop_strings=stop_strings)
|
||||
for input_ids in true_input_ids["input_ids"]:
|
||||
self.assertTrue(criteria(input_ids.unsqueeze(0), scores))
|
||||
for input_ids in false_input_ids["input_ids"]:
|
||||
self.assertFalse(criteria(input_ids.unsqueeze(0), scores))
|
||||
|
||||
def test_criterias_per_row(self):
|
||||
text = "They completed the challenging puzzle, revealing the hidden image at the end"
|
||||
stop_strings = ["end"]
|
||||
|
||||
Reference in New Issue
Block a user