Commit Graph

932 Commits

Author SHA1 Message Date
Vijay Janapa Reddi
ff13efb393 docs(book): Update introduction, TOC, and learning progress from dev branch 2025-10-28 15:35:29 -04:00
Vijay Janapa Reddi
1a638c2498 Merge dev into transformer-training: Add TinyTalks dataset, diagnostic tests, and training improvements 2025-10-28 15:33:37 -04:00
Vijay Janapa Reddi
5bc35376d2 feat(website): Restructure TOC with pedagogically-sound three-tier learning pathway
Reorganized Jupyter Book navigation from scattered sections to coherent ML systems progression:

🏗️ Foundation Tier (01-07): Core systems building blocks
- Tensor, Activations, Layers, Losses, Autograd, Optimizers, Training
- Universal ML computational primitives everyone needs

🧠 Intelligence Tier (08-13): Modern AI algorithms implementation
- DataLoader, Spatial, Tokenization, Embeddings, Attention, Transformers
- Core algorithms that define modern ML systems (not "applications")

 Optimization Tier (14-19): Production systems engineering
- KV-Caching, Profiling, Acceleration, Quantization, Compression, Benchmarking
- Making intelligent algorithms fast, efficient, and scalable

🏅 Capstone Project (20): AI Olympics integration

This mirrors real ML systems engineering roles and builds proper conceptual
understanding for production ML systems work. Students need to understand
the intelligence algorithms before they can optimize them effectively.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-28 15:30:39 -04:00
Vijay Janapa Reddi
a348acf977 fix(package): Add PyTorch-style __call__ methods to exported modules
Resolved transformer training issues by adding __call__ methods to:
- Embedding, PositionalEncoding, EmbeddingLayer (text.embeddings)
- LayerNorm, MLP, TransformerBlock, GPT (models.transformer)
- MultiHeadAttention (core.attention)

This enables PyTorch-style syntax: model(x) instead of model.forward(x)
All transformer diagnostic tests now pass (5/5 ✓)

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-28 13:53:43 -04:00
Vijay Janapa Reddi
ee12c770b6 feat: Add PyTorch-style __call__ methods and update milestone syntax
This commit implements comprehensive PyTorch compatibility improvements:

**Core Changes:**
- Add __call__ methods to all neural network components in modules 11-18
- Enable PyTorch-standard calling syntax: model(input) vs model.forward(input)
- Maintain backward compatibility - forward() methods still work

**Modules Updated:**
- Module 11 (Embeddings): Embedding, PositionalEncoding, EmbeddingLayer
- Module 12 (Attention): MultiHeadAttention
- Module 13 (Transformers): LayerNorm, MLP, TransformerBlock, GPT
- Module 17 (Quantization): QuantizedLinear
- Module 18 (Compression): Linear, Sequential classes

**Milestone Updates:**
- Replace all .forward() calls with direct () calls in milestone examples
- Update transformer milestones (vaswani_shakespeare, tinystories_gpt, tinytalks_gpt)
- Update CNN and MLP milestone examples
- Update MILESTONE_TEMPLATE.py for consistency

**Educational Benefits:**
- Students now write identical syntax to production PyTorch code
- Seamless transition from TinyTorch to PyTorch development
- Industry-standard calling conventions from day one

**Implementation Pattern:**
```python
def __call__(self, *args, **kwargs):
    """Allows the component to be called like a function."""
    return self.forward(*args, **kwargs)
```

All changes maintain full backward compatibility while enabling PyTorch-style usage.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-28 13:46:05 -04:00
Vijay Janapa Reddi
ee47236591 feat(milestones): Add TinyTalks diagnostic features for systematic testing
Major improvements to tinytalks_gpt.py:

1. Level Filtering
   - New --levels flag to train on specific difficulty levels (e.g. --levels 1)
   - Filters dataset by heuristic pattern matching
   - Enables progressive testing: L1 → L1+2 → All

2. Live Prediction Testing
   - test_model_predictions() shows real Q&A during training
   - Tests every 5 epochs + first/last epoch
   - Configurable test prompts based on selected levels

3. Optimized Defaults (~500K params)
   - embed_dim: 128 → 96
   - epochs: 20 → 30
   - batch_size: 32 → 16
   - Based on research for small transformers

