Less flaky test_assisted_decoding_matches_greedy_search (#23451)

* fix

* fix

---------

Co-authored-by: ydshieh <ydshieh@users.noreply.github.com>
This commit is contained in:
Yih-Dar
2023-05-18 17:28:22 +02:00
committed by GitHub
parent 21f7e81b6b
commit 2406dbdcfa

View File

@@ -1477,46 +1477,57 @@ class GenerationTesterMixin:
): ):
return return
# enable cache # This for loop is a naive and temporary effort to make the test less flaky.
config, input_ids, attention_mask, max_length = self._get_input_ids_and_config(batch_size=1) failed = 0
for i in range(10):
# enable cache
config, input_ids, attention_mask, max_length = self._get_input_ids_and_config(batch_size=1)
# NOTE: assisted generation only works with cache on at the moment. # NOTE: assisted generation only works with cache on at the moment.
if not hasattr(config, "use_cache"): if not hasattr(config, "use_cache"):
return return
config.use_cache = True config.use_cache = True
config.is_decoder = True config.is_decoder = True
model = model_class(config).to(torch_device).eval() model = model_class(config).to(torch_device).eval()
output_greedy = model.generate( output_greedy = model.generate(
input_ids, input_ids,
attention_mask=attention_mask, attention_mask=attention_mask,
max_length=max_length, max_length=max_length,
num_beams=1, num_beams=1,
do_sample=False, do_sample=False,
output_scores=True, output_scores=True,
output_hidden_states=True, output_hidden_states=True,
output_attentions=True, output_attentions=True,
return_dict_in_generate=True, return_dict_in_generate=True,
) )
# Note: with assisted generate, if the same model is used as assistant, then all assistant tokens will # Note: with assisted generate, if the same model is used as assistant, then all assistant tokens will
# be correct # be correct
output_assisted = model.generate( output_assisted = model.generate(
input_ids, input_ids,
attention_mask=attention_mask, attention_mask=attention_mask,
max_length=max_length, max_length=max_length,
num_beams=1, num_beams=1,
do_sample=False, do_sample=False,
assistant_model=model, assistant_model=model,
output_scores=True, output_scores=True,
output_hidden_states=True, output_hidden_states=True,
output_attentions=True, output_attentions=True,
return_dict_in_generate=True, return_dict_in_generate=True,
) )
self.assertListEqual(output_greedy.sequences.tolist(), output_assisted.sequences.tolist()) try:
self.assertListEqual(output_greedy.sequences.tolist(), output_assisted.sequences.tolist())
for output in (output_greedy, output_assisted): for output in (output_greedy, output_assisted):
self._check_outputs(output, input_ids, model.config, use_cache=True) self._check_outputs(output, input_ids, model.config, use_cache=True)
except AssertionError:
failed += 1
if failed > 1:
self.assertListEqual(output_greedy.sequences.tolist(), output_assisted.sequences.tolist())
for output in (output_greedy, output_assisted):
self._check_outputs(output, input_ids, model.config, use_cache=True)
def test_assisted_decoding_sample(self): def test_assisted_decoding_sample(self):
# Seeded assisted decoding will not match sample for the same seed, as the forward pass does not return the # Seeded assisted decoding will not match sample for the same seed, as the forward pass does not return the