Let's build GPT: from scratch, in code, spelled out.
Build a character-level GPT from scratch in PyTorch to understand the Transformer architecture powering ChatGPT.
Key takeaways
10 points- 1Self-attention is data-dependent weighted aggregation via Q·K^T softmax.
- 2Residual connections create gradient super-highways that stabilize deep stacks.
- 3LayerNorm is applied per-token (rows) before each sub-layer in modern Transformers.
- 4Causal masking is realized by filling the upper triangle with -inf before softmax.
- 5Positional embeddings are required because attention itself has no notion of order.
- 6Pre-training yields a document completer; RLHF aligns it to be an assistant.
- 7Batch size and block size trade off parallelism against context length.
- 8Cross-entropy expects logits shaped (B*T, C); always flatten before the loss.
- 9Scaled attention keeps softmax from collapsing to one-hot at initialization.
- 10A 10 M parameter decoder already produces recognizable Shakespeare after minutes on an A100.
Summary
A hands-on walkthrough that implements a GPT-style language model from scratch.
Core Architecture
- GPT stands for Generatively Pre-trained Transformer; the Transformer neural net performs the heavy lifting (0:00-12:02).
- Decoder-only blocks with causal masking let each token attend only to previous tokens (1:12:25-1:24:28).
Data and Tokenization
- Tiny Shakespeare (1 MB) is tokenized at character level into 65 unique integers (12:05-24:06).
- 90/10 train/val split; data is served in B=64 batches of T=256-token blocks (12:05-24:06).
Self-Attention Mechanism
- Each token emits query, key and value vectors; affinities are dot products scaled by sqrt(head_size) (1:00:22-1:12:24).
- Lower-triangular masking plus softmax yields data-dependent weighted aggregation (48:17-1:00:20).
Transformer Block
- Multi-head attention is followed by an MLP, both wrapped with residual connections and pre-layer-norm (1:24:28-1:36:31).
- Dropout (0.2) regularizes the 6-layer, 384-dim model (1:36:32-1:48:34).
Training and Scaling
- Adam (3e-4) on cross-entropy reduces loss from ~4.7 to 1.48 after 15 min on A100 (36:12-48:16).
- Pre-training on internet text followed by RLHF produces ChatGPT-style assistants (1:48:35-1:56:19).
Deep read
Executive Summary
Andrej Karpathy implements a minimal decoder-only Transformer (nanoGPT) line-by-line, training it on Tiny Shakespeare to reproduce the core mechanics of GPT-2/3. The tutorial moves from a bigram baseline through batched self-attention, residual blocks, layer norm and dropout, finishing with a 10 M parameter model whose loss reaches 1.48. The final section maps the toy pre-training run onto the two-stage pipeline (internet pre-training then RLHF) that produced ChatGPT.
Speaker: Andrej Karpathy
00:00:00 Introduction and Context: ChatGPT, Transformers, Shakespeare, and the Goal
- Karpathy frames the video around building a Generatively Pretrained Transformer (GPT) and relates it to ChatGPT, GPT-2/3, and the broader AI landscape.
- Emphasis on following the Attention is All You Need paper and the practical lineage via GPT-2/3, with a focus on understanding autoregressive language modeling, tensors, PyTorch nn basics, and the educational aim of a character-level transformer trained on a small dataset.
- The plan is to build intuition and implement a transformer-based language model (character level) on a toy dataset (tiny Shakespeare) to demonstrate how the system operates under the hood.
"Transformer is the neuron nut that actually does all the heavy lifting under the hood" (paraphrase of Karpathy’s framing)
- Notable resources mentioned early: Colab for the video, GitHub nanoGPT repo, OpenAI GPT-3 paper, ChatGPT blog, and general Colab/GPUs guidance.
- Key tradeoff highlighted: we won’t replicate production-grade ChatGPT (which is trained on vast internet data with extensive pre-training, fine-tuning, and RLHF) but we will explore the fundamentals via a smaller dataset and a simple GPT-style model.
00:07:52 Data, Tokenization, and Dataset Choice
- Data source: Tiny Shakespeare (concatenation of Shakespeare works, ~1 MB). Used to model character sequences and learn character-level dependencies.
- Concept: modeling how characters follow each other; given a chunk, predict the next character. This reveals patterns in the language style of Shakespeare without requiring word-level modeling.
- Tokenization approach chosen for this tutorial: character-level encoding. Each character maps to an integer ID; a reverse map exists to decode.
- Why not subword/tokenizers here? While subword models (e.g., BPE, SentencePiece, Byte-Pair Encoding, TikToken) are common in practice and reduce sequence length, the lecture emphasizes a simple, transparent character-level tokenizer to illustrate core ideas clearly.
Key term: tokens = units the model uses to represent text; for this setup, tokens are characters.
00:09:28 Tokenization, Train/Val Split
- Build two dictionaries: char2i and i2char for encoding/decoding between characters and integers.
- Vocabulary size is 65 characters (including space and punctuation).
- Data preprocessing steps: read text, extract the set of unique characters, sort them for deterministic ordering, and define vocab_size = 65.
- Train/validation split: use the first 90% for training and the last 10% for validation to measure overfitting and generalization.
00:14:27 Data Loader: Batches of Chunks of Data
- Training on chunks instead of the entire dataset: sample random chunks from the training data with a fixed block size (context length).
- Example of chunking: if block_size = 8, you feed in 8 characters as input (X) and 8 characters as targets (Y) that are the next characters for each position in X (offset by +1).
- The design exposes multiple training examples within a single chunk: an 8-character chunk yields 8 training examples, for contexts lengths 1..8.
- The reason to include all context lengths in training is to teach the model to predict next tokens starting from various-length contexts, which helps during inference when starting with little context.
01:00:18 Positional Encoding and the Crux: Version 4 Self-Attention
- The crucial transition is to implement self-attention, which enables tokens to attend to past tokens in the sequence.
- Early versions illustrate naive context aggregation; soon, matrix-multiplication-based attention is introduced for efficient computation and differentiable weighting.
- The notes introduce important concepts:
- Attention as a communication mechanism between positions in a sequence. - Attention operates over sets; it has no inherent notion of
Action Items
- Build a toy GPT by implementing a decoder-only Transformer with attention, head-size, and a feedforward block, starting on a character-level Shakespeare dataset.
- Experiment with block_size (context length), embedding dimensions, number of heads, and layers while monitoring train/val loss.
- Add positional embeddings and ensure proper masking for autoregressive generation.
- Incorporate residual connections and LayerNorm (pre-norm formulation) for improved training stability.
- Prototype multi-head attention and verify its impact on loss and generation quality.
- Scale up gradually: increase dataset size, context length, model depth, and apply dropout to prevent overfitting.
- Generate long samples from the trained model to qualitatively assess learning of language patterns (e.g., Shakespeare-like text).
- Read foundational papers (Attention Is All You Need; GPT-2/3) to understand how pretraining, fine-tuning, and alignment shapes real-world agents like ChatGPT.
- Leverage Colab or Lambda GPU Cloud to run experiments; document experiments and checkpoints for reproducibility.
Mental Models
Glossary
- GPT
- Generative Pretrained Transformer; a decoder-only Transformer trained to predict the next token, used for autoregressive text generation.
- Transformer
- A neural network architecture that uses self-attention and feedforward layers to model sequences, enabling parallelizable computation and long-range dependencies.
- Self-Attention
- An attention mechanism where the input sequence attends to itself, computing relationships between every pair of positions (Q, K, V projections).
- Multi-Head Attention
- Parallel self-attention mechanisms (heads) that capture different types of dependencies; outputs are concatenated and projected back to the model dimension.
- Positional Encoding
- A mechanism to inject order information into token representations since attention alone is permutation-invariant.
- Block Size
- Context length; the maximum number of past tokens the model uses to predict the next token during training and inference.
- Tokenization
- The process of converting text into a sequence of tokens (integers). In this thread, a character-level tokenization is used.
- Cross-Attention
- Attention where the queries come from one source and the keys/values come from another (useful for encoder-decoder architectures).
- LayerNorm
- Layer normalization; normalizes features within each sample to stabilize learning in deep networks.
- Dropout
- Regularization technique that randomly zeros out a subset of activations during training to prevent co-adaptation and overfitting.
- Residual Connection
- Skip connections that add the input of a sublayer to its output, helping gradients flow through deep networks.
Notable Quotes
“"Transformer is the neuron nut that actually does all the heavy lifting under the hood. It comes from this paper in 2017."”
“"Attention is all you need"”
“"Attention is a communication mechanism"”
“"Attention has no notion of space"”
“"There is no communication across batch dimension"”
“"scaled self-attention. why divide by sqrt(head_size)"”
“"We train a decoder-only Transformer following this famous paper Attention Is All You Need"”
“"The GPU I'm training the model on is from Lambda GPU Cloud"”
Topics
More from Neural Networks: Zero to Hero
See all




