Commit Graph

139 Commits

Author SHA1 Message Date
Vijay Janapa Reddi
48b519c42e Merge feature/tinytorch-core into feature/book-volumes
# Conflicts:
#	README.md
#	tinytorch/src/01_tensor/01_tensor.py
#	tinytorch/src/15_quantization/ABOUT.md
2026-03-02 09:38:08 -05:00
Vijay Janapa Reddi
bf9c402827 Adds callout-definition blocks to all Vol.2 chapters and fixes pre-commit hook errors
- Adds standardized callout-definition blocks with bold term + clear definition
  to all Vol.2 chapters (distributed training, inference, network fabrics, etc.)
- Fixes caption_inline_python errors: replaces Python inline refs in table
  captions with static text in responsible_engr, appendix_fleet, appendix_reliability,
  compute_infrastructure
- Fixes undefined_inline_ref errors: adds missing code fence for PlatformEconomics
  class in ops_scale.qmd; converts display math blocks with Python refs to prose
- Fixes render-pattern errors: moves inline Python outside $...$ math delimiters
  in conclusion, fleet_orchestration, inference, introduction, network_fabrics,
  responsible_ai, security_privacy, sustainable_ai, distributed_training
- Fixes dropcap errors: restructures drop-cap sentences in hw_acceleration and
  nn_architectures to not start with cross-references
- Fixes unreferenced-label errors: removes @ prefix from @sec-/@tbl- refs inside
  Python comment strings in training, model_compression, ml_systems
- Adds clientA to codespell ignore words (TikZ node label in edge_intelligence)
- Updates mlsys constants, hardware, models, and test_units for Vol.2 calculations
- Updates _quarto.yml and references.bib for two-volume structure
2026-03-01 10:44:33 -05:00
Vijay Janapa Reddi
2de66f1c0f refactor: complete Gold Standard audit for core foundation chapters; unify Volume 1 and Volume 2 math; verify physical realism of hardware constants 2026-02-25 08:31:21 -05:00
Aditya Mulik
e5d8395265 The multiplication process example is corrected 2026-02-25 05:24:01 -05:00
Vijay Janapa Reddi
85c0cd4c83 Merge branch 'feature/tinytorch-core' into dev 2026-02-22 13:22:37 -05:00
Vijay Janapa Reddi
1733596996 style(module 19): fix table alignment in MLPerf benchmark docs 2026-02-22 13:22:31 -05:00
Vijay Janapa Reddi
55007e1f6e docs(module 19): add educational purposes disclaimer for MLPerf 2026-02-22 13:20:12 -05:00
Vijay Janapa Reddi
68d446edcf docs(module 19): add MLPerf trademark attribution and pedagogical framing
- MLPerf® is a trademark of MLCommons
- This module teaches the principles of MLPerf-style benchmarking
2026-02-22 13:19:43 -05:00
Vijay Janapa Reddi
335bad8cd0 fix(module 19): address benchmarking feedback from issue #1196
- Fix Pareto diagram: swap A/C so line has positive slope (latency vs accuracy)
- Add missing Image Classification writeup to Standard Benchmark Tasks
- Treat anomaly_detection as binary classification (np.rand(2))
- Convert MLPerf inputs to Tensors + transpose HWC→CHW for TinyTorch models

