Let's reproduce GPT-2 (124M)
Andrej Karpathy
Summary
This video is a hands-on, deep-dive technical guide on reproducing the GPT-2 (124M) model from scratch using PyTorch. The expert walks through the entire pipeline: loading original model weights, building the Transformer architecture step-by-step (embedding layers, self-attention, MLP blocks), implementing optimization techniques like AdamW with weight decay, and integrating high-performance practices such as BF16 mixed precision, Torch Compile, and Flash Attention. Finally, the analysis covers data scaling, training at scale using Distributed Data Parallel (DDP) on multi-GPU setups, and benchmarking with standard tasks like HellaSwag.
Chapters
- 0:00Project Setup and Introduction
- 20:13Model Architecture Overview
- 41:44Attention and Weights
- 1:12:17Training Setup and Multi-GPU
- 1:40:14Loss Calculation and Optimization
- 2:17:18Performance and Efficiency
- 2:55:35Distributed Training and DDP
- 3:27:30Evaluation and Results
Topics covered
- Model Architecture
- Weight Loading and Porting
+4 more topics (full content when you follow the channel)
Transcript excerpt
hi everyone so today we are going to be continuing our Zero to Hero series and in particular today we are going to reproduce the gpt2 model the 124 million version of it so when openi released gpt2 this was 2019 and they released it with this blog post on top of that they released this paper and on top of that they released this code on GitHub so open a/ gpt2 now when we talk about reproducing gpt2 we have to be careful because in particular in this video we're going to be reproducing the 124 million parameter model so the thing to realize is that there's always a miniseries when these are releases are made so there are the gpt2 miniseries made up of models at different sizes and usually the biggest model is called the gpt2 but basically the reason we do that is because you can put the model sizes on the x-axis of plots like this and on the Y AIS you put a lot of uh Downstream metrics th…