mirror of
https://github.com/MLSysBook/TinyTorch.git
synced 2026-03-11 18:33:34 -05:00
Fix module test execution pattern with if __name__ == '__main__' guards
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 ✓
This commit is contained in:
@@ -345,6 +345,9 @@ def test_unit_optimizer_base():
|
||||
|
||||
print("✅ Base Optimizer works correctly!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_unit_optimizer_base()
|
||||
|
||||
# %% [markdown]
|
||||
"""
|
||||
## SGD - Stochastic Gradient Descent
|
||||
@@ -560,6 +563,9 @@ def test_unit_sgd_optimizer():
|
||||
|
||||
print("✅ SGD optimizer works correctly!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_unit_sgd_optimizer()
|
||||
|
||||
# %% [markdown]
|
||||
"""
|
||||
## Adam - Adaptive Moment Estimation
|
||||
@@ -803,6 +809,9 @@ def test_unit_adam_optimizer():
|
||||
|
||||
print("✅ Adam optimizer works correctly!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_unit_adam_optimizer()
|
||||
|
||||
# %% [markdown]
|
||||
"""
|
||||
## AdamW - Adam with Decoupled Weight Decay
|
||||
@@ -1039,6 +1048,9 @@ def test_unit_adamw_optimizer():
|
||||
|
||||
print("✅ AdamW optimizer works correctly!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_unit_adamw_optimizer()
|
||||
|
||||
# %% [markdown]
|
||||
"""
|
||||
## 4. Integration: Bringing It Together
|
||||
@@ -1409,7 +1421,8 @@ def test_module():
|
||||
|
||||
# %%
|
||||
# Run comprehensive module test
|
||||
test_module()
|
||||
if __name__ == "__main__":
|
||||
test_module()
|
||||
|
||||
# %% [markdown]
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user