GPT-2 is a transformer-based language model developed by OpenAI, released in 2019 as the successor to GPT-1 and predecessor to GPT-3. The model comes in several sizes, each with different parameter counts and architectural dimensions.
Model Variants and Parameter Counts
OpenAI released GPT-2 in four different sizes, each with increasing complexity and capability:
- GPT-2 Small: 117 million parameters, 12 decoder-transformer blocks, with embedding size of 768 dimensions[1][3]
- GPT-2 Medium: 345 million parameters, 24 decoder-transformer blocks, with embedding size of 1024 dimensions[1][8]
- GPT-2 Large: 762 million parameters, 36 decoder-transformer blocks, with embedding size of 1280 dimensions[1][8]
- GPT-2 Very Large (XL): 1.5 billion parameters, 48 decoder-transformer blocks, with embedding size of 1600 dimensions[1][7][8]
The full GPT-2 model (Very Large) represents a significant scale-up from GPT-1, with approximately 10 times more parameters and trained on more than 10 times the amount of data[2][8].
Core Architectural Dimensions
The GPT-2 architecture is based on the transformer decoder and includes several key dimensions:
Vocabulary and Context Size
- Vocabulary size: 50,257 tokens[1][2][5]
- Maximum sequence length (context window): 1024 tokens[1][2][5]
- Embedding dimension: Varies by model size (768 for Small, up to 1600 for XL)[1][3]
Attention Mechanism
- Attention type: Masked self-attention (cannot see future tokens)[7]
- Number of attention heads: 12 (in the Small model)[5]
- Attention dropout rate (attn_pdrop): 0.1[2][5]
Layer Configuration
- Layer normalization: Applied to the input of each sub-block rather than the output (different from GPT-1)[1][3]
- Additional layer normalization: Added after the final self-attention block[1][3]
- Activation function: gelu_new (Gaussian Error Linear Unit)[5]
- Dropout rates:
- Residual dropout (resid_pdrop): 0.1[2][5]
- Embedding dropout (embd_pdrop): 0.1[2][5]
- Attention dropout (attn_pdrop): 0.1[2][5]
- Layer normalization epsilon: 1e-5[5]
Training Details
GPT-2 was trained on a dataset called WebText, which consists of:
- 8 million web pages[2][3][8]
- Approximately 40GB of text data[3][8]
- Content from web pages that had received at least 3 upvotes on Reddit prior to December 2017[8]
The training differed from GPT-1 in several ways:
- Batch size: Increased from 64 in GPT-1 to 512 in GPT-2[1]
- Training objective: Causal language modeling (predicting the next token)[2][5]
- Training cost: While the exact training time is unknown, comparable models had costs ranging from 6,912(BERT)to245,000 (XLNet)[8]
Implementation Details
Some specific implementation details that affect the model's dimensions:
- Initialization range: 0.02 (standard deviation for the truncated normal initializer)[2][5]
- Weight initialization: Modified to account for the accumulation on the residual path with model depth, using a scaling factor of 1/√N where N is the number of residual layers[3]
- Position embeddings: Absolute position embeddings for each of the 1024 possible positions in the input[7]
- Parameters calculation inconsistency: There's a noted discrepancy between the parameter counts in the paper and the actual implementation, with the paper potentially omitting the language model head in its calculation[6]
Optimized Variants
To address GPT-2's resource intensity, variants like DistilGPT2 were created using knowledge distillation to produce smaller, faster models that maintain most of the performance while being:
- 33% smaller
- Twice as fast
- At the cost of scoring slightly lower on quality benchmarks[8]
GPT-2's architecture set the foundation for its successor GPT-3, which scaled up to 175 billion parameters while maintaining a similar architectural approach with modifications to allow for greater scaling[8].