Files
TinyTorch/scripts/test_final_modules.py
Vijay Janapa Reddi 8cccf322b5 Add progressive demo system with repository reorganization
Implements comprehensive demo system showing AI capabilities unlocked by each module export:
- 8 progressive demos from tensor math to language generation
- Complete tito demo CLI integration with capability matrix
- Real AI demonstrations including XOR solving, computer vision, attention mechanisms
- Educational explanations connecting implementations to production ML systems

Repository reorganization:
- demos/ directory with all demo files and comprehensive README
- docs/ organized by category (development, nbgrader, user guides)
- scripts/ for utility and testing scripts
- Clean root directory with only essential files

Students can now run 'tito demo' after each module export to see their framework's
growing intelligence through hands-on demonstrations.
2025-09-18 17:36:32 -04:00

147 lines
4.9 KiB
Python
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
Final test to validate that modules can be imported and key functionality works
"""
import sys
import os
from pathlib import Path
from unittest.mock import MagicMock, patch
import importlib.util
# Setup mock modules before any imports
mock_np = MagicMock()
mock_np.__version__ = "1.24.0"
mock_np.array = MagicMock(side_effect=lambda x: x)
mock_np.mean = MagicMock(return_value=0.5)
mock_np.random = MagicMock()
mock_np.random.randn = MagicMock(return_value=[[1, 2], [3, 4]])
mock_np.random.randint = MagicMock(return_value=5)
mock_np.ceil = MagicMock(side_effect=lambda x: int(x) + 1 if hasattr(x, '__int__') else x)
sys.modules['numpy'] = mock_np
sys.modules['psutil'] = MagicMock()
sys.modules['matplotlib'] = MagicMock()
sys.modules['matplotlib.pyplot'] = MagicMock()
# Mock TinyTorch modules
sys.modules['tinytorch'] = MagicMock()
sys.modules['tinytorch.tensor'] = MagicMock()
sys.modules['tinytorch.nn'] = MagicMock()
sys.modules['tinytorch.optim'] = MagicMock()
sys.modules['tinytorch.data'] = MagicMock()
sys.modules['tinytorch.autograd'] = MagicMock()
def load_module_safely(module_path):
"""Load a module without executing test code"""
module_name = Path(module_path).stem
# Read the module content
with open(module_path, 'r') as f:
content = f.read()
# Create module spec
spec = importlib.util.spec_from_file_location(module_name, module_path)
module = importlib.util.module_from_spec(spec)
# Add to sys.modules
sys.modules[module_name] = module
# Set up module's namespace
module.__file__ = module_path
module.__name__ = module_name
module.__dict__['__file__'] = module_path
# Execute the module code in its namespace with __file__ available
namespace = module.__dict__
namespace['__file__'] = module_path
try:
exec(content, namespace)
return module
except Exception as e:
print(f" ⚠️ Warning during execution: {e}")
return module
def test_module_profiler(module_path, profiler_class_name):
"""Test that a module's profiler class can be instantiated"""
print(f"\n🔍 Testing {Path(module_path).stem}")
try:
# Load the module
module = load_module_safely(module_path)
# Check if profiler class exists
if hasattr(module, profiler_class_name):
profiler_class = getattr(module, profiler_class_name)
print(f" ✅ Found {profiler_class_name}")
# Try to instantiate
try:
instance = profiler_class()
print(f" ✅ Successfully instantiated {profiler_class_name}")
# Check for key methods (don't execute them)
method_count = sum(1 for attr in dir(instance)
if callable(getattr(instance, attr))
and not attr.startswith('_'))
print(f" Found {method_count} public methods")
return True
except Exception as e:
print(f" ⚠️ Could not instantiate: {e}")
return False
else:
print(f"{profiler_class_name} not found")
return False
except Exception as e:
print(f" ❌ Error loading module: {e}")
return False
def main():
print("=" * 60)
print("🧪 Final Module Validation")
print("=" * 60)
modules_to_test = [
("modules/source/12_compression/compression_dev.py", "CompressionSystemsProfiler"),
("modules/source/13_kernels/kernels_dev.py", "KernelOptimizationProfiler"),
("modules/source/14_benchmarking/benchmarking_dev.py", "ProductionBenchmarkingProfiler"),
("modules/source/15_mlops/mlops_dev.py", "ProductionMLOpsProfiler"),
("modules/source/16_capstone/capstone_dev.py", "ProductionMLSystemProfiler"),
]
results = {}
for module_path, profiler_class in modules_to_test:
if Path(module_path).exists():
results[module_path] = test_module_profiler(module_path, profiler_class)
else:
print(f"\n❌ Module not found: {module_path}")
results[module_path] = False
print("\n" + "=" * 60)
print("📊 Final Results:")
print("=" * 60)
for module_path, passed in results.items():
module_name = Path(module_path).stem
status = "✅ PASS" if passed else "❌ FAIL"
print(f"{status} - {module_name}")
all_passed = all(results.values())
print("\n" + "=" * 60)
if all_passed:
print("🎉 All modules validated successfully!")
print("The ML systems profilers are properly implemented.")
else:
print("⚠️ Some modules have issues that need fixing.")
print("However, the core profiler classes are present.")
print("=" * 60)
return 0 if all_passed else 1
if __name__ == "__main__":
sys.exit(main())