Save this video free

Let's reproduce GPT-2 (124M)

Andrej Karpathy · 4:01:26 · Watch on YouTube

These notes were made with YouTube Collector. Paste any YouTube link and get the same: a summary, chapters and a searchable transcript.

Overview

Andrej Karpathy builds a GPT-2 124M language model in PyTorch, first matching OpenAI’s released checkpoint and then training new weights from scratch. He develops the model and data pipeline, accelerates training with BF16, torch.compile, FlashAttention, fused AdamW, and eight-GPU DistributedDataParallel, then trains on FineWeb-Edu and evaluates against GPT-2 using validation loss and HellaSwag.

Key takeaways

Chapters

0:00 GPT-2 124M: Scope, Scaling, and Reproduction Goals
3:39 Loading OpenAI GPT-2 Through Hugging Face Transformers
13:47 From the Original Transformer to GPT-2’s Decoder-Only Design
18:20 GPT-2 Residual Blocks and Pre-Layer Normalization
23:20 GPT-2’s GELU MLP and Batched Multi-Head Attention
28:08 Mapping Hugging Face Checkpoint Weights into the New Model
31:00 Forward Pass: Tokens and Positions to Vocabulary Logits
33:31 Encoding the GPT-2 Sampling Prefix with tiktoken
37:02 Autoregressive Generation and Top-k Sampling
41:47 Random Initialization and Automatic Device Selection
45:50 Tiny Shakespeare as a First Training Dataset
50:00 Building Input and Target Batches by Shifting Tokens
52:53 Cross-Entropy Loss for Next-Token Prediction
56:43 AdamW, Gradient Clearing, and Single-Batch Overfitting
1:01:59 A Simple Sequential Data Loader for Shakespeare
1:06:16 Tying GPT-2’s Token Embedding and Language-Model Weights
1:10:10 Matching GPT-2’s Weight Initialization
1:13:48 Scaling Residual-Branch Initialization with Model Depth
1:22:19 Profiling the A100 and Understanding Precision Tradeoffs
1:26:40 TF32: Faster Matrix Multiplication with Minimal Code Changes
1:31:40 BF16 Automatic Mixed Precision in PyTorch
1:36:40 torch.compile and Fusing Elementwise GPU Operations
1:41:40 GPU Memory Hierarchy: HBM, Caches, and Registers
1:46:40 FlashAttention Avoids Materializing the Attention Matrix
1:51:40 Padding GPT-2’s Vocabulary to a GPU-Friendly Size
1:56:40 Using GPT-3’s Training Details to Configure GPT-2
2:01:40 Adam Hyperparameters and Global Gradient Clipping
2:06:40 Cosine Learning-Rate Decay with Warmup
2:11:40 Weight Decay, Parameter Groups, and Fused AdamW
2:16:40 Simulating GPT-3’s Large Batch with Gradient Accumulation
2:21:40 Why Gradient Accumulation Must Average the Loss
2:26:40 Launching Eight Processes with PyTorch DistributedDataParallel
2:31:40 Sharding Sequential Batches Across DDP Ranks
2:36:40 DDP Gradient Synchronization and Loss Logging
2:41:40 From Tiny Shakespeare to GPT-2’s Web-Text Data
2:46:40 Choosing FineWeb-Edu for a 10-Billion-Token Run
2:51:40 Tokenizing FineWeb-Edu into One-Hundred-Million-Token Shards
2:56:40 Setting the Training Horizon from Tokens and Batch Size
3:01:40 Starting the FineWeb-Edu Pretraining Run
3:06:40 Validation Loss, Sampling, and Training Logs
3:11:40 HellaSwag: Evaluating Sentence-Completion Knowledge
3:16:40 Scoring HellaSwag Choices with Next-Token Loss
3:21:40 Adding HellaSwag Evaluation to the Training Loop
3:26:40 Mid-Run Generations Show Learning Progress
3:31:40 After One Epoch: GPT-2 124M Benchmark Results
3:36:40 Overnight Training: Forty Billion Tokens and Better Text
3:43:20 Data Shuffling, Learning-Rate Tuning, and Checkpoints
3:51:40 Pretraining Versus Chat Fine-Tuning, and the llm.c Comparison
3:56:40 Reproduction Results, Remaining Issues, and Project Resources

Keep these chapters and the full searchable transcript in your own library.

Transcript excerpt

0:00 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

0:46 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 that you're interested in like translation summarization question answering and so on and you can chart out these scaling laws so basically as the model size increases you're getting better and better at Downstream metrics and so in particular for gpt2 if we scroll down in paper there are four models in the gpt2 miniseries starting at 124 million all the way up to 1558 million now the reason my numbers the way I say them disagree with this table is that this table is wrong if you actually go to the uh gpt2 uh GitHub repo they sort of say that um there was an error in how they added up

1:33 the parameters but basically this is the 124 million parameter model Etc so the

This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.

Related notes

Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Andrej Karpathy.

Want the full transcript?

Save this video in YouTube Collector to get its complete searchable transcript, your own AI summaries, and a library that keeps every video you collect in one place.