The ten labs, in order
The labs follow the main track of the repository. Do 01-03 in order, right after the Transformer guide and before the tutorial Build an LLM. After that, do each later lab right after its module guide; each one only assumes 01-03 (lab 05 also uses the ideas of 04, lab 10 those of 06).
- 01
Attention
Causal mask, stable softmax, scaled dot-product attention from matmul + softmax, head split/merge, multi-head attention (checked against
nn.MultiheadAttention)Read with: step-by-step page; transformer guide: 2a worked example, 2c-2e multi-head, 2.3 causal mask
not checked - 02
Transformer block
LayerNorm and RMSNorm, GELU MLP and SwiGLU, a pre-norm block, a closed-form parameter count (GPT-2 block, Llama-2-7B block)
Read with: step-by-step page; architecture, what changed since 2017; preview, covered in the next tutorial: Build an LLM: model
not checked - 03
GPT + generation
A tiny GPT forward pass, greedy and top-k sampling, generation with a KV cache that must equal full recompute (including chunked prefill)
Read with: step-by-step page; 2.4 generation with a KV cache; preview, covered in the next tutorial: Build an LLM: sampling, KV cache
not checked - 04
Vision Transformer
Patchify by reshape and by Conv2d (they must agree), unpatchify, CLS token, position embeddings, the ViT forward pass, a shape tracker
Read with: ViT guide: 2.1 inference pipeline (shape flow), 2.2 patch embedding, 2.3 position embeddings, 2.5 where the parameters and compute go
not checked - 05
VLM fusion
Image tokens per resolution, the 2x2 pixel-shuffle merge, an MLP projector from vision width to LLM width, the joint image + text sequence and its causal / image-bidirectional mask (a partial prefix-LM)
Read with: VLM guide: the key idea, 1.1 shape flow, 2 the fusion step, 4 image token reduction
not checked - 06
Diffusion
Noise schedule, closed-form forward noising q(x_t | x_0), the epsilon-prediction loss, a training loop, a DDIM sampler; trains on 8 Gaussians on a ring
Read with: Diffusion guide: 1 forward process, 3.1 DDIM, 5 code walkthrough
not checked - 07
Mamba selective scan
Discretization (simple and ZOH), the sequential selective scan, a log-depth parallel scan, the one-token recurrent step; all three must agree
Read with: Mamba guide: 1 selective SSM, 2 two modes of computation
not checked - 08
MoE routing
Softmax and sigmoid top-k routers, the load-balancing loss, per-expert loop vs sort/scatter dispatch, capacity factor with token dropping
Read with: MoE guide: 2 the router, 5 inference (5.4 capacity, 5.5 grouped GEMM)
not checked - 09
BEV projection
Camera intrinsics and extrinsics, projecting 3-D points to pixels, BEV pillar points, reference points with a visibility mask, bilinear sampling with
grid_sample, averaging over viewsRead with: BEV guide: 3 camera projection (with the worked example), 4 spatial cross-attention
not checked - 10
Flow matching
The linear interpolation path, the velocity target, training a velocity MLP on two-mode action chunks, an Euler sampler, chunking and receding-horizon execution
Read with: VLA guide: 2 flow matching from scratch; Diffusion guide 3.2 flow matching
not checked
Robot foundation models (robot-fm) have no separate lab: GR00T N1's "System 1" is a flow-matching action head like the one in lab 10, conditioned on a VLM like the one in lab 05.
Three ways to work
In your browser, with the local lab runner (recommended). From the repository root:
python labs/serve.py --open
It prints a URL, http://127.0.0.1:8765/labs/index.html#token=..., and opens the lab hub. Each lab page (lab01.html ... lab10.html) shows the maths, one code cell per function with hints that open one at a time, and Run / Save / Run checks buttons. Your code runs in a Python process on your computer, one per lab, so variables persist between cells as in a notebook. Save writes the cells to labs/labNN_*.py (the previous version goes to labs/.backup/), so you can switch to any editor at any time. The runner needs only the standard library and your PyTorch; it listens on 127.0.0.1 only and every request must carry the token from the URL. Without the runner (opened as a file, or on GitHub) the pages still show everything, read-only.
notebooks/, generated from the lab files. Jupyter: pip install notebook (or pip install jupyterlab), then jupyter notebook labs/notebooks/. VS Code: install the Python and Jupyter extensions, pip install ipykernel, and open a .ipynb. The last cell runs the same checks on the functions defined in the notebook: check.run_lab(1, namespace=globals()). Notebook work stays in the notebook; it does not change labs/labNN_*.py.check.py. Colab opens the notebooks from GitHub, so it shows the version that has been pushed to master.You can also simply edit labs/labNN_*.py in any editor and run the checker below.
Run the checks
From the repository root:
python labs/check.py # check every lab python labs/check.py 3 # check lab 3 only (also: 03, lab03, or several numbers) python labs/lab03_gpt_generate.py # the same, by running the lab file itself python labs/check.py 3 -v # also print the full traceback of a failing step python labs/check.py --solutions # run the checks against the reference solutions
Every lab is split into steps, usually one per function, and each step prints one line:
| Status | Meaning |
|---|---|
PASS | your code matches the reference |
TODO | your code still raises NotImplementedError; the line names the function to write next |
FAIL | your code runs but gives a wrong answer, or crashes; the message shows expected vs got (worst entry, shapes) and often a hint about the likely mistake |
Everything is seeded, so the same code always gives the same result. Each check runs in well under a second, except the two training steps (labs 06 and 10), which train a tiny MLP for about 5-20 s on a CPU.
How to work through a lab
- Read the guide section listed above, then the maths at the top of the lab page (the docstring at the top of the lab file). It writes the maths out step by step with the shape of every tensor.
- Implement the functions in file order. Each function's docstring states its inputs, outputs and conventions (for example "True = may attend").
- After each function, run the checks (Run checks on the lab page, the last cell of the notebook, or
python labs/check.py N). Its step should turn fromTODOtoPASS. - When everything passes, try the variations suggested in the docstrings and in the guide's exercises; rerun the checks to make sure you broke nothing.
To start a lab again from scratch: git checkout labs/lab03_gpt_generate.py. Every save from a lab page first copies the previous version of the file to labs/.backup/.
How to get unstuck
Go down this list in order and stop as soon as you can continue:
- Read the FAIL message. It names the quantity that is wrong, shows the worst entry (got vs expected), and for common mistakes a
hint:line (for example "you reshaped [B, N, d] straight to [B, h, N, d_k]" or "this is POST-norm"). - Check shapes. Print
x.shapeafter every line, and compare with the shapes in the docstring. Most bugs in these labs are a transpose or a missing broadcast dimension. - Rerun with
-vto see the full traceback when your code crashes. - Read Hint 1 (on the lab page, or in the comments above the
TODO). Only if you are still stuck, Hint 2, then Hint 3 (Hint 3 is close to the answer). - Compare with the reference solution in
solutions/(on a lab page: Show solution under the cell). Look only at the function you are stuck on, close it, and write it again yourself. - The full models in the module folders (
../transformer/transformer.py,../mamba/mamba.py,../bev/bev.py, ...) show the same pieces in context.
Do not edit check.py to make a step pass; the checks are the specification.
Files
labs/ README.md this page check.py the checker (all check functions live here) lab01_attention.py ... the ten learner skeletons lab10_flow_matching.py solutions/ complete reference solutions with the same API serve.py the local lab runner (python labs/serve.py --open) index.html the lab hub; lab01.html ... lab10.html: one page per lab notebooks/ one Jupyter / Colab notebook per lab assets/ the pages' stylesheet and script tools/build_pages.py regenerates the pages and notebooks from the lab files
The pages and notebooks are generated: after changing a lab file or this README, run python labs/tools/build_pages.py (on the untouched skeletons) and commit the results.