Dive into Deep Learning · §39.8
Contributor Guide: Single-Source Markdown Build, Framework Tabs, and Git Workflow
Markdown source of truth · multi-framework tabs · #@save library generation · output fingerprinting · lint & audit · Git PR workflow
Motivation
.ipynb files fail in Git: Nested JSON mixes prose, code, base64 PNGs, and execution counters—a 1-word fix + cell run modifies 500+ lines and causes constant merge conflicts.chapter_*/*.md): Pure human-readable Markdown gives 1-line Git diffs, while executed cell outputs live separately in outputs/<fw>/*.json.The Build Pipeline
chapter_*/*.md (never _notebooks/, *.qmd, _slides/, or d2l/*.py).outputs/<fw>/*.json and derives the HTML website, PDFs, per-framework .ipynb notebooks, Reveal.js slide decks, and the d2l package automatically.Cross-Framework Parity
%%tab <fw>) vs. untagged cells: Tagged cells route only to their named framework(s); untagged Python cells are shared and execute in every framework notebook generated by that file.pytorch and jax in Advanced chapters).Code Identity & Reuse
#sec-slug-k): Assigned by tools/add_cell_ids.py, keying committed outputs in outputs/<fw>/ and slide references (@cell-id) across prose edits.#@save library extractor (make lib): Scans chapters in book order into d2l/*.py under a last-definition-wins rule (e.g., §5.7 GPU Trainer overrides §2.4 CPU Trainer in d2l, while §2.4 runs its own in-notebook definition).Developer Setup
./bootstrap.sh: Pulls Git LFS plot assets and creates .venv-build with Quarto and Python build tools.gmake html): Splices committed outputs from outputs/<fw>/*.json into .qmd files and renders the interactive website and slides in seconds on any laptop (requires GNU Make \ge 4.3).Reproducibility Audit
d2l.X symbol hash.#@save helper marks only downstream notebooks that call it.Hidden Side-Effect Channels
audit_outputs.py gate: Because a fresh kernel must replay cells 1\dots k-1 anyway, invalidating k\dots n guarantees 100% soundness (hard error on stale text; warning on stale images).Proposing a Change
git switch -c fix-topic).chapter_*/*.md, check git diff to confirm no generated files were hand-edited, push to your fork, and run gh pr create.Verification Loop
< 100 ms): Run python3 tools/lint_source.py --corpus <file>.md and gmake lib (if #@save changed).gmake -B _notebooks/<fw>/<ch>/<f>.executed and gmake capture-outputs FILES=<ch>/<f>.md for every framework tab, then verify with gmake audit-outputs && gmake html.Editor Integration
If you prefer interactive cell execution over editing raw Markdown fences, the repository includes a dedicated VS Code extension in .vscode-extension/:
.ipynb notebook of the active .md section with the matching framework kernel preselected (make kernels).chapter_*/*.md source file, so a generated .ipynb never becomes an orphaned edit.outputs/<fw>/ with a single command.AI-Assisted Engineering
Three architectural properties of d2l-neu make coding agents unusually effective for bounded, multi-file engineering tasks:
CLAUDE.md, docs/build-system.md, docs/writing-guide.md, docs/code-guide.md, and docs/slide-guide.md document every rule in machine-readable form.lint_source.py, execute every framework notebook, run audit_outputs.py, and inspect tracebacks autonomously.tools/gen_*_figures.py is fast to automate and easy for a human to review in git diff.git status shows changes in chapter_*/*.md (plus captured outputs/), never ad-hoc patches inside _notebooks/ or d2l/*.py.Information-Theoretic Bisection
N_{\text{runs}} \ge \frac{\log_2 64 - H_b(P_e) - P_e \log_2 63}{C}
git bisect fails: Across 64 = 2^6 commits, a noisy test (p = 0.90 pass on good, q = 0.30 on bad) steers all 6 binary steps correctly only \sim 26\% of the time.Check Your Understanding
Test your grasp of the D2L build system on these three questions:
.md file has 5 cells: (1) %%tab pytorch defining x, (2) %%tab jax defining x, (3) an untagged cell y = x * 2, (4) %%tab pytorch modifying x, and (5) an untagged print(y.sum()). If you edit cell (3), which notebooks change and must be re-executed?torch==2.11.0+cu124 on Linux to torch==2.11.0 CPU on macOS. Does tools/audit_outputs.py mark the committed PyTorch outputs stale? What if you upgrade to torch==2.12.0?Trainer.prepare_batch is defined with #@save in §2.4 (CPU-only) and redefined with #@save in §5.7 (moving batches to GPU). Which version is in d2l/torch.py, and which version runs at the end of the §2.4 notebook?Check Your Understanding
Answers:
pytorch and jax), and editing it invalidates the prefix fingerprint of cells (3), (4), and (5) in both frameworks.2.11.0+cu124 \to 2.11.0 CPU does not mark outputs stale because the fingerprint strips local build tags (+cu124) so CPU laptops can build the site without false alarms. Upgrading to 2.12.0 marks all PyTorch outputs stale.d2l/torch.py contains the §5.7 GPU-aware definition (last definition in book order wins). When §2.4 starts, d2l.Trainer.prepare_batch holds the §5.7 version until §2.4’s own @d2l.add_to_class cell executes and overwrites it in memory with the CPU version for the remainder of §2.4.Key Takeaways
| Build Mechanism | Design Principle | Contributor Action |
|---|---|---|
Single source (chapter_*/*.md) |
Generates .qmd, .ipynb, _slides/, and d2l/*.py |
Edit only the .md source (or via the VS Code sync extension); never hand-edit generated files |
| Multi-framework tabs | %%tab <fw> scopes code; untagged cells run in all frameworks |
When editing a shared cell, re-execute and capture outputs for every framework tab in that file |
#@save library extraction |
make lib collects #@save blocks in book order (last wins) |
Run make lib before re-executing notebooks whenever you edit a #@save block |
| Prefix output fingerprinting | Hashes cell prefix, major.minor.patch fw version & d2l.X closure |
Run make capture-outputs and verify make audit-outputs reports zero stale text cells |
Source linter (lint_source.py) |
Fast (<100\text{ ms}) check on div/tab balance, cell IDs & xrefs | Run python3 tools/lint_source.py --corpus <file>.md before every commit |
| Git & coding-agent review | Small branch-per-change PRs with executable verification | Review git diff to ensure agents edited sources rather than generated files or weakened checks |