# Labs: implement it yourself

Hands-on Jupyter notebooks for each module. You re-implement the book's core functions in
cells marked `# YOUR CODE HERE`; a grader cell right below tells you ✅ or ❌ (with the
expected value, what you returned and a hint).

## Setup (once)

Requires Python 3.12 (PyTorch has no wheels for very new Pythons yet).

```bash
cd labs
python3.12 -m venv .venv
.venv/bin/pip install -r requirements.txt
.venv/bin/python -m ipykernel install --user --name rmfs --display-name "Python (rmfs)"
```

or simply `./labs/setup_venv.sh`.

> **Project on an exFAT / FAT drive?** (e.g. a portable SSD) Each small file then occupies a
> whole allocation block, and a PyTorch venv can grow to 40+ GB. Put the venv on your internal
> disk and symlink it: `RMFS_VENV_DIR=~/.venvs/rmfs ./labs/setup_venv.sh`. On this machine,
> `labs/.venv` is such a symlink to `~/.venvs/rmfs`.

## Run a lab

```bash
labs/.venv/bin/jupyter lab labs/          # then open chNN_lab.ipynb, kernel "Python (rmfs)"
```

At the top of every lab:

```python
MODE = "toy"    # default: tiny random model + character tokenizer, runs in seconds on any CPU, no downloads
MODE = "real"   # Qwen3 0.6B from the book (downloads ~1.2 GB into labs/qwen3/ once)
```

Toy mode checks that your code is *mechanically* right (shapes, caching, masking, losses); the
text it generates is gibberish by design. Switch to real mode to reproduce the book's results.

Stuck? Solutions are in `labs/solutions/` (try first!).

**Capstone** (`capstone_lab.ipynb`): chains your functions from labs 3-8 (baseline →
self-consistency → self-refinement → GRPO → distillation) and prints one results table.
Set `SOURCE = "mine"` to import *your* lab notebooks (unfinished functions fall back to the
reference, with a note) or `SOURCE = "solution"` for the reference versions.

## For authors / CI

```bash
labs/.venv/bin/pytest labs/tests -q                                  # every lab, toy mode
labs/.venv/bin/python tools/make_notebook.py --all                   # rebuild notebooks from labs/specs/
labs/.venv/bin/python tools/make_notebook.py --all --check           # fail if a notebook is stale
```

* Conventions: [`STYLE.md`](STYLE.md)
* Real signatures of the `reasoning-from-scratch` package + toy model: [`API_NOTES.md`](API_NOTES.md)
* Shared helpers: `lab_utils/toy_model.py`, `lab_utils/grader.py` (`check`, `check_value`, `raises`, ...),
  `lab_utils/testing.py`, `lab_utils/nbimport.py` (import definitions from another lab notebook)
* Tests force `MPLBACKEND=Agg` (in `tests/conftest.py` and `lab_utils/testing.py`): `plt.show()` would
  otherwise block on macOS when a notebook is executed in-process.

## Layout

```
labs/
  chNN_lab.ipynb            learner notebooks (generated from specs/)
  solutions/chNN_solution.ipynb
  specs/chNN_lab.py         source of truth for both notebooks
  tests/test_chNN.py        pytest: reference functions, grader, notebook execution
  lab_utils/                toy model, grader, test helpers
  requirements.txt          pinned versions (torch 2.10.0, reasoning-from-scratch 0.2.0, ...)
```