4. Better Diagnostics
   - Shows which levels are being trained on
   - Displays filtered dataset size
   - Live feedback shows if model is actually learning

This enables systematic debugging:
- Start with Level 1 only (47 greetings)
- Verify it learns simple Q&A
- Progressively add complexity

Usage:
  # Train on Level 1 only (simplest)
  python tinytalks_gpt.py --levels 1

  # Train on Levels 1 and 2
  python tinytalks_gpt.py --levels 1,2

  # Train on all levels (default)
  python tinytalks_gpt.py
2025-10-28 12:36:06 -04:00
Vijay Janapa Reddi
8338733af2 fix(milestones): Improve tinystories_gpt.py training output frequency
- Changed logging from every 20 batches to every 10 batches
- Show first batch immediately for instant feedback
- Display both current loss and running average
- Format: 'Batch X/500 | Loss: X.XXXX | Avg: X.XXXX'

This provides continuous visual feedback during training so users can
see the model learning in real-time.
2025-10-28 12:21:30 -04:00
Vijay Janapa Reddi
c88da0b031 test(milestones): Add diagnostic script for TinyTalks learning verification
- test_tinytalks_learning.py validates tokenizer functionality
- Checks that Q&A patterns are correctly encoded
- Helps diagnose why model might not be learning
- Confirms vocabulary building and decode/encode cycles

Also removes obsolete TRAINING_FIXED.md documentation.
2025-10-28 12:15:48 -04:00
Vijay Janapa Reddi
c8b700ee9a feat(milestones): Add tinytalks_gpt.py - Transformer training on TinyTalks dataset
- Complete GPT training pipeline for TinyTalks Q&A dataset
- Character-level tokenization using Module 10 (CharTokenizer)
- Configurable architecture (embed_dim, num_layers, num_heads)
- Beautiful Rich UI with progress tracking
- Interactive Q&A demo after training
- Optimized for educational use (fast feedback, clear learning progression)

Training completes in ~20 minutes with visible loss decrease.
Students see their first transformer learn to answer questions.

Usage:
  python milestones/05_2017_transformer/tinytalks_gpt.py [options]

Options:
  --epochs N          Number of training epochs (default: 20)
  --batch-size N      Batch size (default: 32)
  --embed-dim N       Embedding dimension (default: 128)
  --num-layers N      Number of transformer layers (default: 4)
  --num-heads N       Number of attention heads (default: 4)
2025-10-28 12:15:26 -04:00
Vijay Janapa Reddi
c64717188f feat(datasets): Add TinyTalks v1.0 - Educational Q&A dataset for transformer training
- 301 Q&A pairs across 5 progressive difficulty levels
- 17.5 KB total size, optimized for 3-5 minute training
- Includes train/val/test splits (70/15/15)
- Professional documentation (README, DATASHEET, CHANGELOG, SUMMARY)
- Validation and statistics scripts
- Licensed under CC BY 4.0

Dataset designed specifically for TinyTorch Module 13 (Transformers) to provide
immediate learning feedback for students training their first transformer model.
2025-10-28 12:15:04 -04:00
Vijay Janapa Reddi
10b1d040b0 docs: Add comprehensive training fix documentation
Documented the complete debugging process and fixes:

Two Critical Bugs Fixed:
1. Learning Rate: 3e-4 → 1e-2 (optimal for 4.8M params)
2. Computation Graph: Tensor(data.reshape) → tensor.reshape()

Validation Results:
- Single batch: 84.9% improvement (4.84 → 0.73)
- Multi-batch: 38% improvement (2.81 → 1.73) in 3 batches
- All gradients flow correctly
- DataLoader works properly

Status: READY FOR PRODUCTION TRAINING 
2025-10-28 10:47:41 -04:00
Vijay Janapa Reddi
f37f1dc608 fix: Critical bug - preserve computation graph in training loop
CRITICAL BUG FOUND AND FIXED through systematic debugging:

Root Cause:
Training loop was breaking computation graph by creating new Tensors
from .data, preventing gradients from flowing back to model!

Bug Location (both scripts):
  logits_2d = Tensor(logits.data.reshape(...))  #  BREAKS GRAPH!
  targets_1d = Tensor(batch_target.data.reshape(...))

