fix num_assistant_tokens with heuristic schedule (#28759)
* fix heuristic num_assistant_tokens_schedule * Update src/transformers/generation/configuration_utils.py Co-authored-by: Joao Gante <joaofranciscocardosogante@gmail.com> * Update src/transformers/generation/candidate_generator.py Co-authored-by: Joao Gante <joaofranciscocardosogante@gmail.com> * Update utils.py check that candidate_generator.assistant_model exists since some some speculations (like ngram and PLD) don't have assistant_model attribute * Update src/transformers/generation/candidate_generator.py Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com> * Update tests/generation/test_utils.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * make fixup * merge conflict * fix docstring * make fixup --------- Co-authored-by: Joao Gante <joaofranciscocardosogante@gmail.com> Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com> Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com>
This commit is contained in:
@@ -3490,3 +3490,49 @@ class GenerationIntegrationTests(unittest.TestCase, GenerationIntegrationTestsMi
|
||||
encoder_outputs=encoder_outputs,
|
||||
)
|
||||
self.assertListEqual(outputs_assisted.tolist(), outputs_foo.tolist())
|
||||
|
||||
def test_assisted_decoding_num_assistant_tokens_heuristic_schedule(self):
|
||||
# This test ensures that the assisted generation num_assistant_tokens 'heuristic' schedule works properly.
|
||||
|
||||
prompt = "Alice and Bob"
|
||||
checkpoint = "EleutherAI/pythia-160m-deduped"
|
||||
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
||||
inputs = tokenizer(prompt, return_tensors="pt")
|
||||
|
||||
model = AutoModelForCausalLM.from_pretrained(checkpoint)
|
||||
|
||||
assistant_model = model
|
||||
assistant_model.generation_config.num_assistant_tokens = 5
|
||||
assistant_model.generation_config.num_assistant_tokens_schedule = "heuristic"
|
||||
generation_kwargs = {
|
||||
"eos_token_id": -1,
|
||||
"max_new_tokens": 5,
|
||||
"do_sample": False,
|
||||
"assistant_model": assistant_model,
|
||||
}
|
||||
model.generate(**inputs, **generation_kwargs)
|
||||
# update_candidate_strategy is called only once and therefore, assistant_model.generation_config.num_assistant_tokens should be either 4 or 7
|
||||
self.assertTrue(assistant_model.generation_config.num_assistant_tokens in (4, 7))
|
||||
|
||||
def test_assisted_decoding_num_assistant_tokens_heuristic_transient_schedule(self):
|
||||
# This test ensures that the assisted generation num_assistant_tokens 'heuristic' schedule works properly.
|
||||
|
||||
prompt = "Alice and Bob"
|
||||
checkpoint = "EleutherAI/pythia-160m-deduped"
|
||||
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
||||
inputs = tokenizer(prompt, return_tensors="pt")
|
||||
|
||||
model = AutoModelForCausalLM.from_pretrained(checkpoint)
|
||||
|
||||
assistant_model = model
|
||||
assistant_model.generation_config.num_assistant_tokens = 5
|
||||
assistant_model.generation_config.num_assistant_tokens_schedule = "heuristic_transient"
|
||||
generation_kwargs = {
|
||||
"eos_token_id": -1,
|
||||
"max_new_tokens": 5,
|
||||
"do_sample": False,
|
||||
"assistant_model": assistant_model,
|
||||
}
|
||||
model.generate(**inputs, **generation_kwargs)
|
||||
# update_candidate_strategy is called once but assistant_model.generation_config.num_assistant_tokens should stay 5
|
||||
self.assertEqual(assistant_model.generation_config.num_assistant_tokens, 5)
|
||||
|
||||
Reference in New Issue
Block a user