GPT-2 40 min Build GPT-2 from Scratch in PyTorch: A Full Walkthrough How to build GPT-2 from scratch in PyTorch: tokenization, causal self-attention, transformer blocks, weight tying, and a 124M training loop that runs. May 11, 2026