Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add text generation #253

Merged
merged 23 commits into from
Feb 2, 2023
Merged

Add text generation #253

merged 23 commits into from
Feb 2, 2023

Conversation

jimypbr
Copy link
Contributor

@jimypbr jimypbr commented Jan 25, 2023

Implement text generation for decoder and encoder/decoder models: GPT2, BART, T5

Adds:

  • Greedy search
  • Sampling
  • Beam search
  • Beam sample
  • Pipelines for text2text, translation, and summarization

Example of using generate with GPT2:

from optimum.graphcore.modeling_utils import to_pipelined
from optimum.graphcore import IPUConfig
from transformers import GPT2TokenizerFast, GPT2LMHeadModel

tokenizer = GPT2TokenizerFast.from_pretrained("gpt2-medium")
model = GPT2LMHeadModel.from_pretrained("gpt2-medium", pad_token_id=tokenizer.eos_token_id)
input_ids = tokenizer.encode('I enjoy walking with my cute dog', return_tensors="pt")

ipu_config = IPUConfig(layers_per_ipu=[8, 16])
pipelined_model = to_pipelined(model, ipu_config)
pipelined_model = pipelined_model.half().parallelize()

sample_output = pipelined_model.generate(
    input_ids, 
    do_sample=True, 
    max_length=200, 
    top_k=0,
    top_p=0.92,
    use_cache=False,
)

print("Output:\n" + 100 * '-')
print(tokenizer.decode(sample_output[0], skip_special_tokens=True))

Pipelines are also supported:

from optimum.graphcore import pipeline

ipu_config = IPUConfig(layers_per_ipu=[16], ipus_per_replica=1)
text2text_generator = opipeline("text2text-generation", model="google/flan-t5-small", input_max_length=45, ipu_config=ipu_config.to_dict())

text2text_generator("question: What is 42 ? context: 42 is the answer to life, the universe and everything")
text2text_generator("Translate to German:  My name is Arthur.")

@jimypbr jimypbr force-pushed the new-textgeneration branch from 09d7262 to f988b08 Compare January 25, 2023 17:29
@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Jan 26, 2023

The documentation is not available anymore as the PR was closed or merged.

@jimypbr jimypbr force-pushed the new-textgeneration branch from 85478bf to c133674 Compare February 1, 2023 14:26
Copy link
Collaborator

@katalinic-gc katalinic-gc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jimypbr jimypbr merged commit da1377c into main Feb 2, 2023
@jimypbr jimypbr deleted the new-textgeneration branch April 3, 2023 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants