Hands-on track

Hands-on labs: build the core of each model yourself

Reading a guide tells you what a model does. These labs make you write it. Each lab is one Python file with function signatures, docstrings that spell out the maths with tensor shapes, graded hints, and raise NotImplementedError # TODO where your code goes. A checker compares your code with independent references: PyTorch's own implementations (F.scaled_dot_product_attention, nn.MultiheadAttention with copied weights, F.conv2d, F.grid_sample), closed-form maths, the worked examples from the guides, and brute-force Python loops.

You need only PyTorch (2.1 or newer) and a CPU. No downloads, no GPU.

Three ways to work

1

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.

2
In Jupyter or VS Code, with the notebooks in 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.
3
In Google Colab: use "Open in Colab" on a lab page, for example lab 01 in Colab. PyTorch is preinstalled there, and the first cell downloads 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:

StatusMeaning
PASSyour code matches the reference
TODOyour code still raises NotImplementedError; the line names the function to write next
FAILyour 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

  1. 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.
  2. Implement the functions in file order. Each function's docstring states its inputs, outputs and conventions (for example "True = may attend").
  3. 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 from TODO to PASS.
  4. 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:

  1. 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").
  2. Check shapes. Print x.shape after every line, and compare with the shapes in the docstring. Most bugs in these labs are a transpose or a missing broadcast dimension.
  3. Rerun with -v to see the full traceback when your code crashes.
  4. 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).
  5. 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.
  6. 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.