Skip to main content

GPT-2

GPT-2 is a causal Transformer LM: each token attends only to previous tokens. Scaled from GPT with more parameters and data; strong at open-ended generation.

Adapted for this playbook from the 🤗 Transformers documentation by Hugging Face. Official page: https://huggingface.co/docs/transformers/model_doc/gpt2. Images © Hugging Face (documentation-images) unless noted. This is not a substitute for the upstream docs — verify against the current version.

Covers Hugging Face pages: model_doc/gpt2

Quick usage​

from transformers import pipeline

pipe = pipeline(task="text-generation", model="openai-community/gpt2")
print(pipe("Hello, I'm a language model", max_new_tokens=40))
from transformers import AutoModelForCausalLM, AutoTokenizer

model = AutoModelForCausalLM.from_pretrained(
"openai-community/gpt2", device_map="auto", attn_implementation="sdpa"
)
tok = AutoTokenizer.from_pretrained("openai-community/gpt2")
inputs = tok("Hello, I'm a language model", return_tensors="pt").to(model.device)
out = model.generate(**inputs, cache_implementation="static", max_new_tokens=40)
print(tok.decode(out[0], skip_special_tokens=True))

Checkpoints: openai-community. Serving example with vLLM transformers backend is documented upstream.

Official API​

GPT-2 model doc.

Discussion

Comments​

Share feedback or questions about this page. No account required.

Loading comments…