GPT-2
17 memberslucid.models.text.gpt2GPT-2 family — Radford et al., 2019 (pre-LN decoder-only transformer).
Radford, Alec, et al. "Language Models are Unsupervised Multitask Learners." OpenAI Technical Report, 2019.
GPT-2 retains GPT-1's decoder-only causal-LM objective
but introduces three architectural refinements that have since
become standard for autoregressive transformers. Pre-LayerNorm
moves the LayerNorm to the input of every sub-block (attention and
MLP), so each block computes
instead of ; combined with
a final LayerNorm (ln_f) after the last block, this yields
much more stable optimisation at depth. Residual projection weights
are initialised with an extra factor (where
is the layer count), which keeps activation variance
roughly invariant to depth.
The tokenizer switches to a byte-level BPE with a 50257-symbol
vocabulary (50256 merges plus the <|endoftext|> sentinel), and
context length doubles from 512 to 1024. The released family spans
four sizes — small (124M, the defaults above), medium (355M),
large (774M), and xl (1.5B) — by scaling
while holding intermediate_size = 4 \cdot \text{hidden\_size} and head-dim = 64 fixed.
The paper's headline finding is that a sufficiently large LM trained on WebText (~40 GB scraped from outbound Reddit links) performs competitive zero-shot transfer across reading comprehension, translation, summarisation, and QA — without any task-specific fine-tuning. This is the empirical seed of in-context learning and the modern prompting paradigm.
Classes
GPT2Config1 methodsConfiguration for every GPT-2 variant.
GPT2DoubleHeadsModel2 methodsGPT-2 + LM head + multiple-choice classification head.
GPT2DoubleHeadsOutput1 methodsJoint LM + multiple-choice output for GPT2DoubleHeadsModel.
GPT2ForSequenceClassification2 methodsGPT-2 with a last-token sequence-classification head.
GPT2LMHeadModel2 methodsGPT-2 with a tied causal-language-modeling head.
GPT2Model4 methodsGPT-2 decoder trunk — N pre-LN transformer blocks topped by ln_f.
GPT2Tokenizer1 methodsGPT-2 tokenizer — pure-Python reference.
GPT2TokenizerFast1 methodsGPT-2 tokenizer — C++-backed.
Functions
gpt2_large→ GPT2ModelConstruct a GPT-2 large (774M) decoder trunk.
gpt2_large_lm→ GPT2LMHeadModelConstruct a GPT-2 large (774M) model with the tied causal-LM head.
gpt2_medium→ GPT2ModelConstruct a GPT-2 medium (355M) decoder trunk.
gpt2_medium_lm→ GPT2LMHeadModelConstruct a GPT-2 medium (355M) model with the tied causal-LM head.
gpt2_small→ GPT2ModelConstruct a GPT-2 small (124M) decoder trunk.
gpt2_small_cls→ GPT2ForSequenceClassificationConstruct a GPT-2 small (124M) model with a sequence-classification head.
gpt2_small_lm→ GPT2LMHeadModelConstruct a GPT-2 small (124M) model with the tied causal-LM head.
gpt2_xlarge→ GPT2ModelConstruct a GPT-2 XL (1.5B) decoder trunk.
gpt2_xlarge_lm→ GPT2LMHeadModelConstruct a GPT-2 XL (1.5B) model with the tied causal-LM head.