microgpt
Open the sourceKarpathy teaches the complete algorithmic essence of training and running a GPT by reducing it to a dependency-free, 200-line Python program. He explains how data, tokenization, autograd, Transformer architecture, optimization, training, and inference fit together, while distinguishing the core algorithm from the engineering required to operate production-scale LLMs.
- A GPT learns statistical patterns in tokenized documents by repeatedly predicting the next token and adjusting its parameters to reduce cross-entropy loss. - Autograd computes how every parameter influences the loss by applying the chain rule backward through a computation graph and accumulating gradients across branches. - The Transformer alternates attention, which communicates information across token positions, with an MLP, which performs local computation at each position. - Training consists of selecting and tokenizing a document, running a forward pass, computing loss, backpropagating gradients, and updating parameters with Adam. - Inference repeatedly samples a token from the model's predicted probability distribution and feeds it back as the next input until an ending token or length limit is reached. - Production LLMs preserve this core structure but add scale, tensor parallelism, larger datasets, more efficient tokenization, post-training, and extensive serving infrastructure.
Based on Andrej Karpathy's public content. Not affiliated with or endorsed by Andrej Karpathy.
Supports these rules
Understand the backward pass
ConfirmedVerify each stage before adding complexity
ConfirmedRun small, comparable experiments
ConfirmedReduce to the minimal readable core
Quotes from this video
“This file contains the full algorithmic content of what is needed: dataset of documents, tokenizer, autograd engine, a GPT-2-like neural network architecture, the Adam optimizer, training loop, and inference loop.”
“Everything else is just efficiency.”
“All of these are important engineering and research contributions but if you understand microgpt, you understand the algorithmic essence.”
“The model’s job is to predict each next token given the tokens before it.”
“Fundamentally, we are still predicting the next token in the sequence but with a lot of engineering spent on making it faster.”
“ChatGPT?** ChatGPT is this same core loop (predict next token, sample, repeat) scaled up enormously, with post-training to make it conversational.”
“Note the (accumulation, not assignment).”
“When a value is used in multiple places in the graph (i.e. the graph branches), gradients flow back along each branch independently and must be summed.”
“Attention is a token communication mechanism.”
“The Transformer intersperses communication (Attention) with computation (MLP).”
“to get increasingly better results. Progression To see the code built up piece by piece as layers of the onion, the advised progression looks something like this:”
“I think this might be one helpful way to step through the code base, where you add one component at a time.”
“Try playing with the script!”
“You can try a different dataset.”
“Or you can train for longer (increase `num_steps`) or increase the size of the model to get increasingly better results.”