Electric Sheaves

Build Your Own LLM

Over eight steps—one per lecture—you will build a complete language model from nothing. You begin with a counting model, pause to wire and inspect a small neural network exactly, then build an autodiff engine and train a neural language model. Attention, a full transformer, training, tokenization, and sampling follow. The end product is a “full-stack baby model”: a few-hundred-thousand- to few-million-parameter GPT that generates recognizable Shakespeare, built with your own hands at every layer of the stack.

The model is small. That is the point. Every mathematical phenomenon in the course — cross-entropy as log-likelihood, the geometry of softmax, the low-rank structure of attention heads, the variance bookkeeping of initialization, even rudimentary scaling laws — is visible in a model you can train in minutes and inspect completely.

Ground rules

  1. If we defined it in lecture, you implement it. No torch.nn.TransformerDecoder, no torch.nn.MultiheadAttention, no F.scaled_dot_product_attention. You may use PyTorch tensors, autograd (from Step 3 on, after you’ve built your own), nn.Linear, nn.Embedding, nn.LayerNorm, and the optimizers (from Step 6, after you’ve written SGD yourself). The line is: primitives yes, the things this course is about, no.
  2. Mathematics first. Each step opens with derivation exercises done on paper. The code is the check on your mathematics — when your handwritten gradient disagrees with finite differences, one of them is wrong, and it isn’t the finite differences.
  3. Small budget, real understanding. Each step is designed for 2–4 hours. If you are past 5, you’re stuck on something silly (we all do it); use the hints, ask, or peek at the solution for the previous step.
  4. AI assistants: off for the core implementation, fine for syntax and error messages. (See the note on the setup page.)

Rhythm and logistics

Joining late / catching up

The project is a chain, but every link is replaceable: each step’s instructions begin from the previous step’s posted solution. If you join at Lecture 5, download solutions/step-04.ipynb, run it top to bottom (~5 minutes), skim its prose, and start Step 5 from there. You lose the experience of the earlier steps, not the ability to continue — and you can back-fill earlier steps any time, since they only require earlier lectures.

The eight steps

The last column gives a reference checkpoint: genuine sample text for a generative model, or a visible component-level result when the step builds one part of the system.

StepYou buildNew mathematics exercisedReference checkpoint
1Character bigram model from countsMLE, cross-entropy, perplexityThs t hethalof finor:
2A hand-wired one-hidden-layer ReLU classifierunits as matrix layers; nonlinear features; softmax geometryexact diamond boundary
3A scalar autodiff engine; then an nn-gram MLP in PyTorchreverse-mode AD; embeddings; SGDHe halled A by lord--
4A causal self-attention headattention matrix, masking, softmax geometry(component)
5The full GPT: multi-head + MLP blocks, residual stream, LayerNormarchitecture as composition; parameter counting(untrained)
6The training run: AdamW, schedules, train/val diagnosticsstochastic optimization in practiceWARWICK: / Nor I, the kind dark of that which he had
7A BPE tokenizer and a proper samplergreedy compression; tilted distributionsfluent-ish Shakespeare
8Capstone: scale, fine-tune, or interpretyour choiceshowcase

What “done” looks like

By the end of Step 8 you will have, in notebooks written by you:

Every one of these artifacts corresponds to a section of the lecture notes. That correspondence — I can point to the line of code where this theorem acts — is the actual deliverable of the course.