In mvGPT, short for Minimum Viable GPT, I build a language model from scratch in PyTorch to understand it from end to end, and then take it through the whole pipeline around it, from pretraining and fine-tuning to reinforcement learning, evaluation and deployment.
Why build one
Most of us use language models every day now, and a lot of engineers build products on top of them, but far fewer of us know what goes on inside one. My own research is in reinforcement learning and reasoning, so I wanted to understand the whole of it, from the blocks of parameters inside a model to the way it's trained.
After years at Morgan Stanley and Google building large distributed systems, I'm just as curious about the engineering around a model, from profiling it and training it across several GPUs to serving it to many users at once. The practices that keep large systems running matter for language models as much as for other software, so the series makes room for those too.
The series shows how those blocks work together in code you can read line by line, without the engineering tricks a model needs at scale getting in the way, and the maths comes in only where the code needs it, right next to the lines that use it.
I made it for engineers and researchers who want to know what goes on inside a language model, not only how to call one. Some Python and PyTorch will help, although every change is typed out on screen along with the reasons behind it.
Where it starts
The model starts out with 14.6 million parameters, small enough to train in about ten minutes on a laptop, and the first thing it learns is to write short children's stories from TinyStories. The series opens with Attention Is All You Need and what we change from it, writes our decoder-only architecture in PyTorch class by class, and then pretrains it from random tokens to short stories, with a tokenizer, the next-token objective, a training loop and an evaluation all written from scratch.
From there, we fine-tune it to write the story an instruction asks for, and add a KV cache so the model runs only the newest token instead of the whole story again. Then we deploy it, saving it as a GPT-2 on the Hugging Face Hub, serving it from a Gradio app on a VPS and running it right in the browser with Transformers.js, which you can still try on the series' website.
The training pipeline
With a model that follows instructions, we go through the ways models learn from preferences and rewards. In DPO, our model learns from pairs of stories it wrote, preferring the one that uses more of the words it was asked for, and in PPO it learns from the stories it writes as they're scored, with a critic and a penalty for drifting too far from where it started.
Later on, GRPO replaces PPO's critic by having the model write eight stories per instruction and scoring them against one another, which is the reinforcement learning behind DeepSeek-R1. A later episode trains the same thing with Hugging Face's TRL, to see how it compares with the version we wrote by hand.
LoRA fine-tunes the model again while training only about 2% of the weights, in a small low-rank update next to the frozen ones, and we measure what that saves and what the model learns compared with full fine-tuning.
Scaling up, a little
Most episodes stay on a laptop on purpose, but a few rent GPUs by the hour, starting with one that pretrains a model about six times bigger, in GPT-2 small's shape, on four RTX 4090s with PyTorch's DistributedDataParallel, to see how the speed scales from one GPU to four. A later episode pretrains a model of that shape overnight on educational web pages, on a single RTX 4090 rented for under four dollars, and on HellaSwag it ends up within ten questions of GPT-2 small, out of just over ten thousand, which isn't bad at all for what it cost!
Newer ideas from the research
A lot of the series follows ideas from recent papers, built from scratch and measured one at a time. The model gets rotary position embeddings, and later YaRN to stretch them to four times the original context window, along with a byte-level BPE tokenizer written in C++ and flash attention, first in PyTorch and then as a Triton kernel. It also gets a modern transformer block with RMSNorm, SwiGLU and QK-norm, then grouped-query attention for a smaller KV cache, and a mixture of experts with a router in place of the MLP in every block.
We race AdamW against Lion, Sophia and Muon, all four with their own sweep of the learning rate, and turn our GPT into a masked diffusion model that writes a story all at once by unhiding it. We even replace attention altogether with Hyena's long convolutions, measuring all of these against what we had before.
On the inference side, we write speculative decoding, where a draft guesses the next few tokens and the model checks them in a single pass, and constrained decoding, which keeps the output within a list of names with no training at all. Batching then lets us serve the model as an API to many users at once, with vLLM as the alternative.
More recently, we've looked inside the model too, writing out what the attention heads look at while the model copies a name into a query, and training a sparse autoencoder on the residual stream to find features we can read and use to steer what the model writes. We've also ported it to Apple's MLX and written the GPU kernels in Mojo, racing them against PyTorch on a Mac.
Tools, agents and a graph database
Once the web model was trained, we fine-tuned it to call a calculator in plain text, and then turned it into an agent that writes files and hands work to subagents, all of them fresh contexts of the same model, so we could measure what the subagents buy us. That agent then learned to write Cypher to ask smollerGraph, the graph database I'm building in another series, for facts, so it can take two facts from the graph and combine them with the calculator, which is where the two series start to meet.
mvGPT meets nanoMMO too, as the game has a storyteller NPC that writes you a story from a few words with our model, and the episode on batching measures the server against a crowd of players from the game.
Following along
The code is on GitHub under the MIT licence, together with the docs, and the trained models are on Hugging Face. All the steps of all the episodes are tagged in the repository, so you can check out the code wherever you are in a video and run it yourself, or open the notebook for an episode in Google Colab and run it on a Colab GPU.
New episodes go up on the YouTube playlist, and the series' website has a page per episode, with the code at every step.