Fix:
  logits_2d = logits.reshape(...)  #  PRESERVES GRAPH!
  targets_1d = batch_target.reshape(...)

Debugging Process:
1. Created comprehensive debug_training.py script
2. Tested 7 aspects systematically:
   - Data alignment 
   - Loss calculation 
   - Gradient computation 
   - Parameter updates 
   - Loss decrease (single batch)  84.9% improvement!
   - Learning rate sensitivity 
   - Multi-batch training  Loss stuck

3. Discovered: Same batch works, different batches don't
4. Root cause: Computation graph broken in training loop

Additional Fix:
Updated learning rate from 3e-4 to 1e-2 (optimal for 4.8M param model)
- Large models (100M+): 3e-4
- Our model (4.8M): 1e-2 (validated by debug script)

This is the SAME bug pattern we fixed in modules earlier - creating
new Tensors from .data breaks the autograd chain!
2025-10-28 10:44:50 -04:00
Vijay Janapa Reddi
829a70face feat: Add TinyStories training as easier alternative to Shakespeare
Created tinystories_gpt.py - simpler training task than Shakespeare:

Why TinyStories is Better:
 Simple vocabulary (~3K words vs ~20K archaic words)
 Clear sentence structure (children's stories)
 Designed specifically for small models (1M-50M params)
 Faster convergence and better results
 Dataset purpose-built for this use case

Changes:
- Created download_tinystories.py to fetch 21MB validation set
- Adapted vaswani_shakespeare.py → tinystories_gpt.py
- Uses TinyStoriesDataset instead of ShakespeareDataset
- Updated all documentation and prompts
- Generation prompt: 'Once upon a time' instead of 'To be or not'

Dataset Stats:
- Size: 21MB validation set (vs 1MB Shakespeare)
- Characters: 22M (20x more data!)
- Words: 4.4M simple words
- Vocabulary: 67 unique characters

Model works with same 4.8M param transformer:
- 6 layers, 8 heads, 256-dim embeddings
- Learning rate: 3e-4 (standard)
- Expected: Much faster learning than Shakespeare

Quick test shows training works correctly with stable loss ~4.9
2025-10-28 10:09:52 -04:00
Vijay Janapa Reddi
228b5793f6 fix: Correct TransformerBlock parameter - pass mlp_ratio not hidden_dim
CRITICAL BUG FIX:
 Before: Passing hidden_dim=1024 as mlp_ratio argument
   Result: hidden_dim = 256 * 1024 = 262,144 neurons!
   Model size: 808M parameters (OUT OF MEMORY!)

 After: Passing mlp_ratio=4 correctly
   Result: hidden_dim = 256 * 4 = 1,024 neurons
   Model size: 4.8M parameters (reasonable!)

The bug was in vaswani_shakespeare.py line 173:
  hidden_dim = embed_dim * 4  # 1024
  block = TransformerBlock(embed_dim, num_heads, hidden_dim)  #  WRONG!

TransformerBlock signature is:
  def __init__(self, embed_dim, num_heads, mlp_ratio=4, ...)

So hidden_dim=1024 was interpreted as mlp_ratio=1024, causing:
  hidden_dim = embed_dim * mlp_ratio = 256 * 1024 = 262,144!

Fix: Pass mlp_ratio=4 directly instead of calculating hidden_dim
2025-10-28 09:52:42 -04:00
Vijay Janapa Reddi
d5161e7191 test: Add simple pattern learning tests for transformer
Created systematic tests to verify transformer learning on simple tasks:

test_05_transformer_simple_patterns.py:
- Test 1: Constant prediction (always predict 5) → 100% 
- Test 2: Copy task (failed due to causal masking) → Expected behavior
- Test 3: Sequence completion ([0,1,2]→[1,2,3]) → 100% 
- Test 4: Pattern repetition ([a,b,a,b,...]) → 100% 

test_05_debug_copy_task.py:
- Explains why copy task fails (causal masking)
- Tests next-token prediction (correct task) → 100% 
- Tests memorization vs generalization → 50% (reasonable)

Key insight: Autoregressive models predict NEXT token, not SAME token.
Position 0 cannot see itself, so "copy" is impossible. The correct
task is next-token prediction: [1,2,3,4]→[2,3,4,5]

These tests prove the transformer architecture works correctly before
attempting full Shakespeare training.
2025-10-28 09:44:39 -04:00
Vijay Janapa Reddi
70b447a469 fix: Add missing typing imports to Module 10 tokenization
Issue: CharTokenizer was failing with NameError: name 'List' is not defined
Root cause: typing imports were not marked with #| export

Fix:
 Added #| export directive to import block in tokenization_dev.py
 Re-exported module using 'tito export 10_tokenization'
 typing.List, Dict, Tuple, Optional, Set now properly exported

Verification:
- CharTokenizer.build_vocab() works 
- encode() and decode() work 
- Tested on Shakespeare sample text 

This fixes the integration with vaswani_shakespeare.py which now properly
uses CharTokenizer from Module 10 instead of manual tokenization.
2025-10-28 09:44:24 -04:00
Vijay Janapa Reddi
69d5621c0c refactor: Use CharTokenizer from Module 10 instead of manual tokenization
Pedagogical improvement - demonstrate using student-built modules:

Changes:
 Added Module 10 to required modules list
 Import CharTokenizer from tinytorch.text.tokenization
 ShakespeareDataset now uses CharTokenizer instead of manual dict
 Updated decode() to use tokenizer.decode()
 Updated documentation to reference Module 10

Why this matters:
- Students built CharTokenizer in Module 10 - they should see it used!
- "Eat your own dog food" - use the modules we teach
- Demonstrates proper module integration in NLP pipeline
- Consistent with pedagogical progression: Module 10 → 11 → 12 → 13

Before (Manual):
  self.char_to_idx = {ch: i for i, ch in enumerate(self.chars)}
  self.data = [self.char_to_idx[ch] for ch in text]

After (Module 10):
  self.tokenizer = CharTokenizer()
  self.tokenizer.build_vocab([text])
  self.data = self.tokenizer.encode(text)

Complete NLP Pipeline Now Used:
- Module 02: Tensor (autograd)
- Module 03: Activations (ReLU, Softmax)
- Module 04: Layers (Linear), Losses (CrossEntropyLoss)
- Module 08: DataLoader, Dataset, Adam optimizer
- Module 10: CharTokenizer ← NOW USED!
- Module 11: Embedding, PositionalEncoding
- Module 12: MultiHeadAttention
- Module 13: LayerNorm, TransformerBlock
2025-10-28 09:40:41 -04:00
Vijay Janapa Reddi
6e28844171 fix: Update transformer config to industry best practices
Critical fixes based on Karpathy's nanoGPT/minGPT and GPT-2 standards:

Phase 1 - Critical Fixes:
 Learning rate: 0.001 → 0.0003 (3e-4, standard for transformers)
   - Previous LR was 3x too high, causing unstable training
   - Industry standard from Vaswani et al. 2017 & GPT-2

 Training steps: 500 → 10,000 (20x increase)
   - Epochs: 5 → 20
   - Max batches: 100 → 500 per epoch
   - Need 5K-10K steps minimum for 2.5M params on 1MB text

Phase 2 - Better Performance:
 Context length: 64 → 128 chars (~10 → 20 words)
   - Shakespeare sentences average 15-20 words
   - Longer context = better coherence

 Model capacity: 500K → 2.5M params (5x increase)
   - embed_dim: 128 → 256
   - num_layers: 4 → 6
   - num_heads: 4 → 8
   - Matches minGPT recommendations for character-level tasks
   - Head dimension: 256/8 = 32 (optimal)

Expected Results:
- Training time: ~45-60 minutes (was: ~10 min)
- Final loss: ~0.8-1.2 (was: ~1.5-2.0)
- Quality: Coherent Shakespeare-style sentences (was: random chars)

Documentation:
- Added CONFIG_ANALYSIS.md: Full comparison to nanoGPT/GPT-2/minGPT
- Added CONFIG_CHANGES.md: Detailed rationale for each change
- Updated docstring: Realistic performance expectations
2025-10-28 09:33:20 -04:00
Vijay Janapa Reddi
b3b819486b test: Add comprehensive transformer learning verification
Created systematic 6-test suite to verify transformer can actually learn:

Test 1 - Forward Pass: 
- Verifies correct output shapes

Test 2 - Loss Computation: 
- Verifies loss is scalar with _grad_fn

Test 3 - Gradient Computation: 
- Verifies ALL 37 parameters receive gradients
- Critical check after gradient flow fixes

Test 4 - Parameter Updates: 
- Verifies optimizer updates ALL 37 parameters
- Ensures no parameters are frozen

Test 5 - Loss Decrease: 
- Verifies loss decreases over 10 steps
- Result: 81.9% improvement

Test 6 - Single Batch Overfit: 
- THE critical test - can model memorize?
- Result: 98.5% improvement (3.71 → 0.06 loss)
- Proves learning capacity

ALL TESTS PASS - Transformer is ready for Shakespeare training!
2025-10-28 09:20:10 -04:00
Vijay Janapa Reddi
fad3f7cc46 chore: Remove temporary documentation files from tests/
Removed files created during debugging:
- tests/regression/GRADIENT_FLOW_TEST_SUMMARY.md (info now in test docstrings)
- tests/debug_posenc.py (temporary debug script)

Test organization is clean:
- Module tests: tests/XX_modulename/
- Integration tests: tests/integration/
- Regression tests: tests/regression/ (gradient flow tests)
- Milestone tests: tests/milestones/
- System tests: tests/system/

All actual test files remain and pass.
2025-10-28 08:40:31 -04:00
Vijay Janapa Reddi
c0b4f22f12 docs: Add gradient flow test suite summary
Summary of comprehensive test coverage:
- 18 tests total (9 regression + 9 NLP component)
- All tests pass 
- Covers modules 01, 02, 03, 05, 10, 11, 12, 13
- Verifies all 37 GPT parameters receive gradients
- Documents test execution and results
2025-10-28 08:35:56 -04:00
Vijay Janapa Reddi
fc4cb76a4c test: Add comprehensive NLP component gradient flow tests
Created exhaustive test suite for all NLP modules:

Module 10 - Tokenization:
- Verified encode/decode functionality
- No gradients needed (preprocessing)

Module 11 - Embeddings:
-  Embedding lookup preserves requires_grad
-  EmbeddingBackward correctly accumulates gradients
-  Sparse gradient updates (only used indices)
-  PositionalEncoding adds positional info
-  Gradients flow through addition

Module 12 - Attention:
-  Scaled dot-product attention: Q, K, V all receive gradients
-  Works with and without causal masking
-  Multi-head attention: ALL projections (Q, K, V, out) receive gradients
-  Reshape and permute operations preserve gradients
-  Batched attention computation works correctly

Module 13 - Transformer:
-  LayerNorm: gamma and beta receive gradients
-  MLP: both linear layers receive gradients
-  TransformerBlock: ALL 10 parameters receive gradients
  - Both LayerNorms (ln1, ln2)
  - All attention projections
  - Both MLP layers
  - Residual connections don't break flow

Full GPT Model:
-  End-to-end gradient flow verified
-  ALL 37 parameters receive gradients
-  Token + position embeddings
-  All transformer blocks
-  Final LayerNorm + LM head

Results: 9/9 tests PASS 
All NLP components have correct gradient flow!
2025-10-28 08:35:20 -04:00
Vijay Janapa Reddi
c97ba799b0 docs: Add comprehensive gradient flow fixes documentation
Documented complete journey of fixing transformer gradient flow:
- All 5 critical fixes with code examples
- Before/after metrics showing 0% → 100% gradient flow
- Key insights and lessons learned
- Testing strategy that caught all issues
- Ready for Phase 2 of transformer validation
2025-10-28 08:24:44 -04:00
Vijay Janapa Reddi
df43b8aedf chore: Remove temporary debug test files
Cleaned up debug files created during gradient flow debugging:
- test_*.py (isolated component tests)
- debug_*.py (gradient flow tracing)
- trace_*.py (transformer block tracing)

All issues are now fixed and verified by:
- tests/milestones/test_05_transformer_architecture.py (Phase 1)
- Actual Shakespeare training milestone running successfully
2025-10-28 08:23:53 -04:00
Vijay Janapa Reddi
6cb37bc406 fix(autograd): Complete transformer gradient flow - ALL PARAMETERS NOW WORK!
Critical fixes to enable full gradient flow through transformer:

1. PermuteBackward:
   - Added general axis permutation backward function
   - Handles multi-dimensional transposes like (0, 2, 1, 3)
   - Fixed MultiHeadAttention breaking graph with np.transpose

2. GELUBackward:
   - Implemented GELU activation gradient
   - Uses tanh approximation derivative formula
   - Patched GELU.forward() in enable_autograd()

3. MultiHeadAttention fixes:
   - Replaced raw np.transpose with permute_axes helper
   - Now attaches PermuteBackward to preserve computation graph
   - Q/K/V projections now receive gradients 

Results:
- Before: 0/21 parameters with gradients (0%)
- After: 21/21 parameters with gradients (100%) 
- Single batch overfit: 4.66 → 0.10 (97.9% improvement!) 
- ALL Phase 1 architecture tests PASS 

Gradient flow verified through:
- Token + Position embeddings 
- LayerNorm (all 3 instances) 
- Multi-Head Attention (Q, K, V, out projections) 
- MLP (both linear layers) 
- LM head 

The transformer architecture is now fully differentiable!
2025-10-28 08:18:20 -04:00
Vijay Janapa Reddi
578b6d7d84 fix(autograd): Add SoftmaxBackward and patch Softmax.forward()
- Implemented SoftmaxBackward with proper gradient formula
- Patched Softmax.forward() in enable_autograd()
- Fixed LayerNorm gamma/beta to have requires_grad=True

Progress:
- Softmax now correctly computes gradients
- LayerNorm parameters initialized with requires_grad
- Still debugging: Q/K/V projections, LayerNorms in blocks, MLP first layer

Current: 9/21 parameters receive gradients (was 0/21)
2025-10-28 08:04:19 -04:00
Vijay Janapa Reddi
ff8702ed33 fix(autograd): Add EmbeddingBackward and ReshapeBackward
Critical fixes for transformer gradient flow:

EmbeddingBackward:
- Implements scatter-add gradient accumulation for embedding lookups
- Added to Module 05 (autograd_dev.py)
- Module 11 imports and uses it in Embedding.forward()
- Gradients now flow back to embedding weights

ReshapeBackward:
- reshape() was breaking computation graph (no _grad_fn)
- Added backward function that reshapes gradient back to original shape
- Patched Tensor.reshape() in enable_autograd()
- Critical for GPT forward pass (logits.reshape before loss)

Results:
- Before: 0/37 parameters receive gradients, loss stuck
- After: 13/37 parameters receive gradients (35%)
- Single batch overfitting: 4.46 → 0.03 (99.4% improvement!)
- MODEL NOW LEARNS! 🎉

Remaining work: 24 parameters still missing gradients (likely attention)

Tests added:
- tests/milestones/test_05_transformer_architecture.py (Phase 1)
- Multiple debug scripts to isolate issues
2025-10-28 07:56:20 -04:00
Vijay Janapa Reddi
471d2afcc0 docs: Add comprehensive gradient flow fix summary
- Documents all 10 commits and fixes
- Explains root cause analysis
- Before/after code examples
- Test coverage details
- Key learnings about computation graph integrity
- 386 lines of detailed documentation
2025-10-27 22:45:07 -04:00
Vijay Janapa Reddi
6733f2d040 test: Move gradient flow tests to proper locations
- Deleted root-level tests/test_gradient_flow.py
- Comprehensive tests now in tests/regression/test_gradient_flow_fixes.py
- Module-specific tests in tests/05_autograd/test_batched_matmul_backward.py
- Better test organization following TinyTorch conventions
2025-10-27 22:41:03 -04:00
Vijay Janapa Reddi
4c93844a6c fix(module-05): Add TransposeBackward and fix MatmulBackward for batched ops
TransposeBackward:
- New backward function for transpose operation
- Patch Tensor.transpose() to track gradients
- Critical for attention (Q @ K.T) gradient flow

MatmulBackward batched fix:
- Change np.dot to np.matmul for batched 3D+ tensors
- Use np.swapaxes instead of .T for proper batched transpose
- Fixes gradient shapes in attention mechanisms

Tests added:
- tests/05_autograd/test_batched_matmul_backward.py (3 tests)
- Updated tests/regression/test_gradient_flow_fixes.py (9 tests total)

All gradient flow issues for transformer training are now resolved!
2025-10-27 20:35:06 -04:00
Vijay Janapa Reddi
c7af13d8c2 fix(milestones): Fix milestone scripts and transformer setup
Milestone 01 (Perceptron):
- Remove TRAINING_AVAILABLE check artifact

Milestone 04 (CNN):
- Fix data_path to correct location (../03_1986_mlp/data/digits_8x8.npz)

Milestone 05 (Transformer):
- Fix project_root calculation
- Change Adam 'learning_rate' arg to 'lr'
- Add positional encoding params to parameters()
- Use CrossEntropyLoss from tinytorch.core.losses
- Use Tensor.reshape() instead of .data extraction
- All params explicitly set requires_grad=True
2025-10-27 20:30:43 -04:00
Vijay Janapa Reddi
a832851b7d fix(module-13): Rewrite LayerNorm to use Tensor operations
- Change from .data extraction to Tensor arithmetic (x - mean, diff * diff, x / std)
- Preserve computation graph through normalization
- std tensor now preserves requires_grad correctly

LayerNorm is used before and after attention in transformer blocks
2025-10-27 20:30:21 -04:00
Vijay Janapa Reddi
4a5c15c7cd fix(module-12): Rewrite attention to use batched Tensor operations
Major rewrite for gradient flow:
- scaled_dot_product_attention: Use Tensor ops (matmul, transpose, softmax)
- MultiHeadAttention: Process all heads in parallel with 4D batched tensors
- No explicit batch loops or .data extraction
- Proper mask broadcasting for (batch * heads) dimension

This is the most complex fix - attention is now fully differentiable end-to-end
2025-10-27 20:30:12 -04:00
Vijay Janapa Reddi
8cff435db9 fix(module-11): Fix Embedding and PositionalEncoding gradient flow
- Embedding.forward() now preserves requires_grad from weight tensor
- PositionalEncoding.forward() uses Tensor addition (x + pos) instead of .data
- Critical for transformer input embeddings to have gradients

Both changes ensure gradient flows from loss back to embedding weights
2025-10-27 20:30:03 -04:00
Vijay Janapa Reddi
fcecbe53d5 fix(module-05): Add SubBackward and DivBackward for autograd
- Implement gradient functions for subtraction and division operations
- Patch Tensor.__sub__ and Tensor.__truediv__ in enable_autograd()
- Required for LayerNorm (x - mean) and (normalized / std) operations

These operations are used extensively in normalization layers
2025-10-27 20:29:54 -04:00
Vijay Janapa Reddi
8c1be08f7c fix(module-03): Rewrite Dropout to use Tensor operations
- Change from x.data * mask to Tensor multiplication (x * mask_tensor * scale)
- Preserves computation graph and gradient flow
- Required for transformer with dropout regularization
2025-10-27 20:29:43 -04:00
Vijay Janapa Reddi
baf572738b fix(module-02): Rewrite Softmax to use Tensor operations
- Preserve computation graph by using Tensor arithmetic (x - x_max, exp / sum)
- No more .data extraction that breaks gradient flow
- Numerically stable with max subtraction before exp

Required for transformer attention softmax gradient flow
2025-10-27 20:29:35 -04:00
Vijay Janapa Reddi
db1f0a21b6 fix(module-01): Fix batched matmul and transpose grad preservation
- Change np.dot to np.matmul for proper batched 3D tensor multiplication
- Add requires_grad preservation in transpose() operation
- Fixes attention mechanism gradient flow issues

Regression tests added in tests/regression/test_gradient_flow_fixes.py
2025-10-27 20:28:53 -04:00
Vijay Janapa Reddi
86f20a3ba7 🎨 Add Rich CLI formatting to transformer milestone 05
Updates to vaswani_shakespeare.py:
- Add Rich console, Panel, Table, and box imports
- Replace all print() statements with console.print() with Rich markup
- Add beautiful Panel.fit() boxes for major sections (Act 1, Systems Analysis, Success)
- Use Rich color tags: [bold], [cyan], [green], [yellow], [dim]
- Format training progress with colored loss values
- Display generated text in green
- Add architectural visualization with Rich panels

Updates to transformers_dev.py:
- Remove all try/except fallback implementations
- Clean imports only (no development scaffolding)
- Use proper module imports from tinytorch package

Milestone now matches the beautiful CLI pattern from cnn_digits.py
2025-10-27 16:51:18 -04:00
Vijay Janapa Reddi
1bfb1cbfe1 Complete transformer module fixes and milestone 05
Module 13 (Transformers) fixes:
- Remove all try/except fallback implementations (clean imports only)
- Fix MultiHeadAttention signature (2 args: x, mask)
- Add GELU() class instance to MLP (not standalone function)
- Clean imports: Tensor, Linear, MultiHeadAttention, Embedding, PositionalEncoding, GELU

Milestone 05 status:
 Architecture test passes
 Model builds successfully (67M parameters)
 Forward pass works
 Shakespeare dataset loads and tokenizes
 DataLoader creates batches properly

Ready for training and text generation
cd /Users/VJ/GitHub/TinyTorch && PYTHONPATH=/Users/VJ/GitHub/TinyTorch: python3 milestones/05_2017_transformer/vaswani_shakespeare.py --test-only --quick-test 2>&1 | tail -15
2025-10-27 16:46:06 -04:00
Vijay Janapa Reddi
8546e3e694 🤖 Fix transformer module exports and milestone 05 imports
Module export fixes:
- Add #|default_exp models.transformer directive to transformers module
- Add imports (MultiHeadAttention, GELU, etc.) to export block
- Export dataloader module (08_dataloader)
- All modules now properly exported to tinytorch package

Milestone 05 fixes:
- Correct import paths (text.embeddings, data.loader, models.transformer)
- Fix Linear.weight vs Linear.weights typo
- Fix indentation in training loop
- Call .forward() explicitly on transformer components

Status: Architecture test mode works, model builds successfully
TODO: Fix TransformerBlock/MultiHeadAttention signature mismatch in module 13
2025-10-27 16:17:55 -04:00
Vijay Janapa Reddi
645ef478a2 Add Shakespeare dataset to DatasetManager
- Add get_shakespeare() method to download tiny-shakespeare.txt
- Downloads from Karpathy's char-rnn repository (1MB corpus)
- Returns raw text for character-level language modeling
- Follows same pattern as MNIST/CIFAR-10 downloads
- Includes test in main() function
2025-10-27 13:03:36 -04:00
Vijay Janapa Reddi
f02fe68973 🔄 Rename milestone 06: mlperf → scaling (2020 GPT-3 era)
- 06_2020_scaling represents the scale crisis that made systems optimization essential
- Covers modules 14-19 (KV-cache through benchmarking)
- Complete decade progression: 1957 → 1969 → 1986 → 1998 → 2017 → 2020
2025-10-27 13:00:30 -04:00
Vijay Janapa Reddi
c4d5e4ebf8 🏗️ Restructure milestones with decade-based naming
- Rename to clean, focused convention: 01_1957_perceptron, 02_1969_xor, etc.
- Drop dramatic language (crisis, revival, revolution, era)
- 06_2018_mlperf → 06_2020_scaling (matches GPT-3 scale era)
- Tells clear story: 1950s → 2020s ML evolution
- Each milestone represents major architectural/systems shift
- Remove redundant step1/2/3 files from transformer milestone
2025-10-27 13:00:06 -04:00
Vijay Janapa Reddi
0ae627d0ea Clean root directory: remove debug scripts, status files, and redundant docs 2025-10-26 19:03:15 -04:00
Vijay Janapa Reddi
f1ae1728c6 🧹 Remove book/_build/ artifacts from git tracking
- Added book/_build/ to .gitignore
- Removed 540 auto-generated Jupyter Book build files from tracking
- Files remain locally for viewing but won't be committed anymore
- Reduces repo size and prevents merge conflicts on generated files
2025-10-25 17:37:43 -04:00
Vijay Janapa Reddi
59bbf7f93e 🧹 Remove git-rewrite temporary files 2025-10-25 17:36:10 -04:00
Vijay Janapa Reddi
f9449ee3d4 Merge remote dev branch with local website updates 2025-10-25 17:35:34 -04:00
Vijay Janapa Reddi
9982d7c4d8 🧹 Clean up book files
- Remove command-reference.md (consolidated into tito-essentials)
- Update resources.md and testing-framework.md
2025-10-25 17:31:08 -04:00
Vijay Janapa Reddi
019c8ba815 🧹 Clean up git-rewrite temporary files 2025-10-25 17:27:20 -04:00