Skip to content

Commit

Permalink
Fixing empty prompts for text-generation when BOS exists. (#13859)
Browse files Browse the repository at this point in the history
* Fixing empty prompts for text-generation when BOS exists.

* Fixing odd case with Pegasus.

* Fixing Bert is Assertion Error.
  • Loading branch information
Narsil authored and LysandreJik committed Oct 6, 2021
1 parent c2901b0 commit a8186b0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/transformers/pipelines/text_generation.py
Expand Up @@ -158,6 +158,9 @@ def preprocess(self, prompt_text, prefix=""):

def _forward(self, model_inputs, **generate_kwargs):
input_ids = model_inputs["input_ids"]
# Allow empty prompts
if input_ids.shape[1] == 0:
input_ids = None
prompt_text = model_inputs.pop("prompt_text")
generated_sequence = self.model.generate(input_ids=input_ids, **generate_kwargs) # BS x SL
return {"generated_sequence": generated_sequence, "input_ids": input_ids, "prompt_text": prompt_text}
Expand Down
11 changes: 11 additions & 0 deletions tests/test_pipelines_text_generation.py
Expand Up @@ -106,3 +106,14 @@ def run_pipeline_test(self, model, tokenizer, feature_extractor):
outputs = text_generator("This is a test", return_full_text=True)
self.assertEqual(outputs, [{"generated_text": ANY(str)}])
self.assertTrue(outputs[0]["generated_text"].startswith("This is a test"))

# Empty prompt is slighly special
# it requires BOS token to exist.
# Special case for Pegasus which will always append EOS so will
# work even without BOS.
if text_generator.tokenizer.bos_token_id is not None or "Pegasus" in tokenizer.__class__.__name__:
outputs = text_generator("")
self.assertEqual(outputs, [{"generated_text": ANY(str)}])
else:
with self.assertRaises((ValueError, AssertionError)):
outputs = text_generator("")

0 comments on commit a8186b0

Please sign in to comment.