Fixes harvard-edge/cs249r_book#1196
2026-02-22 13:18:08 -05:00
Vijay Janapa Reddi
94d079b57c Merge feature/tinytorch-core into dev (fixes #1184) 2026-02-21 09:39:50 -05:00
Vijay Janapa Reddi
69f46d4f7e Clarifies memoization computation savings
Refines the explanation of K,V computation savings in the memoization module,
quantifying redundant computations and highlighting the efficiency gain.

The paper and module now specify that generating 100 tokens requires 5,050
total K,V computations, but only 100 are necessary, resulting in 4,950
redundant calculations.
2026-02-19 17:59:10 -05:00
unknown
5f7a696077 Improve activation graph visualization in Module 02
- Clarify node labeling
- Improve spacing for readability
- No API changes
2026-02-20 03:48:10 +05:30
Vijay Janapa Reddi
850a91adc6 fix(docs): align notebook filenames with tito convention across all docs
Notebooks use short names (tensor.ipynb, not 01_tensor.ipynb) but docs
and Binder postBuild scripts used the prefixed form. This caused broken
Binder links and incorrect paths in troubleshooting guides.

Fixes: harvard-edge/cs249r_book#1176
2026-02-17 18:31:44 -05:00
Vijay Janapa Reddi
e7f9223680 feat(site): convert all 20 ABOUT.md files to MyST notebooks with computed values
Replace hardcoded numerical values across all module ABOUT.md files with
Python-computed values using myst_nb glue() references. Each file is now a
MyST Markdown Notebook that executes inline code cells to compute memory
sizes, FLOPs, compression ratios, and other quantitative values.

Key changes:
- Add file_format: mystnb frontmatter and code-cell blocks to all 20 files
- All arithmetic (memory calculations, speedups, ratios) now computed inline
- Fix multiple arithmetic errors discovered during conversion
- Enable execute_notebooks: "cache" in PDF config for glue resolution
- Fix jupyter-book version constraint in Makefile
2026-02-17 18:11:31 -05:00
Vijay Janapa Reddi
672eee9618 fix(pdf): cap Mermaid figure sizes and fix nested code fences
Mermaid diagrams were oversized in PDF output. Reduced viewport width
from 800→600 and added LaTeX preamble to cap mermaid figures at
0.75\linewidth. Also fixed 7 admonition blocks across 5 ABOUT.md files
where nested triple-backtick code fences broke the MyST parser, causing
raw markdown to render in PDF output.
2026-02-17 15:15:56 -05:00
Vijay Janapa Reddi
c13c4c6b9c fix: align MLPerf box-drawing in benchmarking module, set tree indent
- Fix misaligned ASCII art in Module 19 benchmark architecture diagram
- Set default workbench.tree.indent to 16px in VS Code extension
2026-02-17 13:53:32 -05:00
Vijay Janapa Reddi
09de699545 fix(exports): add missing #| export directives across 10 modules
Systematic audit of all 20 modules against module-developer agent rules
found 9 standalone helper functions missing #| export — these are called
by exported code at runtime but were excluded from the generated package,
causing NameError/AttributeError in CI.

Modules fixed:
- 05_dataloader: _pad_image, _random_crop_region (used by RandomCrop)
- 06_autograd: _stable_softmax, _one_hot_encode (prior session)
- 07_optimizers: 5 mixin classes + monkey-patches (prior session)
- 08_training: 7 monkey-patched Trainer methods (prior session)
- 10_tokenization: _count_byte_pairs, _merge_pair (used by BPETokenizer)
- 11_embeddings: _compute_sinusoidal_table (prior session)
- 12_attention: _compute_attention_scores, _scale_scores, _apply_mask (prior)
- 15_quantization: _collect_layer_inputs, _quantize_single_layer (used by quantize_model)
- 18_memoization: _cached_generation_step, _create_cache_storage, _cached_attention_forward (used by enable_kv_cache)
- 19_benchmarking: rename TinyMLPerf→MLPerf, fix monkey-patch naming (prior)

Also includes: vscode-ext icon refactor (ThemeIcon migration).

All 789 tests pass (unit, integration, e2e, CLI).
2026-02-15 17:38:03 -05:00
Vijay Janapa Reddi
910240a3f6 chore(tinytorch): add VS Code extension and sync module updates
Add the TinyTorch VS Code extension source package and align module code/docs references so APIs, milestones, and progression notes remain consistent across the curriculum.
2026-02-15 14:02:09 -05:00
Vijay Janapa Reddi
fd7cab5c75 refactor(modules): decompose oversized solution blocks for pedagogical consistency
Break down large BEGIN/END SOLUTION blocks (45+ lines) into smaller,
unit-testable pieces following the one-concept-per-block principle:

- Module 08: train_epoch (58 lines) -> _process_batch + _optimizer_update + train_epoch
- Module 09: BatchNorm2d.forward (70 lines) -> _validate_input + _get_stats + forward
- Module 11: Embedding (62 lines) -> __init__ + forward; PositionalEncoding (82 lines) -> __init__ + forward
- Module 19: generate_report (108 lines), generate_compliance_report (86 lines),
  _run_accuracy_test (52 lines) each decomposed into helper + composition

Each new helper has its own unit test, all test_module() functions updated,
naming conventions enforced (_prefix for private monkey-patch targets).
2026-02-15 10:37:39 -05:00
Vijay Janapa Reddi
9c1ca3e441 style(tinytorch): standardize formatting and conventions across all 20 modules
Audit and fix consistency issues across all module source files:

- Standardize ML Systems header to "ML Systems Reflection Questions" (01, 13)
- Fix section ordering: test_module before ML Systems in modules 16, 17
- Rename demo_spatial() to demo_convolutions() to match module name (09)
- Rename demo_*_with_profiler() to explore_*_with_profiler() (15, 16, 17)
- Fix test naming to use test_unit_* prefix consistently (03, 05, 11, 12)
- Add missing emojis in test_module/demo patterns (02, 15)
- Standardize tito command format to number-only (01, 03, 06, 07, 18)
- Fix implementation headers: hyphen to colon separator (09, 12)
- Add missing "Where This Code Lives" package section (13)
- Fix export command in module summary (05, 06)
2026-02-15 09:37:25 -05:00
Vijay Janapa Reddi
81cdbba67b refactor(tinytorch): function decomposition, naming conventions, and progressive disclosure
Three categories of changes across 17 modules:

1. Function decomposition (Modules 01,03,05-15,18-19): Break large
   monolithic functions into focused _helper + orchestrator pattern.
   Each helper teaches one concept with its own unit test.

2. Naming convention fixes (Modules 08,09,11,18,19): Ensure underscore
   convention is consistent — standalone _func in export cells renamed
   to func (public API), monkey-patched method names match target
   visibility, removed unnecessary #| export from internal helpers.

3. Progressive disclosure (Modules 02-05,08,11-15): Remove forward
   references to future modules. Replace "you'll learn in Module N"
   with concrete descriptions. Trim connection maps to only show
   current and prior modules. Keep end-of-module "Next" teasers
   as motivational breadcrumbs.

All 17 modified modules pass their test suites.
2026-02-14 16:52:15 -05:00
Vijay Janapa Reddi
99b0eb1387 fix(tinytorch): correct INT8 zero-point values in Module 15 quantization docs
Documentation examples were computed using UINT8 (0-255) zero-point
formula but the code implements signed INT8 (-128 to 127). Fixed all
hardcoded diagram values and docstring examples to match the actual
code output. The code logic was always correct; only the documentation
numbers were wrong.

Fixes: zero-point 88 -> -39, 64 -> -64, 42 -> -43
Fixes: quantized result [-128, 12, 127] -> [-128, -27, 127]
Fixes: dequantize docstring example with correct parameters
Ref: https://github.com/harvard-edge/cs249r_book/issues/1150
2026-02-13 17:06:29 -05:00
Vijay Janapa Reddi
a9c2ba0180 fix(tinytorch): enforce progressive disclosure and move EmbeddingBackward to Module 11
Audit all 20 modules for progressive disclosure violations and fix ~50 issues:

- Module 01: Replace "neural network" framing with "linear transformation" in
  ASCII tables, docstrings, test names, and reflection questions
- Modules 02-04: Remove gradient/neural-network terminology before M06 teaches it
- Module 06: Remove EmbeddingBackward (moved to Module 11 where embeddings are taught)
- Module 11: Add EmbeddingBackward with pedagogical gather/scatter ASCII diagram,
  remove runtime import from autograd, fix "Attention-Ready" forward references
- Modules 12-13: Replace FlashAttention references with generic efficiency language
- Module 17: Fix profiler module number (15 → 14)
- Module 19: Remove Module 20 forward dependency from OlympicEvent
- Module 20: Fix pipeline diagram module numbering and demo ordering

Zero changes to executable logic — all edits target docstrings, comments,
ASCII art, class placement, and test descriptions.
2026-02-13 13:15:24 -05:00
Vijay Janapa Reddi
3947b2defa fix(tinytorch): enforce progressive disclosure across 9 modules
Audit found docstrings/comments revealing concepts from later modules.
All edits are docstring/comment-only — no code, imports, or tests changed.

Module 01: Replace neural network terminology with generic math examples
Module 02: Remove gradient flow references, reframe layer terminology
Module 05: Remove optimizer/backward from pipeline diagrams
Module 06: Replace transformer/embedding references with general patterns
Module 07: Replace embedding/transformer terminology with generic terms
Module 10: Replace detailed embedding analysis with brief Module 11 teaser
Module 13: Fix swapped dependency numbers, trim KV-cache explanation
Module 14: Remove quantization/compression references from docstrings
Module 17: Fix factually wrong Module 18 teaser description

231 tests pass across all modified modules.
2026-02-13 10:00:50 -05:00
Vijay Janapa Reddi
0630674a71 fix(module16): correct sparsity percentage bugs in compression module
- Fix incorrect percentile claim in pruning ASCII diagram (rewrote with
  20 values and correct 50th percentile threshold)
- Fix 7000% sparsity display in demo_compression_with_profiler where
  measure_sparsity() returns percentage (0-100) but code treated it as
  fraction (0-1), causing double multiplication

Closes harvard-edge/cs249r_book#1168
2026-02-11 18:55:12 -05:00
Vijay Janapa Reddi
9d840fc1a0 Merge branch 'dev' into feature/book-volumes 2026-02-10 13:12:34 -05:00
Dang Truong
af23c13999 fix small typo (#1163) 2026-02-06 02:09:30 -05:00
Vijay Janapa Reddi
c1c8c11eec fix(layers): correct Xavier/Glorot initialization terminology
The formula sqrt(1/fan_in) is actually LeCun initialization (1998),
not Xavier/Glorot. True Xavier uses sqrt(2/(fan_in+fan_out)).

- Rename XAVIER_SCALE_FACTOR → INIT_SCALE_FACTOR
- Update all comments to say "LeCun-style initialization"
- Add note explaining difference between LeCun, Xavier, and He init
- Keep the simpler formula for pedagogical clarity

Fixes #1161
2026-02-05 20:11:50 -05:00
Vijay Janapa Reddi
4fb5173503 docs(activations): expand GELU explanation with both approximation forms
Show the exact definition, tanh approximation, and sigmoid approximation
side by side so students understand where 1.702 comes from and why we
chose the sigmoid form. Avoids erf notation in favor of plain-language
description of Φ(x) appropriate for Module 2 students.

Related to harvard-edge/cs249r_book#1154
2026-02-04 08:49:41 -05:00
Vijay Janapa Reddi
c3a9230ea8 fix(activations): correct misleading GELU hint about 1.702 constant
The hint claimed 1.702 comes from √(2/π) ≈ 0.798, which is incorrect.
The 1.702 constant is empirically fitted so that sigmoid(1.702x) ≈ Φ(x),
the Gaussian CDF. The √(2/π) constant appears in the separate tanh-based
GELU approximation, not the sigmoid approximation used here.

Fixes harvard-edge/cs249r_book#1154
2026-02-04 08:42:58 -05:00
Vijay Janapa Reddi
20a4ba2379 fix(attention): correct O(n²) complexity explanation and memory table bug
- Clarify that attention time complexity is O(n²×d), not O(n²), since each
  of the n² query-key pairs requires a d-dimensional dot product
- Fix Total Memory column in analyze_attention_memory_overhead() which was
  duplicating the Optimizer column instead of summing all components
- Update KEY INSIGHT multiplier from 4x to 7x to match corrected total

Fixes harvard-edge/cs249r_book#1150
2026-02-04 08:37:32 -05:00
Vijay Janapa Reddi
0c7509ff35 feat(site): add embedded PDF slide viewer to all module pages
Replace the slide download card with an inline PDF viewer using PDF.js:
- Change grid layout from 4 cards (2x2) to 3 cards in a row
- Add embedded slide viewer with navigation, zoom, and fullscreen
- Load slides from local _static/slides/ for reliable CORS handling
- Add "· AI-generated" subtitle to match audio card pattern
- Use 🔥 icon consistently across all viewers

Affected: 20 module ABOUT.md files + big-picture.md
2026-02-03 12:06:52 -05:00
AndreaMattiaGaravagno
ad94870ed2 docs(slides): specify genai usage (#1149) 2026-02-01 10:01:47 -05:00
Vijay Janapa Reddi
eb84df491e docs(paper): clarify Adam memory overhead + fix LaTeX labels
- Clarify "3× memory" → "3× optimizer-related memory (gradients plus
  two state buffers)" for accuracy
- Fix lstlisting label syntax: label=lst:... → label={lst:...}
- Remove stale figure reference, use prose instead
- Fix convolutions comment: 6 → 7 nested loops (matches actual code)
- Remove unused benchmark_table3.py
2026-01-28 17:19:55 -05:00
Vijay Janapa Reddi
d2b56498b3 Fix convolution FLOPs calculation in profiling example
112 × 112 × 7 × 7 × 3 × 64 × 2 = 236,027,904 (not 235,012,096)
2026-01-27 08:38:18 -05:00
Vijay Janapa Reddi
91a75750df Fix computation errors in module examples
- Convolution: Position(1,1) result 8→7, final output corrected
- Pooling: Fix element lists and average values for bottom windows
- Matrix multiplication: Fix docstring results 13→16, 37→49
- Neural network layer: Fix matmul results in forward pass example

Fixes harvard-edge/cs249r_book#1144
2026-01-27 08:32:27 -05:00
Vijay Janapa Reddi
77baa56d25 Fix convolution example computation error (8 → 7)
Position (1,1) with region [[6,7],[0,1]] and kernel [[1,0],[0,1]]
correctly computes to 6×1 + 7×0 + 0×0 + 1×1 = 7, not 8.

Fixes harvard-edge/cs249r_book#1144
2026-01-27 07:47:31 -05:00
Dang Truong
11deebdc30 fix: fix Softmax forward pass implementation (#1141) 2026-01-26 08:32:03 -05:00
Dang Truong
6eceda4d78 fix: remove unnecessary Sigmoid clipping (#1140) 2026-01-26 08:31:53 -05:00
Dang Truong
6c8d431c18 fix: fix typo and answer render error in Activations module (#1139) 2026-01-26 08:31:40 -05:00
Vijay Janapa Reddi
e17cd3aaa6 feat: add slide deck cards to modules 08 and 20
- Update 08_training and 20_capstone with 2x2 card layout
- Add slide deck download links to GitHub release
- Standardize card order and colors across all modules
2026-01-25 15:08:11 -05:00
Vijay Janapa Reddi
eab5122691 feat: add slide deck cards to all module pages
- Add 4th card (Slide Deck) to 2x2 grid layout on 18 modules
- Host PDFs via GitHub release (tinytorch-slides-v0.1.0)
- Card order: Audio → Binder → Source → Slides
- Colors: Orange (#f97316), Teal (#14b8a6), Sky Blue (#0ea5e9)
2026-01-25 13:21:06 -05:00
Vijay Janapa Reddi
6f8efe8a94 fix: update test assertion for new error message format
The reshape error message was updated to the 3-part educational
pattern, but the integration test was still checking for the old
message text. Updated to use case-insensitive matching.
2026-01-25 11:35:32 -05:00
Vijay Janapa Reddi
0b2c1bfb95 feat: upgrade error messages to 3-part educational pattern
Improve ~43 error messages across 12 modules to follow the
What/Why/Fix pattern (💡🔧) that teaches students at the
moment they hit an error:

-  What failed (with actual tensor shapes/values)
- 💡 Why it failed (conceptual insight)
- 🔧 How to fix (concrete code using their data)

Key improvements:
- Add anticipatory checks for common mistakes (e.g., 3D input
  to Conv2D when 4D expected suggests adding batch dimension)
- Dropout error now explains p is DROP probability, not KEEP
- Shape mismatch errors show both dimensions and suggest fixes
- Abstract method errors provide implementation templates

Modules updated: tensor, layers, dataloader, optimizers,
convolutions, tokenization, embeddings, attention, quantization,
acceleration, memoization, benchmarking

All 20 module tests pass.
2026-01-25 10:28:03 -05:00
Vijay Janapa Reddi
56c05085d0 style(modules): standardize unit test emoji to test tube
Update unit test markers across all 20 modules for consistency:
- Changed header emoji from microscope to test tube
- Maintains visual consistency across module test sections
2026-01-24 19:04:04 -05:00
Vijay Janapa Reddi
f4a748fd78 fix(optimizers,tokenization): set gradients after optimizer init, make vocab_size optional
Module 07 (optimizers):
- Fixed bug where param.grad was set before optimizer creation
- Optimizer.__init__ resets param.grad to None for all parameters
- Moved all gradient assignments to occur AFTER optimizer creation
- Fixes GitHub issue #1131

Module 10 (tokenization):
- Made BPETokenizer.train() vocab_size parameter optional with default None
- Fixes test failure when calling train() without explicit vocab_size

All 20 inline tests pass (verified via tito dev test --inline --ci)
2026-01-24 19:03:50 -05:00
Vijay Janapa Reddi
103545ee23 Merge remote-tracking branch 'origin/dev' into feature/volume-restructure
# Conflicts:
#	.codespell-ignore-words.txt
#	README.md
2026-01-24 18:41:46 -05:00
Vijay Janapa Reddi
2c9b0dccbf fix: restore Conv2dBackward and MaxPool2dBackward for CNN gradient flow
- Restore Conv2dBackward class removed in commit 23c5eb2b5
- Restore MaxPool2dBackward class for pooling gradient routing
- Update Conv2d/MaxPool2d forward() to attach _grad_fn
- Set requires_grad=True on Conv2d weights and bias
- Add enable_autograd() to Module 11 (Embeddings) for progressive disclosure
- Remove skip markers from convolution gradient tests

CNN training now works correctly - conv weights receive gradients and update
during training. All 40 convolution tests pass.
2026-01-24 17:39:11 -05:00
Vijay Janapa Reddi
aafd7a8c67 refactor(modules): standardize formatting and fix NBGrader directives
- Standardize test emoji usage (🔬 for unit tests, 🧪 for module tests)
- Restore missing NBGrader solution blocks in Module 15 (quantization)
- Fix missing #| default_exp directives in modules 05, 12, 13, 15, 17
- Remove duplicate #| default_exp directives
- Ensure all exports are only for core functionality (no tests/demos)
- Apply consistent styling across all 20 modules via module-developer agent
2026-01-24 10:07:18 -05:00
Vijay Janapa Reddi
64444e41d3 Merge remote-tracking branch 'origin/dev' into feature/tinytorch 2026-01-23 14:31:09 -05:00