New to hugging face. Trying to use gpt2 model to generate some text, why the following code yields infinity result?
chatgpt says it might due to EOS not correct. I tried a few but does not help.
import torch
from transformers import GPT2LMHeadModel, GPT2Tokenizermodel = GPT2LMHeadModel.from_pretrained(“gpt2”, torchscript=True).eval()
tokenizer
tokenizer = GPT2Tokenizer.from_pretrained(“gpt2”)
in_text = “Lionel Messi is something”
in_tokens = torch.tensor(tokenizer.encode(in_text))inference
#token_eos = torch.tensor([198]) # line break symbol
token_eos = torch.tensor([50256])
out_token = None
i = 0
with torch.no_grad():
while out_token != token_eos:
logits, _ = model(in_tokens)
out_token = torch.argmax(logits[-1, :], dim=0, keepdim=True)
in_tokens = torch.cat((in_tokens, out_token), 0)
text = tokenizer.decode(in_tokens)
print(f’step {i} input: {text}', flush=True)
i += 1out_text = tokenizer.decode(in_tokens)
print(f’ Input: {in_text}‘)
print(f’Output: {out_text}’)