mirror of
https://github.com/MLSysBook/TinyTorch.git
synced 2026-08-01 18:59:41 -05:00
- Removed 01_setup module (archived to archive/setup_module) - Renumbered all modules: tensor is now 01, activations is 02, etc. - Added tito setup command for environment setup and package installation - Added numeric shortcuts: tito 01, tito 02, etc. for quick module access - Fixed view command to find dev files correctly - Updated module dependencies and references - Improved user experience: immediate ML learning instead of boring setup
74 KiB
74 KiB
In [ ]:
#| default_exp utils.benchmark
import time
import json
import hashlib
import tracemalloc
from datetime import datetime
from pathlib import Path
from typing import Dict, Any, List, Optional, Tuple, Union, Callable
import numpy as np
import pickle
# Import TinyTorch profiler from Module 15
try:
from tinytorch.utils.profiler import SimpleProfiler, profile_function
HAS_PROFILER = True
except ImportError:
print("Warning: TinyTorch profiler not available. Using basic timing.")
HAS_PROFILER = FalseIn [ ]:
class TinyMLPerf:
"""
TinyMLPerf benchmark suite - The Olympics of ML Systems Optimization!
Provides three standard competition events:
- MLP Sprint: Fastest feedforward inference
- CNN Marathon: Efficient convolution operations
- Transformer Decathlon: Complete attention-based model performance
Each event uses standardized models and datasets for fair competition.
"""
def __init__(self, profiler_warmup_runs: int = 3, profiler_timing_runs: int = 10):
"""
Initialize TinyMLPerf benchmark suite.
Args:
profiler_warmup_runs: Number of warmup runs for stable measurements
profiler_timing_runs: Number of timing runs for statistical reliability
"""
self.warmup_runs = profiler_warmup_runs
self.timing_runs = profiler_timing_runs
self.benchmark_models = {}
self.benchmark_datasets = {}
print("🏆 TinyMLPerf Competition Suite Initialized!")
print("🎯 Three Events: MLP Sprint, CNN Marathon, Transformer Decathlon")
# Load standard benchmark models
self._load_benchmark_models()
self._load_benchmark_datasets()
def _load_benchmark_models(self):
"""Load standard benchmark models for each competition event"""
print("📥 Loading TinyMLPerf Benchmark Models...")
# MLP Sprint - Simple feedforward model
class MLPBenchmark:
def __init__(self):
self.weights1 = np.random.randn(784, 128).astype(np.float32) * 0.1
self.bias1 = np.random.randn(128).astype(np.float32) * 0.1
self.weights2 = np.random.randn(128, 64).astype(np.float32) * 0.1
self.bias2 = np.random.randn(64).astype(np.float32) * 0.1
self.weights3 = np.random.randn(64, 10).astype(np.float32) * 0.1
self.bias3 = np.random.randn(10).astype(np.float32) * 0.1
def forward(self, x):
# 3-layer MLP with ReLU activations
h1 = np.maximum(0, x @ self.weights1 + self.bias1) # ReLU
h2 = np.maximum(0, h1 @ self.weights2 + self.bias2) # ReLU
return h2 @ self.weights3 + self.bias3 # Output layer
def predict(self, x):
return self.forward(x)
# CNN Marathon - Convolutional model
class CNNBenchmark:
def __init__(self):
# Simplified CNN weights (real CNN would need proper conv operations)
self.conv1_weights = np.random.randn(3, 3, 1, 32).astype(np.float32) * 0.1
self.conv2_weights = np.random.randn(3, 3, 32, 64).astype(np.float32) * 0.1
self.fc_weights = np.random.randn(1600, 10).astype(np.float32) * 0.1 # Flattened size
self.fc_bias = np.random.randn(10).astype(np.float32) * 0.1
def forward(self, x):
# Simplified CNN (students will optimize real convolutions)
batch_size = x.shape[0]
# Simulate conv + pooling by flattening and projecting
x_flat = x.reshape(batch_size, -1) # Flatten input
if x_flat.shape[1] != 1600:
# Adjust to expected size
x_flat = x_flat[:, :1600] if x_flat.shape[1] > 1600 else np.pad(x_flat, ((0, 0), (0, 1600 - x_flat.shape[1])), 'constant')
return x_flat @ self.fc_weights + self.fc_bias
def predict(self, x):
return self.forward(x)
# Transformer Decathlon - Attention-based model
class TransformerBenchmark:
def __init__(self, d_model=128, n_heads=8, seq_len=64):
self.d_model = d_model
self.n_heads = n_heads
self.seq_len = seq_len
self.head_dim = d_model // n_heads
# Multi-head attention weights
self.wq = np.random.randn(d_model, d_model).astype(np.float32) * 0.1
self.wk = np.random.randn(d_model, d_model).astype(np.float32) * 0.1
self.wv = np.random.randn(d_model, d_model).astype(np.float32) * 0.1
self.wo = np.random.randn(d_model, d_model).astype(np.float32) * 0.1
# Feed forward weights
self.ff1 = np.random.randn(d_model, d_model * 4).astype(np.float32) * 0.1
self.ff2 = np.random.randn(d_model * 4, d_model).astype(np.float32) * 0.1
def forward(self, x):
# Simplified transformer block (students will optimize real attention)
batch_size, seq_len, d_model = x.shape
# Self-attention (simplified)
q = x @ self.wq # [batch, seq, d_model]
k = x @ self.wk
v = x @ self.wv
# Simplified attention computation (real would be multi-head)
scores = q @ k.transpose(0, 2, 1) / np.sqrt(d_model) # [batch, seq, seq]
attn = np.exp(scores) / (np.sum(np.exp(scores), axis=-1, keepdims=True) + 1e-8)
out = attn @ v # [batch, seq, d_model]
# Skip connection + layer norm (simplified)
out = out + x # Residual connection
# Feed forward network
ff_out = np.maximum(0, out @ self.ff1) # ReLU
ff_out = ff_out @ self.ff2
# Another skip connection
out = ff_out + out
# Global average pooling for classification
return np.mean(out, axis=1) # [batch, d_model]
def predict(self, x):
return self.forward(x)
# Store benchmark models
self.benchmark_models = {
'mlp_sprint': MLPBenchmark(),
'cnn_marathon': CNNBenchmark(),
'transformer_decathlon': TransformerBenchmark()
}
print("✅ Benchmark models loaded successfully!")
for event, model in self.benchmark_models.items():
print(f" 📋 {event.title()}: {type(model).__name__}")
def _load_benchmark_datasets(self):
"""Load standard benchmark datasets for each competition event"""
print("📊 Loading TinyMLPerf Benchmark Datasets...")
# MLP Sprint dataset - MNIST-like flattened images
mlp_data = {
'inputs': np.random.randn(100, 784).astype(np.float32), # Batch of 100 samples
'targets': np.eye(10)[np.random.randint(0, 10, 100)], # One-hot labels
'event': 'MLP Sprint',
'description': 'Feedforward inference on flattened 28x28 images'
}
# CNN Marathon dataset - Image-like data
cnn_data = {
'inputs': np.random.randn(50, 28, 28, 1).astype(np.float32), # Batch of 50 images
'targets': np.eye(10)[np.random.randint(0, 10, 50)],
'event': 'CNN Marathon',
'description': 'Convolutional inference on 28x28x1 images'
}
# Transformer Decathlon dataset - Sequence data
transformer_data = {
'inputs': np.random.randn(32, 64, 128).astype(np.float32), # Batch of 32 sequences
'targets': np.eye(10)[np.random.randint(0, 10, 32)],
'event': 'Transformer Decathlon',
'description': 'Self-attention inference on 64-token sequences'
}
self.benchmark_datasets = {
'mlp_sprint': mlp_data,
'cnn_marathon': cnn_data,
'transformer_decathlon': transformer_data
}
print("✅ Benchmark datasets loaded successfully!")
for event, data in self.benchmark_datasets.items():
print(f" 🎯 {data['event']}: {data['inputs'].shape} -> {data['targets'].shape}")
def load_benchmark(self, event_name: str) -> Tuple[Any, Dict[str, Any]]:
"""
Load a specific benchmark model and dataset.
Args:
event_name: Name of competition event ('mlp_sprint', 'cnn_marathon', 'transformer_decathlon')
Returns:
Tuple of (model, dataset) for the specified event
"""
if event_name not in self.benchmark_models:
available = list(self.benchmark_models.keys())
raise ValueError(f"Event '{event_name}' not found. Available: {available}")
model = self.benchmark_models[event_name]
dataset = self.benchmark_datasets[event_name]
print(f"📋 Loaded benchmark: {dataset['event']}")
print(f" Model: {type(model).__name__}")
print(f" Data: {dataset['description']}")
return model, dataset
def get_available_events(self) -> Dict[str, str]:
"""Get list of available competition events with descriptions"""
return {
'mlp_sprint': 'Fastest feedforward neural network inference',
'cnn_marathon': 'Efficient convolutional neural network processing',
'transformer_decathlon': 'Complete attention mechanism optimization'
}In [ ]:
def test_tinymlperf_benchmark_suite():
"""Test the TinyMLPerf benchmark suite"""
print("Testing TinyMLPerf Benchmark Suite...")
# Initialize benchmark suite
tinyperf = TinyMLPerf(profiler_warmup_runs=2, profiler_timing_runs=3)
# Test each event
events = tinyperf.get_available_events()
print(f"\n🏆 Available Events: {len(events)}")
for event_name, description in events.items():
print(f"\n📋 Testing {event_name}...")
model, dataset = tinyperf.load_benchmark(event_name)
# Test model inference
inputs = dataset['inputs']
outputs = model.predict(inputs)
print(f" ✅ Inference successful: {inputs.shape} -> {outputs.shape}")
# Verify output shape makes sense
batch_size = inputs.shape[0]
assert outputs.shape[0] == batch_size, f"Batch size mismatch: {outputs.shape[0]} != {batch_size}"
print(f" ✅ Output shape verified")
print(f"\n✅ TinyMLPerf benchmark suite test complete!")
return tinyperfIn [ ]:
class CompetitionProfiler:
"""
Competition profiling infrastructure using TinyTorch's Module 15 profiler.
Provides rigorous performance measurement for fair competition by:
- Using standardized profiling from Module 15
- Multiple timing runs with statistical analysis
- Memory usage tracking and analysis
- Hardware-independent relative scoring
"""
def __init__(self, warmup_runs: int = 3, timing_runs: int = 10):
"""
Initialize competition profiler.
Args:
warmup_runs: Number of warmup runs to stabilize performance
timing_runs: Number of timing runs for statistical reliability
"""
self.warmup_runs = warmup_runs
self.timing_runs = timing_runs
self.has_profiler = HAS_PROFILER
if not self.has_profiler:
print("⚠️ Warning: Advanced profiling unavailable, using basic timing")
else:
print("✅ Using TinyTorch Module 15 profiler for advanced metrics")
def benchmark_model(self, model, dataset: Dict[str, Any],
baseline_model=None, baseline_time: Optional[float] = None) -> Dict[str, Any]:
"""
Benchmark a model using rigorous profiling methodology.
Args:
model: Model to benchmark (must have predict() or forward() method)
dataset: Dataset dictionary with 'inputs' key
baseline_model: Optional baseline model for speedup calculation
baseline_time: Optional baseline time for speedup calculation
Returns:
Comprehensive benchmarking results with performance metrics
"""
print(f"🏁 Benchmarking {dataset.get('event', 'Model')}...")
inputs = dataset['inputs']
results = {
'event': dataset.get('event', 'Unknown'),
'model_type': type(model).__name__,
'input_shape': inputs.shape,
'benchmark_timestamp': datetime.now().isoformat()
}
if self.has_profiler:
# Use advanced profiling from Module 15
results.update(self._profile_with_tinytorch_profiler(model, inputs))
else:
# Fallback to basic timing
results.update(self._profile_basic_timing(model, inputs))
# Calculate speedup if baseline provided
if baseline_model is not None:
baseline_results = self.benchmark_model(baseline_model, dataset)
speedup = baseline_results['mean_inference_time'] / results['mean_inference_time']
results['speedup_vs_baseline'] = speedup
elif baseline_time is not None:
speedup = baseline_time / results['mean_inference_time']
results['speedup_vs_baseline'] = speedup
self._print_benchmark_results(results)
return results
def _profile_with_tinytorch_profiler(self, model, inputs: np.ndarray) -> Dict[str, Any]:
"""Profile using Module 15's advanced profiler"""
profiler = SimpleProfiler(track_memory=True, track_cpu=True)
# Run multiple profiling sessions for statistical reliability
profile_results = []
for run in range(self.timing_runs):
# Each profiling session includes warmup
result = profiler.profile(
model.predict, inputs,
name=f"inference_run_{run}",
warmup=True # Profiler handles warmup
)
profile_results.append(result)
# Aggregate statistics across runs
wall_times = [r['wall_time'] for r in profile_results]
cpu_times = [r['cpu_time'] for r in profile_results]
aggregated = {
'mean_inference_time': np.mean(wall_times),
'std_inference_time': np.std(wall_times),
'min_inference_time': np.min(wall_times),
'max_inference_time': np.max(wall_times),
'p95_inference_time': np.percentile(wall_times, 95),
'mean_cpu_time': np.mean(cpu_times),
'cpu_efficiency': np.mean([r['cpu_efficiency'] for r in profile_results]),
'profiling_method': 'TinyTorch Module 15 Profiler'
}
# Add memory metrics from last run (most representative)
last_result = profile_results[-1]
if 'memory_delta_mb' in last_result:
aggregated.update({
'memory_delta_mb': last_result['memory_delta_mb'],
'peak_memory_mb': last_result['peak_memory_mb'],
'result_size_mb': last_result.get('result_size_mb', 0)
})
return aggregated
def _profile_basic_timing(self, model, inputs: np.ndarray) -> Dict[str, Any]:
"""Fallback basic timing without advanced profiling"""
# Warmup runs
for _ in range(self.warmup_runs):
_ = model.predict(inputs)
# Timing runs
times = []
for _ in range(self.timing_runs):
start = time.perf_counter()
_ = model.predict(inputs)
end = time.perf_counter()
times.append(end - start)
return {
'mean_inference_time': np.mean(times),
'std_inference_time': np.std(times),
'min_inference_time': np.min(times),
'max_inference_time': np.max(times),
'p95_inference_time': np.percentile(times, 95),
'profiling_method': 'Basic Timing'
}
def _print_benchmark_results(self, results: Dict[str, Any]):
"""Print formatted benchmark results"""
print(f"\n📊 {results['event']} Benchmark Results:")
print(f" Model: {results['model_type']}")
print(f" Input: {results['input_shape']}")
print(f" Mean Time: {results['mean_inference_time']*1000:.2f} ± {results['std_inference_time']*1000:.2f} ms")
print(f" Best Time: {results['min_inference_time']*1000:.2f} ms")
print(f" P95 Time: {results['p95_inference_time']*1000:.2f} ms")
if 'speedup_vs_baseline' in results:
print(f" 🚀 Speedup: {results['speedup_vs_baseline']:.2f}x faster")
if 'memory_delta_mb' in results:
print(f" 💾 Memory: {results['memory_delta_mb']:.2f} MB delta, {results['peak_memory_mb']:.2f} MB peak")
print(f" 📏 Method: {results['profiling_method']}")In [ ]:
def test_competition_profiler():
"""Test the competition profiler with benchmark models"""
print("Testing Competition Profiler...")
# Initialize TinyMLPerf and profiler
tinyperf = TinyMLPerf(profiler_warmup_runs=2, profiler_timing_runs=3)
profiler = CompetitionProfiler(warmup_runs=2, timing_runs=3)
# Test MLP Sprint profiling
mlp_model, mlp_dataset = tinyperf.load_benchmark('mlp_sprint')
mlp_results = profiler.benchmark_model(mlp_model, mlp_dataset)
# Test CNN Marathon profiling
cnn_model, cnn_dataset = tinyperf.load_benchmark('cnn_marathon')
cnn_results = profiler.benchmark_model(cnn_model, cnn_dataset)
# Test speedup calculation with baseline
print(f"\n🏃 Testing Speedup Calculation...")
cnn_speedup_results = profiler.benchmark_model(
cnn_model, cnn_dataset,
baseline_time=mlp_results['mean_inference_time'] # Use MLP as baseline
)
print(f"\n✅ Competition profiler test complete!")
return profiler, mlp_results, cnn_resultsIn [ ]:
class TinyMLPerfCompetition:
"""
TinyMLPerf Competition Framework - The Olympics of ML Optimization!
Manages three exciting competition events:
- MLP Sprint: Fastest feedforward network
- CNN Marathon: Most efficient convolutions
- Transformer Decathlon: Ultimate attention optimization
Features hardware-independent relative scoring and transparent leaderboards.
"""
def __init__(self, results_dir: str = "tinymlperf_results"):
"""
Initialize TinyMLPerf competition.
Args:
results_dir: Directory to store competition results and leaderboards
"""
self.results_dir = Path(results_dir)
self.results_dir.mkdir(exist_ok=True)
self.tinyperf = TinyMLPerf()
self.profiler = CompetitionProfiler(warmup_runs=3, timing_runs=5)
# Load baseline models for relative scoring
self.baselines = self._establish_baselines()
print("🏆 TinyMLPerf Competition Initialized!")
print("🎯 Three Events Ready for Competition!")
def _establish_baselines(self) -> Dict[str, float]:
"""Establish baseline performance for relative scoring"""
print("📏 Establishing baseline performance for relative scoring...")
baselines = {}
events = ['mlp_sprint', 'cnn_marathon', 'transformer_decathlon']
for event in events:
model, dataset = self.tinyperf.load_benchmark(event)
results = self.profiler.benchmark_model(model, dataset)
baselines[event] = results['mean_inference_time']
print(f" {event}: {baselines[event]*1000:.2f} ms baseline")
return baselines
def submit_entry(self, team_name: str, event_name: str, optimized_model,
optimization_description: str = "", github_url: str = "") -> Dict[str, Any]:
"""
Submit an optimized model to TinyMLPerf competition.
Args:
team_name: Name of the competing team
event_name: Competition event ('mlp_sprint', 'cnn_marathon', 'transformer_decathlon')
optimized_model: The optimized model to submit
optimization_description: Description of optimization techniques used
github_url: Link to code repository (for transparency)
Returns:
Submission results with performance metrics and scoring
"""
if event_name not in self.baselines:
available = list(self.baselines.keys())
raise ValueError(f"Event '{event_name}' not available. Choose from: {available}")
print(f"🚀 TINYMLPERF SUBMISSION")
print(f"🏆 Event: {event_name.replace('_', ' ').title()}")
print(f"👥 Team: {team_name}")
print("-" * 60)
# Load benchmark dataset for this event
_, dataset = self.tinyperf.load_benchmark(event_name)
# Benchmark the submitted model
results = self.profiler.benchmark_model(
optimized_model, dataset,
baseline_time=self.baselines[event_name]
)
# Calculate competition score (relative speedup)
baseline_time = self.baselines[event_name]
submission_time = results['mean_inference_time']
speedup_score = baseline_time / submission_time
# Create submission record
submission = {
'submission_id': self._generate_submission_id(team_name, event_name),
'timestamp': datetime.now().isoformat(),
'team_name': team_name,
'event_name': event_name,
'optimization_description': optimization_description,
'github_url': github_url,
'performance_metrics': results,
'speedup_score': speedup_score,
'baseline_time_ms': baseline_time * 1000,
'submission_time_ms': submission_time * 1000
}
# Save submission
self._save_submission(submission)
# Display results
self._display_submission_results(submission)
return submission
def _generate_submission_id(self, team_name: str, event_name: str) -> str:
"""Generate unique submission ID"""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
team_hash = hashlib.md5(team_name.encode()).hexdigest()[:6]
return f"{event_name}_{team_hash}_{timestamp}"
def _save_submission(self, submission: Dict[str, Any]):
"""Save submission to results directory"""
filename = f"{submission['submission_id']}.json"
filepath = self.results_dir / filename
with open(filepath, 'w') as f:
json.dump(submission, f, indent=2, default=str)
print(f"💾 Submission saved: {filepath}")
def _display_submission_results(self, submission: Dict[str, Any]):
"""Display formatted submission results"""
metrics = submission['performance_metrics']
speedup = submission['speedup_score']
print(f"\n🏆 SUBMISSION RESULTS")
print(f"=" * 50)
print(f"Team: {submission['team_name']}")
print(f"Event: {submission['event_name'].replace('_', ' ').title()}")
print(f"\n⏱️ Performance:")
print(f" Your Time: {submission['submission_time_ms']:.2f} ms")
print(f" Baseline: {submission['baseline_time_ms']:.2f} ms")
print(f" 🚀 Speedup: {speedup:.2f}x {'FASTER' if speedup > 1.0 else 'slower'}")
if 'memory_delta_mb' in metrics:
print(f" 💾 Memory: {metrics['memory_delta_mb']:.2f} MB")
# Award celebration for good performance
if speedup >= 3.0:
print(f"\n🎉 AMAZING! 3x+ speedup achieved!")
elif speedup >= 2.0:
print(f"\n🏆 EXCELLENT! 2x+ speedup!")
elif speedup >= 1.5:
print(f"\n⭐ GREAT! 50%+ speedup!")
elif speedup >= 1.1:
print(f"\n✅ Good optimization!")
else:
print(f"\n🤔 Keep optimizing - you can do better!")
if submission['optimization_description']:
print(f"\n💡 Techniques Used:")
print(f" {submission['optimization_description']}")
def display_leaderboard(self, event_name: str, top_n: int = 10) -> List[Dict[str, Any]]:
"""
Display leaderboard for a specific event.
Args:
event_name: Event to show leaderboard for
top_n: Number of top entries to display
Returns:
List of top submissions
"""
submissions = self._load_event_submissions(event_name)
if not submissions:
print(f"🏆 {event_name.replace('_', ' ').title()} Leaderboard")
print("No submissions yet! Be the first to compete!")
return []
# Sort by speedup score (highest first)
submissions.sort(key=lambda s: s['speedup_score'], reverse=True)
top_submissions = submissions[:top_n]
print(f"\n🏆 TINYMLPERF LEADERBOARD - {event_name.replace('_', ' ').title()}")
print("=" * 80)
print(f"{'Rank':<6} {'Team':<20} {'Speedup':<10} {'Time (ms)':<12} {'Techniques':<25}")
print("-" * 80)
for i, submission in enumerate(top_submissions):
rank = i + 1
team = submission['team_name'][:19]
speedup = f"{submission['speedup_score']:.2f}x"
time_ms = f"{submission['submission_time_ms']:.2f}"
techniques = submission['optimization_description'][:24] + "..." if len(submission['optimization_description']) > 24 else submission['optimization_description']
print(f"{rank:<6} {team:<20} {speedup:<10} {time_ms:<12} {techniques:<25}")
print("-" * 80)
print(f"Showing top {len(top_submissions)} of {len(submissions)} submissions")
return top_submissions
def display_all_leaderboards(self):
"""Display leaderboards for all events"""
events = ['mlp_sprint', 'cnn_marathon', 'transformer_decathlon']
for event in events:
self.display_leaderboard(event, top_n=5)
print()
def _load_event_submissions(self, event_name: str) -> List[Dict[str, Any]]:
"""Load all submissions for a specific event"""
submissions = []
for filepath in self.results_dir.glob(f"{event_name}_*.json"):
try:
with open(filepath, 'r') as f:
submission = json.load(f)
submissions.append(submission)
except Exception as e:
print(f"Warning: Could not load {filepath}: {e}")
return submissions
def get_team_progress(self, team_name: str) -> Dict[str, List[Dict[str, Any]]]:
"""Get all submissions from a specific team across all events"""
all_files = list(self.results_dir.glob("*.json"))
team_submissions = {'mlp_sprint': [], 'cnn_marathon': [], 'transformer_decathlon': []}
for filepath in all_files:
try:
with open(filepath, 'r') as f:
submission = json.load(f)
if submission['team_name'] == team_name:
event = submission['event_name']
if event in team_submissions:
team_submissions[event].append(submission)
except Exception as e:
continue
# Sort by timestamp
for event in team_submissions:
team_submissions[event].sort(key=lambda s: s['timestamp'])
return team_submissionsIn [ ]:
def test_tinymlperf_competition():
"""Test the TinyMLPerf competition framework"""
print("Testing TinyMLPerf Competition Framework...")
# Initialize competition
competition = TinyMLPerfCompetition()
# Create some test optimized models
class FastMLPModel:
"""Simulated optimized MLP - smaller and faster"""
def __init__(self):
# Smaller model for speed
self.weights1 = np.random.randn(784, 64).astype(np.float32) * 0.1
self.bias1 = np.random.randn(64).astype(np.float32) * 0.1
self.weights2 = np.random.randn(64, 10).astype(np.float32) * 0.1
self.bias2 = np.random.randn(10).astype(np.float32) * 0.1
def predict(self, x):
h1 = np.maximum(0, x @ self.weights1 + self.bias1)
return h1 @ self.weights2 + self.bias2
class EfficientCNNModel:
"""Simulated optimized CNN"""
def __init__(self):
# Optimized weights
self.fc_weights = np.random.randn(1600, 10).astype(np.float32) * 0.05
self.fc_bias = np.random.randn(10).astype(np.float32) * 0.05
def predict(self, x):
batch_size = x.shape[0]
x_flat = x.reshape(batch_size, -1)
if x_flat.shape[1] != 1600:
x_flat = x_flat[:, :1600] if x_flat.shape[1] > 1600 else np.pad(x_flat, ((0, 0), (0, 1600 - x_flat.shape[1])), 'constant')
return x_flat @ self.fc_weights + self.fc_bias
# Submit optimized models to competition
print("\n🚀 Submitting Competition Entries...")
# MLP Sprint submissions
mlp_submission1 = competition.submit_entry(
team_name="Speed Demons",
event_name="mlp_sprint",
optimized_model=FastMLPModel(),
optimization_description="Reduced hidden layer size for 2x speedup",
github_url="https://github.com/speed-demons/fast-mlp"
)
mlp_submission2 = competition.submit_entry(
team_name="Lightning Fast",
event_name="mlp_sprint",
optimized_model=FastMLPModel(),
optimization_description="Quantization + kernel optimization",
github_url="https://github.com/lightning-fast/mlp-opt"
)
# CNN Marathon submission
cnn_submission = competition.submit_entry(
team_name="CNN Champions",
event_name="cnn_marathon",
optimized_model=EfficientCNNModel(),
optimization_description="Custom convolution kernels + memory optimization",
github_url="https://github.com/cnn-champions/efficient-cnn"
)
# Display leaderboards
print("\n📊 Competition Leaderboards:")
competition.display_all_leaderboards()
print("\n✅ TinyMLPerf competition framework test complete!")
return competitionIn [ ]:
class InnovationDetector:
"""
Detect and score innovative optimization techniques in submitted models.
Rewards creativity by analyzing models for advanced optimization patterns:
- Quantization techniques
- Pruning strategies
- Knowledge distillation
- Custom kernel implementations
- Novel architectural innovations
"""
def __init__(self):
"""Initialize innovation detector"""
self.innovation_patterns = {
'quantization': ['quantized', 'int8', 'int16', 'low_precision', 'quantize'],
'pruning': ['pruned', 'sparse', 'sparsity', 'prune', 'structured_pruning'],
'distillation': ['distilled', 'teacher', 'student', 'knowledge_distillation', 'kd'],
'custom_kernels': ['custom_kernel', 'optimized_kernel', 'cuda', 'vectorized', 'simd'],
'memory_optimization': ['memory_pool', 'in_place', 'gradient_checkpointing', 'memory_efficient'],
'compression': ['compressed', 'huffman', 'lz4', 'weight_sharing', 'parameter_sharing']
}
def analyze_innovation(self, model, optimization_description: str) -> Dict[str, Any]:
"""
Analyze a model for innovative optimization techniques.
Args:
model: The optimized model to analyze
optimization_description: Text description of optimizations
Returns:
Innovation analysis with detected techniques and scores
"""
innovation_score = 0.0
detected_techniques = []
# Analyze optimization description
desc_lower = optimization_description.lower()
for technique, patterns in self.innovation_patterns.items():
for pattern in patterns:
if pattern in desc_lower:
detected_techniques.append(technique)
innovation_score += 0.2
break # Only count each technique once
# Analyze model attributes for innovation markers
model_innovation = self._analyze_model_attributes(model)
detected_techniques.extend(model_innovation['techniques'])
innovation_score += model_innovation['score']
# Bonus for multiple techniques (creativity reward)
if len(detected_techniques) >= 3:
innovation_score += 0.3 # Combination bonus
# Cap innovation score
innovation_score = min(innovation_score, 1.0)
return {
'innovation_score': innovation_score,
'detected_techniques': list(set(detected_techniques)), # Remove duplicates
'num_techniques': len(set(detected_techniques)),
'creativity_bonus': len(detected_techniques) >= 3
}
def _analyze_model_attributes(self, model) -> Dict[str, Any]:
"""Analyze model object for innovation attributes"""
techniques = []
score = 0.0
# Check for common optimization attributes
optimization_attributes = [
('quantized', 'quantization'),
('pruned', 'pruning'),
('distilled', 'distillation'),
('compressed', 'compression'),
('memory_optimized', 'memory_optimization'),
('custom_kernels', 'custom_kernels')
]
for attr, technique in optimization_attributes:
if hasattr(model, attr) and getattr(model, attr):
techniques.append(technique)
score += 0.15
# Check for unusual model architectures (creativity indicator)
if hasattr(model, 'innovative_architecture') and getattr(model, 'innovative_architecture'):
techniques.append('novel_architecture')
score += 0.25
return {'techniques': techniques, 'score': score}
def generate_innovation_report(self, analysis: Dict[str, Any]) -> str:
"""Generate human-readable innovation report"""
score = analysis['innovation_score']
techniques = analysis['detected_techniques']
if score == 0:
return "No innovative techniques detected. Consider exploring quantization, pruning, or custom optimizations!"
report = f"Innovation Score: {score:.2f}/1.00\n"
report += f"Detected Techniques ({len(techniques)}):\n"
for technique in techniques:
report += f" • {technique.replace('_', ' ').title()}\n"
if analysis['creativity_bonus']:
report += "🌟 Creativity Bonus: Multiple optimization techniques combined!\n"
# Award levels
if score >= 0.8:
report += "🏆 INNOVATION MASTER - Outstanding creativity!"
elif score >= 0.6:
report += "🚀 INNOVATION EXPERT - Excellent techniques!"
elif score >= 0.4:
report += "⭐ INNOVATION PRACTITIONER - Good optimization work!"
else:
report += "🔍 INNOVATION EXPLORER - Keep experimenting!"
return report
# Enhanced competition class with innovation scoring
class TinyMLPerfCompetitionPlus(TinyMLPerfCompetition):
"""
Enhanced TinyMLPerf Competition with innovation detection and advanced scoring.
Extends the base competition with:
- Innovation technique detection
- Advanced composite scoring
- Creativity rewards
- Multi-dimensional leaderboards
"""
def __init__(self, results_dir: str = "tinymlperf_results"):
"""Initialize enhanced competition with innovation detection"""
super().__init__(results_dir)
self.innovation_detector = InnovationDetector()
print("🔬 Innovation detection enabled!")
def submit_entry(self, team_name: str, event_name: str, optimized_model,
optimization_description: str = "", github_url: str = "") -> Dict[str, Any]:
"""Submit entry with innovation analysis"""
# Get base submission
submission = super().submit_entry(team_name, event_name, optimized_model,
optimization_description, github_url)
# Add innovation analysis
innovation_analysis = self.innovation_detector.analyze_innovation(
optimized_model, optimization_description
)
submission['innovation_analysis'] = innovation_analysis
# Calculate composite score (speed + innovation)
speed_score = submission['speedup_score'] # Relative speedup
innovation_score = innovation_analysis['innovation_score']
# Weighted composite: 70% speed, 30% innovation
composite_score = 0.7 * speed_score + 0.3 * innovation_score
submission['composite_score'] = composite_score
# Display innovation results
print(f"\n🔬 Innovation Analysis:")
innovation_report = self.innovation_detector.generate_innovation_report(innovation_analysis)
print(innovation_report)
print(f"\n🏆 Composite Score: {composite_score:.3f} (Speed: {speed_score:.2f}, Innovation: {innovation_score:.2f})")
# Re-save with innovation data
self._save_submission(submission)
return submission
def display_innovation_leaderboard(self, event_name: str, top_n: int = 10):
"""Display leaderboard ranked by innovation score"""
submissions = self._load_event_submissions(event_name)
# Filter submissions with innovation data
innovation_submissions = [s for s in submissions if 'innovation_analysis' in s]
if not innovation_submissions:
print(f"🔬 Innovation Leaderboard - {event_name.replace('_', ' ').title()}")
print("No innovation submissions yet!")
return
# Sort by innovation score
innovation_submissions.sort(key=lambda s: s['innovation_analysis']['innovation_score'], reverse=True)
top_submissions = innovation_submissions[:top_n]
print(f"\n🔬 INNOVATION LEADERBOARD - {event_name.replace('_', ' ').title()}")
print("=" * 80)
print(f"{'Rank':<6} {'Team':<20} {'Innovation':<12} {'Techniques':<8} {'Description':<25}")
print("-" * 80)
for i, submission in enumerate(top_submissions):
rank = i + 1
team = submission['team_name'][:19]
innovation = f"{submission['innovation_analysis']['innovation_score']:.3f}"
num_tech = submission['innovation_analysis']['num_techniques']
description = submission['optimization_description'][:24]
print(f"{rank:<6} {team:<20} {innovation:<12} {num_tech:<8} {description:<25}")
print("-" * 80)
print(f"Top {len(top_submissions)} most innovative submissions")
def display_composite_leaderboard(self, event_name: str, top_n: int = 10):
"""Display leaderboard ranked by composite score (speed + innovation)"""
submissions = self._load_event_submissions(event_name)
# Filter submissions with composite scores
composite_submissions = [s for s in submissions if 'composite_score' in s]
if not composite_submissions:
print(f"🏆 Composite Leaderboard - {event_name.replace('_', ' ').title()}")
print("No composite submissions yet!")
return
# Sort by composite score
composite_submissions.sort(key=lambda s: s['composite_score'], reverse=True)
top_submissions = composite_submissions[:top_n]
print(f"\n🏆 COMPOSITE LEADERBOARD - {event_name.replace('_', ' ').title()}")
print("=" * 90)
print(f"{'Rank':<6} {'Team':<18} {'Composite':<11} {'Speed':<9} {'Innovation':<11} {'Techniques'}")
print("-" * 90)
for i, submission in enumerate(top_submissions):
rank = i + 1
team = submission['team_name'][:17]
composite = f"{submission['composite_score']:.3f}"
speed = f"{submission['speedup_score']:.2f}x"
innovation = f"{submission['innovation_analysis']['innovation_score']:.3f}"
techniques = ", ".join(submission['innovation_analysis']['detected_techniques'][:3])[:20]
print(f"{rank:<6} {team:<18} {composite:<11} {speed:<9} {innovation:<11} {techniques}")
print("-" * 90)
print(f"Top {len(top_submissions)} best overall submissions (70% speed + 30% innovation)")
def display_all_enhanced_leaderboards(self):
"""Display all leaderboard types for all events"""
events = ['mlp_sprint', 'cnn_marathon', 'transformer_decathlon']
for event in events:
print(f"\n{'='*60}")
print(f"🏆 {event.replace('_', ' ').title()} - All Leaderboards")
print(f"{'='*60}")
# Speed leaderboard
self.display_leaderboard(event, top_n=5)
print()
# Innovation leaderboard
self.display_innovation_leaderboard(event, top_n=5)
print()
# Composite leaderboard
self.display_composite_leaderboard(event, top_n=5)
print()In [ ]:
def test_enhanced_competition():
"""Test enhanced competition with innovation detection"""
print("Testing Enhanced TinyMLPerf Competition...")
# Initialize enhanced competition
competition = TinyMLPerfCompetitionPlus()
# Create innovative models with optimization attributes
class QuantizedFastMLP:
"""Simulated quantized MLP"""
def __init__(self):
self.weights1 = np.random.randn(784, 64).astype(np.int8) # Quantized weights
self.bias1 = np.random.randn(64).astype(np.float32) * 0.1
self.weights2 = np.random.randn(64, 10).astype(np.int8)
self.bias2 = np.random.randn(10).astype(np.float32) * 0.1
self.quantized = True # Innovation marker
def predict(self, x):
# Simulate quantized computation
h1 = np.maximum(0, x @ self.weights1.astype(np.float32) * 0.1 + self.bias1)
return h1 @ self.weights2.astype(np.float32) * 0.1 + self.bias2
class PrunedCNN:
"""Simulated pruned CNN"""
def __init__(self):
self.fc_weights = np.random.randn(1600, 10).astype(np.float32) * 0.05
self.fc_bias = np.random.randn(10).astype(np.float32) * 0.05
self.pruned = True # Innovation marker
self.sparsity = 0.7 # 70% of weights pruned
def predict(self, x):
batch_size = x.shape[0]
x_flat = x.reshape(batch_size, -1)
if x_flat.shape[1] != 1600:
x_flat = x_flat[:, :1600] if x_flat.shape[1] > 1600 else np.pad(x_flat, ((0, 0), (0, 1600 - x_flat.shape[1])), 'constant')
return x_flat @ self.fc_weights + self.fc_bias
# Submit innovative entries
print("\n🚀 Submitting Innovative Entries...")
# Quantized MLP submission
quantized_submission = competition.submit_entry(
team_name="Quantum Quantizers",
event_name="mlp_sprint",
optimized_model=QuantizedFastMLP(),
optimization_description="INT8 quantization with custom SIMD kernels for 3x speedup",
github_url="https://github.com/quantum-quantizers/quantized-mlp"
)
# Pruned CNN submission
pruned_submission = competition.submit_entry(
team_name="Pruning Pioneers",
event_name="cnn_marathon",
optimized_model=PrunedCNN(),
optimization_description="Structured pruning + knowledge distillation + memory optimization",
github_url="https://github.com/pruning-pioneers/pruned-cnn"
)
# Display enhanced leaderboards
print("\n📊 Enhanced Competition Leaderboards:")
competition.display_all_enhanced_leaderboards()
print("\n✅ Enhanced competition test complete!")
return competitionIn [ ]:
def run_complete_tinymlperf_demo():
"""Run comprehensive TinyMLPerf competition demonstration"""
print("🏆 TINYMLPERF - THE ULTIMATE ML SYSTEMS COMPETITION")
print("=" * 80)
print("\n1. 🏗️ Setting up TinyMLPerf Benchmark Suite...")
# Test benchmark suite
tinyperf = test_tinymlperf_benchmark_suite()
print("\n2. ⚡ Testing Competition Profiling...")
# Test profiling infrastructure
profiler, mlp_results, cnn_results = test_competition_profiler()
print("\n3. 🚀 Running Basic Competition...")
# Test basic competition
basic_competition = test_tinymlperf_competition()
print("\n4. 🔬 Testing Enhanced Competition with Innovation...")
# Test enhanced competition
enhanced_competition = test_enhanced_competition()
print("\n" + "=" * 80)
print("🎉 TINYMLPERF DEMO COMPLETE!")
print("=" * 80)
print("\n🏆 TinyMLPerf Competition Ready:")
print("✅ Three exciting events: MLP Sprint, CNN Marathon, Transformer Decathlon")
print("✅ TinyTorch Module 15 profiler integration for rigorous benchmarking")
print("✅ Hardware-independent relative scoring (speedup ratios)")
print("✅ Transparent leaderboards with evidence requirements")
print("✅ Innovation detection and creativity rewards")
print("✅ Composite scoring balancing speed and innovation")
print("\n🚀 Competition Features:")
print("• Standardized benchmark models and datasets")
print("• Statistical reliability with multiple timing runs")
print("• Multiple leaderboard categories (speed, innovation, composite)")
print("• GitHub integration for transparency and reproducibility")
print("• Automatic technique detection and innovation scoring")
print("\n🎯 Ready to Compete:")
print("1. Optimize your models using techniques from Modules 16-19")
print("2. Submit to TinyMLPerf events using competition.submit_entry()")
print("3. See your results on leaderboards instantly")
print("4. Iterate and improve based on performance feedback")
print("5. Prove your ML systems optimization mastery!")
return {
'benchmark_suite': tinyperf,
'profiler': profiler,
'basic_competition': basic_competition,
'enhanced_competition': enhanced_competition
}In [ ]:
if __name__ == "__main__":
print("Module 20: TinyMLPerf - The Ultimate ML Systems Competition")
print("=" * 80)
# Run complete TinyMLPerf demonstration
results = run_complete_tinymlperf_demo()
print(f"\n🎉 Module 20 complete!")
print(f"🏆 TinyMLPerf competition infrastructure ready!")
print(f"🚀 Time to optimize your models and climb the leaderboards!")