mirror of
https://github.com/MLSysBook/TinyTorch.git
synced 2025-12-05 19:17:52 -06:00
Update test files with progressive integration and checkpoint improvements
This commit is contained in:
@@ -14,7 +14,7 @@ from pathlib import Path
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
|
||||
|
||||
|
||||
class TestModule01Still Working:
|
||||
class TestModule01StillWorking:
|
||||
"""Verify Module 01 (Setup) functionality is still intact."""
|
||||
|
||||
def test_setup_environment_stable(self):
|
||||
|
||||
@@ -14,7 +14,7 @@ setup_integration_test()
|
||||
|
||||
# Import ONLY from TinyTorch package
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.dense import Sequential, create_mlp, MLP
|
||||
from tinytorch.core.activations import ReLU, Sigmoid, Tanh
|
||||
|
||||
@@ -25,8 +25,8 @@ class TestLayersDenseNetworkInterface:
|
||||
def test_dense_layer_to_sequential_network(self):
|
||||
"""Test that Dense layers can be integrated into Sequential networks."""
|
||||
# Create individual dense layers
|
||||
layer1 = Dense(input_size=4, output_size=8)
|
||||
layer2 = Dense(input_size=8, output_size=3)
|
||||
layer1 = Linear(input_size=4, output_size=8)
|
||||
layer2 = Linear(input_size=8, output_size=3)
|
||||
|
||||
# Test integration into Sequential
|
||||
network = Sequential([layer1, ReLU(), layer2])
|
||||
@@ -42,7 +42,7 @@ class TestLayersDenseNetworkInterface:
|
||||
def test_dense_layer_compatibility_with_mlp(self):
|
||||
"""Test that Dense layers are compatible with MLP construction."""
|
||||
# Test that MLP uses same interface as individual Dense layers
|
||||
individual_layer = Dense(input_size=6, output_size=10)
|
||||
individual_layer = Linear(input_size=6, output_size=10)
|
||||
mlp_network = create_mlp(input_size=6, hidden_sizes=[10], output_size=3)
|
||||
|
||||
# Test same input works with both
|
||||
@@ -63,13 +63,13 @@ class TestLayersDenseNetworkInterface:
|
||||
def test_layer_output_as_network_input(self):
|
||||
"""Test that Dense layer output can be used as network input."""
|
||||
# Create preprocessing layer
|
||||
preprocessor = Dense(input_size=5, output_size=8)
|
||||
preprocessor = Linear(input_size=5, output_size=8)
|
||||
|
||||
# Create network that processes preprocessor output
|
||||
network = Sequential([
|
||||
Dense(input_size=8, output_size=12),
|
||||
Linear(input_size=8, output_size=12),
|
||||
ReLU(),
|
||||
Dense(input_size=12, output_size=4)
|
||||
Linear(input_size=12, output_size=4)
|
||||
])
|
||||
|
||||
# Test pipeline: input → layer → network
|
||||
@@ -88,7 +88,7 @@ class TestLayersDenseNetworkInterface:
|
||||
base_network = create_mlp(input_size=4, hidden_sizes=[6], output_size=8)
|
||||
|
||||
# Add additional processing layer
|
||||
final_layer = Dense(input_size=8, output_size=2)
|
||||
final_layer = Linear(input_size=8, output_size=2)
|
||||
|
||||
# Test composition
|
||||
x = Tensor(np.random.randn(2, 4))
|
||||
@@ -117,11 +117,11 @@ class TestLayerNetworkDataFlow:
|
||||
|
||||
for batch_size, input_size, hidden_size, output_size in shape_configs:
|
||||
# Create layer and network
|
||||
layer = Dense(input_size=input_size, output_size=hidden_size)
|
||||
layer = Linear(input_size=input_size, output_size=hidden_size)
|
||||
network = Sequential([
|
||||
Dense(input_size=hidden_size, output_size=hidden_size),
|
||||
Linear(input_size=hidden_size, output_size=hidden_size),
|
||||
ReLU(),
|
||||
Dense(input_size=hidden_size, output_size=output_size)
|
||||
Linear(input_size=hidden_size, output_size=output_size)
|
||||
])
|
||||
|
||||
# Test data flow
|
||||
@@ -136,7 +136,7 @@ class TestLayerNetworkDataFlow:
|
||||
def test_dtype_preservation_across_layer_network_boundary(self):
|
||||
"""Test data type preservation across layer-network boundaries."""
|
||||
# Test float32 flow
|
||||
layer_f32 = Dense(input_size=4, output_size=6)
|
||||
layer_f32 = Linear(input_size=4, output_size=6)
|
||||
network_f32 = create_mlp(input_size=6, hidden_sizes=[8], output_size=2)
|
||||
|
||||
x_f32 = Tensor(np.random.randn(2, 4).astype(np.float32))
|
||||
@@ -148,7 +148,7 @@ class TestLayerNetworkDataFlow:
|
||||
assert network_out_f32.dtype == np.float32, "Network should preserve float32 from layer"
|
||||
|
||||
# Test float64 flow
|
||||
layer_f64 = Dense(input_size=4, output_size=6)
|
||||
layer_f64 = Linear(input_size=4, output_size=6)
|
||||
network_f64 = create_mlp(input_size=6, hidden_sizes=[8], output_size=2)
|
||||
|
||||
x_f64 = Tensor(np.random.randn(2, 4).astype(np.float64))
|
||||
@@ -162,8 +162,8 @@ class TestLayerNetworkDataFlow:
|
||||
def test_error_handling_at_layer_network_boundary(self):
|
||||
"""Test error handling when layer-network interfaces are incompatible."""
|
||||
# Create mismatched layer and network
|
||||
layer = Dense(input_size=4, output_size=6)
|
||||
mismatched_network = Sequential([Dense(input_size=8, output_size=2)]) # Expects 8, gets 6
|
||||
layer = Linear(input_size=4, output_size=6)
|
||||
mismatched_network = Sequential([Linear(input_size=8, output_size=2)]) # Expects 8, gets 6
|
||||
|
||||
x = Tensor(np.random.randn(1, 4))
|
||||
layer_output = layer(x) # Shape (1, 6)
|
||||
@@ -183,17 +183,17 @@ class TestLayerNetworkSystemIntegration:
|
||||
def test_multi_stage_processing_pipeline(self):
|
||||
"""Test multi-stage processing using layers and networks."""
|
||||
# Stage 1: Preprocessing layer
|
||||
preprocessor = Dense(input_size=8, output_size=12)
|
||||
preprocessor = Linear(input_size=8, output_size=12)
|
||||
|
||||
# Stage 2: Feature extraction network
|
||||
feature_extractor = Sequential([
|
||||
Dense(input_size=12, output_size=16),
|
||||
Linear(input_size=12, output_size=16),
|
||||
ReLU(),
|
||||
Dense(input_size=16, output_size=10)
|
||||
Linear(input_size=16, output_size=10)
|
||||
])
|
||||
|
||||
# Stage 3: Classification layer
|
||||
classifier = Dense(input_size=10, output_size=3)
|
||||
classifier = Linear(input_size=10, output_size=3)
|
||||
|
||||
# Test complete pipeline
|
||||
x = Tensor(np.random.randn(4, 8))
|
||||
@@ -211,15 +211,15 @@ class TestLayerNetworkSystemIntegration:
|
||||
def test_parallel_layer_processing(self):
|
||||
"""Test parallel processing with multiple layers feeding into network."""
|
||||
# Create parallel processing layers
|
||||
branch1 = Dense(input_size=6, output_size=4)
|
||||
branch2 = Dense(input_size=6, output_size=4)
|
||||
branch3 = Dense(input_size=6, output_size=4)
|
||||
branch1 = Linear(input_size=6, output_size=4)
|
||||
branch2 = Linear(input_size=6, output_size=4)
|
||||
branch3 = Linear(input_size=6, output_size=4)
|
||||
|
||||
# Fusion network
|
||||
fusion_network = Sequential([
|
||||
Dense(input_size=12, output_size=8), # 4+4+4=12 from parallel branches
|
||||
Linear(input_size=12, output_size=8), # 4+4+4=12 from parallel branches
|
||||
ReLU(),
|
||||
Dense(input_size=8, output_size=2)
|
||||
Linear(input_size=8, output_size=2)
|
||||
])
|
||||
|
||||
# Test parallel processing
|
||||
@@ -249,18 +249,18 @@ class TestLayerNetworkSystemIntegration:
|
||||
"""Test that layers and networks can be replaced modularly."""
|
||||
# Create modular components
|
||||
input_processors = [
|
||||
Dense(input_size=5, output_size=8),
|
||||
Dense(input_size=5, output_size=8), # Different instance
|
||||
Linear(input_size=5, output_size=8),
|
||||
Linear(input_size=5, output_size=8), # Different instance
|
||||
]
|
||||
|
||||
core_networks = [
|
||||
create_mlp(input_size=8, hidden_sizes=[10], output_size=6),
|
||||
Sequential([Dense(input_size=8, output_size=6)]), # Different architecture
|
||||
Sequential([Linear(input_size=8, output_size=6)]), # Different architecture
|
||||
]
|
||||
|
||||
output_processors = [
|
||||
Dense(input_size=6, output_size=3),
|
||||
Dense(input_size=6, output_size=3), # Different instance
|
||||
Linear(input_size=6, output_size=3),
|
||||
Linear(input_size=6, output_size=3), # Different instance
|
||||
]
|
||||
|
||||
# Test all combinations work
|
||||
@@ -286,8 +286,8 @@ class TestLayerNetworkInterfaceStandards:
|
||||
"""Test that layers and networks have consistent callable interface."""
|
||||
# Create different components
|
||||
components = [
|
||||
Dense(input_size=4, output_size=6),
|
||||
Sequential([Dense(input_size=4, output_size=6)]),
|
||||
Linear(input_size=4, output_size=6),
|
||||
Sequential([Linear(input_size=4, output_size=6)]),
|
||||
create_mlp(input_size=4, hidden_sizes=[8], output_size=6),
|
||||
MLP([4, 8, 6])
|
||||
]
|
||||
@@ -307,8 +307,8 @@ class TestLayerNetworkInterfaceStandards:
|
||||
def test_component_property_consistency(self):
|
||||
"""Test that layers and networks have consistent properties."""
|
||||
# Create components
|
||||
layer = Dense(input_size=3, output_size=5)
|
||||
network = Sequential([Dense(input_size=3, output_size=5)])
|
||||
layer = Linear(input_size=3, output_size=5)
|
||||
network = Sequential([Linear(input_size=3, output_size=5)])
|
||||
mlp = create_mlp(input_size=3, hidden_sizes=[], output_size=5)
|
||||
|
||||
# Test that all components can be used interchangeably
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""
|
||||
Module 04: Progressive Integration Tests
|
||||
Tests that Module 05 (Dense/Networks) works correctly AND that the entire foundation stack works.
|
||||
Tests that Module 05 (Linear/Networks) works correctly AND that the entire foundation stack works.
|
||||
|
||||
DEPENDENCY CHAIN: 01_setup → 02_tensor → 03_activations → 04_layers → 05_dense
|
||||
This is the FOUNDATION MILESTONE - everything should work together for neural networks!
|
||||
@@ -81,41 +81,41 @@ class TestEntireFoundationStack:
|
||||
assert True, "Layer foundation not implemented yet"
|
||||
|
||||
|
||||
class TestDenseNetworkCapability:
|
||||
class TestLinearNetworkCapability:
|
||||
"""Test that Module 05 enables full neural network capability."""
|
||||
|
||||
def test_dense_layer_creation(self):
|
||||
"""Test Dense layer works with the foundation."""
|
||||
"""Test Linear layer works with the foundation."""
|
||||
try:
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Create Dense layer
|
||||
layer = Dense(10, 5)
|
||||
# Create Linear layer
|
||||
layer = Linear(10, 5)
|
||||
|
||||
# Should have proper weights and bias
|
||||
assert hasattr(layer, 'weight'), "Dense broken: No weights"
|
||||
assert layer.weight.shape == (10, 5), "Dense broken: Wrong weight shape"
|
||||
assert hasattr(layer, 'weight'), "Linear broken: No weights"
|
||||
assert layer.weight.shape == (10, 5), "Linear broken: Wrong weight shape"
|
||||
|
||||
# Should work with tensor input
|
||||
x = Tensor(np.random.randn(32, 10))
|
||||
output = layer(x)
|
||||
|
||||
assert output.shape == (32, 5), "Dense broken: Wrong output shape"
|
||||
assert output.shape == (32, 5), "Linear broken: Wrong output shape"
|
||||
|
||||
except ImportError:
|
||||
assert True, "Dense layer not implemented yet"
|
||||
assert True, "Linear layer not implemented yet"
|
||||
|
||||
def test_multi_layer_network(self):
|
||||
"""Test building multi-layer networks."""
|
||||
try:
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Sigmoid
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Build 3-layer network for XOR
|
||||
hidden = Dense(2, 4, bias=True)
|
||||
output = Dense(4, 1, bias=True)
|
||||
hidden = Linear(2, 4, bias=True)
|
||||
output = Linear(4, 1, bias=True)
|
||||
relu = ReLU()
|
||||
sigmoid = Sigmoid()
|
||||
|
||||
@@ -123,7 +123,7 @@ class TestDenseNetworkCapability:
|
||||
X = Tensor(np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=np.float32))
|
||||
|
||||
# Forward pass through complete network
|
||||
h = hidden(X) # Dense transformation
|
||||
h = hidden(X) # Linear transformation
|
||||
h_act = relu(h) # Non-linear activation
|
||||
out = output(h_act) # Output transformation
|
||||
predictions = sigmoid(out) # Output activation
|
||||
@@ -142,7 +142,7 @@ class TestXORProblemSolution:
|
||||
def test_xor_network_architecture(self):
|
||||
"""Test XOR network architecture is possible."""
|
||||
try:
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Sigmoid
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
@@ -151,8 +151,8 @@ class TestXORProblemSolution:
|
||||
y_target = np.array([[0], [1], [1], [0]], dtype=np.float32)
|
||||
|
||||
# Network: 2 -> 4 -> 1 (sufficient for XOR)
|
||||
hidden = Dense(2, 4, bias=True)
|
||||
output = Dense(4, 1, bias=True)
|
||||
hidden = Linear(2, 4, bias=True)
|
||||
output = Linear(4, 1, bias=True)
|
||||
relu = ReLU()
|
||||
sigmoid = Sigmoid()
|
||||
|
||||
@@ -179,14 +179,14 @@ class TestXORProblemSolution:
|
||||
def test_nonlinear_problem_solvability(self):
|
||||
"""Test that non-linear problems are now solvable."""
|
||||
try:
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Create network that can solve non-linear problems
|
||||
layer1 = Dense(2, 10)
|
||||
layer1 = Linear(2, 10)
|
||||
relu = ReLU()
|
||||
layer2 = Dense(10, 1)
|
||||
layer2 = Linear(10, 1)
|
||||
|
||||
# Test various non-linear patterns
|
||||
patterns = [
|
||||
@@ -214,14 +214,14 @@ class TestFoundationMilestoneReadiness:
|
||||
def test_mnist_mlp_architecture_possible(self):
|
||||
"""Test we can build MNIST MLP architecture."""
|
||||
try:
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Softmax
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# MNIST MLP: 784 -> 128 -> 64 -> 10
|
||||
layer1 = Dense(784, 128)
|
||||
layer2 = Dense(128, 64)
|
||||
layer3 = Dense(64, 10)
|
||||
layer1 = Linear(784, 128)
|
||||
layer2 = Linear(128, 64)
|
||||
layer3 = Linear(64, 10)
|
||||
relu = ReLU()
|
||||
softmax = Softmax()
|
||||
|
||||
@@ -246,13 +246,13 @@ class TestFoundationMilestoneReadiness:
|
||||
def test_classification_capability(self):
|
||||
"""Test basic classification capability."""
|
||||
try:
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Softmax
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Simple classifier: features -> hidden -> classes
|
||||
feature_layer = Dense(20, 10)
|
||||
classifier = Dense(10, 3) # 3 classes
|
||||
feature_layer = Linear(20, 10)
|
||||
classifier = Linear(10, 3) # 3 classes
|
||||
relu = ReLU()
|
||||
softmax = Softmax()
|
||||
|
||||
@@ -280,7 +280,7 @@ class TestCompleteStackValidation:
|
||||
"""Test complete neural network from scratch."""
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Sigmoid
|
||||
|
||||
# End-to-end test: Build and run a complete neural network
|
||||
@@ -290,9 +290,9 @@ class TestCompleteStackValidation:
|
||||
X = Tensor(np.random.randn(10, 5))
|
||||
|
||||
# 2. Network architecture (from layers foundation)
|
||||
layer1 = Dense(5, 8)
|
||||
layer2 = Dense(8, 3)
|
||||
layer3 = Dense(3, 1)
|
||||
layer1 = Linear(5, 8)
|
||||
layer2 = Linear(8, 3)
|
||||
layer3 = Linear(3, 1)
|
||||
|
||||
# 3. Activations (from activation foundation)
|
||||
relu = ReLU()
|
||||
@@ -320,13 +320,13 @@ class TestCompleteStackValidation:
|
||||
"""Test foundation remains stable under computational load."""
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
|
||||
# Stress test: Large network
|
||||
layer1 = Dense(100, 200)
|
||||
layer2 = Dense(200, 100)
|
||||
layer3 = Dense(100, 10)
|
||||
layer1 = Linear(100, 200)
|
||||
layer2 = Linear(200, 100)
|
||||
layer3 = Linear(100, 10)
|
||||
relu = ReLU()
|
||||
|
||||
# Large batch
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
"""
|
||||
Integration test for Module 04: Dense
|
||||
Integration test for Module 04: Linear
|
||||
|
||||
Validates that the dense module integrates correctly with the TinyTorch package.
|
||||
This is a quick validation test, not a comprehensive capability test.
|
||||
@@ -26,13 +26,13 @@ def test_dense_module_integration():
|
||||
}
|
||||
|
||||
try:
|
||||
# Test 1: Dense networks import from package
|
||||
# Test 1: Linear networks import from package
|
||||
try:
|
||||
from tinytorch.core.dense import MLP, DenseNetwork
|
||||
from tinytorch.core.dense import MLP, LinearNetwork
|
||||
results["tests"].append({
|
||||
"name": "dense_import",
|
||||
"status": "✅ PASS",
|
||||
"description": "Dense network classes import from package"
|
||||
"description": "Linear network classes import from package"
|
||||
})
|
||||
except ImportError as e:
|
||||
# Try alternative imports
|
||||
@@ -41,34 +41,34 @@ def test_dense_module_integration():
|
||||
results["tests"].append({
|
||||
"name": "dense_import",
|
||||
"status": "✅ PASS",
|
||||
"description": "Dense networks import from alternative location"
|
||||
"description": "Linear networks import from alternative location"
|
||||
})
|
||||
except ImportError:
|
||||
results["tests"].append({
|
||||
"name": "dense_import",
|
||||
"status": "❌ FAIL",
|
||||
"description": f"Dense import failed: {e}"
|
||||
"description": f"Linear import failed: {e}"
|
||||
})
|
||||
results["success"] = False
|
||||
results["errors"].append(f"Dense import error: {e}")
|
||||
results["errors"].append(f"Linear import error: {e}")
|
||||
return results
|
||||
|
||||
# Test 2: Dense network instantiation
|
||||
# Test 2: Linear network instantiation
|
||||
try:
|
||||
mlp = MLP(input_size=4, hidden_sizes=[8, 4], output_size=2)
|
||||
results["tests"].append({
|
||||
"name": "dense_creation",
|
||||
"status": "✅ PASS",
|
||||
"description": "Dense networks can be instantiated"
|
||||
"description": "Linear networks can be instantiated"
|
||||
})
|
||||
except Exception as e:
|
||||
results["tests"].append({
|
||||
"name": "dense_creation",
|
||||
"status": "❌ FAIL",
|
||||
"description": f"Dense creation failed: {e}"
|
||||
"description": f"Linear creation failed: {e}"
|
||||
})
|
||||
results["success"] = False
|
||||
results["errors"].append(f"Dense creation error: {e}")
|
||||
results["errors"].append(f"Linear creation error: {e}")
|
||||
return results
|
||||
|
||||
# Test 3: Integration with previous modules
|
||||
@@ -86,7 +86,7 @@ def test_dense_module_integration():
|
||||
results["tests"].append({
|
||||
"name": "module_integration",
|
||||
"status": "✅ PASS",
|
||||
"description": "Dense networks work with previous modules"
|
||||
"description": "Linear networks work with previous modules"
|
||||
})
|
||||
except ImportError as e:
|
||||
results["tests"].append({
|
||||
@@ -111,7 +111,7 @@ def test_dense_module_integration():
|
||||
results["tests"].append({
|
||||
"name": "network_structure",
|
||||
"status": "✅ PASS",
|
||||
"description": "Dense network has proper layer structure"
|
||||
"description": "Linear network has proper layer structure"
|
||||
})
|
||||
except Exception as e:
|
||||
results["tests"].append({
|
||||
@@ -135,7 +135,7 @@ def test_dense_module_integration():
|
||||
results["tests"].append({
|
||||
"name": "required_methods",
|
||||
"status": "✅ PASS",
|
||||
"description": "Dense network has all required methods"
|
||||
"description": "Linear network has all required methods"
|
||||
})
|
||||
else:
|
||||
results["tests"].append({
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
"""
|
||||
Tests for Module 04: Dense/Networks
|
||||
Tests for Module 04: Linear/Networks
|
||||
"""
|
||||
|
||||
import pytest
|
||||
@@ -11,30 +11,30 @@ from pathlib import Path
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
|
||||
|
||||
|
||||
class TestDenseExports:
|
||||
"""Test that Dense layer is properly exported."""
|
||||
class TestLinearExports:
|
||||
"""Test that Linear layer is properly exported."""
|
||||
|
||||
def test_dense_import(self):
|
||||
"""Test Dense can be imported from correct location."""
|
||||
from tinytorch.core.layers import Dense
|
||||
assert Dense is not None
|
||||
"""Test Linear can be imported from correct location."""
|
||||
from tinytorch.core.layers import Linear
|
||||
assert Linear is not None
|
||||
|
||||
def test_dense_creation(self):
|
||||
"""Test Dense layer can be created."""
|
||||
from tinytorch.core.layers import Dense
|
||||
layer = Dense(10, 5)
|
||||
"""Test Linear layer can be created."""
|
||||
from tinytorch.core.layers import Linear
|
||||
layer = Linear(10, 5)
|
||||
assert layer.weight.shape == (10, 5)
|
||||
|
||||
|
||||
class TestDenseForward:
|
||||
"""Test Dense layer forward pass."""
|
||||
class TestLinearForward:
|
||||
"""Test Linear layer forward pass."""
|
||||
|
||||
def test_forward_shape(self):
|
||||
"""Test output shape is correct."""
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
layer = Dense(10, 5)
|
||||
layer = Linear(10, 5)
|
||||
x = Tensor(np.random.randn(32, 10))
|
||||
output = layer(x)
|
||||
|
||||
@@ -42,10 +42,10 @@ class TestDenseForward:
|
||||
|
||||
def test_forward_with_bias(self):
|
||||
"""Test forward pass with bias."""
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
layer = Dense(10, 5, bias=True)
|
||||
layer = Linear(10, 5, bias=True)
|
||||
x = Tensor(np.zeros((1, 10)))
|
||||
output = layer(x)
|
||||
|
||||
@@ -54,10 +54,10 @@ class TestDenseForward:
|
||||
|
||||
def test_forward_without_bias(self):
|
||||
"""Test forward pass without bias."""
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
layer = Dense(10, 5, bias=False)
|
||||
layer = Linear(10, 5, bias=False)
|
||||
x = Tensor(np.zeros((1, 10)))
|
||||
output = layer(x)
|
||||
|
||||
@@ -65,15 +65,15 @@ class TestDenseForward:
|
||||
assert np.allclose(output.data, 0)
|
||||
|
||||
|
||||
class TestDenseIntegration:
|
||||
"""Test Dense layer integration with other modules."""
|
||||
class TestLinearIntegration:
|
||||
"""Test Linear layer integration with other modules."""
|
||||
|
||||
def test_dense_with_tensor(self):
|
||||
"""Test Dense works with Tensor (Module 02)."""
|
||||
from tinytorch.core.layers import Dense
|
||||
"""Test Linear works with Tensor (Module 02)."""
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
layer = Dense(10, 5)
|
||||
layer = Linear(10, 5)
|
||||
|
||||
# Weights and bias should be Tensors
|
||||
assert isinstance(layer.weight, Tensor)
|
||||
@@ -81,12 +81,12 @@ class TestDenseIntegration:
|
||||
assert isinstance(layer.bias, Tensor)
|
||||
|
||||
def test_dense_with_activations(self):
|
||||
"""Test Dense works with activations (Module 03)."""
|
||||
from tinytorch.core.layers import Dense
|
||||
"""Test Linear works with activations (Module 03)."""
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Sigmoid
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
layer = Dense(10, 5)
|
||||
layer = Linear(10, 5)
|
||||
relu = ReLU()
|
||||
sigmoid = Sigmoid()
|
||||
|
||||
@@ -100,13 +100,13 @@ class TestDenseIntegration:
|
||||
assert np.all(h_sigmoid.data >= 0) and np.all(h_sigmoid.data <= 1)
|
||||
|
||||
def test_dense_chain(self):
|
||||
"""Test chaining multiple Dense layers."""
|
||||
from tinytorch.core.layers import Dense
|
||||
"""Test chaining multiple Linear layers."""
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
layer1 = Dense(784, 128)
|
||||
layer2 = Dense(128, 64)
|
||||
layer3 = Dense(64, 10)
|
||||
layer1 = Linear(784, 128)
|
||||
layer2 = Linear(128, 64)
|
||||
layer3 = Linear(64, 10)
|
||||
|
||||
x = Tensor(np.random.randn(32, 784))
|
||||
h1 = layer1(x)
|
||||
|
||||
@@ -16,13 +16,13 @@ class TestXORCapability:
|
||||
|
||||
def test_xor_network_structure(self):
|
||||
"""Test building network for XOR problem."""
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Sigmoid
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Build XOR network: 2 -> 4 -> 1
|
||||
hidden = Dense(2, 4, bias=True)
|
||||
output = Dense(4, 1, bias=True)
|
||||
hidden = Linear(2, 4, bias=True)
|
||||
output = Linear(4, 1, bias=True)
|
||||
relu = ReLU()
|
||||
sigmoid = Sigmoid()
|
||||
|
||||
@@ -40,11 +40,11 @@ class TestXORCapability:
|
||||
|
||||
def test_xor_network_expressiveness(self):
|
||||
"""Test that network has enough capacity for XOR."""
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
# XOR needs at least 2 hidden units
|
||||
hidden = Dense(2, 4) # 4 hidden units is sufficient
|
||||
output = Dense(4, 1)
|
||||
hidden = Linear(2, 4) # 4 hidden units is sufficient
|
||||
output = Linear(4, 1)
|
||||
|
||||
# Count parameters
|
||||
hidden_params = 2 * 4 + 4 # weights + bias
|
||||
@@ -56,13 +56,13 @@ class TestXORCapability:
|
||||
|
||||
def test_nonlinearity_required(self):
|
||||
"""Test that non-linearity is essential for XOR."""
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Without activation, network is just linear
|
||||
layer1 = Dense(2, 4)
|
||||
layer2 = Dense(4, 1)
|
||||
layer1 = Linear(2, 4)
|
||||
layer2 = Linear(4, 1)
|
||||
relu = ReLU()
|
||||
|
||||
X = Tensor(np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=np.float32))
|
||||
@@ -82,14 +82,14 @@ class TestMLPCapabilities:
|
||||
|
||||
def test_universal_approximation(self):
|
||||
"""Test that MLPs can approximate continuous functions."""
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Wide hidden layer can approximate any function
|
||||
layer1 = Dense(1, 100) # Wide hidden layer
|
||||
layer1 = Linear(1, 100) # Wide hidden layer
|
||||
relu = ReLU()
|
||||
layer2 = Dense(100, 1)
|
||||
layer2 = Linear(100, 1)
|
||||
|
||||
# Test on simple function: sin(x)
|
||||
x = np.linspace(-np.pi, np.pi, 50).reshape(-1, 1)
|
||||
@@ -105,16 +105,16 @@ class TestMLPCapabilities:
|
||||
|
||||
def test_deep_network(self):
|
||||
"""Test building deep networks."""
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Build 5-layer network
|
||||
layers = [
|
||||
Dense(100, 50),
|
||||
Dense(50, 25),
|
||||
Dense(25, 12),
|
||||
Dense(12, 6),
|
||||
Dense(6, 1)
|
||||
Linear(100, 50),
|
||||
Linear(50, 25),
|
||||
Linear(25, 12),
|
||||
Linear(12, 6),
|
||||
Linear(6, 1)
|
||||
]
|
||||
|
||||
x = Tensor(np.random.randn(16, 100))
|
||||
|
||||
@@ -87,11 +87,11 @@ class TestDenseNetworkCapability:
|
||||
def test_dense_layer_creation(self):
|
||||
"""Test Dense layer works with the foundation."""
|
||||
try:
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Create Dense layer
|
||||
layer = Dense(10, 5)
|
||||
layer = Linear(10, 5)
|
||||
|
||||
# Should have proper weights and bias
|
||||
assert hasattr(layer, 'weights'), "Dense broken: No weights"
|
||||
@@ -109,13 +109,13 @@ class TestDenseNetworkCapability:
|
||||
def test_multi_layer_network(self):
|
||||
"""Test building multi-layer networks."""
|
||||
try:
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Sigmoid
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Build 3-layer network for XOR
|
||||
hidden = Dense(2, 4, use_bias=True)
|
||||
output = Dense(4, 1, use_bias=True)
|
||||
hidden = Linear(2, 4, use_bias=True)
|
||||
output = Linear(4, 1, use_bias=True)
|
||||
relu = ReLU()
|
||||
sigmoid = Sigmoid()
|
||||
|
||||
@@ -142,7 +142,7 @@ class TestXORProblemSolution:
|
||||
def test_xor_network_architecture(self):
|
||||
"""Test XOR network architecture is possible."""
|
||||
try:
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Sigmoid
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
@@ -151,8 +151,8 @@ class TestXORProblemSolution:
|
||||
y_target = np.array([[0], [1], [1], [0]], dtype=np.float32)
|
||||
|
||||
# Network: 2 -> 4 -> 1 (sufficient for XOR)
|
||||
hidden = Dense(2, 4, use_bias=True)
|
||||
output = Dense(4, 1, use_bias=True)
|
||||
hidden = Linear(2, 4, use_bias=True)
|
||||
output = Linear(4, 1, use_bias=True)
|
||||
relu = ReLU()
|
||||
sigmoid = Sigmoid()
|
||||
|
||||
@@ -179,14 +179,14 @@ class TestXORProblemSolution:
|
||||
def test_nonlinear_problem_solvability(self):
|
||||
"""Test that non-linear problems are now solvable."""
|
||||
try:
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Create network that can solve non-linear problems
|
||||
layer1 = Dense(2, 10)
|
||||
layer1 = Linear(2, 10)
|
||||
relu = ReLU()
|
||||
layer2 = Dense(10, 1)
|
||||
layer2 = Linear(10, 1)
|
||||
|
||||
# Test various non-linear patterns
|
||||
patterns = [
|
||||
@@ -214,14 +214,14 @@ class TestFoundationMilestoneReadiness:
|
||||
def test_mnist_mlp_architecture_possible(self):
|
||||
"""Test we can build MNIST MLP architecture."""
|
||||
try:
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Softmax
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# MNIST MLP: 784 -> 128 -> 64 -> 10
|
||||
layer1 = Dense(784, 128)
|
||||
layer2 = Dense(128, 64)
|
||||
layer3 = Dense(64, 10)
|
||||
layer1 = Linear(784, 128)
|
||||
layer2 = Linear(128, 64)
|
||||
layer3 = Linear(64, 10)
|
||||
relu = ReLU()
|
||||
softmax = Softmax()
|
||||
|
||||
@@ -246,13 +246,13 @@ class TestFoundationMilestoneReadiness:
|
||||
def test_classification_capability(self):
|
||||
"""Test basic classification capability."""
|
||||
try:
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Softmax
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Simple classifier: features -> hidden -> classes
|
||||
feature_layer = Dense(20, 10)
|
||||
classifier = Dense(10, 3) # 3 classes
|
||||
feature_layer = Linear(20, 10)
|
||||
classifier = Linear(10, 3) # 3 classes
|
||||
relu = ReLU()
|
||||
softmax = Softmax()
|
||||
|
||||
@@ -280,7 +280,7 @@ class TestCompleteStackValidation:
|
||||
"""Test complete neural network from scratch."""
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Sigmoid
|
||||
|
||||
# End-to-end test: Build and run a complete neural network
|
||||
@@ -290,9 +290,9 @@ class TestCompleteStackValidation:
|
||||
X = Tensor(np.random.randn(10, 5))
|
||||
|
||||
# 2. Network architecture (from layers foundation)
|
||||
layer1 = Dense(5, 8)
|
||||
layer2 = Dense(8, 3)
|
||||
layer3 = Dense(3, 1)
|
||||
layer1 = Linear(5, 8)
|
||||
layer2 = Linear(8, 3)
|
||||
layer3 = Linear(3, 1)
|
||||
|
||||
# 3. Activations (from activation foundation)
|
||||
relu = ReLU()
|
||||
@@ -320,13 +320,13 @@ class TestCompleteStackValidation:
|
||||
"""Test foundation remains stable under computational load."""
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
|
||||
# Stress test: Large network
|
||||
layer1 = Dense(100, 200)
|
||||
layer2 = Dense(200, 100)
|
||||
layer3 = Dense(100, 10)
|
||||
layer1 = Linear(100, 200)
|
||||
layer2 = Linear(200, 100)
|
||||
layer3 = Linear(100, 10)
|
||||
relu = ReLU()
|
||||
|
||||
# Large batch
|
||||
|
||||
@@ -25,10 +25,10 @@ class TestPriorStackStillWorking:
|
||||
# Core functionality should work
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
# Should still be able to build networks
|
||||
layer = Dense(10, 5)
|
||||
layer = Linear(10, 5)
|
||||
x = Tensor(np.random.randn(4, 10))
|
||||
output = layer(x)
|
||||
assert output.shape == (4, 5), "Foundation broken: Neural network"
|
||||
@@ -145,7 +145,7 @@ class TestProgressiveStackIntegration:
|
||||
try:
|
||||
from tinytorch.core.data import DataLoader, Dataset
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Softmax
|
||||
|
||||
# Create dataset
|
||||
@@ -165,8 +165,8 @@ class TestProgressiveStackIntegration:
|
||||
dataloader = DataLoader(dataset, batch_size=8, shuffle=True)
|
||||
|
||||
# Create model using prior modules
|
||||
layer1 = Dense(10, 16)
|
||||
layer2 = Dense(16, 3)
|
||||
layer1 = Linear(10, 16)
|
||||
layer2 = Linear(16, 3)
|
||||
relu = ReLU()
|
||||
softmax = Softmax()
|
||||
|
||||
@@ -190,7 +190,7 @@ class TestProgressiveStackIntegration:
|
||||
try:
|
||||
from tinytorch.core.data import DataLoader, Dataset
|
||||
from tinytorch.core.spatial import Conv2D, MaxPool2D
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Image dataset
|
||||
@@ -212,7 +212,7 @@ class TestProgressiveStackIntegration:
|
||||
# CNN components
|
||||
conv1 = Conv2D(in_channels=3, out_channels=16, kernel_size=3)
|
||||
pool = MaxPool2D(kernel_size=2)
|
||||
fc = Dense(16 * 15 * 15, 5) # Approximate after conv/pool
|
||||
fc = Linear(16 * 15 * 15, 5) # Approximate after conv/pool
|
||||
|
||||
# Test CNN pipeline
|
||||
for batch_x, batch_y in dataloader:
|
||||
@@ -373,10 +373,10 @@ class TestRegressionPrevention:
|
||||
# Foundation level (if available)
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
# Neural networks should still work
|
||||
layer = Dense(5, 3)
|
||||
layer = Linear(5, 3)
|
||||
x = Tensor(np.random.randn(2, 5))
|
||||
output = layer(x)
|
||||
assert output.shape == (2, 3), "Foundation level broken"
|
||||
|
||||
@@ -55,11 +55,11 @@ class TestFoundationStackStillWorks:
|
||||
try:
|
||||
# Test foundation components still work
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
|
||||
# Create simple neural network
|
||||
dense = Dense(10, 5)
|
||||
dense = Linear(10, 5)
|
||||
relu = ReLU()
|
||||
|
||||
# Test forward pass
|
||||
@@ -86,7 +86,7 @@ class TestFoundationStackStillWorks:
|
||||
4. Run: tito module complete 05_dense
|
||||
5. Test imports individually:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
|
||||
💡 FOUNDATION REQUIREMENTS:
|
||||
@@ -109,7 +109,7 @@ class TestFoundationStackStillWorks:
|
||||
|
||||
💡 DEBUG STEPS:
|
||||
1. Test each component separately
|
||||
2. Check Dense layer: dense = Dense(5, 3); print(dense.weights.shape)
|
||||
2. Check Dense layer: dense = Linear(5, 3); print(linear.weights.shape)
|
||||
3. Check ReLU: relu = ReLU(); print(relu(Tensor([-1, 1])).data)
|
||||
4. Run foundation tests: python tests/run_all_modules.py --module module_05
|
||||
"""
|
||||
@@ -128,12 +128,12 @@ class TestFoundationStackStillWorks:
|
||||
"""
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Sigmoid
|
||||
|
||||
# Build 3-layer network for XOR problem
|
||||
layer1 = Dense(2, 4, use_bias=True)
|
||||
layer2 = Dense(4, 1, use_bias=True)
|
||||
layer1 = Linear(2, 4, use_bias=True)
|
||||
layer2 = Linear(4, 1, use_bias=True)
|
||||
relu = ReLU()
|
||||
sigmoid = Sigmoid()
|
||||
|
||||
@@ -509,7 +509,7 @@ class TestSpatialIntegration:
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.spatial import Conv2D, MaxPool2D
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Softmax
|
||||
|
||||
# Build mini CNN for CIFAR-10 style classification
|
||||
@@ -523,8 +523,8 @@ class TestSpatialIntegration:
|
||||
|
||||
# Dense layers (after flattening)
|
||||
# 32 channels * 8 * 8 = 2048 features
|
||||
fc1 = Dense(32 * 8 * 8, 128)
|
||||
fc2 = Dense(128, 10)
|
||||
fc1 = Linear(32 * 8 * 8, 128)
|
||||
fc2 = Linear(128, 10)
|
||||
|
||||
# Activations
|
||||
relu = ReLU()
|
||||
@@ -812,7 +812,7 @@ class TestComputerVisionCapabilities:
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.spatial import Conv2D, MaxPool2D
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Softmax
|
||||
|
||||
# Build classifier for 10 classes (CIFAR-10 style)
|
||||
@@ -825,8 +825,8 @@ class TestComputerVisionCapabilities:
|
||||
self.pool2 = MaxPool2D(kernel_size=2)
|
||||
|
||||
# Classification (dense layers)
|
||||
self.fc1 = Dense(64 * 8 * 8, 128) # Assuming 32x32 input
|
||||
self.fc2 = Dense(128, num_classes)
|
||||
self.fc1 = Linear(64 * 8 * 8, 128) # Assuming 32x32 input
|
||||
self.fc2 = Linear(128, num_classes)
|
||||
|
||||
# Activations
|
||||
self.relu = ReLU()
|
||||
@@ -1213,7 +1213,7 @@ class TestModule06Completion:
|
||||
|
||||
# Test 4: CNN architecture building
|
||||
from tinytorch.core.activations import ReLU
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
relu = ReLU()
|
||||
h1 = relu(conv_out)
|
||||
@@ -1221,7 +1221,7 @@ class TestModule06Completion:
|
||||
|
||||
# Flatten and connect to dense
|
||||
flattened = Tensor(h1_pool.data.reshape(2, -1))
|
||||
dense = Dense(flattened.shape[1], 10)
|
||||
dense = Linear(flattened.shape[1], 10)
|
||||
output = dense(flattened)
|
||||
|
||||
assert output.shape == (2, 10)
|
||||
@@ -1251,7 +1251,7 @@ class TestModule06Completion:
|
||||
|
||||
# Test 8: Foundation integration
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense, Layer
|
||||
from tinytorch.core.layers import Linear, Layer
|
||||
from tinytorch.core.activations import ReLU
|
||||
|
||||
# All foundation components should work together
|
||||
|
||||
@@ -56,10 +56,10 @@ class TestAutogradLayerIntegration:
|
||||
"""Test gradients flow through Dense layer."""
|
||||
try:
|
||||
from tinytorch.core.autograd import Variable
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
# Create layer
|
||||
layer = Dense(2, 1, bias=False)
|
||||
layer = Linear(2, 1, bias=False)
|
||||
|
||||
# Input with gradients
|
||||
x = Variable(np.array([[1.0, 2.0]]), requires_grad=True)
|
||||
@@ -185,9 +185,9 @@ class TestAutogradOptimizationIntegration:
|
||||
"""Test parameter updates work correctly."""
|
||||
try:
|
||||
from tinytorch.core.autograd import Variable
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
layer = Dense(1, 1)
|
||||
layer = Linear(1, 1)
|
||||
|
||||
# Convert layer parameters to Variables if needed
|
||||
if not isinstance(layer.weights, Variable):
|
||||
|
||||
@@ -25,11 +25,11 @@ class TestPriorStackStillWorking:
|
||||
# Neural networks + data should work
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.data import Dataset
|
||||
|
||||
# Complete ML pipeline components should work
|
||||
layer = Dense(10, 5)
|
||||
layer = Linear(10, 5)
|
||||
x = Tensor(np.random.randn(4, 10))
|
||||
output = layer(x)
|
||||
assert output.shape == (4, 5), "Foundation broken: Neural network"
|
||||
@@ -63,11 +63,11 @@ class TestModule10OptimizersCore:
|
||||
"""Test SGD optimizer creation and basic functionality."""
|
||||
try:
|
||||
from tinytorch.core.optimizers import SGD
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Create model with parameters
|
||||
layer = Dense(5, 3)
|
||||
layer = Linear(5, 3)
|
||||
|
||||
# Create SGD optimizer
|
||||
optimizer = SGD(layer.parameters(), lr=0.01)
|
||||
@@ -91,10 +91,10 @@ class TestModule10OptimizersCore:
|
||||
"""Test Adam optimizer creation and advanced features."""
|
||||
try:
|
||||
from tinytorch.core.optimizers import Adam
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
# Create model
|
||||
layer = Dense(10, 5)
|
||||
layer = Linear(10, 5)
|
||||
|
||||
# Create Adam optimizer with hyperparameters
|
||||
optimizer = Adam(layer.parameters(), lr=0.001, betas=(0.9, 0.999), eps=1e-8)
|
||||
@@ -115,12 +115,12 @@ class TestModule10OptimizersCore:
|
||||
"""Test that optimizers actually update parameters."""
|
||||
try:
|
||||
from tinytorch.core.optimizers import SGD
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.autograd import Variable
|
||||
|
||||
# Create simple model
|
||||
layer = Dense(2, 1)
|
||||
layer = Linear(2, 1)
|
||||
optimizer = SGD(layer.parameters(), lr=0.1)
|
||||
|
||||
# Get initial weights
|
||||
@@ -157,7 +157,7 @@ class TestProgressiveStackIntegration:
|
||||
"""Test complete training step: forward → backward → optimize."""
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
from tinytorch.core.optimizers import SGD
|
||||
from tinytorch.core.data import Dataset, DataLoader
|
||||
@@ -176,8 +176,8 @@ class TestProgressiveStackIntegration:
|
||||
return Tensor(self.data[idx]), Tensor(self.targets[idx])
|
||||
|
||||
# Create model
|
||||
layer1 = Dense(5, 10)
|
||||
layer2 = Dense(10, 1)
|
||||
layer1 = Linear(5, 10)
|
||||
layer2 = Linear(10, 1)
|
||||
relu = ReLU()
|
||||
|
||||
# Create optimizer
|
||||
@@ -222,14 +222,14 @@ class TestProgressiveStackIntegration:
|
||||
"""Test optimization with convolutional networks."""
|
||||
try:
|
||||
from tinytorch.core.spatial import Conv2D, MaxPool2D
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import Adam
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# CNN architecture
|
||||
conv1 = Conv2D(in_channels=3, out_channels=16, kernel_size=3)
|
||||
pool = MaxPool2D(kernel_size=2)
|
||||
fc = Dense(16 * 15 * 15, 10) # Approximate size
|
||||
fc = Linear(16 * 15 * 15, 10) # Approximate size
|
||||
|
||||
# Collect CNN parameters
|
||||
params = []
|
||||
@@ -265,12 +265,12 @@ class TestOptimizationAlgorithms:
|
||||
"""Test SGD vs Adam optimization behavior."""
|
||||
try:
|
||||
from tinytorch.core.optimizers import SGD, Adam
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Create identical models
|
||||
model_sgd = Dense(10, 1)
|
||||
model_adam = Dense(10, 1)
|
||||
model_sgd = Linear(10, 1)
|
||||
model_adam = Linear(10, 1)
|
||||
|
||||
# Make weights identical
|
||||
model_adam.weights.data = model_sgd.weights.data.copy()
|
||||
@@ -298,9 +298,9 @@ class TestOptimizationAlgorithms:
|
||||
"""Test learning rate scheduling capabilities."""
|
||||
try:
|
||||
from tinytorch.core.optimizers import SGD
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
layer = Dense(5, 1)
|
||||
layer = Linear(5, 1)
|
||||
optimizer = SGD(layer.parameters(), lr=0.1)
|
||||
|
||||
initial_lr = optimizer.lr
|
||||
@@ -327,10 +327,10 @@ class TestOptimizationAlgorithms:
|
||||
"""Test optimizer memory usage and efficiency."""
|
||||
try:
|
||||
from tinytorch.core.optimizers import SGD, Adam
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
# Large model to test memory
|
||||
large_model = Dense(1000, 500)
|
||||
large_model = Linear(1000, 500)
|
||||
|
||||
# SGD should use less memory than Adam
|
||||
sgd_optimizer = SGD(large_model.parameters(), lr=0.01)
|
||||
@@ -361,10 +361,10 @@ class TestProductionOptimization:
|
||||
"""Test gradient clipping for stable training."""
|
||||
try:
|
||||
from tinytorch.core.optimizers import SGD
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
layer = Dense(10, 1)
|
||||
layer = Linear(10, 1)
|
||||
optimizer = SGD(layer.parameters(), lr=0.1)
|
||||
|
||||
# Simulate large gradients
|
||||
@@ -387,9 +387,9 @@ class TestProductionOptimization:
|
||||
"""Test saving and loading optimizer state."""
|
||||
try:
|
||||
from tinytorch.core.optimizers import Adam
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
layer = Dense(5, 1)
|
||||
layer = Linear(5, 1)
|
||||
optimizer = Adam(layer.parameters(), lr=0.001)
|
||||
|
||||
# Take some steps to build state
|
||||
@@ -423,9 +423,9 @@ class TestRegressionPrevention:
|
||||
# Neural networks should still work
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
layer = Dense(5, 3)
|
||||
layer = Linear(5, 3)
|
||||
x = Tensor(np.random.randn(2, 5))
|
||||
output = layer(x)
|
||||
assert output.shape == (2, 3), "Foundation regression: Neural network broken"
|
||||
@@ -471,11 +471,11 @@ class TestRegressionPrevention:
|
||||
# ML pipeline level (if available)
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.data import Dataset
|
||||
|
||||
# Complete ML components should work together
|
||||
layer = Dense(3, 2)
|
||||
layer = Linear(3, 2)
|
||||
x = Tensor(np.random.randn(1, 3))
|
||||
output = layer(x)
|
||||
assert output.shape == (1, 2), "ML pipeline level broken"
|
||||
|
||||
@@ -57,7 +57,7 @@ class TestCompleteMLPipelineStillWorks:
|
||||
# Test complete pipeline still works
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.spatial import Conv2D, MaxPool2D
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Softmax
|
||||
from tinytorch.core.data import Dataset, DataLoader
|
||||
|
||||
@@ -76,7 +76,7 @@ class TestCompleteMLPipelineStillWorks:
|
||||
# Create model components
|
||||
conv = Conv2D(3, 16, kernel_size=3, padding=1)
|
||||
pool = MaxPool2D(kernel_size=2)
|
||||
dense = Dense(16 * 16 * 16, 10) # 4096 -> 10
|
||||
dense = Linear(16 * 16 * 16, 10) # 4096 -> 10
|
||||
relu = ReLU()
|
||||
softmax = Softmax()
|
||||
|
||||
@@ -171,7 +171,7 @@ class TestCompleteMLPipelineStillWorks:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.spatial import Conv2D
|
||||
from tinytorch.core.attention import MultiHeadAttention
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
|
||||
# Test sophisticated architecture integration
|
||||
@@ -179,7 +179,7 @@ class TestCompleteMLPipelineStillWorks:
|
||||
|
||||
# Vision processing
|
||||
cnn = Conv2D(3, 64, kernel_size=3, padding=1)
|
||||
vision_proj = Dense(64 * 32 * 32, 256) # Project spatial features
|
||||
vision_proj = Linear(64 * 32 * 32, 256) # Project spatial features
|
||||
|
||||
# Attention processing
|
||||
attention = MultiHeadAttention(embed_dim=256, num_heads=8)
|
||||
@@ -233,7 +233,7 @@ class TestCompleteMLPipelineStillWorks:
|
||||
Test each component separately:
|
||||
1. CNN: conv = Conv2D(3, 16, 3); out = conv(x)
|
||||
2. Attention: attn = MultiHeadAttention(64, 4); out = attn(x)
|
||||
3. Dense: dense = Dense(100, 50); out = dense(x)
|
||||
3. Dense: dense = Linear(100, 50); out = dense(x)
|
||||
4. Integration: Combine all components step by step
|
||||
"""
|
||||
|
||||
@@ -556,7 +556,7 @@ class TestAutogradIntegration:
|
||||
try:
|
||||
from tinytorch.core.autograd import Variable
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
|
||||
# Test gradients through layers
|
||||
@@ -566,7 +566,7 @@ class TestAutogradIntegration:
|
||||
x = Variable(Tensor(np.random.randn(2, 5)), requires_grad=True)
|
||||
|
||||
# Create layers
|
||||
dense = Dense(5, 3)
|
||||
dense = Linear(5, 3)
|
||||
relu = ReLU()
|
||||
|
||||
# Forward pass through layers
|
||||
@@ -616,7 +616,7 @@ class TestAutogradIntegration:
|
||||
|
||||
Eventually layers need to support:
|
||||
|
||||
class Dense(Layer):
|
||||
class Linear(Layer):
|
||||
def __init__(self, in_features, out_features):
|
||||
# Parameters as Variables
|
||||
self.weights = Variable(Tensor(...), requires_grad=True)
|
||||
@@ -1167,8 +1167,8 @@ class TestModule09Completion:
|
||||
autograd_capabilities["Loss function gradients"] = True
|
||||
|
||||
# Test 6: Layer integration (basic structure)
|
||||
from tinytorch.core.layers import Dense
|
||||
layer = Dense(5, 3)
|
||||
from tinytorch.core.layers import Linear
|
||||
layer = Linear(5, 3)
|
||||
# Layers exist, integration will be implemented
|
||||
autograd_capabilities["Layer integration ready"] = True
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ setup_integration_test()
|
||||
# Import ONLY from TinyTorch package
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.attention import scaled_dot_product_attention, SelfAttention, create_causal_mask
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Softmax
|
||||
from tinytorch.core.dense import Sequential
|
||||
|
||||
@@ -29,7 +29,7 @@ class TestAttentionDensePipelineInterface:
|
||||
|
||||
# Create attention and dense components
|
||||
self_attn = SelfAttention(d_model)
|
||||
dense = Dense(input_size=d_model, output_size=10)
|
||||
dense = Linear(input_size=d_model, output_size=10)
|
||||
|
||||
# Create input
|
||||
x = Tensor(np.random.randn(seq_len, d_model))
|
||||
@@ -54,7 +54,7 @@ class TestAttentionDensePipelineInterface:
|
||||
input_tensor = Tensor(np.random.randn(4, 6))
|
||||
|
||||
# Step 1: Dense layer to project to d_model
|
||||
projection = Dense(input_size=6, output_size=d_model)
|
||||
projection = Linear(input_size=6, output_size=d_model)
|
||||
projected = projection(input_tensor)
|
||||
|
||||
# Step 2: Attention processing (simulating attention in pipeline)
|
||||
@@ -62,7 +62,7 @@ class TestAttentionDensePipelineInterface:
|
||||
attn_output, _ = self_attn(projected.data)
|
||||
|
||||
# Step 3: Back to Dense layer
|
||||
output_projection = Dense(input_size=d_model, output_size=3)
|
||||
output_projection = Linear(input_size=d_model, output_size=3)
|
||||
final_outputs = []
|
||||
for i in range(4):
|
||||
pos_input = Tensor(attn_output[i:i+1])
|
||||
@@ -82,7 +82,7 @@ class TestAttentionDensePipelineInterface:
|
||||
# Create components
|
||||
self_attn = SelfAttention(d_model)
|
||||
relu = ReLU()
|
||||
dense = Dense(input_size=d_model, output_size=d_model)
|
||||
dense = Linear(input_size=d_model, output_size=d_model)
|
||||
|
||||
# Test pipeline: Input → Attention → Activation → Dense
|
||||
x = Tensor(np.random.randn(seq_len, d_model))
|
||||
@@ -112,7 +112,7 @@ class TestAttentionMultiModuleWorkflows:
|
||||
|
||||
# Source processing (encoder-style)
|
||||
src = Tensor(np.random.randn(src_len, d_model))
|
||||
src_projection = Dense(input_size=d_model, output_size=d_model)
|
||||
src_projection = Linear(input_size=d_model, output_size=d_model)
|
||||
src_projected = src_projection(src)
|
||||
|
||||
encoder_attn = SelfAttention(d_model)
|
||||
@@ -120,7 +120,7 @@ class TestAttentionMultiModuleWorkflows:
|
||||
|
||||
# Target processing (decoder-style)
|
||||
tgt = Tensor(np.random.randn(tgt_len, d_model))
|
||||
tgt_projection = Dense(input_size=d_model, output_size=d_model)
|
||||
tgt_projection = Linear(input_size=d_model, output_size=d_model)
|
||||
tgt_projected = tgt_projection(tgt)
|
||||
|
||||
# Cross-attention interface test
|
||||
@@ -131,7 +131,7 @@ class TestAttentionMultiModuleWorkflows:
|
||||
)
|
||||
|
||||
# Final processing
|
||||
output_projection = Dense(input_size=d_model, output_size=10)
|
||||
output_projection = Linear(input_size=d_model, output_size=10)
|
||||
final_outputs = []
|
||||
for i in range(tgt_len):
|
||||
pos_input = Tensor(cross_output[i:i+1])
|
||||
@@ -151,7 +151,7 @@ class TestAttentionMultiModuleWorkflows:
|
||||
|
||||
# Initial processing
|
||||
x = Tensor(np.random.randn(seq_len, d_model))
|
||||
embedding_projection = Dense(input_size=d_model, output_size=d_model)
|
||||
embedding_projection = Linear(input_size=d_model, output_size=d_model)
|
||||
current_repr = embedding_projection(x).data
|
||||
|
||||
# Multi-layer processing with residuals
|
||||
@@ -162,9 +162,9 @@ class TestAttentionMultiModuleWorkflows:
|
||||
|
||||
# Feedforward network (using Dense layers)
|
||||
ff_network = Sequential([
|
||||
Dense(input_size=d_model, output_size=d_model * 2),
|
||||
Linear(input_size=d_model, output_size=d_model * 2),
|
||||
ReLU(),
|
||||
Dense(input_size=d_model * 2, output_size=d_model)
|
||||
Linear(input_size=d_model * 2, output_size=d_model)
|
||||
])
|
||||
|
||||
# Process each position through feedforward
|
||||
@@ -189,7 +189,7 @@ class TestAttentionMultiModuleWorkflows:
|
||||
|
||||
# Input processing
|
||||
sentence = Tensor(np.random.randn(seq_len, d_model))
|
||||
input_projection = Dense(input_size=d_model, output_size=d_model)
|
||||
input_projection = Linear(input_size=d_model, output_size=d_model)
|
||||
projected_input = input_projection(sentence)
|
||||
|
||||
# Attention processing
|
||||
@@ -201,9 +201,9 @@ class TestAttentionMultiModuleWorkflows:
|
||||
|
||||
# Classification head (using Sequential)
|
||||
classifier = Sequential([
|
||||
Dense(input_size=d_model, output_size=d_model // 2),
|
||||
Linear(input_size=d_model, output_size=d_model // 2),
|
||||
ReLU(),
|
||||
Dense(input_size=d_model // 2, output_size=num_classes)
|
||||
Linear(input_size=d_model // 2, output_size=num_classes)
|
||||
])
|
||||
|
||||
# Final classification
|
||||
@@ -231,13 +231,13 @@ class TestAttentionDataFlowCompatibility:
|
||||
x = Tensor(np.random.randn(seq_len, d_model))
|
||||
|
||||
# Processing pipeline
|
||||
input_proj = Dense(input_size=d_model, output_size=d_model)
|
||||
input_proj = Linear(input_size=d_model, output_size=d_model)
|
||||
projected = input_proj(x)
|
||||
|
||||
attn = SelfAttention(d_model)
|
||||
attn_out, _ = attn(projected.data)
|
||||
|
||||
output_proj = Dense(input_size=d_model, output_size=d_model // 2)
|
||||
output_proj = Linear(input_size=d_model, output_size=d_model // 2)
|
||||
|
||||
# Test shape flow
|
||||
for i in range(seq_len):
|
||||
@@ -254,7 +254,7 @@ class TestAttentionDataFlowCompatibility:
|
||||
# Test float32 flow
|
||||
x_f32 = Tensor(np.random.randn(seq_len, d_model).astype(np.float32))
|
||||
|
||||
dense_f32 = Dense(input_size=d_model, output_size=d_model)
|
||||
dense_f32 = Linear(input_size=d_model, output_size=d_model)
|
||||
projected_f32 = dense_f32(x_f32)
|
||||
|
||||
attn_f32 = SelfAttention(d_model)
|
||||
@@ -280,7 +280,7 @@ class TestAttentionDataFlowCompatibility:
|
||||
attn_out, _ = attn(x.data)
|
||||
|
||||
# This should fail gracefully
|
||||
incompatible_dense = Dense(input_size=dense_dim, output_size=10)
|
||||
incompatible_dense = Linear(input_size=dense_dim, output_size=10)
|
||||
|
||||
try:
|
||||
pos_tensor = Tensor(attn_out[0:1]) # Shape (1, 8)
|
||||
@@ -311,9 +311,9 @@ class TestAttentionSystemLevelIntegration:
|
||||
|
||||
# 3. Feedforward network
|
||||
ff_net = Sequential([
|
||||
Dense(input_size=d_model, output_size=d_model * 4),
|
||||
Linear(input_size=d_model, output_size=d_model * 4),
|
||||
ReLU(),
|
||||
Dense(input_size=d_model * 4, output_size=d_model)
|
||||
Linear(input_size=d_model * 4, output_size=d_model)
|
||||
])
|
||||
|
||||
# Process each position through feedforward
|
||||
@@ -350,7 +350,7 @@ class TestAttentionSystemLevelIntegration:
|
||||
SelfAttention(d_model), # Another instance
|
||||
]
|
||||
|
||||
dense_postprocess = Dense(input_size=d_model, output_size=8)
|
||||
dense_postprocess = Linear(input_size=d_model, output_size=8)
|
||||
|
||||
# Test that all variants work in same pipeline
|
||||
for i, attn_variant in enumerate(attention_variants):
|
||||
|
||||
@@ -106,14 +106,14 @@ class TestProgressiveStackIntegration:
|
||||
"""Test neural network enhanced with attention."""
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
from tinytorch.core.attention import MultiHeadAttention
|
||||
|
||||
# Build network: dense → attention → dense
|
||||
encoder = Dense(64, 64)
|
||||
encoder = Linear(64, 64)
|
||||
attention = MultiHeadAttention(embed_dim=64, num_heads=8)
|
||||
decoder = Dense(64, 10)
|
||||
decoder = Linear(64, 10)
|
||||
relu = ReLU()
|
||||
|
||||
# Sequence input
|
||||
@@ -134,14 +134,14 @@ class TestProgressiveStackIntegration:
|
||||
"""Test building transformer-style blocks."""
|
||||
try:
|
||||
from tinytorch.core.attention import MultiHeadAttention
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Transformer block components
|
||||
attention = MultiHeadAttention(embed_dim=128, num_heads=8)
|
||||
ff1 = Dense(128, 512)
|
||||
ff2 = Dense(512, 128)
|
||||
ff1 = Linear(128, 512)
|
||||
ff2 = Linear(512, 128)
|
||||
relu = ReLU()
|
||||
|
||||
# Input sequence
|
||||
@@ -237,21 +237,21 @@ class TestNLPReadiness:
|
||||
"""Test architecture suitable for language modeling."""
|
||||
try:
|
||||
from tinytorch.core.attention import MultiHeadAttention
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Language model components
|
||||
vocab_size, embed_dim, seq_len = 1000, 256, 32
|
||||
|
||||
# Embedding layer (simplified)
|
||||
embedding = Dense(vocab_size, embed_dim)
|
||||
embedding = Linear(vocab_size, embed_dim)
|
||||
|
||||
# Attention layers
|
||||
attention1 = MultiHeadAttention(embed_dim=embed_dim, num_heads=8)
|
||||
attention2 = MultiHeadAttention(embed_dim=embed_dim, num_heads=8)
|
||||
|
||||
# Output projection
|
||||
output_proj = Dense(embed_dim, vocab_size)
|
||||
output_proj = Linear(embed_dim, vocab_size)
|
||||
|
||||
# Token sequence (as embeddings)
|
||||
batch_size = 4
|
||||
@@ -316,10 +316,10 @@ class TestRegressionPrevention:
|
||||
# Foundation level (if available)
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
# Should still be able to build neural networks
|
||||
layer = Dense(10, 5)
|
||||
layer = Linear(10, 5)
|
||||
x = Tensor(np.random.randn(4, 10))
|
||||
output = layer(x)
|
||||
assert output.shape == (4, 5), "Foundation level broken"
|
||||
|
||||
@@ -25,12 +25,12 @@ class TestPriorStackStillWorking:
|
||||
# Complete pipeline should work
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.data import Dataset, DataLoader
|
||||
from tinytorch.core.optimizers import SGD
|
||||
|
||||
# All components should be available
|
||||
layer = Dense(5, 2)
|
||||
layer = Linear(5, 2)
|
||||
optimizer = SGD(layer.parameters(), lr=0.01)
|
||||
|
||||
# Basic functionality should work
|
||||
@@ -45,10 +45,10 @@ class TestPriorStackStillWorking:
|
||||
"""Verify Module 10 (Optimizers) still works."""
|
||||
try:
|
||||
from tinytorch.core.optimizers import SGD, Adam
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
# Optimizers should work
|
||||
layer = Dense(3, 1)
|
||||
layer = Linear(3, 1)
|
||||
sgd = SGD(layer.parameters(), lr=0.01)
|
||||
adam = Adam(layer.parameters(), lr=0.001)
|
||||
|
||||
@@ -66,12 +66,12 @@ class TestModule11TrainingCore:
|
||||
"""Test basic training loop functionality."""
|
||||
try:
|
||||
from tinytorch.core.training import Trainer
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import SGD
|
||||
from tinytorch.core.data import Dataset, DataLoader
|
||||
|
||||
# Create model and optimizer
|
||||
model = Dense(10, 3)
|
||||
model = Linear(10, 3)
|
||||
optimizer = SGD(model.parameters(), lr=0.01)
|
||||
|
||||
# Create simple dataset
|
||||
@@ -155,7 +155,7 @@ class TestProgressiveStackIntegration:
|
||||
"""Test complete end-to-end training process."""
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Softmax
|
||||
from tinytorch.core.optimizers import SGD
|
||||
from tinytorch.core.training import Trainer, CrossEntropyLoss
|
||||
@@ -164,9 +164,9 @@ class TestProgressiveStackIntegration:
|
||||
# Create complete model
|
||||
class SimpleModel:
|
||||
def __init__(self):
|
||||
self.layer1 = Dense(10, 16)
|
||||
self.layer1 = Linear(10, 16)
|
||||
self.relu = ReLU()
|
||||
self.layer2 = Dense(16, 3)
|
||||
self.layer2 = Linear(16, 3)
|
||||
self.softmax = Softmax()
|
||||
|
||||
def __call__(self, x):
|
||||
@@ -228,7 +228,7 @@ class TestProgressiveStackIntegration:
|
||||
"""Test CNN training with spatial operations."""
|
||||
try:
|
||||
from tinytorch.core.spatial import Conv2D, MaxPool2D
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
from tinytorch.core.optimizers import Adam
|
||||
from tinytorch.core.data import Dataset, DataLoader
|
||||
@@ -240,7 +240,7 @@ class TestProgressiveStackIntegration:
|
||||
self.conv1 = Conv2D(in_channels=3, out_channels=16, kernel_size=3)
|
||||
self.pool = MaxPool2D(kernel_size=2)
|
||||
self.relu = ReLU()
|
||||
self.fc = Dense(16 * 15 * 15, 5) # Approximate size
|
||||
self.fc = Linear(16 * 15 * 15, 5) # Approximate size
|
||||
|
||||
def __call__(self, x):
|
||||
h = self.relu(self.conv1(x))
|
||||
@@ -297,12 +297,12 @@ class TestAdvancedTrainingFeatures:
|
||||
"""Test validation during training."""
|
||||
try:
|
||||
from tinytorch.core.training import Trainer
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import SGD
|
||||
from tinytorch.core.data import Dataset, DataLoader
|
||||
|
||||
# Model and optimizer
|
||||
model = Dense(5, 2)
|
||||
model = Linear(5, 2)
|
||||
optimizer = SGD(model.parameters(), lr=0.01)
|
||||
|
||||
# Train and validation datasets
|
||||
@@ -337,10 +337,10 @@ class TestAdvancedTrainingFeatures:
|
||||
"""Test model checkpointing and early stopping."""
|
||||
try:
|
||||
from tinytorch.core.training import Trainer, ModelCheckpoint, EarlyStopping
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import SGD
|
||||
|
||||
model = Dense(5, 1)
|
||||
model = Linear(5, 1)
|
||||
optimizer = SGD(model.parameters(), lr=0.01)
|
||||
|
||||
# Checkpointing
|
||||
@@ -366,9 +366,9 @@ class TestAdvancedTrainingFeatures:
|
||||
try:
|
||||
from tinytorch.core.training import LRScheduler, StepLR
|
||||
from tinytorch.core.optimizers import SGD
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
model = Dense(5, 1)
|
||||
model = Linear(5, 1)
|
||||
optimizer = SGD(model.parameters(), lr=0.1)
|
||||
|
||||
# Learning rate scheduler
|
||||
@@ -398,10 +398,10 @@ class TestProductionTrainingFeatures:
|
||||
"""Test distributed training capabilities."""
|
||||
try:
|
||||
from tinytorch.core.training import DistributedTrainer
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import SGD
|
||||
|
||||
model = Dense(10, 3)
|
||||
model = Linear(10, 3)
|
||||
optimizer = SGD(model.parameters(), lr=0.01)
|
||||
|
||||
# Distributed trainer (if available)
|
||||
@@ -416,10 +416,10 @@ class TestProductionTrainingFeatures:
|
||||
"""Test mixed precision training support."""
|
||||
try:
|
||||
from tinytorch.core.training import Trainer
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import Adam
|
||||
|
||||
model = Dense(20, 10)
|
||||
model = Linear(20, 10)
|
||||
optimizer = Adam(model.parameters(), lr=0.001)
|
||||
|
||||
# Mixed precision trainer
|
||||
@@ -435,10 +435,10 @@ class TestProductionTrainingFeatures:
|
||||
"""Test gradient accumulation for large effective batch sizes."""
|
||||
try:
|
||||
from tinytorch.core.training import Trainer
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import SGD
|
||||
|
||||
model = Dense(10, 3)
|
||||
model = Linear(10, 3)
|
||||
optimizer = SGD(model.parameters(), lr=0.01)
|
||||
|
||||
# Trainer with gradient accumulation
|
||||
@@ -462,12 +462,12 @@ class TestRegressionPrevention:
|
||||
# Complete pipeline should still work
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import SGD
|
||||
from tinytorch.core.data import Dataset
|
||||
|
||||
# All pipeline components should work
|
||||
layer = Dense(3, 2)
|
||||
layer = Linear(3, 2)
|
||||
optimizer = SGD(layer.parameters(), lr=0.01)
|
||||
|
||||
x = Tensor(np.random.randn(1, 3))
|
||||
@@ -523,11 +523,11 @@ class TestRegressionPrevention:
|
||||
# Complete ML pipeline level (if available)
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import SGD
|
||||
|
||||
# Complete training components should work together
|
||||
model = Dense(5, 2)
|
||||
model = Linear(5, 2)
|
||||
optimizer = SGD(model.parameters(), lr=0.01)
|
||||
|
||||
x = Tensor(np.random.randn(3, 5))
|
||||
|
||||
@@ -25,12 +25,12 @@ class TestPriorStackStillWorking:
|
||||
# Complete training should work
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import SGD
|
||||
from tinytorch.core.training import Trainer
|
||||
|
||||
# All training components should be available
|
||||
model = Dense(8, 3)
|
||||
model = Linear(8, 3)
|
||||
optimizer = SGD(model.parameters(), lr=0.01)
|
||||
trainer = Trainer(model, optimizer)
|
||||
|
||||
@@ -70,11 +70,11 @@ class TestModule12CompressionCore:
|
||||
"""Test quantization techniques."""
|
||||
try:
|
||||
from tinytorch.core.compression import Quantizer, quantize_weights
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Create model to quantize
|
||||
layer = Dense(10, 5)
|
||||
layer = Linear(10, 5)
|
||||
|
||||
# Test weight quantization
|
||||
if 'quantize_weights' in locals():
|
||||
@@ -105,10 +105,10 @@ class TestModule12CompressionCore:
|
||||
"""Test weight pruning and sparsity."""
|
||||
try:
|
||||
from tinytorch.core.compression import prune_weights, MagnitudePruner
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
# Create model to prune
|
||||
layer = Dense(20, 10)
|
||||
layer = Linear(20, 10)
|
||||
original_weights = layer.weights.data.copy()
|
||||
|
||||
# Test magnitude-based pruning
|
||||
@@ -144,12 +144,12 @@ class TestModule12CompressionCore:
|
||||
"""Test knowledge distillation compression."""
|
||||
try:
|
||||
from tinytorch.core.compression import DistillationLoss, KnowledgeDistiller
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Teacher and student models
|
||||
teacher = Dense(10, 5) # Large model
|
||||
student = Dense(10, 5) # Smaller model (same size for simplicity)
|
||||
teacher = Linear(10, 5) # Large model
|
||||
student = Linear(10, 5) # Smaller model (same size for simplicity)
|
||||
|
||||
# Test distillation loss
|
||||
if 'DistillationLoss' in locals():
|
||||
@@ -185,14 +185,14 @@ class TestProgressiveStackIntegration:
|
||||
"""Test training with compressed models."""
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import SGD
|
||||
from tinytorch.core.training import Trainer
|
||||
from tinytorch.core.compression import prune_weights, quantize_weights
|
||||
from tinytorch.core.data import Dataset, DataLoader
|
||||
|
||||
# Create model
|
||||
model = Dense(20, 5)
|
||||
model = Linear(20, 5)
|
||||
|
||||
# Apply compression
|
||||
if 'prune_weights' in locals():
|
||||
@@ -222,13 +222,13 @@ class TestProgressiveStackIntegration:
|
||||
"""Test compression with CNN models."""
|
||||
try:
|
||||
from tinytorch.core.spatial import Conv2D
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.compression import prune_weights, quantize_weights
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# CNN model
|
||||
conv1 = Conv2D(in_channels=3, out_channels=16, kernel_size=3)
|
||||
fc = Dense(16 * 30 * 30, 10) # Approximate size
|
||||
fc = Linear(16 * 30 * 30, 10) # Approximate size
|
||||
|
||||
# Apply compression to CNN
|
||||
if 'prune_weights' in locals() and hasattr(conv1, 'weights'):
|
||||
@@ -285,10 +285,10 @@ class TestEfficiencyAndPerformance:
|
||||
"""Test memory reduction from compression."""
|
||||
try:
|
||||
from tinytorch.core.compression import prune_weights, quantize_weights
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
# Large model for memory testing
|
||||
large_model = Dense(1000, 500)
|
||||
large_model = Linear(1000, 500)
|
||||
original_size = large_model.weights.data.nbytes
|
||||
|
||||
# Test pruning memory reduction
|
||||
@@ -319,16 +319,16 @@ class TestEfficiencyAndPerformance:
|
||||
"""Test inference speedup from compression."""
|
||||
try:
|
||||
from tinytorch.core.compression import prune_weights, quantize_weights
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
import time
|
||||
|
||||
# Model for speed testing
|
||||
model = Dense(500, 200)
|
||||
model = Linear(500, 200)
|
||||
|
||||
# Apply compression
|
||||
if 'prune_weights' in locals():
|
||||
compressed_model = Dense(500, 200)
|
||||
compressed_model = Linear(500, 200)
|
||||
compressed_model.weights = prune_weights(model.weights, sparsity=0.8)
|
||||
|
||||
# Test inference time (simplified)
|
||||
@@ -356,10 +356,10 @@ class TestEfficiencyAndPerformance:
|
||||
"""Test model size reduction techniques."""
|
||||
try:
|
||||
from tinytorch.core.compression import compress_model, ModelCompressor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
# Model to compress
|
||||
model = Dense(100, 50)
|
||||
model = Linear(100, 50)
|
||||
original_param_count = model.weights.data.size
|
||||
if hasattr(model, 'bias') and model.bias is not None:
|
||||
original_param_count += model.bias.data.size
|
||||
@@ -392,10 +392,10 @@ class TestProductionCompressionFeatures:
|
||||
"""Test gradual pruning during training."""
|
||||
try:
|
||||
from tinytorch.core.compression import GradualPruner
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import SGD
|
||||
|
||||
model = Dense(50, 20)
|
||||
model = Linear(50, 20)
|
||||
optimizer = SGD(model.parameters(), lr=0.01)
|
||||
|
||||
# Gradual pruner
|
||||
@@ -423,10 +423,10 @@ class TestProductionCompressionFeatures:
|
||||
try:
|
||||
from tinytorch.core.compression import MixedPrecisionCompressor
|
||||
from tinytorch.core.training import Trainer
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import Adam
|
||||
|
||||
model = Dense(30, 10)
|
||||
model = Linear(30, 10)
|
||||
optimizer = Adam(model.parameters(), lr=0.001)
|
||||
|
||||
# Mixed precision + compression
|
||||
@@ -449,10 +449,10 @@ class TestProductionCompressionFeatures:
|
||||
"""Test compression-aware training techniques."""
|
||||
try:
|
||||
from tinytorch.core.compression import CompressionAwareTrainer
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import SGD
|
||||
|
||||
model = Dense(40, 15)
|
||||
model = Linear(40, 15)
|
||||
optimizer = SGD(model.parameters(), lr=0.01)
|
||||
|
||||
# Compression-aware training
|
||||
@@ -482,12 +482,12 @@ class TestRegressionPrevention:
|
||||
# Training pipeline should still work
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import SGD
|
||||
from tinytorch.core.training import Trainer
|
||||
|
||||
# Complete training pipeline should work
|
||||
model = Dense(5, 3)
|
||||
model = Linear(5, 3)
|
||||
optimizer = SGD(model.parameters(), lr=0.01)
|
||||
trainer = Trainer(model, optimizer)
|
||||
|
||||
@@ -542,12 +542,12 @@ class TestRegressionPrevention:
|
||||
# Complete training pipeline level (if available)
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import SGD
|
||||
from tinytorch.core.training import Trainer
|
||||
|
||||
# Complete training system should work
|
||||
model = Dense(8, 4)
|
||||
model = Linear(8, 4)
|
||||
optimizer = SGD(model.parameters(), lr=0.01)
|
||||
trainer = Trainer(model, optimizer)
|
||||
|
||||
|
||||
@@ -17,15 +17,15 @@ class TestTrainingLoopIntegration:
|
||||
def test_basic_training_loop(self):
|
||||
"""Test basic training loop components work together."""
|
||||
try:
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Sigmoid
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.losses import MSELoss
|
||||
|
||||
# Build simple network
|
||||
layer1 = Dense(2, 4)
|
||||
layer1 = Linear(2, 4)
|
||||
relu = ReLU()
|
||||
layer2 = Dense(4, 1)
|
||||
layer2 = Linear(4, 1)
|
||||
sigmoid = Sigmoid()
|
||||
|
||||
# Create loss function
|
||||
@@ -53,11 +53,11 @@ class TestTrainingLoopIntegration:
|
||||
def test_optimizer_integration(self):
|
||||
"""Test optimizer works with model parameters."""
|
||||
try:
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import SGD
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
layer = Dense(10, 5)
|
||||
layer = Linear(10, 5)
|
||||
optimizer = SGD(learning_rate=0.01)
|
||||
|
||||
# Get parameters
|
||||
@@ -110,7 +110,7 @@ class TestDataLoaderIntegration:
|
||||
def test_batch_processing(self):
|
||||
"""Test training handles batches correctly."""
|
||||
try:
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.dataloader import DataLoader
|
||||
|
||||
@@ -121,7 +121,7 @@ class TestDataLoaderIntegration:
|
||||
dataset = list(zip(X, y))
|
||||
dataloader = DataLoader(dataset, batch_size=16, shuffle=True)
|
||||
|
||||
model = Dense(10, 1)
|
||||
model = Linear(10, 1)
|
||||
|
||||
# Process one batch
|
||||
for batch_X, batch_y in dataloader:
|
||||
@@ -140,11 +140,11 @@ class TestDataLoaderIntegration:
|
||||
def test_epoch_training(self):
|
||||
"""Test training for multiple epochs."""
|
||||
try:
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.losses import MSELoss
|
||||
|
||||
model = Dense(5, 1)
|
||||
model = Linear(5, 1)
|
||||
loss_fn = MSELoss()
|
||||
|
||||
# Training data
|
||||
@@ -206,9 +206,9 @@ class TestModelEvaluation:
|
||||
def test_model_evaluation_mode(self):
|
||||
"""Test model can switch between training and evaluation."""
|
||||
try:
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
model = Dense(10, 5)
|
||||
model = Linear(10, 5)
|
||||
|
||||
# Check if model has train/eval methods
|
||||
if hasattr(model, 'train') and hasattr(model, 'eval'):
|
||||
@@ -231,7 +231,7 @@ class TestCompleteMLPipeline:
|
||||
def test_xor_training_pipeline(self):
|
||||
"""Test complete XOR training pipeline."""
|
||||
try:
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Sigmoid
|
||||
from tinytorch.core.losses import MSELoss
|
||||
from tinytorch.core.tensor import Tensor
|
||||
@@ -241,9 +241,9 @@ class TestCompleteMLPipeline:
|
||||
y = Tensor(np.array([[0], [1], [1], [0]], dtype=np.float32))
|
||||
|
||||
# Build network
|
||||
hidden = Dense(2, 4, use_bias=True)
|
||||
hidden = Linear(2, 4, use_bias=True)
|
||||
relu = ReLU()
|
||||
output = Dense(4, 1, use_bias=True)
|
||||
output = Linear(4, 1, use_bias=True)
|
||||
sigmoid = Sigmoid()
|
||||
|
||||
loss_fn = MSELoss()
|
||||
|
||||
@@ -25,13 +25,13 @@ class TestPriorStackStillWorking:
|
||||
# Complete ML system should work
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import Adam
|
||||
from tinytorch.core.training import Trainer
|
||||
from tinytorch.core.compression import prune_weights
|
||||
|
||||
# All ML system components should be available
|
||||
model = Dense(10, 5)
|
||||
model = Linear(10, 5)
|
||||
optimizer = Adam(model.parameters(), lr=0.001)
|
||||
trainer = Trainer(model, optimizer)
|
||||
|
||||
@@ -54,10 +54,10 @@ class TestPriorStackStillWorking:
|
||||
from tinytorch.core.training import Trainer
|
||||
from tinytorch.core.compression import quantize_weights
|
||||
from tinytorch.core.optimizers import SGD
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
# Efficiency features should work
|
||||
model = Dense(8, 3)
|
||||
model = Linear(8, 3)
|
||||
optimizer = SGD(model.parameters(), lr=0.01)
|
||||
trainer = Trainer(model, optimizer)
|
||||
|
||||
@@ -180,7 +180,7 @@ class TestProgressiveStackIntegration:
|
||||
"""Test training pipeline with kernel acceleration."""
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import Adam
|
||||
from tinytorch.core.training import Trainer
|
||||
from tinytorch.core.kernels import enable_optimizations
|
||||
@@ -193,9 +193,9 @@ class TestProgressiveStackIntegration:
|
||||
# Create accelerated training pipeline
|
||||
class AcceleratedModel:
|
||||
def __init__(self):
|
||||
self.layer1 = Dense(50, 100)
|
||||
self.layer2 = Dense(100, 20)
|
||||
self.layer3 = Dense(20, 5)
|
||||
self.layer1 = Linear(50, 100)
|
||||
self.layer2 = Linear(100, 20)
|
||||
self.layer3 = Linear(20, 5)
|
||||
|
||||
def __call__(self, x):
|
||||
h1 = self.layer1(x)
|
||||
@@ -514,9 +514,9 @@ class TestHardwareAcceleration:
|
||||
# Data parallel operations
|
||||
if 'data_parallel' in locals():
|
||||
# Test data parallel wrapper
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
model = Dense(10, 5)
|
||||
model = Linear(10, 5)
|
||||
parallel_model = data_parallel(model, device_ids=[0]) # Single device for testing
|
||||
|
||||
assert hasattr(parallel_model, 'forward'), "Data parallel wrapper broken"
|
||||
@@ -536,13 +536,13 @@ class TestRegressionPrevention:
|
||||
# Complete ML system should still work
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import Adam
|
||||
from tinytorch.core.training import Trainer
|
||||
from tinytorch.core.compression import prune_weights
|
||||
|
||||
# All components should work together
|
||||
model = Dense(8, 4)
|
||||
model = Linear(8, 4)
|
||||
optimizer = Adam(model.parameters(), lr=0.001)
|
||||
trainer = Trainer(model, optimizer)
|
||||
|
||||
@@ -565,10 +565,10 @@ class TestRegressionPrevention:
|
||||
from tinytorch.core.training import Trainer
|
||||
from tinytorch.core.compression import quantize_weights
|
||||
from tinytorch.core.optimizers import SGD
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
# Efficiency features should still work
|
||||
model = Dense(6, 3)
|
||||
model = Linear(6, 3)
|
||||
optimizer = SGD(model.parameters(), lr=0.01)
|
||||
trainer = Trainer(model, optimizer)
|
||||
|
||||
@@ -595,12 +595,12 @@ class TestRegressionPrevention:
|
||||
# Complete ML system level (if available)
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import Adam
|
||||
from tinytorch.core.training import Trainer
|
||||
|
||||
# Complete system should work
|
||||
model = Dense(10, 5)
|
||||
model = Linear(10, 5)
|
||||
optimizer = Adam(model.parameters(), lr=0.001)
|
||||
trainer = Trainer(model, optimizer)
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ class TestCompleteMLSystemStillWorks:
|
||||
# Test complete ML system still works
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.spatial import Conv2D, MaxPool2D
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Softmax
|
||||
from tinytorch.core.optimizers import Adam
|
||||
from tinytorch.core.training import Trainer
|
||||
@@ -69,7 +69,7 @@ class TestCompleteMLSystemStillWorks:
|
||||
self.conv1 = Conv2D(3, 16, kernel_size=3, padding=1)
|
||||
self.pool = MaxPool2D(kernel_size=2)
|
||||
self.conv2 = Conv2D(16, 32, kernel_size=3, padding=1)
|
||||
self.fc = Dense(32 * 8 * 8, 10)
|
||||
self.fc = Linear(32 * 8 * 8, 10)
|
||||
self.relu = ReLU()
|
||||
self.softmax = Softmax()
|
||||
|
||||
@@ -188,13 +188,13 @@ class TestCompleteMLSystemStillWorks:
|
||||
"""
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import SGD
|
||||
from tinytorch.core.training import Trainer, MSELoss
|
||||
from tinytorch.core.autograd import Variable
|
||||
|
||||
# Test training system
|
||||
model = Dense(5, 3)
|
||||
model = Linear(5, 3)
|
||||
optimizer = SGD(model.parameters(), lr=0.01)
|
||||
loss_fn = MSELoss()
|
||||
trainer = Trainer(model, optimizer)
|
||||
@@ -264,11 +264,11 @@ class TestModule14BenchmarkingCore:
|
||||
"""
|
||||
try:
|
||||
from tinytorch.core.benchmarking import benchmark_model
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Test model benchmarking
|
||||
model = Dense(100, 50)
|
||||
model = Linear(100, 50)
|
||||
input_shape = (32, 100) # Batch size 32, 100 features
|
||||
|
||||
# Run benchmark
|
||||
@@ -398,16 +398,16 @@ class TestModule14BenchmarkingCore:
|
||||
"""
|
||||
try:
|
||||
from tinytorch.core.benchmarking import profile_model, ProfilerContext
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Test profiling functionality
|
||||
class TestModel:
|
||||
def __init__(self):
|
||||
self.layer1 = Dense(100, 200)
|
||||
self.layer1 = Linear(100, 200)
|
||||
self.relu = ReLU()
|
||||
self.layer2 = Dense(200, 50)
|
||||
self.layer2 = Linear(200, 50)
|
||||
|
||||
def __call__(self, x):
|
||||
h1 = self.layer1(x)
|
||||
@@ -538,12 +538,12 @@ class TestModule14BenchmarkingCore:
|
||||
"""
|
||||
try:
|
||||
from tinytorch.core.benchmarking import compare_models, PerformanceComparator
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Test model comparison
|
||||
model_small = Dense(50, 25)
|
||||
model_large = Dense(100, 50)
|
||||
model_small = Linear(50, 25)
|
||||
model_large = Linear(100, 50)
|
||||
|
||||
input_shape = (16, 50)
|
||||
|
||||
@@ -682,13 +682,13 @@ class TestBenchmarkingIntegration:
|
||||
try:
|
||||
from tinytorch.core.benchmarking import benchmark_training
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import SGD
|
||||
from tinytorch.core.training import Trainer, MSELoss
|
||||
from tinytorch.core.data import Dataset, DataLoader
|
||||
|
||||
# Create training setup
|
||||
model = Dense(50, 10)
|
||||
model = Linear(50, 10)
|
||||
optimizer = SGD(model.parameters(), lr=0.01)
|
||||
loss_fn = MSELoss()
|
||||
trainer = Trainer(model, optimizer)
|
||||
@@ -824,7 +824,7 @@ class TestBenchmarkingIntegration:
|
||||
from tinytorch.core.benchmarking import benchmark_inference
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.spatial import Conv2D, MaxPool2D
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
|
||||
# Create inference model (CNN for image classification)
|
||||
@@ -833,7 +833,7 @@ class TestBenchmarkingIntegration:
|
||||
self.conv1 = Conv2D(3, 32, kernel_size=3, padding=1)
|
||||
self.pool = MaxPool2D(kernel_size=2)
|
||||
self.conv2 = Conv2D(32, 64, kernel_size=3, padding=1)
|
||||
self.fc = Dense(64 * 8 * 8, 1000) # ImageNet-like
|
||||
self.fc = Linear(64 * 8 * 8, 1000) # ImageNet-like
|
||||
self.relu = ReLU()
|
||||
|
||||
def __call__(self, x):
|
||||
@@ -968,12 +968,12 @@ class TestBenchmarkingIntegration:
|
||||
try:
|
||||
from tinytorch.core.benchmarking import analyze_hardware_performance
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.spatial import Conv2D
|
||||
|
||||
# Test hardware analysis
|
||||
models_for_analysis = [
|
||||
("cpu_intensive", Dense(1000, 1000)), # CPU-friendly
|
||||
("cpu_intensive", Linear(1000, 1000)), # CPU-friendly
|
||||
("memory_intensive", Conv2D(3, 512, 7)), # Memory-bound
|
||||
]
|
||||
|
||||
@@ -999,7 +999,7 @@ class TestBenchmarkingIntegration:
|
||||
|
||||
# Test basic hardware characteristic analysis
|
||||
# CPU-intensive model (many small operations)
|
||||
cpu_model = Dense(500, 500)
|
||||
cpu_model = Linear(500, 500)
|
||||
cpu_input = Tensor(np.random.randn(1, 500))
|
||||
|
||||
# Memory-intensive model (large feature maps)
|
||||
@@ -1111,9 +1111,9 @@ class TestModule14Completion:
|
||||
try:
|
||||
# Test 1: Model benchmarking
|
||||
from tinytorch.core.benchmarking import benchmark_model
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
model = Dense(50, 25)
|
||||
model = Linear(50, 25)
|
||||
results = benchmark_model(model, (16, 50))
|
||||
assert 'latency' in results and 'throughput' in results
|
||||
benchmarking_capabilities["Model benchmarking"] = True
|
||||
@@ -1137,8 +1137,8 @@ class TestModule14Completion:
|
||||
benchmarking_capabilities["Performance comparison"] = True
|
||||
except ImportError:
|
||||
# Can compare manually
|
||||
model1_results = benchmark_model(Dense(25, 10), (8, 25))
|
||||
model2_results = benchmark_model(Dense(50, 10), (8, 50))
|
||||
model1_results = benchmark_model(Linear(25, 10), (8, 25))
|
||||
model2_results = benchmark_model(Linear(50, 10), (8, 50))
|
||||
assert model1_results['latency'] != model2_results['latency']
|
||||
benchmarking_capabilities["Performance comparison"] = True
|
||||
|
||||
@@ -1174,8 +1174,8 @@ class TestModule14Completion:
|
||||
|
||||
# Test 6: Hardware analysis (basic structure)
|
||||
# Analyze different model sizes
|
||||
small_model = Dense(10, 5)
|
||||
large_model = Dense(1000, 500)
|
||||
small_model = Linear(10, 5)
|
||||
large_model = Linear(1000, 500)
|
||||
|
||||
small_results = benchmark_model(small_model, (4, 10))
|
||||
large_results = benchmark_model(large_model, (4, 1000))
|
||||
@@ -1192,7 +1192,7 @@ class TestModule14Completion:
|
||||
class ComplexModel:
|
||||
def __init__(self):
|
||||
self.conv = Conv2D(3, 16, 3)
|
||||
self.dense = Dense(16 * 30 * 30, 10)
|
||||
self.dense = Linear(16 * 30 * 30, 10)
|
||||
self.relu = ReLU()
|
||||
|
||||
def __call__(self, x):
|
||||
|
||||
@@ -57,7 +57,7 @@ class TestCompleteTinyTorchSystemStillWorks:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.spatial import Conv2D, MaxPool2D
|
||||
from tinytorch.core.attention import MultiHeadAttention
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Softmax
|
||||
from tinytorch.core.optimizers import Adam
|
||||
from tinytorch.core.training import Trainer
|
||||
@@ -71,15 +71,15 @@ class TestCompleteTinyTorchSystemStillWorks:
|
||||
# Vision pathway
|
||||
self.vision_conv = Conv2D(3, 64, kernel_size=3, padding=1)
|
||||
self.vision_pool = MaxPool2D(kernel_size=2)
|
||||
self.vision_proj = Dense(64 * 16 * 16, 256)
|
||||
self.vision_proj = Linear(64 * 16 * 16, 256)
|
||||
|
||||
# Language pathway
|
||||
self.language_embed = Dense(1000, 256) # vocab_size=1000
|
||||
self.language_embed = Linear(1000, 256) # vocab_size=1000
|
||||
self.attention = MultiHeadAttention(embed_dim=256, num_heads=8)
|
||||
|
||||
# Fusion
|
||||
self.fusion = Dense(512, 128)
|
||||
self.classifier = Dense(128, 10)
|
||||
self.fusion = Linear(512, 128)
|
||||
self.classifier = Linear(128, 10)
|
||||
|
||||
# Activations
|
||||
self.relu = ReLU()
|
||||
@@ -221,13 +221,13 @@ class TestCompleteTinyTorchSystemStillWorks:
|
||||
"""
|
||||
try:
|
||||
from tinytorch.core.benchmarking import benchmark_model
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.spatial import Conv2D
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Test that benchmarking still works
|
||||
models_to_benchmark = [
|
||||
("dense_model", Dense(100, 50)),
|
||||
("dense_model", Linear(100, 50)),
|
||||
("conv_model", Conv2D(3, 16, kernel_size=3))
|
||||
]
|
||||
|
||||
@@ -307,11 +307,11 @@ class TestModule15MLOpsCore:
|
||||
"""
|
||||
try:
|
||||
from tinytorch.core.mlops import ModelMonitor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Test model monitoring setup
|
||||
model = Dense(50, 10)
|
||||
model = Linear(50, 10)
|
||||
monitor = ModelMonitor(model, metrics=['accuracy', 'latency', 'drift'])
|
||||
|
||||
# Should track the model
|
||||
@@ -493,11 +493,11 @@ class TestModule15MLOpsCore:
|
||||
"""
|
||||
try:
|
||||
from tinytorch.core.mlops import ModelServer, deploy_model
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Test model deployment
|
||||
model = Dense(20, 5)
|
||||
model = Linear(20, 5)
|
||||
|
||||
# Test model server
|
||||
if 'ModelServer' in locals():
|
||||
@@ -665,7 +665,7 @@ class TestModule15MLOpsCore:
|
||||
try:
|
||||
from tinytorch.core.mlops import MLPipeline, PipelineStep
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import SGD
|
||||
from tinytorch.core.training import Trainer
|
||||
|
||||
@@ -717,7 +717,7 @@ class TestModule15MLOpsCore:
|
||||
# Test simpler pipeline coordination
|
||||
# Simulate ML pipeline steps
|
||||
pipeline_state = {
|
||||
'model': Dense(10, 3),
|
||||
'model': Linear(10, 3),
|
||||
'optimizer': None,
|
||||
'trainer': None,
|
||||
'metrics': {},
|
||||
@@ -839,7 +839,7 @@ class TestMLOpsIntegration:
|
||||
"""
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import Adam
|
||||
from tinytorch.core.training import Trainer, MSELoss
|
||||
from tinytorch.core.data import Dataset, DataLoader
|
||||
@@ -849,7 +849,7 @@ class TestMLOpsIntegration:
|
||||
# Production ML workflow simulation
|
||||
|
||||
# Step 1: Model Development
|
||||
model = Dense(50, 10)
|
||||
model = Linear(50, 10)
|
||||
optimizer = Adam(model.parameters(), lr=0.001)
|
||||
loss_fn = MSELoss()
|
||||
trainer = Trainer(model, optimizer)
|
||||
@@ -991,7 +991,7 @@ class TestMLOpsIntegration:
|
||||
try:
|
||||
from tinytorch.core.mlops import ModelValidator, DataValidator
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.benchmarking import benchmark_model
|
||||
|
||||
# CI/ML workflow simulation
|
||||
@@ -1008,7 +1008,7 @@ class TestMLOpsIntegration:
|
||||
f"❌ Data validation failed: {validation_result.get('errors')}"
|
||||
|
||||
# Step 2: Model Testing
|
||||
model = Dense(50, 10)
|
||||
model = Linear(50, 10)
|
||||
|
||||
if 'ModelValidator' in locals():
|
||||
model_validator = ModelValidator()
|
||||
@@ -1169,7 +1169,7 @@ class TestMLOpsIntegration:
|
||||
"""
|
||||
try:
|
||||
from tinytorch.core.mlops import ModelRegistry, ABTestManager
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Model lifecycle management
|
||||
@@ -1179,8 +1179,8 @@ class TestMLOpsIntegration:
|
||||
registry = ModelRegistry()
|
||||
|
||||
# Register models
|
||||
model_v1 = Dense(50, 10)
|
||||
model_v2 = Dense(50, 10) # Improved version
|
||||
model_v1 = Linear(50, 10)
|
||||
model_v2 = Linear(50, 10) # Improved version
|
||||
|
||||
registry.register_model("production_classifier", model_v1, version="1.0")
|
||||
registry.register_model("production_classifier", model_v2, version="2.0")
|
||||
@@ -1200,8 +1200,8 @@ class TestMLOpsIntegration:
|
||||
ab_manager = ABTestManager()
|
||||
|
||||
# Setup A/B test
|
||||
model_a = Dense(50, 10) # Current model
|
||||
model_b = Dense(50, 10) # New model
|
||||
model_a = Linear(50, 10) # Current model
|
||||
model_b = Linear(50, 10) # New model
|
||||
|
||||
ab_manager.setup_test("classifier_experiment",
|
||||
model_a=model_a,
|
||||
@@ -1228,9 +1228,9 @@ class TestMLOpsIntegration:
|
||||
# Step 3: Manual lifecycle simulation
|
||||
lifecycle_state = {
|
||||
'models': {
|
||||
'v1.0': Dense(50, 10),
|
||||
'v2.0': Dense(50, 10),
|
||||
'v2.1': Dense(50, 10),
|
||||
'v1.0': Linear(50, 10),
|
||||
'v2.0': Linear(50, 10),
|
||||
'v2.1': Linear(50, 10),
|
||||
},
|
||||
'current_version': 'v2.1',
|
||||
'rollback_version': 'v2.0',
|
||||
@@ -1396,10 +1396,10 @@ class TestModule15Completion:
|
||||
try:
|
||||
# Test 1: Model monitoring
|
||||
from tinytorch.core.mlops import ModelMonitor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
model = Dense(20, 5)
|
||||
model = Linear(20, 5)
|
||||
monitor = ModelMonitor(model)
|
||||
|
||||
# Test monitoring functionality
|
||||
@@ -1458,9 +1458,9 @@ class TestModule15Completion:
|
||||
# Test 5: Lifecycle management
|
||||
# Model versioning simulation
|
||||
model_versions = {
|
||||
'v1.0': Dense(20, 5),
|
||||
'v2.0': Dense(20, 5),
|
||||
'v2.1': Dense(20, 5)
|
||||
'v1.0': Linear(20, 5),
|
||||
'v2.0': Linear(20, 5),
|
||||
'v2.1': Linear(20, 5)
|
||||
}
|
||||
|
||||
current_version = 'v2.1'
|
||||
@@ -1481,14 +1481,14 @@ class TestModule15Completion:
|
||||
from tinytorch.core.compression import prune_weights
|
||||
|
||||
# Model optimization
|
||||
original_model = Dense(100, 50)
|
||||
original_model = Linear(100, 50)
|
||||
optimized_weights = prune_weights(original_model.weights, sparsity=0.3)
|
||||
|
||||
# Performance comparison
|
||||
original_results = benchmark_model(original_model, (16, 100))
|
||||
|
||||
# Optimized model should maintain functionality
|
||||
optimized_model = Dense(100, 50)
|
||||
optimized_model = Linear(100, 50)
|
||||
optimized_model.weights = optimized_weights
|
||||
|
||||
optimized_input = Tensor(np.random.randn(4, 100))
|
||||
|
||||
@@ -25,7 +25,7 @@ class TestEntireTinyTorchSystemStable:
|
||||
# Complete production ML system should work
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.spatial import Conv2D
|
||||
from tinytorch.core.attention import MultiHeadAttention
|
||||
from tinytorch.core.optimizers import Adam
|
||||
@@ -35,7 +35,7 @@ class TestEntireTinyTorchSystemStable:
|
||||
from tinytorch.core.mlops import ModelMonitor
|
||||
|
||||
# Foundation components
|
||||
model = Dense(16, 8)
|
||||
model = Linear(16, 8)
|
||||
x = Tensor(np.random.randn(4, 16))
|
||||
output = model(x)
|
||||
assert output.shape == (4, 8), "Foundation broken"
|
||||
@@ -347,7 +347,7 @@ class TestCompleteSystemIntegration:
|
||||
try:
|
||||
from tinytorch.core.transformers import TinyGPT
|
||||
from tinytorch.core.spatial import Conv2D, MaxPool2D
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.attention import MultiHeadAttention
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
@@ -360,7 +360,7 @@ class TestCompleteSystemIntegration:
|
||||
self.conv2 = Conv2D(16, 32, kernel_size=3)
|
||||
|
||||
# Vision to language bridge
|
||||
self.vision_proj = Dense(32 * 7 * 7, 128) # Approximate after conv/pool
|
||||
self.vision_proj = Linear(32 * 7 * 7, 128) # Approximate after conv/pool
|
||||
|
||||
# Language model
|
||||
self.language_model = TinyGPT(
|
||||
@@ -421,7 +421,7 @@ class TestCapstoneSystemValidation:
|
||||
"""Test that students can build complete ML systems from scratch."""
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Softmax
|
||||
from tinytorch.core.spatial import Conv2D
|
||||
from tinytorch.core.attention import MultiHeadAttention
|
||||
@@ -433,7 +433,7 @@ class TestCapstoneSystemValidation:
|
||||
# Students should be able to build:
|
||||
|
||||
# 1. Classic neural networks
|
||||
mlp = Dense(784, 10)
|
||||
mlp = Linear(784, 10)
|
||||
x_mlp = Tensor(np.random.randn(32, 784))
|
||||
out_mlp = mlp(x_mlp)
|
||||
assert out_mlp.shape == (32, 10), "MLP capability broken"
|
||||
@@ -476,9 +476,9 @@ class TestCapstoneSystemValidation:
|
||||
from tinytorch.core.kernels import optimized_matmul, enable_optimizations
|
||||
from tinytorch.core.benchmarking import benchmark_model, profile_memory
|
||||
from tinytorch.core.mlops import ModelMonitor, deploy_model
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
model = Dense(100, 50)
|
||||
model = Linear(100, 50)
|
||||
|
||||
# Students should understand:
|
||||
|
||||
@@ -706,12 +706,12 @@ class TestRegressionPrevention:
|
||||
# Complete system should still work perfectly
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import Adam
|
||||
from tinytorch.core.training import Trainer
|
||||
|
||||
# Complete system integration
|
||||
model = Dense(16, 8)
|
||||
model = Linear(16, 8)
|
||||
optimizer = Adam(model.parameters(), lr=0.001)
|
||||
trainer = Trainer(model, optimizer)
|
||||
|
||||
@@ -744,14 +744,14 @@ class TestRegressionPrevention:
|
||||
# Complete ML system level (if available)
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.optimizers import Adam
|
||||
from tinytorch.core.training import Trainer
|
||||
from tinytorch.core.compression import prune_weights
|
||||
from tinytorch.core.kernels import optimized_matmul
|
||||
|
||||
# Complete production system should work
|
||||
model = Dense(20, 10)
|
||||
model = Linear(20, 10)
|
||||
optimizer = Adam(model.parameters(), lr=0.001)
|
||||
trainer = Trainer(model, optimizer)
|
||||
|
||||
|
||||
@@ -19,14 +19,14 @@ def test_checkpoint_03_components():
|
||||
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
except ImportError as e:
|
||||
pytest.fail(f"❌ Cannot import required classes - complete Modules 2-4 first: {e}")
|
||||
|
||||
# Test 1: Dense layer creation with parameters
|
||||
print("🔧 Testing Dense layer creation...")
|
||||
layer = Dense(input_size=10, output_size=5)
|
||||
layer = Linear(input_size=10, output_size=5)
|
||||
|
||||
assert hasattr(layer, 'weights'), "Dense layer should have weights"
|
||||
assert hasattr(layer, 'bias'), "Dense layer should have bias"
|
||||
@@ -78,8 +78,8 @@ def test_checkpoint_03_components():
|
||||
|
||||
# Test 6: Multiple layer types
|
||||
print("🏗️ Testing different layer configurations...")
|
||||
small_layer = Dense(5, 3)
|
||||
large_layer = Dense(100, 50)
|
||||
small_layer = Linear(5, 3)
|
||||
large_layer = Linear(100, 50)
|
||||
|
||||
small_test = Tensor(np.random.randn(2, 5))
|
||||
large_test = Tensor(np.random.randn(1, 100))
|
||||
|
||||
@@ -19,15 +19,15 @@ def test_checkpoint_04_networks():
|
||||
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Sigmoid
|
||||
except ImportError as e:
|
||||
pytest.fail(f"❌ Cannot import required classes - complete Modules 2-5 first: {e}")
|
||||
|
||||
# Test 1: Simple 2-layer network
|
||||
print("🏗️ Testing 2-layer network construction...")
|
||||
input_layer = Dense(input_size=4, output_size=8)
|
||||
output_layer = Dense(input_size=8, output_size=3)
|
||||
input_layer = Linear(input_size=4, output_size=8)
|
||||
output_layer = Linear(input_size=8, output_size=3)
|
||||
activation = ReLU()
|
||||
|
||||
# Test network architecture
|
||||
@@ -43,10 +43,10 @@ def test_checkpoint_04_networks():
|
||||
|
||||
# Test 2: Deep network (3+ layers)
|
||||
print("🏢 Testing deep network construction...")
|
||||
layer1 = Dense(10, 16)
|
||||
layer2 = Dense(16, 8)
|
||||
layer3 = Dense(8, 4)
|
||||
layer4 = Dense(4, 1)
|
||||
layer1 = Linear(10, 16)
|
||||
layer2 = Linear(16, 8)
|
||||
layer3 = Linear(8, 4)
|
||||
layer4 = Linear(4, 1)
|
||||
relu = ReLU()
|
||||
sigmoid = Sigmoid()
|
||||
|
||||
@@ -68,20 +68,20 @@ def test_checkpoint_04_networks():
|
||||
|
||||
# Wide network
|
||||
wide_net = [
|
||||
Dense(5, 50),
|
||||
Linear(5, 50),
|
||||
ReLU(),
|
||||
Dense(50, 50),
|
||||
Linear(50, 50),
|
||||
ReLU(),
|
||||
Dense(50, 10)
|
||||
Linear(50, 10)
|
||||
]
|
||||
|
||||
# Narrow network
|
||||
narrow_net = [
|
||||
Dense(20, 10),
|
||||
Linear(20, 10),
|
||||
ReLU(),
|
||||
Dense(10, 5),
|
||||
Linear(10, 5),
|
||||
ReLU(),
|
||||
Dense(5, 2)
|
||||
Linear(5, 2)
|
||||
]
|
||||
|
||||
# Test both architectures
|
||||
@@ -137,11 +137,11 @@ def test_checkpoint_04_networks():
|
||||
|
||||
# Simple approximator network
|
||||
approx_net = [
|
||||
Dense(1, 5),
|
||||
Linear(1, 5),
|
||||
ReLU(),
|
||||
Dense(5, 5),
|
||||
Linear(5, 5),
|
||||
ReLU(),
|
||||
Dense(5, 1)
|
||||
Linear(5, 1)
|
||||
]
|
||||
|
||||
# Test that network can process the data
|
||||
|
||||
@@ -20,7 +20,7 @@ def test_checkpoint_06_attention():
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.attention import MultiHeadAttention, ScaledDotProductAttention
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
except ImportError as e:
|
||||
pytest.fail(f"❌ Cannot import required classes - complete Modules 2-7 first: {e}")
|
||||
|
||||
@@ -129,8 +129,8 @@ def test_checkpoint_06_attention():
|
||||
attention_layer = MultiHeadAttention(d_model=d_model, num_heads=3)
|
||||
|
||||
# Feed-forward layers
|
||||
ff1 = Dense(d_model, d_model * 4) # Expansion
|
||||
ff2 = Dense(d_model * 4, d_model) # Projection back
|
||||
ff1 = Linear(d_model, d_model * 4) # Expansion
|
||||
ff2 = Linear(d_model * 4, d_model) # Projection back
|
||||
|
||||
# Build transformer block: Attention → FFN
|
||||
attended = attention_layer(input_seq, input_seq, input_seq)
|
||||
|
||||
@@ -20,7 +20,7 @@ def test_checkpoint_07_stability():
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.normalization import BatchNorm1D, LayerNorm
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
except ImportError as e:
|
||||
pytest.fail(f"❌ Cannot import required classes - complete Modules 2-8 first: {e}")
|
||||
@@ -69,16 +69,16 @@ def test_checkpoint_07_stability():
|
||||
|
||||
# Build deep network with normalization
|
||||
layers = [
|
||||
Dense(16, 32),
|
||||
Linear(16, 32),
|
||||
BatchNorm1D(32),
|
||||
ReLU(),
|
||||
Dense(32, 32),
|
||||
Linear(32, 32),
|
||||
BatchNorm1D(32),
|
||||
ReLU(),
|
||||
Dense(32, 16),
|
||||
Linear(32, 16),
|
||||
BatchNorm1D(16),
|
||||
ReLU(),
|
||||
Dense(16, 1)
|
||||
Linear(16, 1)
|
||||
]
|
||||
|
||||
# Test forward pass through deep normalized network
|
||||
@@ -101,21 +101,21 @@ def test_checkpoint_07_stability():
|
||||
# Compare networks with and without normalization
|
||||
# Create identical architectures
|
||||
normalized_net = [
|
||||
Dense(10, 20),
|
||||
Linear(10, 20),
|
||||
BatchNorm1D(20),
|
||||
ReLU(),
|
||||
Dense(20, 10),
|
||||
Linear(20, 10),
|
||||
BatchNorm1D(10),
|
||||
ReLU(),
|
||||
Dense(10, 1)
|
||||
Linear(10, 1)
|
||||
]
|
||||
|
||||
unnormalized_net = [
|
||||
Dense(10, 20),
|
||||
Linear(10, 20),
|
||||
ReLU(),
|
||||
Dense(20, 10),
|
||||
Linear(20, 10),
|
||||
ReLU(),
|
||||
Dense(10, 1)
|
||||
Linear(10, 1)
|
||||
]
|
||||
|
||||
test_input = Tensor(np.random.randn(5, 10))
|
||||
|
||||
@@ -19,7 +19,7 @@ def test_checkpoint_08_differentiation():
|
||||
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Sigmoid
|
||||
from tinytorch.core.losses import MeanSquaredError
|
||||
except ImportError as e:
|
||||
@@ -50,7 +50,7 @@ def test_checkpoint_08_differentiation():
|
||||
print("🧠 Testing neural network gradients...")
|
||||
|
||||
# Create simple network
|
||||
layer = Dense(input_size=2, output_size=1)
|
||||
layer = Linear(input_size=2, output_size=1)
|
||||
activation = Sigmoid()
|
||||
loss_fn = MeanSquaredError()
|
||||
|
||||
@@ -96,9 +96,9 @@ def test_checkpoint_08_differentiation():
|
||||
print("🏗️ Testing multi-layer network gradients...")
|
||||
|
||||
# Build deeper network
|
||||
layer1 = Dense(3, 5)
|
||||
layer2 = Dense(5, 2)
|
||||
layer3 = Dense(2, 1)
|
||||
layer1 = Linear(3, 5)
|
||||
layer2 = Linear(5, 2)
|
||||
layer3 = Linear(2, 1)
|
||||
relu = ReLU()
|
||||
|
||||
# Enable gradients for all parameters
|
||||
|
||||
@@ -19,7 +19,7 @@ def test_checkpoint_09_optimization():
|
||||
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
from tinytorch.core.losses import MeanSquaredError
|
||||
from tinytorch.core.optimizers import SGD, Adam, RMSprop
|
||||
@@ -30,7 +30,7 @@ def test_checkpoint_09_optimization():
|
||||
print("📈 Testing SGD optimizer...")
|
||||
|
||||
# Create simple model and data
|
||||
model = Dense(2, 1)
|
||||
model = Linear(2, 1)
|
||||
model.weights.requires_grad = True
|
||||
model.bias.requires_grad = True
|
||||
|
||||
@@ -62,7 +62,7 @@ def test_checkpoint_09_optimization():
|
||||
print("🚀 Testing Adam optimizer...")
|
||||
|
||||
# Reset model
|
||||
model_adam = Dense(2, 1)
|
||||
model_adam = Linear(2, 1)
|
||||
model_adam.weights.requires_grad = True
|
||||
model_adam.bias.requires_grad = True
|
||||
|
||||
@@ -92,7 +92,7 @@ def test_checkpoint_09_optimization():
|
||||
# Test 3: RMSprop optimizer
|
||||
print("📊 Testing RMSprop optimizer...")
|
||||
|
||||
model_rms = Dense(2, 1)
|
||||
model_rms = Linear(2, 1)
|
||||
model_rms.weights.requires_grad = True
|
||||
model_rms.bias.requires_grad = True
|
||||
|
||||
@@ -116,8 +116,8 @@ def test_checkpoint_09_optimization():
|
||||
lr_small = 0.001
|
||||
lr_large = 0.1
|
||||
|
||||
model_small = Dense(2, 1)
|
||||
model_large = Dense(2, 1)
|
||||
model_small = Linear(2, 1)
|
||||
model_large = Linear(2, 1)
|
||||
|
||||
# Make models identical initially
|
||||
model_large.weights.data = model_small.weights.data.copy()
|
||||
@@ -152,7 +152,7 @@ def test_checkpoint_09_optimization():
|
||||
print("💾 Testing optimizer state...")
|
||||
|
||||
# Adam maintains moving averages
|
||||
model_state = Dense(1, 1)
|
||||
model_state = Linear(1, 1)
|
||||
model_state.weights.requires_grad = True
|
||||
model_state.bias.requires_grad = True
|
||||
|
||||
@@ -179,8 +179,8 @@ def test_checkpoint_09_optimization():
|
||||
print("🎛️ Testing parameter groups...")
|
||||
|
||||
# Create model with different parameter groups
|
||||
layer1 = Dense(3, 4)
|
||||
layer2 = Dense(4, 1)
|
||||
layer1 = Linear(3, 4)
|
||||
layer2 = Linear(4, 1)
|
||||
|
||||
layer1.weights.requires_grad = True
|
||||
layer1.bias.requires_grad = True
|
||||
@@ -213,7 +213,7 @@ def test_checkpoint_09_optimization():
|
||||
print("🎯 Testing convergence...")
|
||||
|
||||
# Simple linear regression: learn y = 2x + 1
|
||||
model_conv = Dense(1, 1)
|
||||
model_conv = Linear(1, 1)
|
||||
model_conv.weights.requires_grad = True
|
||||
model_conv.bias.requires_grad = True
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ def test_checkpoint_10_training():
|
||||
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Sigmoid
|
||||
from tinytorch.core.losses import MeanSquaredError, BinaryCrossEntropy
|
||||
from tinytorch.core.optimizers import Adam, SGD
|
||||
@@ -37,7 +37,7 @@ def test_checkpoint_10_training():
|
||||
y_data = y_data.reshape(-1, 1)
|
||||
|
||||
# Create model
|
||||
model = Dense(2, 1)
|
||||
model = Linear(2, 1)
|
||||
model.weights.requires_grad = True
|
||||
model.bias.requires_grad = True
|
||||
|
||||
@@ -73,7 +73,7 @@ def test_checkpoint_10_training():
|
||||
dataloader = DataLoader(X_data, y_data, batch_size=16, shuffle=True)
|
||||
|
||||
# Batch training
|
||||
model_batch = Dense(2, 1)
|
||||
model_batch = Linear(2, 1)
|
||||
model_batch.weights.requires_grad = True
|
||||
model_batch.bias.requires_grad = True
|
||||
optimizer_batch = SGD([model_batch.weights, model_batch.bias], lr=0.01)
|
||||
@@ -134,9 +134,9 @@ def test_checkpoint_10_training():
|
||||
|
||||
# Classification model
|
||||
classifier = [
|
||||
Dense(3, 5),
|
||||
Linear(3, 5),
|
||||
ReLU(),
|
||||
Dense(5, 1),
|
||||
Linear(5, 1),
|
||||
Sigmoid()
|
||||
]
|
||||
|
||||
@@ -182,7 +182,7 @@ def test_checkpoint_10_training():
|
||||
y_train, y_val = y_data[:split_idx], y_data[split_idx:]
|
||||
|
||||
# Fresh model for validation testing
|
||||
model_val = Dense(2, 1)
|
||||
model_val = Linear(2, 1)
|
||||
model_val.weights.requires_grad = True
|
||||
model_val.bias.requires_grad = True
|
||||
optimizer_val = Adam([model_val.weights, model_val.bias], lr=0.01)
|
||||
@@ -228,7 +228,7 @@ def test_checkpoint_10_training():
|
||||
print("📈 Testing learning curves...")
|
||||
|
||||
# Demonstrate learning progress
|
||||
model_curve = Dense(2, 1)
|
||||
model_curve = Linear(2, 1)
|
||||
model_curve.weights.requires_grad = True
|
||||
model_curve.bias.requires_grad = True
|
||||
optimizer_curve = SGD([model_curve.weights, model_curve.bias], lr=0.1)
|
||||
@@ -263,7 +263,7 @@ def test_checkpoint_10_training():
|
||||
try:
|
||||
# Try using Trainer class if available
|
||||
trainer = Trainer(
|
||||
model=Dense(2, 1),
|
||||
model=Linear(2, 1),
|
||||
optimizer=Adam,
|
||||
loss_fn=MeanSquaredError(),
|
||||
lr=0.01
|
||||
@@ -288,7 +288,7 @@ def test_checkpoint_10_training():
|
||||
print("⚠️ Trainer class not available, pipeline tested via manual steps")
|
||||
|
||||
# Manual pipeline demonstration
|
||||
pipeline_model = Dense(2, 1)
|
||||
pipeline_model = Linear(2, 1)
|
||||
pipeline_model.weights.requires_grad = True
|
||||
pipeline_model.bias.requires_grad = True
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ def test_checkpoint_11_regularization():
|
||||
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
from tinytorch.core.regularization import Dropout, L1Regularization, L2Regularization
|
||||
from tinytorch.core.losses import MeanSquaredError
|
||||
@@ -68,7 +68,7 @@ def test_checkpoint_11_regularization():
|
||||
print("⚖️ Testing L2 regularization...")
|
||||
|
||||
# Create model with large weights
|
||||
model = Dense(5, 3)
|
||||
model = Linear(5, 3)
|
||||
model.weights.data = np.random.randn(5, 3) * 2 # Larger weights
|
||||
model.bias.data = np.random.randn(3) * 2
|
||||
model.weights.requires_grad = True
|
||||
@@ -112,13 +112,13 @@ def test_checkpoint_11_regularization():
|
||||
|
||||
# Complex model (prone to overfitting)
|
||||
model_reg = [
|
||||
Dense(10, 50),
|
||||
Linear(10, 50),
|
||||
ReLU(),
|
||||
Dropout(p=0.3),
|
||||
Dense(50, 50),
|
||||
Linear(50, 50),
|
||||
ReLU(),
|
||||
Dropout(p=0.3),
|
||||
Dense(50, 1)
|
||||
Linear(50, 1)
|
||||
]
|
||||
|
||||
# Set requires_grad for all layers
|
||||
@@ -176,7 +176,7 @@ def test_checkpoint_11_regularization():
|
||||
y_train, y_test = y_full[:split], y_full[split:]
|
||||
|
||||
# Train regularized model
|
||||
gen_model = Dense(8, 1)
|
||||
gen_model = Linear(8, 1)
|
||||
gen_model.weights.requires_grad = True
|
||||
gen_model.bias.requires_grad = True
|
||||
|
||||
@@ -231,13 +231,13 @@ def test_checkpoint_11_regularization():
|
||||
print("🏗️ Testing model complexity trade-offs...")
|
||||
|
||||
# Compare simple vs complex models
|
||||
simple_model = Dense(8, 1)
|
||||
simple_model = Linear(8, 1)
|
||||
complex_model = [
|
||||
Dense(8, 32),
|
||||
Linear(8, 32),
|
||||
ReLU(),
|
||||
Dense(32, 16),
|
||||
Linear(32, 16),
|
||||
ReLU(),
|
||||
Dense(16, 1)
|
||||
Linear(16, 1)
|
||||
]
|
||||
|
||||
# Set requires_grad
|
||||
@@ -276,7 +276,7 @@ def test_checkpoint_11_regularization():
|
||||
strength_results = []
|
||||
|
||||
for strength in strengths:
|
||||
temp_model = Dense(5, 1)
|
||||
temp_model = Linear(5, 1)
|
||||
temp_model.weights.requires_grad = True
|
||||
temp_model.bias.requires_grad = True
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ def test_checkpoint_13_benchmarking():
|
||||
StatisticalValidation, StatisticalValidator, TinyTorchPerf, PerformanceReporter
|
||||
)
|
||||
from tinytorch.core.networks import Sequential
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Softmax
|
||||
from tinytorch.core.training import Trainer, CrossEntropyLoss
|
||||
except ImportError as e:
|
||||
@@ -60,11 +60,11 @@ def test_checkpoint_13_benchmarking():
|
||||
try:
|
||||
# Create a simple model for benchmarking
|
||||
model = Sequential([
|
||||
Dense(10, 50),
|
||||
Linear(10, 50),
|
||||
ReLU(),
|
||||
Dense(50, 20),
|
||||
Linear(50, 20),
|
||||
ReLU(),
|
||||
Dense(20, 5),
|
||||
Linear(20, 5),
|
||||
Softmax()
|
||||
])
|
||||
|
||||
@@ -136,13 +136,13 @@ def test_checkpoint_13_benchmarking():
|
||||
|
||||
try:
|
||||
# Create models of different complexities
|
||||
simple_model = Sequential([Dense(10, 5), ReLU()])
|
||||
simple_model = Sequential([Linear(10, 5), ReLU()])
|
||||
complex_model = Sequential([
|
||||
Dense(100, 200), ReLU(),
|
||||
Dense(200, 400), ReLU(),
|
||||
Dense(400, 200), ReLU(),
|
||||
Dense(200, 50), ReLU(),
|
||||
Dense(50, 10)
|
||||
Linear(100, 200), ReLU(),
|
||||
Linear(200, 400), ReLU(),
|
||||
Linear(400, 200), ReLU(),
|
||||
Linear(200, 50), ReLU(),
|
||||
Linear(50, 10)
|
||||
])
|
||||
|
||||
models = [("simple", simple_model), ("complex", complex_model)]
|
||||
@@ -214,7 +214,7 @@ def test_checkpoint_13_benchmarking():
|
||||
|
||||
try:
|
||||
# Test how performance scales with input size
|
||||
model = Sequential([Dense(50, 20), ReLU(), Dense(20, 10)])
|
||||
model = Sequential([Linear(50, 20), ReLU(), Linear(20, 10)])
|
||||
|
||||
sizes = [1, 10, 50, 100]
|
||||
scaling_results = []
|
||||
@@ -349,7 +349,7 @@ def test_checkpoint_13_benchmarking():
|
||||
|
||||
try:
|
||||
# Test integration with TinyTorch training
|
||||
model = Sequential([Dense(20, 10), ReLU(), Dense(10, 5)])
|
||||
model = Sequential([Linear(20, 10), ReLU(), Linear(10, 5)])
|
||||
|
||||
# Set up training components
|
||||
X_train = Tensor(np.random.randn(100, 20))
|
||||
|
||||
@@ -21,7 +21,7 @@ def test_checkpoint_14_deployment():
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.mlops import ModelMonitor, DriftDetector, RetrainingTrigger, MLOpsPipeline
|
||||
from tinytorch.core.networks import Sequential
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Softmax
|
||||
from tinytorch.core.training import Trainer, CrossEntropyLoss, Accuracy
|
||||
from tinytorch.core.compression import quantize_layer_weights, prune_weights_by_magnitude
|
||||
@@ -47,9 +47,9 @@ def test_checkpoint_14_deployment():
|
||||
|
||||
# Test model registration
|
||||
model = Sequential([
|
||||
Dense(10, 20),
|
||||
Linear(10, 20),
|
||||
ReLU(),
|
||||
Dense(20, 5),
|
||||
Linear(20, 5),
|
||||
Softmax()
|
||||
])
|
||||
|
||||
@@ -203,8 +203,8 @@ def test_checkpoint_14_deployment():
|
||||
|
||||
try:
|
||||
# Simulate model versions
|
||||
model_v1 = Sequential([Dense(10, 5), ReLU(), Dense(5, 3)])
|
||||
model_v2 = Sequential([Dense(10, 8), ReLU(), Dense(8, 3)]) # Improved architecture
|
||||
model_v1 = Sequential([Linear(10, 5), ReLU(), Linear(5, 3)])
|
||||
model_v2 = Sequential([Linear(10, 8), ReLU(), Linear(8, 3)]) # Improved architecture
|
||||
|
||||
model_registry = {
|
||||
'v1.0': {
|
||||
@@ -251,11 +251,11 @@ def test_checkpoint_14_deployment():
|
||||
try:
|
||||
# Test model compression for deployment
|
||||
production_model = Sequential([
|
||||
Dense(50, 100),
|
||||
Linear(50, 100),
|
||||
ReLU(),
|
||||
Dense(100, 50),
|
||||
Linear(100, 50),
|
||||
ReLU(),
|
||||
Dense(50, 10)
|
||||
Linear(50, 10)
|
||||
])
|
||||
|
||||
# Original model size
|
||||
@@ -343,8 +343,8 @@ def test_checkpoint_14_deployment():
|
||||
|
||||
try:
|
||||
# Simulate A/B test between two model versions
|
||||
model_a = Sequential([Dense(10, 15), ReLU(), Dense(15, 5)]) # Control
|
||||
model_b = Sequential([Dense(10, 20), ReLU(), Dense(20, 5)]) # Treatment
|
||||
model_a = Sequential([Linear(10, 15), ReLU(), Linear(15, 5)]) # Control
|
||||
model_b = Sequential([Linear(10, 20), ReLU(), Linear(20, 5)]) # Treatment
|
||||
|
||||
# Simulate user requests
|
||||
test_requests = 100
|
||||
|
||||
@@ -20,7 +20,7 @@ def test_checkpoint_15_acceleration():
|
||||
try:
|
||||
# Import acceleration components
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense, Conv2D
|
||||
from tinytorch.core.layers import Linear, Conv2D
|
||||
from tinytorch.core.activations import ReLU
|
||||
from tinytorch.core.networks import Sequential
|
||||
from tinytorch.core.kernels import (
|
||||
@@ -300,11 +300,11 @@ def test_checkpoint_15_acceleration():
|
||||
try:
|
||||
# Create model for end-to-end acceleration
|
||||
model = Sequential([
|
||||
Dense(128, 256),
|
||||
Linear(128, 256),
|
||||
ReLU(),
|
||||
Dense(256, 128),
|
||||
Linear(256, 128),
|
||||
ReLU(),
|
||||
Dense(128, 10)
|
||||
Linear(128, 10)
|
||||
])
|
||||
|
||||
# Test data
|
||||
|
||||
@@ -20,7 +20,7 @@ def test_checkpoint_16_quantization():
|
||||
try:
|
||||
# Import quantization components
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense, Conv2D
|
||||
from tinytorch.core.layers import Linear, Conv2D
|
||||
from tinytorch.core.activations import ReLU
|
||||
from tinytorch.core.networks import Sequential
|
||||
from tinytorch.core.quantization import INT8Quantizer, QuantizedCNN, calibrate_and_quantize_model
|
||||
@@ -73,7 +73,7 @@ def test_checkpoint_16_quantization():
|
||||
ReLU(),
|
||||
Conv2D(in_channels=16, out_channels=32, kernel_size=3),
|
||||
ReLU(),
|
||||
Dense(32 * 26 * 26, 10) # Assuming 28x28 input
|
||||
Linear(32 * 26 * 26, 10) # Assuming 28x28 input
|
||||
])
|
||||
|
||||
# Generate test data
|
||||
@@ -120,11 +120,11 @@ def test_checkpoint_16_quantization():
|
||||
|
||||
# Performance test model
|
||||
test_model = Sequential([
|
||||
Dense(256, 512),
|
||||
Linear(256, 512),
|
||||
ReLU(),
|
||||
Dense(512, 256),
|
||||
Linear(512, 256),
|
||||
ReLU(),
|
||||
Dense(256, 10)
|
||||
Linear(256, 10)
|
||||
])
|
||||
|
||||
# Test data
|
||||
@@ -166,8 +166,8 @@ def test_checkpoint_16_quantization():
|
||||
realistic_cnn = Sequential([
|
||||
Conv2D(1, 8, 3), ReLU(),
|
||||
Conv2D(8, 16, 3), ReLU(),
|
||||
Dense(16 * 24 * 24, 32), ReLU(),
|
||||
Dense(32, 10)
|
||||
Linear(16 * 24 * 24, 32), ReLU(),
|
||||
Linear(32, 10)
|
||||
])
|
||||
|
||||
# Generate representative calibration dataset
|
||||
@@ -211,9 +211,9 @@ def test_checkpoint_16_quantization():
|
||||
try:
|
||||
# Simulate quantization-aware training concepts
|
||||
training_model = Sequential([
|
||||
Dense(20, 40),
|
||||
Linear(20, 40),
|
||||
ReLU(),
|
||||
Dense(40, 10)
|
||||
Linear(40, 10)
|
||||
])
|
||||
|
||||
# Generate training data
|
||||
|
||||
@@ -20,7 +20,7 @@ def test_checkpoint_17_compression():
|
||||
try:
|
||||
# Import compression components
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense, Conv2D
|
||||
from tinytorch.core.layers import Linear, Conv2D
|
||||
from tinytorch.core.activations import ReLU
|
||||
from tinytorch.core.networks import Sequential
|
||||
from tinytorch.nn.utils.prune import MagnitudePruner, prune_conv_filters, CompressionAnalyzer
|
||||
@@ -116,13 +116,13 @@ def test_checkpoint_17_compression():
|
||||
try:
|
||||
# Create test model
|
||||
test_model = Sequential([
|
||||
Dense(100, 200),
|
||||
Linear(100, 200),
|
||||
ReLU(),
|
||||
Dense(200, 100),
|
||||
Linear(200, 100),
|
||||
ReLU(),
|
||||
Dense(100, 50),
|
||||
Linear(100, 50),
|
||||
ReLU(),
|
||||
Dense(50, 10)
|
||||
Linear(50, 10)
|
||||
])
|
||||
|
||||
# Simulate model weights
|
||||
|
||||
@@ -20,7 +20,7 @@ def test_checkpoint_19_competition():
|
||||
try:
|
||||
# Import competition benchmarking components
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense, Conv2D
|
||||
from tinytorch.core.layers import Linear, Conv2D
|
||||
from tinytorch.core.activations import ReLU, Softmax
|
||||
from tinytorch.core.networks import Sequential
|
||||
from tinytorch.utils.benchmark import (
|
||||
@@ -82,11 +82,11 @@ def test_checkpoint_19_competition():
|
||||
|
||||
# Create test model for profiling
|
||||
test_model = Sequential([
|
||||
Dense(784, 128),
|
||||
Linear(784, 128),
|
||||
ReLU(),
|
||||
Dense(128, 64),
|
||||
Linear(128, 64),
|
||||
ReLU(),
|
||||
Dense(64, 10),
|
||||
Linear(64, 10),
|
||||
Softmax()
|
||||
])
|
||||
|
||||
@@ -132,11 +132,11 @@ def test_checkpoint_19_competition():
|
||||
|
||||
# Create optimized model for submission
|
||||
optimized_model = Sequential([
|
||||
Dense(784, 64), # Smaller than baseline
|
||||
Linear(784, 64), # Smaller than baseline
|
||||
ReLU(),
|
||||
Dense(64, 32), # Further reduction
|
||||
Linear(64, 32), # Further reduction
|
||||
ReLU(),
|
||||
Dense(32, 10),
|
||||
Linear(32, 10),
|
||||
Softmax()
|
||||
])
|
||||
|
||||
@@ -253,8 +253,8 @@ def test_checkpoint_19_competition():
|
||||
competition_results = runner.run_competition(
|
||||
event="mlp_sprint",
|
||||
submission_models=[
|
||||
("baseline", Sequential([Dense(784, 128), ReLU(), Dense(128, 10), Softmax()])),
|
||||
("optimized", Sequential([Dense(784, 64), ReLU(), Dense(64, 10), Softmax()]))
|
||||
("baseline", Sequential([Linear(784, 128), ReLU(), Linear(128, 10), Softmax()])),
|
||||
("optimized", Sequential([Linear(784, 64), ReLU(), Linear(64, 10), Softmax()]))
|
||||
],
|
||||
max_time_budget=30.0 # 30 second time budget
|
||||
)
|
||||
|
||||
@@ -20,7 +20,7 @@ def test_checkpoint_20_capstone():
|
||||
try:
|
||||
# Import all TinyTorch components for complete integration
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense, Embedding
|
||||
from tinytorch.core.layers import Linear, Embedding
|
||||
from tinytorch.core.activations import ReLU, Sigmoid, Softmax, GELU
|
||||
from tinytorch.core.networks import Sequential
|
||||
from tinytorch.core.spatial import Conv2D, MaxPool2D
|
||||
|
||||
@@ -187,14 +187,14 @@ class TestArchitectureCheckpoint:
|
||||
from tinytorch.core.layers import Layer, Dense
|
||||
|
||||
# Test layer exists and is usable
|
||||
layer = Dense(10, 5)
|
||||
layer = Linear(10, 5)
|
||||
assert hasattr(layer, 'forward')
|
||||
assert hasattr(layer, 'weights')
|
||||
assert hasattr(layer, 'bias')
|
||||
|
||||
def test_dense_networks(self):
|
||||
"""Test that dense module enables fully-connected networks."""
|
||||
from tinytorch.core.dense import DenseNetwork
|
||||
from tinytorch.core.dense import LinearNetwork
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Create network
|
||||
@@ -262,10 +262,10 @@ class TestTrainingCheckpoint:
|
||||
def test_optimizers(self):
|
||||
"""Test that optimizers update parameters correctly."""
|
||||
from tinytorch.core.optimizers import SGD, Adam
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
# Create layer with parameters
|
||||
layer = Dense(10, 5)
|
||||
layer = Linear(10, 5)
|
||||
|
||||
# Test SGD
|
||||
sgd = SGD([layer.weights, layer.bias], lr=0.01)
|
||||
@@ -376,7 +376,7 @@ class TestServingCheckpoint:
|
||||
try:
|
||||
# Test all major imports work
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
from tinytorch.core.networks import Sequential
|
||||
from tinytorch.core.optimizers import Adam
|
||||
@@ -385,9 +385,9 @@ class TestServingCheckpoint:
|
||||
|
||||
# Test building a complete model
|
||||
model = Sequential([
|
||||
Dense(784, 128),
|
||||
Linear(784, 128),
|
||||
ReLU(),
|
||||
Dense(128, 10)
|
||||
Linear(128, 10)
|
||||
])
|
||||
|
||||
# Test model has expected structure
|
||||
@@ -454,9 +454,9 @@ def test_capability_statements():
|
||||
|
||||
# Test Architecture capability
|
||||
try:
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.networks import Sequential
|
||||
model = Sequential([Dense(10, 5), Dense(5, 2)])
|
||||
model = Sequential([Linear(10, 5), Linear(5, 2)])
|
||||
capabilities_achieved.append("architecture")
|
||||
except:
|
||||
pass
|
||||
|
||||
@@ -44,12 +44,12 @@ class SimpleCNN:
|
||||
self.flatten = Flatten()
|
||||
|
||||
# Dense layers
|
||||
self.fc1 = Dense(64 * 5 * 5, 256) # Assuming 32x32 input -> 5x5 after conv+pool
|
||||
self.fc2 = Dense(256, num_classes)
|
||||
self.fc1 = Linear(64 * 5 * 5, 256) # Assuming 32x32 input -> 5x5 after conv+pool
|
||||
self.fc2 = Linear(256, num_classes)
|
||||
else:
|
||||
# Fallback: treat as flattened MLP
|
||||
self.fc1 = Dense(32*32*3, 256)
|
||||
self.fc2 = Dense(256, num_classes)
|
||||
self.fc1 = Linear(32*32*3, 256)
|
||||
self.fc2 = Linear(256, num_classes)
|
||||
|
||||
self.relu = ReLU()
|
||||
|
||||
|
||||
@@ -29,9 +29,9 @@ class SimpleMLP:
|
||||
"""Simple MLP for MNIST-style classification."""
|
||||
|
||||
def __init__(self, input_size=784, hidden_size=128, num_classes=10):
|
||||
self.fc1 = Dense(input_size, hidden_size)
|
||||
self.fc1 = Linear(input_size, hidden_size)
|
||||
self.relu = ReLU()
|
||||
self.fc2 = Dense(hidden_size, num_classes)
|
||||
self.fc2 = Linear(hidden_size, num_classes)
|
||||
|
||||
def forward(self, x):
|
||||
"""Forward pass."""
|
||||
|
||||
@@ -58,7 +58,7 @@ class SimpleTinyGPT:
|
||||
self.pos_encoding = PositionalEncoding(max_length, embed_dim)
|
||||
else:
|
||||
# Fallback: simple linear embedding
|
||||
self.embedding = Dense(vocab_size, embed_dim)
|
||||
self.embedding = Linear(vocab_size, embed_dim)
|
||||
|
||||
# Transformer layers
|
||||
if TRANSFORMERS_AVAILABLE and ATTENTION_AVAILABLE:
|
||||
@@ -73,13 +73,13 @@ class SimpleTinyGPT:
|
||||
else:
|
||||
# Fallback: simple feedforward layers
|
||||
self.layers = [
|
||||
Dense(embed_dim, embed_dim * 2),
|
||||
Linear(embed_dim, embed_dim * 2),
|
||||
ReLU(),
|
||||
Dense(embed_dim * 2, embed_dim)
|
||||
Linear(embed_dim * 2, embed_dim)
|
||||
]
|
||||
|
||||
# Output projection
|
||||
self.output_proj = Dense(embed_dim, vocab_size)
|
||||
self.output_proj = Linear(embed_dim, vocab_size)
|
||||
|
||||
def forward(self, x):
|
||||
"""Forward pass."""
|
||||
|
||||
@@ -175,7 +175,7 @@ def test_modern_api_integration():
|
||||
from tinytorch.core.layers import Linear as Dense
|
||||
from tinytorch.core.spatial import MultiChannelConv2D
|
||||
|
||||
dense = Dense(5, 3)
|
||||
dense = Linear(5, 3)
|
||||
conv = MultiChannelConv2D(3, 8, (3, 3))
|
||||
|
||||
# Should be the same classes as new names
|
||||
|
||||
@@ -26,7 +26,7 @@ def test_layers_integration():
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Linear as Dense
|
||||
|
||||
layer = Dense(3, 2)
|
||||
layer = Linear(3, 2)
|
||||
x = Tensor(np.random.randn(5, 3))
|
||||
output = layer(x)
|
||||
|
||||
@@ -41,9 +41,9 @@ def test_layers_integration():
|
||||
try:
|
||||
from tinytorch.core.activations import ReLU, Sigmoid
|
||||
|
||||
layer1 = Dense(4, 8)
|
||||
layer1 = Linear(4, 8)
|
||||
relu = ReLU()
|
||||
layer2 = Dense(8, 4)
|
||||
layer2 = Linear(8, 4)
|
||||
sigmoid = Sigmoid()
|
||||
|
||||
x = Tensor(np.random.randn(2, 4))
|
||||
@@ -60,11 +60,11 @@ def test_layers_integration():
|
||||
print("Test 3: Multi-layer network construction")
|
||||
try:
|
||||
layers = [
|
||||
Dense(10, 20),
|
||||
Linear(10, 20),
|
||||
ReLU(),
|
||||
Dense(20, 15),
|
||||
Linear(20, 15),
|
||||
ReLU(),
|
||||
Dense(15, 5)
|
||||
Linear(15, 5)
|
||||
]
|
||||
|
||||
x = Tensor(np.random.randn(3, 10))
|
||||
@@ -80,7 +80,7 @@ def test_layers_integration():
|
||||
# Test 4: Parameter access
|
||||
print("Test 4: Parameter management")
|
||||
try:
|
||||
layer = Dense(5, 3)
|
||||
layer = Linear(5, 3)
|
||||
|
||||
assert hasattr(layer, 'weights'), "Layer missing weights"
|
||||
assert hasattr(layer, 'bias'), "Layer missing bias"
|
||||
|
||||
@@ -24,7 +24,7 @@ class TestDenseModuleExports:
|
||||
def test_dense_is_callable(self):
|
||||
"""Test Dense can be instantiated."""
|
||||
from tinytorch.core.layers import Linear as Dense
|
||||
layer = Dense(10, 5)
|
||||
layer = Linear(10, 5)
|
||||
assert layer is not None, "Should create Dense layer instance"
|
||||
assert hasattr(layer, 'forward'), "Dense should have forward method"
|
||||
|
||||
@@ -38,7 +38,7 @@ class TestDenseLayerFunctionality:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Create layer
|
||||
layer = Dense(10, 5)
|
||||
layer = Linear(10, 5)
|
||||
|
||||
# Create input
|
||||
batch_size = 32
|
||||
@@ -56,7 +56,7 @@ class TestDenseLayerFunctionality:
|
||||
from tinytorch.core.layers import Linear as Dense
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
layer = Dense(10, 5, bias=True)
|
||||
layer = Linear(10, 5, bias=True)
|
||||
assert hasattr(layer, 'bias'), "Layer should have bias"
|
||||
assert layer.bias is not None, "Bias should be initialized"
|
||||
|
||||
@@ -69,7 +69,7 @@ class TestDenseLayerFunctionality:
|
||||
from tinytorch.core.layers import Linear as Dense
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
layer = Dense(10, 5, bias=False)
|
||||
layer = Linear(10, 5, bias=False)
|
||||
assert layer.bias is None, "Bias should be None when disabled"
|
||||
|
||||
x = Tensor(np.random.randn(1, 10))
|
||||
@@ -81,7 +81,7 @@ class TestDenseLayerFunctionality:
|
||||
from tinytorch.core.layers import Linear as Dense
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
layer = Dense(10, 5)
|
||||
layer = Linear(10, 5)
|
||||
x = Tensor(np.random.randn(4, 10))
|
||||
|
||||
# Test both forward() and __call__()
|
||||
@@ -114,9 +114,9 @@ class TestNetworkComposition:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# Build network manually (without Sequential)
|
||||
layer1 = Dense(784, 128)
|
||||
layer1 = Linear(784, 128)
|
||||
relu = ReLU()
|
||||
layer2 = Dense(128, 10)
|
||||
layer2 = Linear(128, 10)
|
||||
sigmoid = Sigmoid()
|
||||
|
||||
# Test forward pass through all layers
|
||||
@@ -146,9 +146,9 @@ class TestXORCapability:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# XOR network: 2 -> 4 -> 1
|
||||
hidden = Dense(2, 4)
|
||||
hidden = Linear(2, 4)
|
||||
relu = ReLU()
|
||||
output = Dense(4, 1)
|
||||
output = Linear(4, 1)
|
||||
sigmoid = Sigmoid()
|
||||
|
||||
# XOR inputs
|
||||
|
||||
@@ -28,7 +28,7 @@ def test_autograd_integration():
|
||||
from tinytorch.core.training import MeanSquaredError
|
||||
|
||||
# Create simple network
|
||||
layer = Dense(2, 1)
|
||||
layer = Linear(2, 1)
|
||||
layer.weights.requires_grad = True
|
||||
layer.bias.requires_grad = True
|
||||
|
||||
@@ -57,9 +57,9 @@ def test_autograd_integration():
|
||||
try:
|
||||
from tinytorch.core.activations import ReLU, Sigmoid
|
||||
|
||||
layer1 = Dense(2, 3)
|
||||
layer1 = Linear(2, 3)
|
||||
relu = ReLU()
|
||||
layer2 = Dense(3, 1)
|
||||
layer2 = Linear(3, 1)
|
||||
sigmoid = Sigmoid()
|
||||
|
||||
# Enable gradients
|
||||
@@ -85,9 +85,9 @@ def test_autograd_integration():
|
||||
print("Test 3: Multi-layer backpropagation setup")
|
||||
try:
|
||||
# Build 3-layer network
|
||||
layer1 = Dense(4, 8)
|
||||
layer2 = Dense(8, 4)
|
||||
layer3 = Dense(4, 1)
|
||||
layer1 = Linear(4, 8)
|
||||
layer2 = Linear(8, 4)
|
||||
layer3 = Linear(4, 1)
|
||||
|
||||
# Enable all gradients
|
||||
for layer in [layer1, layer2, layer3]:
|
||||
|
||||
@@ -20,7 +20,7 @@ sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
import numpy as np
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
from tinytorch.core.training import CrossEntropyLoss
|
||||
|
||||
@@ -44,12 +44,12 @@ class SimpleCNN:
|
||||
self.flatten = Flatten()
|
||||
|
||||
# Dense layers
|
||||
self.fc1 = Dense(64 * 5 * 5, 256) # Assuming 32x32 input -> 5x5 after conv+pool
|
||||
self.fc2 = Dense(256, num_classes)
|
||||
self.fc1 = Linear(64 * 5 * 5, 256) # Assuming 32x32 input -> 5x5 after conv+pool
|
||||
self.fc2 = Linear(256, num_classes)
|
||||
else:
|
||||
# Fallback: treat as flattened MLP
|
||||
self.fc1 = Dense(32*32*3, 256)
|
||||
self.fc2 = Dense(256, num_classes)
|
||||
self.fc1 = Linear(32*32*3, 256)
|
||||
self.fc2 = Linear(256, num_classes)
|
||||
|
||||
self.relu = ReLU()
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
import numpy as np
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
from tinytorch.core.training import CrossEntropyLoss
|
||||
|
||||
@@ -29,9 +29,9 @@ class SimpleMLP:
|
||||
"""Simple MLP for MNIST-style classification."""
|
||||
|
||||
def __init__(self, input_size=784, hidden_size=128, num_classes=10):
|
||||
self.fc1 = Dense(input_size, hidden_size)
|
||||
self.fc1 = Linear(input_size, hidden_size)
|
||||
self.relu = ReLU()
|
||||
self.fc2 = Dense(hidden_size, num_classes)
|
||||
self.fc2 = Linear(hidden_size, num_classes)
|
||||
|
||||
def forward(self, x):
|
||||
"""Forward pass."""
|
||||
|
||||
@@ -13,7 +13,7 @@ sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
import numpy as np
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.activations import ReLU, Sigmoid
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
def test_basic_tensor_operations():
|
||||
"""Test basic tensor operations."""
|
||||
@@ -74,7 +74,7 @@ def test_dense_layer_basic():
|
||||
print("🏗️ Testing Dense Layer...")
|
||||
|
||||
# Create a simple dense layer
|
||||
dense = Dense(3, 2) # 3 inputs, 2 outputs
|
||||
dense = Linear(3, 2) # 3 inputs, 2 outputs
|
||||
|
||||
# Test with simple input
|
||||
x = Tensor([[1, 0, 1]]) # batch_size=1, input_size=3
|
||||
@@ -83,8 +83,8 @@ def test_dense_layer_basic():
|
||||
print(f" ✓ Dense layer forward pass successful")
|
||||
print(f" Input shape: {x.shape}")
|
||||
print(f" Output shape: {output.shape}")
|
||||
print(f" Weights shape: {dense.weights.shape}")
|
||||
print(f" Bias shape: {dense.bias.shape}")
|
||||
print(f" Weights shape: {linear.weights.shape}")
|
||||
print(f" Bias shape: {linear.bias.shape}")
|
||||
|
||||
# Check output shape is correct
|
||||
assert output.shape == (1, 2), f"Expected output shape (1, 2), got {output.shape}"
|
||||
@@ -102,8 +102,8 @@ def test_simple_forward_pass():
|
||||
print("🚀 Testing Simple Forward Pass...")
|
||||
|
||||
# Create simple 2-layer network manually
|
||||
layer1 = Dense(2, 3) # 2 -> 3
|
||||
layer2 = Dense(3, 1) # 3 -> 1
|
||||
layer1 = Linear(2, 3) # 2 -> 3
|
||||
layer2 = Linear(3, 1) # 3 -> 1
|
||||
relu = ReLU()
|
||||
sigmoid = Sigmoid()
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
import numpy as np
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
|
||||
# Try to import transformer components
|
||||
@@ -58,7 +58,7 @@ class SimpleTinyGPT:
|
||||
self.pos_encoding = PositionalEncoding(embed_dim, max_length)
|
||||
else:
|
||||
# Fallback: simple linear embedding
|
||||
self.embedding = Dense(vocab_size, embed_dim)
|
||||
self.embedding = Linear(vocab_size, embed_dim)
|
||||
|
||||
# Transformer layers
|
||||
if TRANSFORMERS_AVAILABLE and ATTENTION_AVAILABLE:
|
||||
@@ -73,13 +73,13 @@ class SimpleTinyGPT:
|
||||
else:
|
||||
# Fallback: simple feedforward layers
|
||||
self.layers = [
|
||||
Dense(embed_dim, embed_dim * 2),
|
||||
Linear(embed_dim, embed_dim * 2),
|
||||
ReLU(),
|
||||
Dense(embed_dim * 2, embed_dim)
|
||||
Linear(embed_dim * 2, embed_dim)
|
||||
]
|
||||
|
||||
# Output projection
|
||||
self.output_proj = Dense(embed_dim, vocab_size)
|
||||
self.output_proj = Linear(embed_dim, vocab_size)
|
||||
|
||||
def forward(self, x):
|
||||
"""Forward pass."""
|
||||
|
||||
@@ -9,7 +9,7 @@ Required modules:
|
||||
- Module 01: Setup
|
||||
- Module 02: Tensor - Data structures
|
||||
- Module 03: Activations - ReLU, Sigmoid
|
||||
- Module 04: Layers - Dense layers
|
||||
- Module 04: Layers - Linear layers
|
||||
|
||||
This demonstrates the milestone: "Can build a network that learns XOR"
|
||||
"""
|
||||
@@ -21,15 +21,15 @@ sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
import numpy as np
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.activations import ReLU, Sigmoid
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
class SimpleXORNet:
|
||||
"""Simple 2-layer network for XOR problem."""
|
||||
|
||||
|
||||
def __init__(self):
|
||||
self.layer1 = Dense(2, 4) # Input layer: 2 -> 4 hidden
|
||||
self.layer1 = Linear(2, 4) # Input layer: 2 -> 4 hidden
|
||||
self.relu = ReLU()
|
||||
self.layer2 = Dense(4, 1) # Output layer: 4 -> 1 output
|
||||
self.layer2 = Linear(4, 1) # Output layer: 4 -> 1 output
|
||||
self.sigmoid = Sigmoid()
|
||||
|
||||
def forward(self, x):
|
||||
@@ -58,10 +58,10 @@ def test_xor_network_components():
|
||||
x = Tensor([[0, 1], [1, 0]])
|
||||
assert x.shape == (2, 2), f"Expected shape (2, 2), got {x.shape}"
|
||||
|
||||
# Test Dense layer
|
||||
print(" ✓ Testing Dense layer")
|
||||
dense = Dense(2, 3)
|
||||
out = dense(x)
|
||||
# Test Linear layer
|
||||
print(" ✓ Testing Linear layer")
|
||||
linear = Linear(2, 3)
|
||||
out = linear(x)
|
||||
assert out.shape == (2, 3), f"Expected shape (2, 3), got {out.shape}"
|
||||
|
||||
# Test ReLU activation
|
||||
@@ -169,8 +169,8 @@ def run_xor_integration_test():
|
||||
print()
|
||||
print("✅ Milestone Achieved: Can build networks that learn XOR")
|
||||
print(" • Tensors handle data flow")
|
||||
print(" • Activations add nonlinearity")
|
||||
print(" • Dense layers transform representations")
|
||||
print(" • Activations add nonlinearity")
|
||||
print(" • Linear layers transform representations")
|
||||
print(" • Architecture supports learning")
|
||||
print()
|
||||
print("🚀 Ready for Module 5: Training loops!")
|
||||
|
||||
@@ -101,9 +101,9 @@ def check_core_functionality():
|
||||
# Test Dense Layers
|
||||
print("\n🏗️ Testing Dense Layers...")
|
||||
try:
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
dense = Dense(3, 2)
|
||||
dense = Linear(3, 2)
|
||||
x = Tensor([[1, 0, 1]])
|
||||
output = dense(x)
|
||||
|
||||
@@ -193,12 +193,12 @@ def test_milestone_capabilities():
|
||||
print("\n🔥 Milestone 1: XOR Learning Capability")
|
||||
try:
|
||||
from tinytorch.core.tensor import Tensor
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU, Sigmoid
|
||||
|
||||
# Build simple XOR network
|
||||
layer1 = Dense(2, 4)
|
||||
layer2 = Dense(4, 1)
|
||||
layer1 = Linear(2, 4)
|
||||
layer2 = Linear(4, 1)
|
||||
relu = ReLU()
|
||||
sigmoid = Sigmoid()
|
||||
|
||||
@@ -218,9 +218,9 @@ def test_milestone_capabilities():
|
||||
print("\n🖼️ Milestone 2: MNIST Classification Capability")
|
||||
try:
|
||||
# Test MLP for image classification
|
||||
model = Dense(784, 128)
|
||||
model = Linear(784, 128)
|
||||
relu = ReLU()
|
||||
classifier = Dense(128, 10)
|
||||
classifier = Linear(128, 10)
|
||||
|
||||
# Fake MNIST batch
|
||||
images = Tensor(np.random.randn(32, 784))
|
||||
@@ -240,12 +240,12 @@ def test_milestone_capabilities():
|
||||
print("\n📷 Milestone 3: CNN Image Classification Capability")
|
||||
try:
|
||||
# Test basic CNN components (fallback if spatial not available)
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
from tinytorch.core.activations import ReLU
|
||||
|
||||
# Simulate CNN with dense layers (fallback)
|
||||
cnn_features = Dense(3*32*32, 256) # Simulate conv layers
|
||||
classifier = Dense(256, 10)
|
||||
cnn_features = Linear(3*32*32, 256) # Simulate conv layers
|
||||
classifier = Linear(256, 10)
|
||||
relu = ReLU()
|
||||
|
||||
# Fake CIFAR batch (flattened)
|
||||
@@ -267,12 +267,12 @@ def test_milestone_capabilities():
|
||||
try:
|
||||
from tinytorch.core.embeddings import Embedding
|
||||
from tinytorch.core.transformers import LayerNorm
|
||||
from tinytorch.core.layers import Dense
|
||||
from tinytorch.core.layers import Linear
|
||||
|
||||
# Simple transformer components
|
||||
embedding = Embedding(vocab_size=1000, embedding_dim=128)
|
||||
layer_norm = LayerNorm(embed_dim=128)
|
||||
output_proj = Dense(128, 1000)
|
||||
output_proj = Linear(128, 1000)
|
||||
|
||||
# Test sequence processing
|
||||
tokens = Tensor(np.array([[1, 2, 3, 4, 5]]))
|
||||
|
||||
@@ -31,9 +31,9 @@ class TestModuleCapabilities:
|
||||
"""Test a real-world usage scenario."""
|
||||
# Example for Optimizer:
|
||||
# from tinytorch.core.optimizers import Adam
|
||||
# from tinytorch.core.layers import Dense
|
||||
# from tinytorch.core.layers import Linear
|
||||
#
|
||||
# layer = Dense(10, 5)
|
||||
# layer = Linear(10, 5)
|
||||
# optimizer = Adam(learning_rate=0.001)
|
||||
#
|
||||
# # Simulate training step
|
||||
|
||||
@@ -144,3 +144,4 @@ assert output.shape == expected, (
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -99,3 +99,4 @@ def get_capabilities(module_num: str) -> dict:
|
||||
return MODULE_CAPABILITIES.get(module_num, {})
|
||||
|
||||
|
||||
|
||||
|
||||
4
tinytorch/optimization/acceleration.py
generated
4
tinytorch/optimization/acceleration.py
generated
@@ -21,6 +21,10 @@ __all__ = ['vectorized_matmul', 'fused_gelu', 'tiled_matmul']
|
||||
#| default_exp optimization.acceleration
|
||||
#| export
|
||||
|
||||
# %% ../../modules/18_acceleration/18_acceleration.ipynb 5
|
||||
# Import from TinyTorch package (previous modules must be completed and exported)
|
||||
from tinytorch.core.tensor import Tensor
|
||||
|
||||
# %% ../../modules/18_acceleration/18_acceleration.ipynb 7
|
||||
def vectorized_matmul(a: Tensor, b: Tensor) -> Tensor:
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user