PROBLEM:
- nbdev requires #| export directive on EACH cell to export when using # %% markers
- Cell markers inside class definitions split classes across multiple cells
- Only partial classes were being exported to tinytorch package
- Missing matmul, arithmetic operations, and activation classes in exports
SOLUTION:
1. Removed # %% cell markers INSIDE class definitions (kept classes as single units)
2. Added #| export to imports cell at top of each module
3. Added #| export before each exportable class definition in all 20 modules
4. Added __call__ method to Sigmoid for functional usage
5. Fixed numpy import (moved to module level from __init__)
MODULES FIXED:
- 01_tensor: Tensor class with all operations (matmul, arithmetic, shape ops)
- 02_activations: Sigmoid, ReLU, Tanh, GELU, Softmax classes
- 03_layers: Linear, Dropout classes
- 04_losses: MSELoss, CrossEntropyLoss, BinaryCrossEntropyLoss classes
- 05_autograd: Function, AddBackward, MulBackward, MatmulBackward, SumBackward
- 06_optimizers: Optimizer, SGD, Adam, AdamW classes
- 07_training: CosineSchedule, Trainer classes
- 08_dataloader: Dataset, TensorDataset, DataLoader classes
- 09_spatial: Conv2d, MaxPool2d, AvgPool2d, SimpleCNN classes
- 10-20: All exportable classes in remaining modules
TESTING:
- Test functions use 'if __name__ == "__main__"' guards
- Tests run in notebooks but NOT on import
- Rosenblatt Perceptron milestone working perfectly
RESULT:
✅ All 20 modules export correctly
✅ Perceptron (1957) milestone functional
✅ Clean separation: development (modules/source) vs package (tinytorch)
- 12_attention: Export scaled_dot_product_attention, MultiHeadAttention only
- 13_transformers: Export TransformerBlock, GPT only
Continues professional selective export pattern across advanced modules.
Clean public APIs for transformer architecture components.
- 09_spatial: Export Conv2d, MaxPool2d, AvgPool2d only
- 10_tokenization: Export Tokenizer, CharTokenizer, BPETokenizer only
- 11_embeddings: Export Embedding, PositionalEncoding only
Continues professional selective export pattern. Clean public APIs,
development utilities remain in development environment.
- 07_training: Export Trainer, CosineSchedule, clip_grad_norm only
- 08_dataloader: Export Dataset, DataLoader, TensorDataset only
Continues professional selective export pattern across all modules.
Development utilities remain in development, clean public API exported.
BREAKING CHANGE: Refactor from whole-module exports to selective function/class exports
**What Changed:**
- Separate development utilities from production exports
- Each function/class gets individual #| export directive
- Clean Prerequisites & Setup sections in all modules
- Development helpers (import_previous_module) not exported
**Module Export Summary:**
- 01_tensor: Tensor class only
- 02_activations: Sigmoid, ReLU, Tanh, GELU, Softmax only
- 03_layers: Linear, Dropout only
- 04_losses: MSELoss, CrossEntropyLoss, BinaryCrossEntropyLoss, log_softmax only
- 05_autograd: Function class only
- 06_optimizers: SGD, Adam, AdamW only
**Benefits:**
✅ Clean public API (matches PyTorch/TensorFlow patterns)
✅ No development utilities in final package
✅ Professional software education standards
✅ Clear separation of concerns
✅ Educational clarity for students
This matches industry standards for educational ML frameworks.
- Add import_previous_module() helper function to all core modules (01-07)
- Standardize cross-module imports for integration testing
- Add clear Prerequisites & Setup sections explaining module dependencies
- Update integration tests to use standardized import pattern
- Maintain clean separation between development and production code
This provides a consistent, educational approach to module integration
while keeping the codebase maintainable and student-friendly.
✨ Major improvements to Module 05: Autograd
- Add complete Jupyter notebook structure with markdown cells
- Enhance all Function classes with detailed mathematical explanations
- Add comprehensive unit tests with proper test patterns
- Improve enable_autograd() with detailed documentation
- Add integration tests for complex computation graphs
- Include educational visualizations and examples
- Follow TinyTorch standards with ⭐⭐ difficulty rating
- All tests pass: Function classes, Tensor autograd, integration scenarios
🎯 Ready for student use with modern PyTorch 2.0 style autograd
- Remove circular imports where modules imported from themselves
- Convert tinytorch.core imports to sys.path relative imports
- Only import dependencies that are actually used in each module
- Preserve documentation imports in markdown cells
- Use consistent relative path pattern across all modules
- Remove hardcoded absolute paths in favor of relative imports
Affected modules: 02_activations, 03_layers, 04_losses, 06_optimizers,
07_training, 09_spatial, 12_attention, 17_quantization
- Remove demonstrate_complex_computation_graph() function from Module 05 (autograd)
- Remove demonstrate_optimizer_integration() function from Module 06 (optimizers)
- Module 04 (losses) had no demonstration functions to remove
- Keep all core implementations and unit test functions intact
- Keep final test_module() function for integration testing
- All module tests continue to pass after cleanup(https://claude.ai/code)
This change ensures tests run immediately when developing modules but don't execute when modules are imported by other modules.
Changes:
- Protected all test executions with if __name__ == "__main__" blocks
- Unit tests run immediately after function definitions during development
- Module integration test (test_module()) runs at end when executed directly
- Updated module-developer.md with new testing patterns and examples
Benefits:
- Students see immediate feedback when developing (python module_dev.py runs all tests)
- Clean imports: later modules can import earlier ones without triggering tests
- Maintains educational flow: tests visible right after implementations
- Compatible with nbgrader and notebook environments
Tested:
- Module 01 runs all tests when executed directly ✓
- Importing Tensor from tensor_dev doesn't run tests ✓
- Cross-module imports work without test interference ✓
Removed complexity from Module 07 (training):
- Removed DemoModel and TestModel classes
- Unified all tests/demos to use single minimal MockModel
- Module now focuses purely on training infrastructure
What remains:
- Trainer class (the core training orchestrator)
- CosineSchedule (learning rate scheduling)
- clip_grad_norm (gradient clipping utility)
- Training loop mechanics and checkpointing
Impact:
- Cleaner, more focused module
- No distraction from model architecture
- Tests training infrastructure, not model building
- All tests still pass with simplified mocks
The module now teaches exactly what it should: how to train
models, not how to build them.
Major changes to module structure:
1. Updated module-developer.md with clear components-only rule
2. Removed Sequential container from Module 03 (layers)
3. Converted to manual layer composition for transparency
Philosophy:
- Modules build ATOMIC COMPONENTS (Tensor, Linear, ReLU, etc.)
- Milestones/Examples show EXPLICIT COMPOSITION
- Students SEE how their components connect
- No hidden abstractions or black boxes
Module 03 changes:
- REMOVED: Sequential class and tests (~200 lines)
- KEPT: Linear and Dropout as individual components
- UPDATED: Integration demos use manual composition
- Result: Students see explicit layer1.forward(x) calls
Module 07 changes:
- Simplified model classes to minimal test fixtures
- Removed complex neural network teaching examples
- Focus purely on training infrastructure
Impact:
- Clearer learning progression
- Students understand each component's role
- Milestones become showcases of student work
- No magic containers hiding the data flow
Removed redundant test calls from all modules:
- Eliminated verbose if __name__ == '__main__': blocks
- Removed duplicate individual test calls
- Each module now simply calls test_module() directly
Changes made to all 9 modules:
- Module 01 (Tensor): Simplified from 16-line main block to 1 line
- Module 02 (Activations): Simplified from 13-line main block to 1 line
- Module 03 (Layers): Simplified from 17-line main block to 1 line
- Module 04 (Losses): Simplified from 20-line main block to 1 line
- Module 05 (Autograd): Simplified from 19-line main block to 1 line
- Module 06 (Optimizers): Simplified from 17-line main block to 1 line
- Module 07 (Training): Simplified from 16-line main block to 1 line
- Module 08 (DataLoader): Simplified from 17-line main block to 1 line
- Module 09 (Spatial): Simplified from 14-line main block to 1 line
Impact:
- Notebook-friendly: Tests run immediately in Jupyter environments
- No redundancy: test_module() already runs all unit tests
- Cleaner code: ~140 lines of redundant code removed
- Better for students: Simpler, more direct execution flow
Wrapped test code in if __name__ == '__main__': guards for:
- Module 02 (activations): 7 test calls protected
- Module 03 (layers): 7 test calls protected
- Module 04 (losses): 10 test calls protected
- Module 05 (autograd): 7 test calls protected
- Module 06 (optimizers): 8 test calls protected
- Module 07 (training): 7 test calls protected
- Module 09 (spatial): 5 test calls protected
Impact:
- All modules can now be imported cleanly without test execution
- Tests still run when modules are executed directly
- Clean dependency chain throughout the framework
- Follows Python best practices for module structure
This completes the fix for the entire module system. Modules can now
properly import from each other without triggering test code execution.
Critical fixes to resolve module import issues:
1. Module 01 (tensor_dev.py):
- Wrapped all test calls in if __name__ == '__main__': guards
- Tests no longer execute during import
- Clean imports now work: from tensor_dev import Tensor
2. Module 08 (dataloader_dev.py):
- REMOVED redefined Tensor class (was breaking dependency chain)
- Now imports real Tensor from Module 01
- DataLoader uses actual Tensor with full gradient support
Impact:
- Modules properly build on previous work (no isolated implementations)
- Clean dependency chain: each module imports from previous modules
- No test execution during imports = fast, clean module loading
This resolves the root cause where DataLoader had to redefine Tensor
because importing tensor_dev.py would execute all test code.
Major refactoring:
- Eliminated Variable class completely from autograd module
- Implemented progressive enhancement pattern with enable_autograd()
- All modules now use pure Tensor with requires_grad=True
- PyTorch 2.0 compatible API throughout
- Clean separation: Module 01 has simple Tensor, Module 05 enhances with gradients
- Fixed all imports and references across layers, activations, losses
- Educational clarity: students learn modern patterns from day one
The system now follows the principle: 'One Tensor class to rule them all'
No more confusion between Variable and Tensor - everything is just Tensor!
Major Accomplishments:
• Rebuilt all 20 modules with comprehensive explanations before each function
• Fixed explanatory placement: detailed explanations before implementations, brief descriptions before tests
• Enhanced all modules with ASCII diagrams for visual learning
• Comprehensive individual module testing and validation
• Created milestone directory structure with working examples
• Fixed critical Module 01 indentation error (methods were outside Tensor class)
Module Status:
✅ Modules 01-07: Fully working (Tensor → Training pipeline)
✅ Milestone 1: Perceptron - ACHIEVED (95% accuracy on 2D data)
✅ Milestone 2: MLP - ACHIEVED (complete training with autograd)
⚠️ Modules 08-20: Mixed results (import dependencies need fixes)
Educational Impact:
• Students can now learn complete ML pipeline from tensors to training
• Clear progression: basic operations → neural networks → optimization
• Explanatory sections provide proper context before implementation
• Working milestones demonstrate practical ML capabilities
Next Steps:
• Fix import dependencies in advanced modules (9, 11, 12, 17-20)
• Debug timeout issues in modules 14, 15
• First 7 modules provide solid foundation for immediate educational use(https://claude.ai/code)
- Add detailed architectural overview of complete GPT system
- Include step-by-step explanations before each component implementation
- Add comprehensive ASCII diagrams showing:
* Complete GPT architecture with embedding + transformer blocks + output head
* Pre-norm transformer block structure with residual connections
* Layer normalization process visualization
* MLP information flow and parameter scaling
* Attention memory complexity and scaling laws
* Autoregressive generation process and causal masking
- Enhance mathematical foundations with visual representations
- Improve systems analysis with memory wall visualization
- Follow MANDATORY pattern: Explanation → Implementation → Test
- Maintain all existing functionality while dramatically improving clarity
- Add context about why transformers revolutionized AI and scaling laws
Following the clean pattern from Modules 01 and 05:
- Removed demonstrate_complete_networks() from Module 03
- Module now focuses ONLY on layer unit tests
- Created tests/integration/test_layers_integration.py for:
* Complete neural network demonstrations
* MLP, CNN-style, and deep network tests
* Cross-module integration validation
Module 03 now clean and focused on teaching layers
Module 04 already clean - no changes needed
Both modules follow consistent unit test pattern
- Replaced complex decorator with 6 manageable incremental steps
- Each step gives immediate feedback and celebrates small wins
- Narrative-driven learning with clear WHY before HOW
- Students build understanding piece by piece instead of all-or-nothing
- Much better pedagogical experience with frequent rewards
- Steps 1-2 working, Step 3 needs minor gradient fix
- Created elegant decorator that enhances pure Tensor with gradient tracking
- add_autograd(Tensor) transforms existing class without breaking changes
- Backward compatibility: all Module 01-04 code works unchanged
- New capabilities: requires_grad=True enables automatic differentiation
- Python metaprogramming education: students learn advanced patterns
- Clean architecture: no contamination of pure mathematical operations
- Module 01: Pure Tensor class - ZERO gradient code, perfect data structure focus
- Modules 02-04: Clean usage of basic Tensor, no hasattr() hacks anywhere
- Removed Parameter wrapper complexity, use direct Tensor operations
- Each module now focuses ONLY on its core teaching concept
- Prepared elegant decorator pattern for Module 05 autograd extension
- Perfect separation of concerns: data structure → operations → enhancement
- Parameter class now works with basic Tensors initially, upgrades to Variables when autograd available
- Loss functions work with basic tensor operations before autograd module
- Each module can now be built and tested sequentially without needing future modules
- Modules 01-04 work with basic Tensors only
- Module 05 introduces autograd, then earlier modules get gradient capabilities
- Restored proper pedagogical flow for incremental learning
- Updated Linear layer to use autograd operations (matmul, add) for proper gradient propagation
- Fixed Parameter class to wrap Variables with requires_grad=True
- Implemented proper MSELoss and CrossEntropyLoss with backward chaining
- Added broadcasting support in autograd operations for bias gradients
- Fixed memoryview errors in gradient data extraction
- All integration tests now pass - neural networks can learn via backpropagation
- Fixed module 03_layers Tensor/Parameter comparison issues
- Fixed module 05_autograd psutil dependency (made optional)
- Removed duplicate 04_networks module
- Created losses.py with MSELoss and CrossEntropyLoss
- Created minimal MNIST training examples
- All 20 modules now pass individual tests
Note: Gradient flow still needs work for full training capability