Contributor Guide

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

Why D2L Uses Markdown as the Single Source of Truth

Motivation

  • Why raw .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.
  • Single-source Markdown (chapter_*/*.md): Pure human-readable Markdown gives 1-line Git diffs, while executed cell outputs live separately in outputs/<fw>/*.json.

One Source File, Five Generated Artifacts

The Build Pipeline

  • Golden rule: Edit only chapter_*/*.md (never _notebooks/, *.qmd, _slides/, or d2l/*.py).
  • Deterministic fan-out: The build splices outputs/<fw>/*.json and derives the HTML website, PDFs, per-framework .ipynb notebooks, Reveal.js slide decks, and the d2l package automatically.

Multi-Framework Tabs and Notebook Generation

Cross-Framework Parity

  • Tagged (%%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.
  • Shared-cell discipline: Editing an untagged cell requires re-executing and capturing outputs for all frameworks present in the file (pytorch and jax in Advanced chapters).

Permanent Cell IDs and the #@save Library Pipeline

Code Identity & Reuse

  • Permanent Cell IDs (#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).

Repository Layout and Fresh-Clone Bootstrap

Developer Setup

  • One-time ./bootstrap.sh: Pulls Git LFS plot assets and creates .venv-build with Quarto and Python build tools.
  • No-GPU local HTML build (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).

How Output Fingerprinting Detects Stale Notebooks

Reproducibility Audit

  • Three hashes per output JSON: (1) Per-cell prefix code hash (cells 1\dots k, trailing spaces stripped), (2) public framework version (local build tag stripped), and (3) transitive d2l.X symbol hash.
  • Targeted invalidation: Prose edits cause 0 stale cells; editing cell k marks cells k\dots n stale; editing a #@save helper marks only downstream notebooks that call it.

Why Prefix Staleness Beats Static Dataflow Tracing

Hidden Side-Effect Channels

  • Why static AST variable dataflow is unsound: Adding one random draw in cell 3 shifts the PRNG stream for cell 8 without sharing any variable name, and in-place weight/config mutations alter later cells invisibly.
  • Prefix rule + 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).

The Git Contribution Workflow: Fork, Branch, Edit, PR

Proposing a Change

  • Fork & dedicated branch: Use the GitHub pencil icon for 1-line prose fixes, or clone your fork and create one branch per logical change (git switch -c fix-topic).
  • Verify diff & open PR: Edit chapter_*/*.md, check git diff to confirm no generated files were hand-edited, push to your fork, and run gh pr create.

Linting, Executing, Capturing, and Rendering

Verification Loop

  • 1. Lint & Library (< 100 ms): Run python3 tools/lint_source.py --corpus <file>.md and gmake lib (if #@save changed).
  • 2. Execute, Capture & Audit: Run 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.

Interactive Authoring with the D2L VS Code Extension

Editor Integration

If you prefer interactive cell execution over editing raw Markdown fences, the repository includes a dedicated VS Code extension in .vscode-extension/:

  • Two-way source synchronization:
    • Opens the generated .ipynb notebook of the active .md section with the matching framework kernel preselected (make kernels).
    • Edits made to code or prose inside the notebook view are written directly back into the chapter_*/*.md source file, so a generated .ipynb never becomes an orphaned edit.
  • Integrated multi-framework & slide workflow:
    • Switch live between PyTorch, JAX, TensorFlow, and MXNet tab views.
    • Preview the section’s Reveal.js slide deck side-by-side.
    • Trigger full top-to-bottom notebook execution and output capture into outputs/<fw>/ with a single command.

Collaborating Effectively with Coding Agents

AI-Assisted Engineering

Three architectural properties of d2l-neu make coding agents unusually effective for bounded, multi-file engineering tasks:

  • 1. Explicit build & style contracts: 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.
  • 2. Executable verification oracle: An agent can run lint_source.py, execute every framework notebook, run audit_outputs.py, and inspect tracebacks autonomously.
  • 3. Mechanical cross-framework propagation: Syncing a change across PyTorch and JAX tabs or regenerating SVG figures via tools/gen_*_figures.py is fast to automate and easy for a human to review in git diff.
  • Two agent failure modes every human reviewer must guard against:
    1. Editing generated files: Verify git status shows changes in chapter_*/*.md (plus captured outputs/), never ad-hoc patches inside _notebooks/ or d2l/*.py.
    2. Reward hacking / weakening checks: When a test or notebook fails, an agent may loosen a threshold, silence an exception, or simplify the teaching example rather than fixing the root cause—always review the pedagogical diff.

Debugging Regressions Across Noisy Training Runs

Information-Theoretic Bisection

N_{\text{runs}} \ge \frac{\log_2 64 - H_b(P_e) - P_e \log_2 63}{C}

  • Why 1-run 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.
  • Bayesian bisection & Fano’s bound: Updating a posterior P(K = k \mid \text{runs}) after each test locates the bad commit at \ge 95\% confidence in \sim 22\text{--}25 runs, approaching the Shannon capacity limit C.

Concept Check: Tabs, Staleness, and Library Order

Check Your Understanding

Test your grasp of the D2L build system on these three questions:

  1. A source .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?
  2. You upgrade your local PyTorch environment from 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?
  3. 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?

Concept Check: Tabs, Staleness, and Library Order (cont.)

Check Your Understanding

Answers:

  1. Both the PyTorch and JAX notebooks. Because cell (3) is untagged, it belongs to every framework generated from the file (pytorch and jax), and editing it invalidates the prefix fingerprint of cells (3), (4), and (5) in both frameworks.
  2. Switching 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.
  3. 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.

Summary: Contributing to Dive into Deep Learning

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