Skip to content
AI from Scratch
EN

The map

The complete route, with the logic that holds it together. Each stage exists because the previous one ran into a concrete wall. That is the structure of the project and the real history of the field.


The chain of problems

  I want a machine to "learn"
        │
        ├─ [00] What is learning?  ->  adjusting numbers to bring an error down
        │       WALL: a linear model cannot even solve XOR
        │
        ├─ [00.3] Solution: layers + non-linearity
        │       WALL: differentiating by hand does not scale past 20 parameters
        │
        ├─ [03] Solution: autograd (the chain rule, automated)
        │       WALL: and how does TEXT get into any of this?
        │
        ├─ [01] Solution: tokenization (text -> integers)
        │       WALL: what exactly is "a language model"?
        │
        ├─ [02] Answer: P(next token | context). Counting already works.
        │       WALL: counting does not scale. 5 characters of context = 600 GB
        │
        ├─ [04] Solution: embeddings + dense network (grows linearly, not exponentially)
        │       WALL: concatenation is rigid; it does not choose WHAT to look at
        │
        ├─ [05] Solution: ATTENTION (each token chooses where it gets information from)
        │       WALL: it all has to be assembled and actually train
        │
        ├─ [06] Solution: the complete TRANSFORMER. And it works.
        │       WALL: at a certain scale, training runs break
        │
        ├─ [07] Solution: AdamW, warm-up, init, clipping, regularization
        │       WALL: generating is painfully slow and the model does not fit on the laptop
        │
        ├─ [08] Solution: KV cache, sampling, quantization
        │       WALL: the model continues text, it does not answer questions
        │
        ├─ [09] Solution: SFT with loss masking (+ LoRA to make it cheap)
        │       WALL: it imitates whatever you give it, it does not know which answer is BETTER
        │
        ├─ [10] Solution: preference alignment (DPO)
        │       WALL: how does any of this reach a chat box?
        │
        ├─ [11] Solution: server, template, streaming, context management
        │       WALL: it knows nothing new and it cannot act
        │
        ├─ [12] Solution: RAG (data) and tools (action)
        │       WALL: how do I know whether any of this works?
        │
        └─ [13] Solution: evaluate specific capabilities, against a chance baseline

What each stage proves (measured results)

StageClaimHow it is checkedResult
00.2The learning rate has an exact limitCompute 2/λmax of the Hessian and try just above and just below0.04175 converges · 0.04428 explodes
00.3A linear model cannot do XORTrain it to the endPredicts 0.500 for everything
01.2BPE discovers morphology without knowing grammarList the learned tokens'ción', 'ando', ' el', ' de' appear
02.2Gradients and counting end up in the same placeCompare the probability tablesMean difference 0.001
03The autograd is correct34 checks against finite differences34/34 correct
04.2"Vowels cluster in the embeddings"Measure against the chance baselineInput: NO (65.7% vs 64.7%). Output: YES (81.0%)
05The 1/√dk is not decorativeMeasure the softmax entropy with and without itWithout it and dk=64: entropy 0.000 (dead gradient)
06You can train a GPT on a laptopDo it810k parameters, 9.2 min, readable Spanish
06.4What is written in NumPy is a real transformerLoad its weights into a PyTorch GPT and compare logitsRelative difference 1.0e-06, same token in all 256 positions
06.4With enough data, overfitting disappearsTrain 4.2 M parameters with 77 M tokens (Chinchilla)training 0.2304 · validation 0.2387 (nearly equal)
07.1AdamW beats SGDSame model, same seed, 300 stepsSGD 2.68 · momentum 2.25 · AdamW 1.93
07.2Without residuals the gradient diesMeasure the gradient norm at layer 1 with 30 and 100 layersWith init 0.02 and no residual: exactly 0.0
07.3More data beats regularization4 experiments compared6k tokens + dropout: 1.52 · 200k with nothing: 0.81
08.2The KV cache speeds things up without changing the resultCompare logits and timesDifference 2.5e-06 · 16.8× faster
08.3int4 works, but only in groupsQuantize and measure the lossPer tensor: +0.256 · in groups of 64: +0.027
09.1SFT teaches format, not knowledgeTest with held-out questionsSeen: perfect · new: format right, content wrong
09.1Evaluating without chunking blows up memoryMeasure the process RSSPeak 8.9 GB and SIGKILL; in batches, it works
09.2LoRA does not alter the model at the startCompare logits before and after adding itDifference 0.00e+00
10DPO raises the preferred answer over the rejected oneMeasure the margin before and after86.2% → 97.0% accuracy
12.1An LLM's embeddings are no good for searchTF-IDF against the GPT, same testTF-IDF 88% · neural 25%
12.3Tools change the resultSame model, with and without a calculatorAlone: 0% · with the tool: 91%
13An aggregate number does not compare modelsDependency test at 44 charactersMean loss: tied · this test: 100% vs 50%

All of these numbers come from running the scripts. If you run them and get something else, yours is the one that counts.


The seven ideas holding everything up

If you had to keep seven sentences:

  1. A model is a function with gaps in it. Training is filling the gaps so that it fails less. (Stage 00.)
  1. The chain rule automates it. Backpropagation = walking a graph backwards multiplying local derivatives. (Stage 03.)
  1. Text goes in as integers. Whatever ends up inside a token is, for the model, an indivisible atom. (Stage 01.)
  1. A language model returns a probability distribution over the next token. Nothing more. (Stage 02.)
  1. Embeddings make generalization possible. Similar contexts → similar vectors → the model gets combinations right that it never saw. (Stage 04.)
  1. Attention lets each token choose where it gets its information from, by content rather than position. (Stage 05.)
  1. The rest is scale and engineering. There is no secret eighth idea. (Stages 06-13.)

If you have an afternoon: 00 → 01 → 02 → 05 → docs/01-del-teclado-al-token.md. You will understand 70% with 20% of the effort.

If you have a weekend: all of it in order, running every script.

If you have a technical background and want the core: mininn/tensor.py → etapas/06_transformer/gpt.py → etapas/08_inferencia/02_kv_cache.py. Three files, and they contain the essentials.

If what you want is to use AI, not build it: docs/01, docs/05 and stage

  1. Knowing where it fails is what will serve you most.

Index of documents