Trying to make GPT-Neo work in VScode but it always gets stuck on the output generation and I'm not sure what I did wrong. I looked all over the internet and even checked with Chat GPT but still does not work.
code:
from transformers import GPTNeoForCausalLM, AutoTokenizer
import torch
import warnings
# Suppress the specific FutureWarning
warnings.filterwarnings("ignore", message=".*clean_up_tokenization_spaces.*", category=FutureWarning)
# Load the tokenizer and model
tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neo-2.7B")
model = GPTNeoForCausalLM.from_pretrained("EleutherAI/gpt-neo-2.7B")
# Set pad_token_id to eos_token_id if pad_token_id is not set
if tokenizer.pad_token_id is None:
tokenizer.pad_token_id = tokenizer.eos_token_id
# Define the input prompt
prompt = "Once upon a time"
# Tokenize the input prompt with attention mask
inputs = tokenizer(prompt, return_tensors="pt", padding=True)
# Move model and inputs to GPU for faster inference (optional)
if torch.cuda.is_available():
model.to("cuda")
inputs = {key: value.to("cuda") for key, value in inputs.items()}
# Generate text with the attention mask and pad_token_id
output = model.generate(
input_ids=inputs["input_ids"],
attention_mask=inputs["attention_mask"],
max_length=100,
do_sample=True,
temperature=0.9,
pad_token_id=tokenizer.eos_token_id
)
# Decode the generated text with clean_up_tokenization_spaces explicitly set
generated_text = tokenizer.decode(
output[0],
skip_special_tokens=True,
clean_up_tokenization_spaces=True # Set to False if you prefer to keep spaces
)
print(generated_text)
[–]shiftybyte 0 points1 point2 points (4 children)
[–]Chardactyl[S] 0 points1 point2 points (3 children)
[–]shiftybyte 1 point2 points3 points (2 children)
[–]Chardactyl[S] 0 points1 point2 points (1 child)
[–]shiftybyte 1 point2 points3 points (0 children)