Views
No views yet
1import os
2import torch
3
4from transformers import GPT2Tokenizer, GPT2LMHeadModel
5
6tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
7model = GPT2LMHeadModel.from_pretrained("robowaifudev/megatron-gpt2-345m")
8
9if torch.cuda.is_available():
10 device = torch.device("cuda")
11 model.half()
12else:
13 device = torch.device("cpu")
14model.to(device)
15model.eval()
16
17# Generate
18prompt = (
19"It was a bright cold day in April, and the clocks were striking thirteen. Winston Smith,"
20)
21input_ids = tokenizer.encode(prompt, return_tensors="pt").to(device)
22output = model.generate(
23 input_ids=input_ids,
24 max_length=len(input_ids) + 128,
25 do_sample=True,
26 top_k=64,
27 top_p=0.9,
28 temperature=0.8,
29 num_return_sequences=2,
30 repetition_penalty=1.025
31)
32
33# Output the text
34print("Prompt:", prompt)
35print("*" * 3)
36for i, sentence in enumerate(output):
37 text = tokenizer.decode(sentence, clean_up_tokenization_spaces=True)
38 print(f"{i}:", text)
39 print("*" * 3)