{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9ff31aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| default_exp benchmarking.benchmark\n",
    "#| export"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d49b6d28",
   "metadata": {
    "cell_marker": "\"\"\""
   },
   "source": [
    "# Module 19: Benchmarking - TorchPerf Olympics Preparation\n",
    "\n",
    "Welcome to the final implementation module! You've learned individual optimization techniques in Modules 14-18. Now you'll build the benchmarking infrastructure that powers **TorchPerf Olympics** - the capstone competition framework.\n",
    "\n",
    "## 🔗 Prerequisites & Progress\n",
    "**You've Built**: Complete ML framework with profiling, acceleration, quantization, and compression\n",
    "**You'll Build**: TorchPerf benchmarking system for fair model comparison and capstone submission\n",
    "**You'll Enable**: Systematic optimization combination and competitive performance evaluation\n",
    "\n",
    "**Connection Map**:\n",
    "```\n",
    "Individual Optimizations (M14-18) → Benchmarking (M19) → TorchPerf Olympics (Capstone)\n",
    "(techniques)                        (evaluation)         (competition)\n",
    "```\n",
    "\n",
    "## 🏅 TorchPerf Olympics: The Capstone Framework\n",
    "\n",
    "The TorchPerf Olympics is your capstone competition! Choose your event:\n",
    "- 🏃 **Latency Sprint**: Minimize inference time (fastest model wins)\n",
    "- 🏋️ **Memory Challenge**: Minimize model size (smallest footprint wins)  \n",
    "- 🎯 **Accuracy Contest**: Maximize accuracy within constraints\n",
    "- 🏋️‍♂️ **All-Around**: Best balanced performance across all metrics\n",
    "- 🚀 **Extreme Push**: Most aggressive optimization while staying viable\n",
    "\n",
    "## Learning Objectives\n",
    "By the end of this module, you will:\n",
    "1. Implement professional benchmarking infrastructure with statistical rigor\n",
    "2. Learn to combine optimization techniques strategically (order matters!)\n",
    "3. Build the TorchPerf class - your standardized capstone submission framework\n",
    "4. Understand ablation studies and systematic performance evaluation\n",
    "\n",
    "🔥 Carry the torch. Optimize the model. Win the gold! 🏅"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1dd61735",
   "metadata": {
    "cell_marker": "\"\"\""
   },
   "source": [
    "## 📦 Where This Code Lives in the Final Package\n",
    "\n",
    "**Learning Side:** You work in `modules/19_benchmarking/benchmarking_dev.py`  \n",
    "**Building Side:** Code exports to `tinytorch.benchmarking.benchmark`\n",
    "\n",
    "```python\n",
    "# How to use this module:\n",
    "from tinytorch.benchmarking.benchmark import Benchmark, OlympicEvent\n",
    "\n",
    "# For capstone submission:\n",
    "benchmark = Benchmark([baseline_model, optimized_model],\n",
    "                     [{\"name\": \"baseline\"}, {\"name\": \"optimized\"}])\n",
    "results = benchmark.run_latency_benchmark()\n",
    "```\n",
    "\n",
    "**Why this matters:**\n",
    "- **Learning:** Complete benchmarking ecosystem in one focused module for rigorous evaluation\n",
    "- **TorchPerf Olympics:** The Benchmark class provides the standardized framework for capstone submissions\n",
    "- **Consistency:** All benchmarking operations and reporting in benchmarking.benchmark\n",
    "- **Integration:** Works seamlessly with optimization modules (M14-18) for complete systems evaluation"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdb58292",
   "metadata": {
    "cell_marker": "\"\"\""
   },
   "source": [
    "# 1. Introduction - What is Fair Benchmarking?\n",
    "\n",
    "Benchmarking in ML systems isn't just timing code - it's about making fair, reproducible comparisons that guide real optimization decisions. Think of it like standardized testing: everyone takes the same test under the same conditions.\n",
    "\n",
    "Consider comparing three models: a base CNN, a quantized version, and a pruned version. Without proper benchmarking, you might conclude the quantized model is \"fastest\" because you measured it when your CPU was idle, while testing the others during peak system load. Fair benchmarking controls for these variables.\n",
    "\n",
    "The challenge: ML models have multiple competing objectives (accuracy vs speed vs memory), measurements can be noisy, and \"faster\" depends on your hardware and use case.\n",
    "\n",
    "## Benchmarking as a Systems Engineering Discipline\n",
    "\n",
    "Professional ML benchmarking requires understanding measurement uncertainty and controlling for confounding factors:\n",
    "\n",
    "**Statistical Foundations**: We need enough measurements to achieve statistical significance. Running a model once tells you nothing about its true performance - you need distributions.\n",
    "\n",
    "**System Noise Sources**:\n",
    "- **Thermal throttling**: CPU frequency drops when hot\n",
    "- **Background processes**: OS interrupts and other applications\n",
    "- **Memory pressure**: Garbage collection, cache misses\n",
    "- **Network interference**: For distributed models\n",
    "\n",
    "**Fair Comparison Requirements**:\n",
    "- Same hardware configuration\n",
    "- Same input data distributions\n",
    "- Same measurement methodology\n",
    "- Statistical significance testing\n",
    "\n",
    "This module builds infrastructure that addresses all these challenges while generating actionable insights for optimization decisions."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a41ba608",
   "metadata": {
    "cell_marker": "\"\"\""
   },
   "source": [
    "# 2. Mathematical Foundations - Statistics for Performance Engineering\n",
    "\n",
    "Benchmarking is applied statistics. We measure noisy processes (model inference) and need to extract reliable insights about their true performance characteristics.\n",
    "\n",
    "## Central Limit Theorem in Practice\n",
    "\n",
    "When you run a model many times, the distribution of measurements approaches normal (regardless of the underlying noise distribution). This lets us:\n",
    "- Compute confidence intervals for the true mean\n",
    "- Detect statistically significant differences between models\n",
    "- Control for measurement variance\n",
    "\n",
    "```\n",
    "Single measurement: Meaningless\n",
    "Few measurements: Unreliable\n",
    "Many measurements: Statistical confidence\n",
    "```\n",
    "\n",
    "## Multi-Objective Optimization Theory\n",
    "\n",
    "ML systems exist on a **Pareto frontier** - you can't simultaneously maximize accuracy and minimize latency without trade-offs. Good benchmarks reveal this frontier:\n",
    "\n",
    "```\n",
    "Accuracy\n",
    "    ↑\n",
    "    |  A ●     ← Model A: High accuracy, high latency\n",
    "    |\n",
    "    |    B ●  ← Model B: Balanced trade-off\n",
    "    |\n",
    "    |      C ●← Model C: Low accuracy, low latency\n",
    "    |__________→ Latency (lower is better)\n",
    "```\n",
    "\n",
    "The goal: Find the optimal operating point for your specific constraints.\n",
    "\n",
    "## Measurement Uncertainty and Error Propagation\n",
    "\n",
    "Every measurement has uncertainty. When combining metrics (like accuracy per joule), uncertainties compound:\n",
    "\n",
    "- **Systematic errors**: Consistent bias (timer overhead, warmup effects)\n",
    "- **Random errors**: Statistical noise (thermal variation, OS scheduling)\n",
    "- **Propagated errors**: How uncertainty spreads through calculations\n",
    "\n",
    "Professional benchmarking quantifies and minimizes these uncertainties."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3698099e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import time\n",
    "import statistics\n",
    "import matplotlib.pyplot as plt\n",
    "from typing import Dict, List, Tuple, Any, Optional, Callable, Union\n",
    "from dataclasses import dataclass, field\n",
    "from pathlib import Path\n",
    "import json\n",
    "import psutil\n",
    "import platform\n",
    "from contextlib import contextmanager\n",
    "import warnings\n",
    "\n",
    "# Import Profiler from Module 15 for measurement reuse\n",
    "from tinytorch.profiling.profiler import Profiler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ba1d3dc",
   "metadata": {
    "lines_to_next_cell": 1
   },
   "outputs": [],
   "source": [
    "#| export\n",
    "from enum import Enum\n",
    "\n",
    "class OlympicEvent(Enum):\n",
    "    \"\"\"\n",
    "    TorchPerf Olympics event categories.\n",
    "    \n",
    "    Each event optimizes for different objectives with specific constraints.\n",
    "    Students choose their event and compete for medals!\n",
    "    \"\"\"\n",
    "    LATENCY_SPRINT = \"latency_sprint\"      # Minimize latency (accuracy >= 85%)\n",
    "    MEMORY_CHALLENGE = \"memory_challenge\"   # Minimize memory (accuracy >= 85%)\n",
    "    ACCURACY_CONTEST = \"accuracy_contest\"   # Maximize accuracy (latency < 100ms, memory < 10MB)\n",
    "    ALL_AROUND = \"all_around\"               # Best balanced score across all metrics\n",
    "    EXTREME_PUSH = \"extreme_push\"           # Most aggressive optimization (accuracy >= 80%)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4bd5a37",
   "metadata": {
    "cell_marker": "\"\"\""
   },
   "source": [
    "# 3. Implementation - Building Professional Benchmarking Infrastructure\n",
    "\n",
    "We'll build a comprehensive benchmarking system that handles statistical analysis, multi-dimensional comparison, and automated reporting. Each component builds toward production-quality evaluation tools.\n",
    "\n",
    "The architecture follows a hierarchical design:\n",
    "```\n",
    "Profiler (Module 15) ← Base measurement tools\n",
    "       ↓\n",
    "BenchmarkResult ← Statistical container for measurements\n",
    "       ↓\n",
    "Benchmark ← Uses Profiler + adds multi-model comparison\n",
    "       ↓\n",
    "BenchmarkSuite ← Multi-metric comprehensive evaluation\n",
    "       ↓\n",
    "TinyMLPerf ← Standardized industry-style benchmarks\n",
    "```\n",
    "\n",
    "**Key Architectural Decision**: The `Benchmark` class reuses `Profiler` from Module 15 for individual model measurements, then adds statistical comparison across multiple models. This demonstrates proper systems architecture - build once, reuse everywhere!\n",
    "\n",
    "Each level adds capability while maintaining statistical rigor at the foundation."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17a008af",
   "metadata": {
    "cell_marker": "\"\"\"",
    "lines_to_next_cell": 1
   },
   "source": [
    "## BenchmarkResult - Statistical Analysis Container\n",
    "\n",
    "Before measuring anything, we need a robust container that stores measurements and computes statistical properties. This is the foundation of all our benchmarking.\n",
    "\n",
    "### Why Statistical Analysis Matters\n",
    "\n",
    "Single measurements are meaningless in performance engineering. Consider timing a model:\n",
    "- Run 1: 1.2ms (CPU was idle)\n",
    "- Run 2: 3.1ms (background process started)\n",
    "- Run 3: 1.4ms (CPU returned to normal)\n",
    "\n",
    "Without statistics, which number do you trust? BenchmarkResult solves this by:\n",
    "- Computing confidence intervals for the true mean\n",
    "- Detecting outliers and measurement noise\n",
    "- Providing uncertainty estimates for decision making\n",
    "\n",
    "### Statistical Properties We Track\n",
    "\n",
    "```\n",
    "Raw measurements: [1.2, 3.1, 1.4, 1.3, 1.5, 1.1, 1.6]\n",
    "                           ↓\n",
    "        Statistical Analysis\n",
    "                           ↓\n",
    "Mean: 1.46ms ± 0.25ms (95% confidence interval)\n",
    "Median: 1.4ms (less sensitive to outliers)\n",
    "CV: 17% (coefficient of variation - relative noise)\n",
    "```\n",
    "\n",
    "The confidence interval tells us: \"We're 95% confident the true mean latency is between 1.21ms and 1.71ms.\" This guides optimization decisions with statistical backing."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58b069fb",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "benchmark-dataclass",
     "solution": true
    }
   },
   "outputs": [],
   "source": [
    "@dataclass\n",
    "class BenchmarkResult:\n",
    "    \"\"\"\n",
    "    Container for benchmark measurements with statistical analysis.\n",
    "\n",
    "    TODO: Implement a robust result container that stores measurements and metadata\n",
    "\n",
    "    APPROACH:\n",
    "    1. Store raw measurements and computed statistics\n",
    "    2. Include metadata about test conditions\n",
    "    3. Provide methods for statistical analysis\n",
    "    4. Support serialization for result persistence\n",
    "\n",
    "    EXAMPLE:\n",
    "    >>> result = BenchmarkResult(\"model_accuracy\", [0.95, 0.94, 0.96])\n",
    "    >>> print(f\"Mean: {result.mean:.3f} ± {result.std:.3f}\")\n",
    "    Mean: 0.950 ± 0.010\n",
    "\n",
    "    HINTS:\n",
    "    - Use statistics module for robust mean/std calculations\n",
    "    - Store both raw data and summary statistics\n",
    "    - Include confidence intervals for professional reporting\n",
    "    \"\"\"\n",
    "    ### BEGIN SOLUTION\n",
    "    metric_name: str\n",
    "    values: List[float]\n",
    "    metadata: Dict[str, Any] = field(default_factory=dict)\n",
    "\n",
    "    def __post_init__(self):\n",
    "        \"\"\"Compute statistics after initialization.\"\"\"\n",
    "        if not self.values:\n",
    "            raise ValueError(\"BenchmarkResult requires at least one measurement\")\n",
    "\n",
    "        self.mean = statistics.mean(self.values)\n",
    "        self.std = statistics.stdev(self.values) if len(self.values) > 1 else 0.0\n",
    "        self.median = statistics.median(self.values)\n",
    "        self.min_val = min(self.values)\n",
    "        self.max_val = max(self.values)\n",
    "        self.count = len(self.values)\n",
    "\n",
    "        # 95% confidence interval for the mean\n",
    "        if len(self.values) > 1:\n",
    "            t_score = 1.96  # Approximate for large samples\n",
    "            margin_error = t_score * (self.std / np.sqrt(self.count))\n",
    "            self.ci_lower = self.mean - margin_error\n",
    "            self.ci_upper = self.mean + margin_error\n",
    "        else:\n",
    "            self.ci_lower = self.ci_upper = self.mean\n",
    "\n",
    "    def to_dict(self) -> Dict[str, Any]:\n",
    "        \"\"\"Convert to dictionary for serialization.\"\"\"\n",
    "        return {\n",
    "            'metric_name': self.metric_name,\n",
    "            'values': self.values,\n",
    "            'mean': self.mean,\n",
    "            'std': self.std,\n",
    "            'median': self.median,\n",
    "            'min': self.min_val,\n",
    "            'max': self.max_val,\n",
    "            'count': self.count,\n",
    "            'ci_lower': self.ci_lower,\n",
    "            'ci_upper': self.ci_upper,\n",
    "            'metadata': self.metadata\n",
    "        }\n",
    "\n",
    "    def __str__(self) -> str:\n",
    "        return f\"{self.metric_name}: {self.mean:.4f} ± {self.std:.4f} (n={self.count})\"\n",
    "    ### END SOLUTION\n",
    "\n",
    "def test_unit_benchmark_result():\n",
    "    \"\"\"🔬 Test BenchmarkResult statistical calculations.\"\"\"\n",
    "    print(\"🔬 Unit Test: BenchmarkResult...\")\n",
    "\n",
    "    # Test basic statistics\n",
    "    values = [1.0, 2.0, 3.0, 4.0, 5.0]\n",
    "    result = BenchmarkResult(\"test_metric\", values)\n",
    "\n",
    "    assert result.mean == 3.0\n",
    "    assert abs(result.std - statistics.stdev(values)) < 1e-10\n",
    "    assert result.median == 3.0\n",
    "    assert result.min_val == 1.0\n",
    "    assert result.max_val == 5.0\n",
    "    assert result.count == 5\n",
    "\n",
    "    # Test confidence intervals\n",
    "    assert result.ci_lower < result.mean < result.ci_upper\n",
    "\n",
    "    # Test serialization\n",
    "    result_dict = result.to_dict()\n",
    "    assert result_dict['metric_name'] == \"test_metric\"\n",
    "    assert result_dict['mean'] == 3.0\n",
    "\n",
    "    print(\"✅ BenchmarkResult works correctly!\")\n",
    "\n",
    "test_unit_benchmark_result()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8205c609",
   "metadata": {
    "cell_marker": "\"\"\"",
    "lines_to_next_cell": 1
   },
   "source": [
    "## High-Precision Timing Infrastructure\n",
    "\n",
    "Accurate timing is the foundation of performance benchmarking. System clocks have different precision and behavior, so we need a robust timing mechanism.\n",
    "\n",
    "### Timing Challenges in Practice\n",
    "\n",
    "Consider what happens when you time a function:\n",
    "```\n",
    "User calls: time.time()\n",
    "            ↓\n",
    "Operating System scheduling delays (μs to ms)\n",
    "            ↓\n",
    "Timer system call overhead (~1μs)\n",
    "            ↓\n",
    "Hardware clock resolution (ns to μs)\n",
    "            ↓\n",
    "Your measurement\n",
    "```\n",
    "\n",
    "For microsecond-precision timing, each of these can introduce significant error.\n",
    "\n",
    "### Why perf_counter() Matters\n",
    "\n",
    "Python's `time.perf_counter()` is specifically designed for interval measurement:\n",
    "- **Monotonic**: Never goes backwards (unaffected by system clock adjustments)\n",
    "- **High resolution**: Typically nanosecond precision\n",
    "- **Low overhead**: Optimized system call\n",
    "\n",
    "### Timing Best Practices\n",
    "\n",
    "```\n",
    "Context Manager Pattern:\n",
    "┌─────────────────┐\n",
    "│  with timer():  │ ← Start timing\n",
    "│    operation()  │ ← Your code runs\n",
    "│  # End timing   │ ← Automatic cleanup\n",
    "└─────────────────┘\n",
    "    ↓\n",
    "elapsed = timer.elapsed\n",
    "```\n",
    "\n",
    "This pattern ensures timing starts/stops correctly even if exceptions occur."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec6dd3bb",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "timer-context",
     "solution": true
    }
   },
   "outputs": [],
   "source": [
    "@contextmanager\n",
    "def precise_timer():\n",
    "    \"\"\"\n",
    "    High-precision timing context manager for benchmarking.\n",
    "\n",
    "    TODO: Implement a context manager that provides accurate timing measurements\n",
    "\n",
    "    APPROACH:\n",
    "    1. Use time.perf_counter() for high precision\n",
    "    2. Handle potential interruptions and system noise\n",
    "    3. Return elapsed time when context exits\n",
    "    4. Provide warmup capability for JIT compilation\n",
    "\n",
    "    EXAMPLE:\n",
    "    >>> with precise_timer() as timer:\n",
    "    ...     time.sleep(0.1)  # Some operation\n",
    "    >>> print(f\"Elapsed: {timer.elapsed:.4f}s\")\n",
    "    Elapsed: 0.1001s\n",
    "\n",
    "    HINTS:\n",
    "    - perf_counter() is monotonic and high-resolution\n",
    "    - Store start time in __enter__, compute elapsed in __exit__\n",
    "    - Handle any exceptions gracefully\n",
    "    \"\"\"\n",
    "    ### BEGIN SOLUTION\n",
    "    class Timer:\n",
    "        def __init__(self):\n",
    "            self.elapsed = 0.0\n",
    "            self.start_time = None\n",
    "\n",
    "        def __enter__(self):\n",
    "            self.start_time = time.perf_counter()\n",
    "            return self\n",
    "\n",
    "        def __exit__(self, exc_type, exc_val, exc_tb):\n",
    "            if self.start_time is not None:\n",
    "                self.elapsed = time.perf_counter() - self.start_time\n",
    "            return False  # Don't suppress exceptions\n",
    "\n",
    "    return Timer()\n",
    "    ### END SOLUTION\n",
    "\n",
    "def test_unit_precise_timer():\n",
    "    \"\"\"🔬 Test precise_timer context manager.\"\"\"\n",
    "    print(\"🔬 Unit Test: precise_timer...\")\n",
    "\n",
    "    # Test basic timing\n",
    "    with precise_timer() as timer:\n",
    "        time.sleep(0.01)  # 10ms sleep\n",
    "\n",
    "    # Should be close to 0.01 seconds (allow some variance)\n",
    "    assert 0.005 < timer.elapsed < 0.05, f\"Expected ~0.01s, got {timer.elapsed}s\"\n",
    "\n",
    "    # Test multiple uses\n",
    "    times = []\n",
    "    for _ in range(3):\n",
    "        with precise_timer() as timer:\n",
    "            time.sleep(0.001)  # 1ms sleep\n",
    "        times.append(timer.elapsed)\n",
    "\n",
    "    # All times should be reasonably close\n",
    "    assert all(0.0005 < t < 0.01 for t in times)\n",
    "\n",
    "    print(\"✅ precise_timer works correctly!\")\n",
    "\n",
    "test_unit_precise_timer()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e369a7a0",
   "metadata": {
    "cell_marker": "\"\"\"",
    "lines_to_next_cell": 1
   },
   "source": [
    "## Benchmark Class - Core Measurement Engine\n",
    "\n",
    "The Benchmark class implements the core measurement logic for different metrics. It handles the complex orchestration of multiple models, datasets, and measurement protocols.\n",
    "\n",
    "### Benchmark Architecture Overview\n",
    "\n",
    "```\n",
    "Benchmark Execution Flow:\n",
    "┌─────────────┐    ┌──────────────┐    ┌─────────────────┐\n",
    "│   Models    │    │   Datasets   │    │ Measurement     │\n",
    "│ [M1, M2...] │ → │ [D1, D2...]  │ → │ Protocol        │\n",
    "└─────────────┘    └──────────────┘    └─────────────────┘\n",
    "                                               ↓\n",
    "                           ┌─────────────────────────────────┐\n",
    "                           │        Benchmark Loop           │\n",
    "                           │ 1. Warmup runs (JIT, cache)    │\n",
    "                           │ 2. Measurement runs (statistics)│\n",
    "                           │ 3. System info capture         │\n",
    "                           │ 4. Result aggregation          │\n",
    "                           └─────────────────────────────────┘\n",
    "                                        ↓\n",
    "                    ┌────────────────────────────────────┐\n",
    "                    │          BenchmarkResult           │\n",
    "                    │ • Statistical analysis             │\n",
    "                    │ • Confidence intervals             │\n",
    "                    │ • Metadata (system, conditions)    │\n",
    "                    └────────────────────────────────────┘\n",
    "```\n",
    "\n",
    "### Why Warmup Runs Matter\n",
    "\n",
    "Modern systems have multiple layers of adaptation:\n",
    "- **JIT compilation**: Code gets faster after being run several times\n",
    "- **CPU frequency scaling**: Processors ramp up under load\n",
    "- **Cache warming**: Data gets loaded into faster memory\n",
    "- **Branch prediction**: CPU learns common execution paths\n",
    "\n",
    "Without warmup, your first few measurements don't represent steady-state performance.\n",
    "\n",
    "### Multiple Benchmark Types\n",
    "\n",
    "Different metrics require different measurement strategies:\n",
    "\n",
    "**Latency Benchmarking**:\n",
    "- Focus: Time per inference\n",
    "- Key factors: Input size, model complexity, hardware utilization\n",
    "- Measurement: High-precision timing of forward pass\n",
    "\n",
    "**Accuracy Benchmarking**:\n",
    "- Focus: Quality of predictions\n",
    "- Key factors: Dataset representativeness, evaluation protocol\n",
    "- Measurement: Correct predictions / total predictions\n",
    "\n",
    "**Memory Benchmarking**:\n",
    "- Focus: Peak and average memory usage\n",
    "- Key factors: Model size, batch size, intermediate activations\n",
    "- Measurement: Process memory monitoring during inference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9daff37",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "benchmark-class",
     "solution": true
    }
   },
   "outputs": [],
   "source": [
    "#| export\n",
    "class Benchmark:\n",
    "    \"\"\"\n",
    "    Professional benchmarking system for ML models and operations.\n",
    "\n",
    "    TODO: Implement a comprehensive benchmark runner with statistical rigor\n",
    "\n",
    "    APPROACH:\n",
    "    1. Support multiple models, datasets, and metrics\n",
    "    2. Run repeated measurements with proper warmup\n",
    "    3. Control for system variance and compute confidence intervals\n",
    "    4. Generate structured results for analysis\n",
    "\n",
    "    EXAMPLE:\n",
    "    >>> benchmark = Benchmark(models=[model1, model2], datasets=[test_data])\n",
    "    >>> results = benchmark.run_accuracy_benchmark()\n",
    "    >>> benchmark.plot_results(results)\n",
    "\n",
    "    HINTS:\n",
    "    - Use warmup runs to stabilize performance\n",
    "    - Collect multiple samples for statistical significance\n",
    "    - Store metadata about system conditions\n",
    "    - Provide different benchmark types (accuracy, latency, memory)\n",
    "    \"\"\"\n",
    "    ### BEGIN SOLUTION\n",
    "    def __init__(self, models: List[Any], datasets: List[Any],\n",
    "                 warmup_runs: int = 5, measurement_runs: int = 10):\n",
    "        \"\"\"Initialize benchmark with models and datasets.\"\"\"\n",
    "        self.models = models\n",
    "        self.datasets = datasets\n",
    "        self.warmup_runs = warmup_runs\n",
    "        self.measurement_runs = measurement_runs\n",
    "        self.results = {}\n",
    "        \n",
    "        # Use Profiler from Module 15 for measurements\n",
    "        self.profiler = Profiler()\n",
    "\n",
    "        # System information for metadata\n",
    "        self.system_info = {\n",
    "            'platform': platform.platform(),\n",
    "            'processor': platform.processor(),\n",
    "            'python_version': platform.python_version(),\n",
    "            'memory_gb': psutil.virtual_memory().total / (1024**3),\n",
    "            'cpu_count': psutil.cpu_count()\n",
    "        }\n",
    "\n",
    "    def run_latency_benchmark(self, input_shape: Tuple[int, ...] = (1, 28, 28)) -> Dict[str, BenchmarkResult]:\n",
    "        \"\"\"Benchmark model inference latency using Profiler.\"\"\"\n",
    "        results = {}\n",
    "\n",
    "        for i, model in enumerate(self.models):\n",
    "            model_name = getattr(model, 'name', f'model_{i}')\n",
    "            \n",
    "            # Create input tensor for profiling\n",
    "            try:\n",
    "                from tinytorch.core.tensor import Tensor\n",
    "                input_tensor = Tensor(np.random.randn(*input_shape).astype(np.float32))\n",
    "            except:\n",
    "                # Fallback for simple models\n",
    "                input_tensor = np.random.randn(*input_shape).astype(np.float32)\n",
    "\n",
    "            # Use Profiler to measure latency with proper warmup and iterations\n",
    "            try:\n",
    "                latency_ms = self.profiler.measure_latency(\n",
    "                    model, \n",
    "                    input_tensor,\n",
    "                    warmup=self.warmup_runs,\n",
    "                    iterations=self.measurement_runs\n",
    "                )\n",
    "                \n",
    "                # Profiler returns single median value\n",
    "                # For BenchmarkResult, we need multiple measurements\n",
    "                # Run additional measurements for statistical analysis\n",
    "                latencies = []\n",
    "                for _ in range(self.measurement_runs):\n",
    "                    single_latency = self.profiler.measure_latency(\n",
    "                        model, input_tensor, warmup=0, iterations=1\n",
    "                    )\n",
    "                    latencies.append(single_latency)\n",
    "                \n",
    "            except:\n",
    "                # Fallback: use precise_timer for models that don't support profiler\n",
    "                latencies = []\n",
    "                for _ in range(self.measurement_runs):\n",
    "                    with precise_timer() as timer:\n",
    "                        try:\n",
    "                            if hasattr(model, 'forward'):\n",
    "                                model.forward(input_tensor)\n",
    "                            elif hasattr(model, 'predict'):\n",
    "                                model.predict(input_tensor)\n",
    "                            elif callable(model):\n",
    "                                model(input_tensor)\n",
    "                            else:\n",
    "                                time.sleep(0.001)\n",
    "                        except:\n",
    "                            time.sleep(0.001 + np.random.normal(0, 0.0001))\n",
    "                    latencies.append(timer.elapsed * 1000)\n",
    "\n",
    "            results[model_name] = BenchmarkResult(\n",
    "                f\"{model_name}_latency_ms\",\n",
    "                latencies,\n",
    "                metadata={'input_shape': input_shape, **self.system_info}\n",
    "            )\n",
    "\n",
    "        return results\n",
    "\n",
    "    def run_accuracy_benchmark(self) -> Dict[str, BenchmarkResult]:\n",
    "        \"\"\"Benchmark model accuracy across datasets.\"\"\"\n",
    "        results = {}\n",
    "\n",
    "        for i, model in enumerate(self.models):\n",
    "            model_name = getattr(model, 'name', f'model_{i}')\n",
    "            accuracies = []\n",
    "\n",
    "            for dataset in self.datasets:\n",
    "                # Simulate accuracy measurement\n",
    "                # In practice, this would evaluate the model on the dataset\n",
    "                try:\n",
    "                    if hasattr(model, 'evaluate'):\n",
    "                        accuracy = model.evaluate(dataset)\n",
    "                    else:\n",
    "                        # Simulate accuracy for demonstration\n",
    "                        base_accuracy = 0.85 + i * 0.05  # Different models have different base accuracies\n",
    "                        accuracy = base_accuracy + np.random.normal(0, 0.02)  # Add noise\n",
    "                        accuracy = max(0.0, min(1.0, accuracy))  # Clamp to [0, 1]\n",
    "                except:\n",
    "                    # Fallback simulation\n",
    "                    accuracy = 0.80 + np.random.normal(0, 0.05)\n",
    "                    accuracy = max(0.0, min(1.0, accuracy))\n",
    "\n",
    "                accuracies.append(accuracy)\n",
    "\n",
    "            results[model_name] = BenchmarkResult(\n",
    "                f\"{model_name}_accuracy\",\n",
    "                accuracies,\n",
    "                metadata={'num_datasets': len(self.datasets), **self.system_info}\n",
    "            )\n",
    "\n",
    "        return results\n",
    "\n",
    "    def run_memory_benchmark(self, input_shape: Tuple[int, ...] = (1, 28, 28)) -> Dict[str, BenchmarkResult]:\n",
    "        \"\"\"Benchmark model memory usage using Profiler.\"\"\"\n",
    "        results = {}\n",
    "\n",
    "        for i, model in enumerate(self.models):\n",
    "            model_name = getattr(model, 'name', f'model_{i}')\n",
    "            memory_usages = []\n",
    "\n",
    "            for run in range(self.measurement_runs):\n",
    "                try:\n",
    "                    # Use Profiler to measure memory\n",
    "                    memory_stats = self.profiler.measure_memory(model, input_shape)\n",
    "                    # Use peak_memory_mb as the primary metric\n",
    "                    memory_used = memory_stats['peak_memory_mb']\n",
    "                except:\n",
    "                    # Fallback: measure with psutil\n",
    "                    process = psutil.Process()\n",
    "                    memory_before = process.memory_info().rss / (1024**2)  # MB\n",
    "\n",
    "                    try:\n",
    "                        dummy_input = np.random.randn(*input_shape).astype(np.float32)\n",
    "                        if hasattr(model, 'forward'):\n",
    "                            model.forward(dummy_input)\n",
    "                        elif hasattr(model, 'predict'):\n",
    "                            model.predict(dummy_input)\n",
    "                        elif callable(model):\n",
    "                            model(dummy_input)\n",
    "                    except:\n",
    "                        pass\n",
    "\n",
    "                    memory_after = process.memory_info().rss / (1024**2)  # MB\n",
    "                    memory_used = max(0, memory_after - memory_before)\n",
    "\n",
    "                    # If no significant memory change detected, estimate from parameters\n",
    "                    if memory_used < 1.0:\n",
    "                        try:\n",
    "                            param_count = self.profiler.count_parameters(model)\n",
    "                            memory_used = param_count * 4 / (1024**2)  # 4 bytes per float32\n",
    "                        except:\n",
    "                            memory_used = 8 + np.random.normal(0, 1)  # Default estimate\n",
    "\n",
    "                memory_usages.append(max(0, memory_used))\n",
    "\n",
    "            results[model_name] = BenchmarkResult(\n",
    "                f\"{model_name}_memory_mb\",\n",
    "                memory_usages,\n",
    "                metadata={'input_shape': input_shape, **self.system_info}\n",
    "            )\n",
    "\n",
    "        return results\n",
    "\n",
    "    def compare_models(self, metric: str = \"latency\") -> pd.DataFrame:\n",
    "        \"\"\"Compare models across a specific metric.\"\"\"\n",
    "        if metric == \"latency\":\n",
    "            results = self.run_latency_benchmark()\n",
    "        elif metric == \"accuracy\":\n",
    "            results = self.run_accuracy_benchmark()\n",
    "        elif metric == \"memory\":\n",
    "            results = self.run_memory_benchmark()\n",
    "        else:\n",
    "            raise ValueError(f\"Unknown metric: {metric}\")\n",
    "\n",
    "        # Convert to DataFrame for easy comparison\n",
    "        comparison_data = []\n",
    "        for model_name, result in results.items():\n",
    "            comparison_data.append({\n",
    "                'model': model_name.replace(f'_{metric}', '').replace('_ms', '').replace('_mb', ''),\n",
    "                'metric': metric,\n",
    "                'mean': result.mean,\n",
    "                'std': result.std,\n",
    "                'ci_lower': result.ci_lower,\n",
    "                'ci_upper': result.ci_upper,\n",
    "                'count': result.count\n",
    "            })\n",
    "\n",
    "        return pd.DataFrame(comparison_data)\n",
    "    ### END SOLUTION\n",
    "\n",
    "def test_unit_benchmark():\n",
    "    \"\"\"🔬 Test Benchmark class functionality.\"\"\"\n",
    "    print(\"🔬 Unit Test: Benchmark...\")\n",
    "\n",
    "    # Create mock models for testing\n",
    "    class MockModel:\n",
    "        def __init__(self, name):\n",
    "            self.name = name\n",
    "\n",
    "        def forward(self, x):\n",
    "            time.sleep(0.001)  # Simulate computation\n",
    "            return x\n",
    "\n",
    "    models = [MockModel(\"fast_model\"), MockModel(\"slow_model\")]\n",
    "    datasets = [{\"data\": \"test1\"}, {\"data\": \"test2\"}]\n",
    "\n",
    "    benchmark = Benchmark(models, datasets, warmup_runs=2, measurement_runs=3)\n",
    "\n",
    "    # Test latency benchmark\n",
    "    latency_results = benchmark.run_latency_benchmark()\n",
    "    assert len(latency_results) == 2\n",
    "    assert \"fast_model\" in latency_results\n",
    "    assert all(isinstance(result, BenchmarkResult) for result in latency_results.values())\n",
    "\n",
    "    # Test accuracy benchmark\n",
    "    accuracy_results = benchmark.run_accuracy_benchmark()\n",
    "    assert len(accuracy_results) == 2\n",
    "    assert all(0 <= result.mean <= 1 for result in accuracy_results.values())\n",
    "\n",
    "    # Test memory benchmark\n",
    "    memory_results = benchmark.run_memory_benchmark()\n",
    "    assert len(memory_results) == 2\n",
    "    assert all(result.mean >= 0 for result in memory_results.values())\n",
    "\n",
    "    # Test comparison\n",
    "    comparison_df = benchmark.compare_models(\"latency\")\n",
    "    assert len(comparison_df) == 2\n",
    "    assert \"model\" in comparison_df.columns\n",
    "    assert \"mean\" in comparison_df.columns\n",
    "\n",
    "    print(\"✅ Benchmark works correctly!\")\n",
    "\n",
    "test_unit_benchmark()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1530cb11",
   "metadata": {
    "cell_marker": "\"\"\"",
    "lines_to_next_cell": 1
   },
   "source": [
    "## BenchmarkSuite - Comprehensive Multi-Metric Evaluation\n",
    "\n",
    "The BenchmarkSuite orchestrates multiple benchmark types and generates comprehensive reports. This is where individual measurements become actionable engineering insights.\n",
    "\n",
    "### Why Multi-Metric Analysis Matters\n",
    "\n",
    "Single metrics mislead. Consider these three models:\n",
    "- **Model A**: 95% accuracy, 100ms latency, 50MB memory\n",
    "- **Model B**: 90% accuracy, 20ms latency, 10MB memory\n",
    "- **Model C**: 85% accuracy, 10ms latency, 5MB memory\n",
    "\n",
    "Which is \"best\"? It depends on your constraints:\n",
    "- **Server deployment**: Model A (accuracy matters most)\n",
    "- **Mobile app**: Model C (memory/latency critical)\n",
    "- **Edge device**: Model B (balanced trade-off)\n",
    "\n",
    "### Multi-Dimensional Comparison Workflow\n",
    "\n",
    "```\n",
    "BenchmarkSuite Execution Pipeline:\n",
    "┌──────────────┐\n",
    "│   Models     │ ← Input: List of models to compare\n",
    "│ [M1,M2,M3]   │\n",
    "└──────┬───────┘\n",
    "       ↓\n",
    "┌──────────────┐\n",
    "│ Metric Types │ ← Run each benchmark type\n",
    "│ • Latency    │\n",
    "│ • Accuracy   │\n",
    "│ • Memory     │\n",
    "│ • Energy     │\n",
    "└──────┬───────┘\n",
    "       ↓\n",
    "┌──────────────┐\n",
    "│ Result       │ ← Aggregate into unified view\n",
    "│ Aggregation  │\n",
    "└──────┬───────┘\n",
    "       ↓\n",
    "┌──────────────┐\n",
    "│ Analysis &   │ ← Generate insights\n",
    "│ Reporting    │   • Best performer per metric\n",
    "│              │   • Trade-off analysis\n",
    "│              │   • Use case recommendations\n",
    "└──────────────┘\n",
    "```\n",
    "\n",
    "### Pareto Frontier Analysis\n",
    "\n",
    "The suite automatically identifies Pareto-optimal solutions - models that aren't strictly dominated by others across all metrics. This reveals the true trade-off space for optimization decisions.\n",
    "\n",
    "### Energy Efficiency Modeling\n",
    "\n",
    "Since direct energy measurement requires specialized hardware, we estimate energy based on computational complexity and memory usage. This provides actionable insights for battery-powered deployments."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "49bc9ee6",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "benchmark-suite",
     "solution": true
    }
   },
   "outputs": [],
   "source": [
    "#| export\n",
    "class BenchmarkSuite:\n",
    "    \"\"\"\n",
    "    Comprehensive benchmark suite for ML systems evaluation.\n",
    "\n",
    "    TODO: Implement a full benchmark suite that runs multiple test categories\n",
    "\n",
    "    APPROACH:\n",
    "    1. Combine multiple benchmark types (latency, accuracy, memory, energy)\n",
    "    2. Generate comprehensive reports with visualizations\n",
    "    3. Support different model categories and hardware configurations\n",
    "    4. Provide recommendations based on results\n",
    "\n",
    "    EXAMPLE:\n",
    "    >>> suite = BenchmarkSuite(models, datasets)\n",
    "    >>> report = suite.run_full_benchmark()\n",
    "    >>> suite.generate_report(report)\n",
    "\n",
    "    HINTS:\n",
    "    - Organize results by benchmark type and model\n",
    "    - Create Pareto frontier analysis for trade-offs\n",
    "    - Include system information and test conditions\n",
    "    - Generate actionable insights and recommendations\n",
    "    \"\"\"\n",
    "    ### BEGIN SOLUTION\n",
    "    def __init__(self, models: List[Any], datasets: List[Any],\n",
    "                 output_dir: str = \"benchmark_results\"):\n",
    "        \"\"\"Initialize comprehensive benchmark suite.\"\"\"\n",
    "        self.models = models\n",
    "        self.datasets = datasets\n",
    "        self.output_dir = Path(output_dir)\n",
    "        self.output_dir.mkdir(exist_ok=True)\n",
    "\n",
    "        self.benchmark = Benchmark(models, datasets)\n",
    "        self.results = {}\n",
    "\n",
    "    def run_full_benchmark(self) -> Dict[str, Dict[str, BenchmarkResult]]:\n",
    "        \"\"\"Run all benchmark categories.\"\"\"\n",
    "        print(\"🔬 Running comprehensive benchmark suite...\")\n",
    "\n",
    "        # Run all benchmark types\n",
    "        print(\"  📊 Measuring latency...\")\n",
    "        self.results['latency'] = self.benchmark.run_latency_benchmark()\n",
    "\n",
    "        print(\"  🎯 Measuring accuracy...\")\n",
    "        self.results['accuracy'] = self.benchmark.run_accuracy_benchmark()\n",
    "\n",
    "        print(\"  💾 Measuring memory usage...\")\n",
    "        self.results['memory'] = self.benchmark.run_memory_benchmark()\n",
    "\n",
    "        # Simulate energy benchmark (would require specialized hardware)\n",
    "        print(\"  ⚡ Estimating energy efficiency...\")\n",
    "        self.results['energy'] = self._estimate_energy_efficiency()\n",
    "\n",
    "        return self.results\n",
    "\n",
    "    def _estimate_energy_efficiency(self) -> Dict[str, BenchmarkResult]:\n",
    "        \"\"\"Estimate energy efficiency (simplified simulation).\"\"\"\n",
    "        energy_results = {}\n",
    "\n",
    "        for i, model in enumerate(self.models):\n",
    "            model_name = getattr(model, 'name', f'model_{i}')\n",
    "\n",
    "            # Energy roughly correlates with latency * memory usage\n",
    "            if 'latency' in self.results and 'memory' in self.results:\n",
    "                latency_result = self.results['latency'].get(model_name)\n",
    "                memory_result = self.results['memory'].get(model_name)\n",
    "\n",
    "                if latency_result and memory_result:\n",
    "                    # Energy ∝ power × time, power ∝ memory usage\n",
    "                    energy_values = []\n",
    "                    for lat, mem in zip(latency_result.values, memory_result.values):\n",
    "                        # Simplified energy model: energy = base + latency_factor * time + memory_factor * memory\n",
    "                        energy = 0.1 + (lat / 1000) * 2.0 + mem * 0.01  # Joules\n",
    "                        energy_values.append(energy)\n",
    "\n",
    "                    energy_results[model_name] = BenchmarkResult(\n",
    "                        f\"{model_name}_energy_joules\",\n",
    "                        energy_values,\n",
    "                        metadata={'estimated': True, **self.benchmark.system_info}\n",
    "                    )\n",
    "\n",
    "        # Fallback if no latency/memory results\n",
    "        if not energy_results:\n",
    "            for i, model in enumerate(self.models):\n",
    "                model_name = getattr(model, 'name', f'model_{i}')\n",
    "                # Simulate energy measurements\n",
    "                energy_values = [0.5 + np.random.normal(0, 0.1) for _ in range(5)]\n",
    "                energy_results[model_name] = BenchmarkResult(\n",
    "                    f\"{model_name}_energy_joules\",\n",
    "                    energy_values,\n",
    "                    metadata={'estimated': True, **self.benchmark.system_info}\n",
    "                )\n",
    "\n",
    "        return energy_results\n",
    "\n",
    "    def plot_results(self, save_plots: bool = True):\n",
    "        \"\"\"Generate visualization plots for benchmark results.\"\"\"\n",
    "        if not self.results:\n",
    "            print(\"No results to plot. Run benchmark first.\")\n",
    "            return\n",
    "\n",
    "        fig, axes = plt.subplots(2, 2, figsize=(15, 12))\n",
    "        fig.suptitle('ML Model Benchmark Results', fontsize=16, fontweight='bold')\n",
    "\n",
    "        # Plot each metric type\n",
    "        metrics = ['latency', 'accuracy', 'memory', 'energy']\n",
    "        units = ['ms', 'accuracy', 'MB', 'J']\n",
    "\n",
    "        for idx, (metric, unit) in enumerate(zip(metrics, units)):\n",
    "            ax = axes[idx // 2, idx % 2]\n",
    "\n",
    "            if metric in self.results:\n",
    "                model_names = []\n",
    "                means = []\n",
    "                stds = []\n",
    "\n",
    "                for model_name, result in self.results[metric].items():\n",
    "                    clean_name = model_name.replace(f'_{metric}', '').replace('_ms', '').replace('_mb', '').replace('_joules', '')\n",
    "                    model_names.append(clean_name)\n",
    "                    means.append(result.mean)\n",
    "                    stds.append(result.std)\n",
    "\n",
    "                bars = ax.bar(model_names, means, yerr=stds, capsize=5, alpha=0.7)\n",
    "                ax.set_title(f'{metric.capitalize()} Comparison')\n",
    "                ax.set_ylabel(f'{metric.capitalize()} ({unit})')\n",
    "                ax.tick_params(axis='x', rotation=45)\n",
    "\n",
    "                # Color bars by performance (green = better)\n",
    "                if metric in ['latency', 'memory', 'energy']:  # Lower is better\n",
    "                    best_idx = means.index(min(means))\n",
    "                else:  # Higher is better (accuracy)\n",
    "                    best_idx = means.index(max(means))\n",
    "\n",
    "                for i, bar in enumerate(bars):\n",
    "                    if i == best_idx:\n",
    "                        bar.set_color('green')\n",
    "                        bar.set_alpha(0.8)\n",
    "            else:\n",
    "                ax.text(0.5, 0.5, f'No {metric} data', ha='center', va='center', transform=ax.transAxes)\n",
    "                ax.set_title(f'{metric.capitalize()} Comparison')\n",
    "\n",
    "        plt.tight_layout()\n",
    "\n",
    "        if save_plots:\n",
    "            plot_path = self.output_dir / 'benchmark_comparison.png'\n",
    "            plt.savefig(plot_path, dpi=300, bbox_inches='tight')\n",
    "            print(f\"📊 Plots saved to {plot_path}\")\n",
    "\n",
    "        plt.show()\n",
    "\n",
    "    def plot_pareto_frontier(self, x_metric: str = 'latency', y_metric: str = 'accuracy'):\n",
    "        \"\"\"Plot Pareto frontier for two competing objectives.\"\"\"\n",
    "        if x_metric not in self.results or y_metric not in self.results:\n",
    "            print(f\"Missing data for {x_metric} or {y_metric}\")\n",
    "            return\n",
    "\n",
    "        plt.figure(figsize=(10, 8))\n",
    "\n",
    "        x_values = []\n",
    "        y_values = []\n",
    "        model_names = []\n",
    "\n",
    "        for model_name in self.results[x_metric].keys():\n",
    "            clean_name = model_name.replace(f'_{x_metric}', '').replace('_ms', '').replace('_mb', '').replace('_joules', '')\n",
    "            if clean_name in [mn.replace(f'_{y_metric}', '') for mn in self.results[y_metric].keys()]:\n",
    "                x_val = self.results[x_metric][model_name].mean\n",
    "\n",
    "                # Find corresponding y value\n",
    "                y_key = None\n",
    "                for key in self.results[y_metric].keys():\n",
    "                    if clean_name in key:\n",
    "                        y_key = key\n",
    "                        break\n",
    "\n",
    "                if y_key:\n",
    "                    y_val = self.results[y_metric][y_key].mean\n",
    "                    x_values.append(x_val)\n",
    "                    y_values.append(y_val)\n",
    "                    model_names.append(clean_name)\n",
    "\n",
    "        # Plot points\n",
    "        plt.scatter(x_values, y_values, s=100, alpha=0.7)\n",
    "\n",
    "        # Label points\n",
    "        for i, name in enumerate(model_names):\n",
    "            plt.annotate(name, (x_values[i], y_values[i]),\n",
    "                        xytext=(5, 5), textcoords='offset points')\n",
    "\n",
    "        # Determine if lower or higher is better for each metric\n",
    "        x_lower_better = x_metric in ['latency', 'memory', 'energy']\n",
    "        y_lower_better = y_metric in ['latency', 'memory', 'energy']\n",
    "\n",
    "        plt.xlabel(f'{x_metric.capitalize()} ({\"lower\" if x_lower_better else \"higher\"} is better)')\n",
    "        plt.ylabel(f'{y_metric.capitalize()} ({\"lower\" if y_lower_better else \"higher\"} is better)')\n",
    "        plt.title(f'Pareto Frontier: {x_metric.capitalize()} vs {y_metric.capitalize()}')\n",
    "        plt.grid(True, alpha=0.3)\n",
    "\n",
    "        # Save plot\n",
    "        plot_path = self.output_dir / f'pareto_{x_metric}_vs_{y_metric}.png'\n",
    "        plt.savefig(plot_path, dpi=300, bbox_inches='tight')\n",
    "        print(f\"📊 Pareto plot saved to {plot_path}\")\n",
    "        plt.show()\n",
    "\n",
    "    def generate_report(self) -> str:\n",
    "        \"\"\"Generate comprehensive benchmark report.\"\"\"\n",
    "        if not self.results:\n",
    "            return \"No benchmark results available. Run benchmark first.\"\n",
    "\n",
    "        report_lines = []\n",
    "        report_lines.append(\"# ML Model Benchmark Report\")\n",
    "        report_lines.append(\"=\" * 50)\n",
    "        report_lines.append(\"\")\n",
    "\n",
    "        # System information\n",
    "        report_lines.append(\"## System Information\")\n",
    "        system_info = self.benchmark.system_info\n",
    "        for key, value in system_info.items():\n",
    "            report_lines.append(f\"- {key}: {value}\")\n",
    "        report_lines.append(\"\")\n",
    "\n",
    "        # Results summary\n",
    "        report_lines.append(\"## Benchmark Results Summary\")\n",
    "        report_lines.append(\"\")\n",
    "\n",
    "        for metric_type, results in self.results.items():\n",
    "            report_lines.append(f\"### {metric_type.capitalize()} Results\")\n",
    "            report_lines.append(\"\")\n",
    "\n",
    "            # Find best performer\n",
    "            if metric_type in ['latency', 'memory', 'energy']:\n",
    "                # Lower is better\n",
    "                best_model = min(results.items(), key=lambda x: x[1].mean)\n",
    "                comparison_text = \"fastest\" if metric_type == 'latency' else \"most efficient\"\n",
    "            else:\n",
    "                # Higher is better\n",
    "                best_model = max(results.items(), key=lambda x: x[1].mean)\n",
    "                comparison_text = \"most accurate\"\n",
    "\n",
    "            report_lines.append(f\"**Best performer**: {best_model[0]} ({comparison_text})\")\n",
    "            report_lines.append(\"\")\n",
    "\n",
    "            # Detailed results\n",
    "            for model_name, result in results.items():\n",
    "                clean_name = model_name.replace(f'_{metric_type}', '').replace('_ms', '').replace('_mb', '').replace('_joules', '')\n",
    "                report_lines.append(f\"- **{clean_name}**: {result.mean:.4f} ± {result.std:.4f}\")\n",
    "            report_lines.append(\"\")\n",
    "\n",
    "        # Recommendations\n",
    "        report_lines.append(\"## Recommendations\")\n",
    "        report_lines.append(\"\")\n",
    "\n",
    "        if len(self.results) >= 2:\n",
    "            # Find overall best trade-off model\n",
    "            if 'latency' in self.results and 'accuracy' in self.results:\n",
    "                report_lines.append(\"### Accuracy vs Speed Trade-off\")\n",
    "\n",
    "                # Simple scoring: normalize metrics and combine\n",
    "                latency_results = self.results['latency']\n",
    "                accuracy_results = self.results['accuracy']\n",
    "\n",
    "                scores = {}\n",
    "                for model_name in latency_results.keys():\n",
    "                    clean_name = model_name.replace('_latency', '').replace('_ms', '')\n",
    "\n",
    "                    # Find corresponding accuracy\n",
    "                    acc_key = None\n",
    "                    for key in accuracy_results.keys():\n",
    "                        if clean_name in key:\n",
    "                            acc_key = key\n",
    "                            break\n",
    "\n",
    "                    if acc_key:\n",
    "                        # Normalize: latency (lower better), accuracy (higher better)\n",
    "                        lat_vals = [r.mean for r in latency_results.values()]\n",
    "                        acc_vals = [r.mean for r in accuracy_results.values()]\n",
    "\n",
    "                        norm_latency = 1 - (latency_results[model_name].mean - min(lat_vals)) / (max(lat_vals) - min(lat_vals) + 1e-8)\n",
    "                        norm_accuracy = (accuracy_results[acc_key].mean - min(acc_vals)) / (max(acc_vals) - min(acc_vals) + 1e-8)\n",
    "\n",
    "                        # Combined score (equal weight)\n",
    "                        scores[clean_name] = (norm_latency + norm_accuracy) / 2\n",
    "\n",
    "                if scores:\n",
    "                    best_overall = max(scores.items(), key=lambda x: x[1])\n",
    "                    report_lines.append(f\"- **Best overall trade-off**: {best_overall[0]} (score: {best_overall[1]:.3f})\")\n",
    "                    report_lines.append(\"\")\n",
    "\n",
    "        report_lines.append(\"### Usage Recommendations\")\n",
    "        if 'accuracy' in self.results and 'latency' in self.results:\n",
    "            acc_results = self.results['accuracy']\n",
    "            lat_results = self.results['latency']\n",
    "\n",
    "            # Find highest accuracy model\n",
    "            best_acc_model = max(acc_results.items(), key=lambda x: x[1].mean)\n",
    "            best_lat_model = min(lat_results.items(), key=lambda x: x[1].mean)\n",
    "\n",
    "            report_lines.append(f\"- **For maximum accuracy**: Use {best_acc_model[0].replace('_accuracy', '')}\")\n",
    "            report_lines.append(f\"- **For minimum latency**: Use {best_lat_model[0].replace('_latency_ms', '')}\")\n",
    "            report_lines.append(\"- **For production deployment**: Consider the best overall trade-off model above\")\n",
    "\n",
    "        report_lines.append(\"\")\n",
    "        report_lines.append(\"---\")\n",
    "        report_lines.append(\"Report generated by TinyTorch Benchmarking Suite\")\n",
    "\n",
    "        # Save report\n",
    "        report_text = \"\\n\".join(report_lines)\n",
    "        report_path = self.output_dir / 'benchmark_report.md'\n",
    "        with open(report_path, 'w') as f:\n",
    "            f.write(report_text)\n",
    "\n",
    "        print(f\"📄 Report saved to {report_path}\")\n",
    "        return report_text\n",
    "    ### END SOLUTION\n",
    "\n",
    "def test_unit_benchmark_suite():\n",
    "    \"\"\"🔬 Test BenchmarkSuite comprehensive functionality.\"\"\"\n",
    "    print(\"🔬 Unit Test: BenchmarkSuite...\")\n",
    "\n",
    "    # Create mock models\n",
    "    class MockModel:\n",
    "        def __init__(self, name):\n",
    "            self.name = name\n",
    "\n",
    "        def forward(self, x):\n",
    "            time.sleep(0.001)\n",
    "            return x\n",
    "\n",
    "    models = [MockModel(\"efficient_model\"), MockModel(\"accurate_model\")]\n",
    "    datasets = [{\"test\": \"data\"}]\n",
    "\n",
    "    # Create temporary directory for test output\n",
    "    import tempfile\n",
    "    with tempfile.TemporaryDirectory() as tmp_dir:\n",
    "        suite = BenchmarkSuite(models, datasets, output_dir=tmp_dir)\n",
    "\n",
    "        # Run full benchmark\n",
    "        results = suite.run_full_benchmark()\n",
    "\n",
    "        # Verify all benchmark types completed\n",
    "        assert 'latency' in results\n",
    "        assert 'accuracy' in results\n",
    "        assert 'memory' in results\n",
    "        assert 'energy' in results\n",
    "\n",
    "        # Verify results structure\n",
    "        for metric_results in results.values():\n",
    "            assert len(metric_results) == 2  # Two models\n",
    "            assert all(isinstance(result, BenchmarkResult) for result in metric_results.values())\n",
    "\n",
    "        # Test report generation\n",
    "        report = suite.generate_report()\n",
    "        assert \"Benchmark Report\" in report\n",
    "        assert \"System Information\" in report\n",
    "        assert \"Recommendations\" in report\n",
    "\n",
    "        # Verify files are created\n",
    "        output_path = Path(tmp_dir)\n",
    "        assert (output_path / 'benchmark_report.md').exists()\n",
    "\n",
    "    print(\"✅ BenchmarkSuite works correctly!\")\n",
    "\n",
    "test_unit_benchmark_suite()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f1ca772",
   "metadata": {
    "cell_marker": "\"\"\"",
    "lines_to_next_cell": 1
   },
   "source": [
    "## TinyMLPerf - Standardized Industry Benchmarking\n",
    "\n",
    "TinyMLPerf provides standardized benchmarks that enable fair comparison across different systems, similar to how MLPerf works for larger models. This is crucial for reproducible research and industry adoption.\n",
    "\n",
    "### Why Standardization Matters\n",
    "\n",
    "Without standards, every team benchmarks differently:\n",
    "- Different datasets, input sizes, measurement protocols\n",
    "- Different accuracy metrics, latency definitions\n",
    "- Different hardware configurations, software stacks\n",
    "\n",
    "This makes it impossible to compare results across papers, products, or research groups.\n",
    "\n",
    "### TinyMLPerf Benchmark Architecture\n",
    "\n",
    "```\n",
    "TinyMLPerf Benchmark Structure:\n",
    "┌─────────────────────────────────────────────────────────┐\n",
    "│                  Benchmark Definition                   │\n",
    "│ • Standard datasets (CIFAR-10, Speech Commands, etc.)  │\n",
    "│ • Fixed input shapes and data types                     │\n",
    "│ • Target accuracy and latency thresholds               │\n",
    "│ • Measurement protocol (warmup, runs, etc.)            │\n",
    "└─────────────────────────────────────────────────────────┘\n",
    "                           ↓\n",
    "┌─────────────────────────────────────────────────────────┐\n",
    "│                 Execution Protocol                      │\n",
    "│ 1. Model registration and validation                   │\n",
    "│ 2. Warmup phase (deterministic random inputs)          │\n",
    "│ 3. Measurement phase (statistical sampling)            │\n",
    "│ 4. Accuracy evaluation (ground truth comparison)       │\n",
    "│ 5. Compliance checking (thresholds, statistical tests) │\n",
    "└─────────────────────────────────────────────────────────┘\n",
    "                           ↓\n",
    "┌─────────────────────────────────────────────────────────┐\n",
    "│              Compliance Determination                   │\n",
    "│ PASS: accuracy ≥ target AND latency ≤ target           │\n",
    "│ FAIL: Either constraint violated                        │\n",
    "│ Report: Detailed metrics + system information          │\n",
    "└─────────────────────────────────────────────────────────┘\n",
    "```\n",
    "\n",
    "### Standard Benchmark Tasks\n",
    "\n",
    "**Keyword Spotting**: Wake word detection from audio\n",
    "- Input: 1-second 16kHz audio samples\n",
    "- Task: Binary classification (keyword present/absent)\n",
    "- Target: 90% accuracy, <100ms latency\n",
    "\n",
    "**Visual Wake Words**: Person detection in images\n",
    "- Input: 96×96 RGB images\n",
    "- Task: Binary classification (person present/absent)\n",
    "- Target: 80% accuracy, <200ms latency\n",
    "\n",
    "**Anomaly Detection**: Industrial sensor monitoring\n",
    "- Input: 640-element sensor feature vectors\n",
    "- Task: Binary classification (anomaly/normal)\n",
    "- Target: 85% accuracy, <50ms latency\n",
    "\n",
    "### Reproducibility Requirements\n",
    "\n",
    "All TinyMLPerf benchmarks use:\n",
    "- **Fixed random seeds**: Deterministic input generation\n",
    "- **Standardized hardware**: Reference implementations for comparison\n",
    "- **Statistical validation**: Multiple runs with confidence intervals\n",
    "- **Compliance reporting**: Machine-readable results format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c48dd641",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "tinymlperf",
     "solution": true
    }
   },
   "outputs": [],
   "source": [
    "#| export\n",
    "class TinyMLPerf:\n",
    "    \"\"\"\n",
    "    TinyMLPerf-style standardized benchmarking for edge ML systems.\n",
    "\n",
    "    TODO: Implement standardized benchmarks following TinyMLPerf methodology\n",
    "\n",
    "    APPROACH:\n",
    "    1. Define standard benchmark tasks and datasets\n",
    "    2. Implement standardized measurement protocols\n",
    "    3. Ensure reproducible results across different systems\n",
    "    4. Generate compliance reports for fair comparison\n",
    "\n",
    "    EXAMPLE:\n",
    "    >>> perf = TinyMLPerf()\n",
    "    >>> results = perf.run_keyword_spotting_benchmark(model)\n",
    "    >>> perf.generate_compliance_report(results)\n",
    "\n",
    "    HINTS:\n",
    "    - Use fixed random seeds for reproducibility\n",
    "    - Implement warm-up and measurement phases\n",
    "    - Follow TinyMLPerf power and latency measurement standards\n",
    "    - Generate standardized result formats\n",
    "    \"\"\"\n",
    "    ### BEGIN SOLUTION\n",
    "    def __init__(self, random_seed: int = 42):\n",
    "        \"\"\"Initialize TinyMLPerf benchmark suite.\"\"\"\n",
    "        self.random_seed = random_seed\n",
    "        np.random.seed(random_seed)\n",
    "\n",
    "        # Standard TinyMLPerf benchmark configurations\n",
    "        self.benchmarks = {\n",
    "            'keyword_spotting': {\n",
    "                'input_shape': (1, 16000),  # 1 second of 16kHz audio\n",
    "                'target_accuracy': 0.90,\n",
    "                'max_latency_ms': 100,\n",
    "                'description': 'Wake word detection'\n",
    "            },\n",
    "            'visual_wake_words': {\n",
    "                'input_shape': (1, 96, 96, 3),  # 96x96 RGB image\n",
    "                'target_accuracy': 0.80,\n",
    "                'max_latency_ms': 200,\n",
    "                'description': 'Person detection in images'\n",
    "            },\n",
    "            'anomaly_detection': {\n",
    "                'input_shape': (1, 640),  # Machine sensor data\n",
    "                'target_accuracy': 0.85,\n",
    "                'max_latency_ms': 50,\n",
    "                'description': 'Industrial anomaly detection'\n",
    "            },\n",
    "            'image_classification': {\n",
    "                'input_shape': (1, 32, 32, 3),  # CIFAR-10 style\n",
    "                'target_accuracy': 0.75,\n",
    "                'max_latency_ms': 150,\n",
    "                'description': 'Tiny image classification'\n",
    "            }\n",
    "        }\n",
    "\n",
    "    def run_standard_benchmark(self, model: Any, benchmark_name: str,\n",
    "                             num_runs: int = 100) -> Dict[str, Any]:\n",
    "        \"\"\"Run a standardized TinyMLPerf benchmark.\"\"\"\n",
    "        if benchmark_name not in self.benchmarks:\n",
    "            raise ValueError(f\"Unknown benchmark: {benchmark_name}. \"\n",
    "                           f\"Available: {list(self.benchmarks.keys())}\")\n",
    "\n",
    "        config = self.benchmarks[benchmark_name]\n",
    "        print(f\"🔬 Running TinyMLPerf {benchmark_name} benchmark...\")\n",
    "        print(f\"   Target: {config['target_accuracy']:.1%} accuracy, \"\n",
    "              f\"<{config['max_latency_ms']}ms latency\")\n",
    "\n",
    "        # Generate standardized test inputs\n",
    "        input_shape = config['input_shape']\n",
    "        test_inputs = []\n",
    "        for i in range(num_runs):\n",
    "            # Use deterministic random generation for reproducibility\n",
    "            np.random.seed(self.random_seed + i)\n",
    "            if len(input_shape) == 2:  # Audio/sequence data\n",
    "                test_input = np.random.randn(*input_shape).astype(np.float32)\n",
    "            else:  # Image data\n",
    "                test_input = np.random.randint(0, 256, input_shape).astype(np.float32) / 255.0\n",
    "            test_inputs.append(test_input)\n",
    "\n",
    "        # Warmup phase (10% of runs)\n",
    "        warmup_runs = max(1, num_runs // 10)\n",
    "        print(f\"   Warming up ({warmup_runs} runs)...\")\n",
    "        for i in range(warmup_runs):\n",
    "            try:\n",
    "                if hasattr(model, 'forward'):\n",
    "                    model.forward(test_inputs[i])\n",
    "                elif hasattr(model, 'predict'):\n",
    "                    model.predict(test_inputs[i])\n",
    "                elif callable(model):\n",
    "                    model(test_inputs[i])\n",
    "            except:\n",
    "                pass  # Skip if model doesn't support this input\n",
    "\n",
    "        # Measurement phase\n",
    "        print(f\"   Measuring performance ({num_runs} runs)...\")\n",
    "        latencies = []\n",
    "        predictions = []\n",
    "\n",
    "        for i, test_input in enumerate(test_inputs):\n",
    "            with precise_timer() as timer:\n",
    "                try:\n",
    "                    if hasattr(model, 'forward'):\n",
    "                        output = model.forward(test_input)\n",
    "                    elif hasattr(model, 'predict'):\n",
    "                        output = model.predict(test_input)\n",
    "                    elif callable(model):\n",
    "                        output = model(test_input)\n",
    "                    else:\n",
    "                        # Simulate prediction\n",
    "                        output = np.random.rand(2) if benchmark_name in ['keyword_spotting', 'visual_wake_words'] else np.random.rand(10)\n",
    "\n",
    "                    predictions.append(output)\n",
    "                except:\n",
    "                    # Fallback simulation\n",
    "                    predictions.append(np.random.rand(2))\n",
    "\n",
    "                latencies.append(timer.elapsed * 1000)  # Convert to ms\n",
    "\n",
    "        # Simulate accuracy calculation (would use real labels in practice)\n",
    "        # Generate synthetic ground truth labels\n",
    "        np.random.seed(self.random_seed)\n",
    "        if benchmark_name in ['keyword_spotting', 'visual_wake_words']:\n",
    "            # Binary classification\n",
    "            true_labels = np.random.randint(0, 2, num_runs)\n",
    "            predicted_labels = []\n",
    "            for pred in predictions:\n",
    "                try:\n",
    "                    if hasattr(pred, 'data'):\n",
    "                        pred_array = pred.data\n",
    "                    else:\n",
    "                        pred_array = np.array(pred)\n",
    "\n",
    "                    if len(pred_array.shape) > 1:\n",
    "                        pred_array = pred_array.flatten()\n",
    "\n",
    "                    if len(pred_array) >= 2:\n",
    "                        predicted_labels.append(1 if pred_array[1] > pred_array[0] else 0)\n",
    "                    else:\n",
    "                        predicted_labels.append(1 if pred_array[0] > 0.5 else 0)\n",
    "                except:\n",
    "                    predicted_labels.append(np.random.randint(0, 2))\n",
    "        else:\n",
    "            # Multi-class classification\n",
    "            num_classes = 10 if benchmark_name == 'image_classification' else 5\n",
    "            true_labels = np.random.randint(0, num_classes, num_runs)\n",
    "            predicted_labels = []\n",
    "            for pred in predictions:\n",
    "                try:\n",
    "                    if hasattr(pred, 'data'):\n",
    "                        pred_array = pred.data\n",
    "                    else:\n",
    "                        pred_array = np.array(pred)\n",
    "\n",
    "                    if len(pred_array.shape) > 1:\n",
    "                        pred_array = pred_array.flatten()\n",
    "\n",
    "                    predicted_labels.append(np.argmax(pred_array) % num_classes)\n",
    "                except:\n",
    "                    predicted_labels.append(np.random.randint(0, num_classes))\n",
    "\n",
    "        # Calculate accuracy\n",
    "        correct_predictions = sum(1 for true, pred in zip(true_labels, predicted_labels) if true == pred)\n",
    "        accuracy = correct_predictions / num_runs\n",
    "\n",
    "        # Add some realistic noise based on model complexity\n",
    "        model_name = getattr(model, 'name', 'unknown_model')\n",
    "        if 'efficient' in model_name.lower():\n",
    "            accuracy = min(0.95, accuracy + 0.1)  # Efficient models might be less accurate\n",
    "        elif 'accurate' in model_name.lower():\n",
    "            accuracy = min(0.98, accuracy + 0.2)  # Accurate models perform better\n",
    "\n",
    "        # Compile results\n",
    "        results = {\n",
    "            'benchmark_name': benchmark_name,\n",
    "            'model_name': getattr(model, 'name', 'unknown_model'),\n",
    "            'accuracy': accuracy,\n",
    "            'mean_latency_ms': np.mean(latencies),\n",
    "            'std_latency_ms': np.std(latencies),\n",
    "            'p50_latency_ms': np.percentile(latencies, 50),\n",
    "            'p90_latency_ms': np.percentile(latencies, 90),\n",
    "            'p99_latency_ms': np.percentile(latencies, 99),\n",
    "            'max_latency_ms': np.max(latencies),\n",
    "            'throughput_fps': 1000 / np.mean(latencies),\n",
    "            'target_accuracy': config['target_accuracy'],\n",
    "            'target_latency_ms': config['max_latency_ms'],\n",
    "            'accuracy_met': accuracy >= config['target_accuracy'],\n",
    "            'latency_met': np.mean(latencies) <= config['max_latency_ms'],\n",
    "            'compliant': accuracy >= config['target_accuracy'] and np.mean(latencies) <= config['max_latency_ms'],\n",
    "            'num_runs': num_runs,\n",
    "            'random_seed': self.random_seed\n",
    "        }\n",
    "\n",
    "        print(f\"   Results: {accuracy:.1%} accuracy, {np.mean(latencies):.1f}ms latency\")\n",
    "        print(f\"   Compliance: {'✅ PASS' if results['compliant'] else '❌ FAIL'}\")\n",
    "\n",
    "        return results\n",
    "\n",
    "    def run_all_benchmarks(self, model: Any) -> Dict[str, Dict[str, Any]]:\n",
    "        \"\"\"Run all TinyMLPerf benchmarks on a model.\"\"\"\n",
    "        all_results = {}\n",
    "\n",
    "        print(f\"🚀 Running full TinyMLPerf suite on {getattr(model, 'name', 'model')}...\")\n",
    "        print(\"=\" * 60)\n",
    "\n",
    "        for benchmark_name in self.benchmarks.keys():\n",
    "            try:\n",
    "                results = self.run_standard_benchmark(model, benchmark_name)\n",
    "                all_results[benchmark_name] = results\n",
    "                print()\n",
    "            except Exception as e:\n",
    "                print(f\"   ❌ Failed to run {benchmark_name}: {e}\")\n",
    "                all_results[benchmark_name] = {'error': str(e)}\n",
    "\n",
    "        return all_results\n",
    "\n",
    "    def generate_compliance_report(self, results: Dict[str, Dict[str, Any]],\n",
    "                                 output_path: str = \"tinymlperf_report.json\") -> str:\n",
    "        \"\"\"Generate TinyMLPerf compliance report.\"\"\"\n",
    "        # Calculate overall compliance\n",
    "        compliant_benchmarks = []\n",
    "        total_benchmarks = 0\n",
    "\n",
    "        report_data = {\n",
    "            'tinymlperf_version': '1.0',\n",
    "            'random_seed': self.random_seed,\n",
    "            'timestamp': time.strftime('%Y-%m-%d %H:%M:%S'),\n",
    "            'model_name': 'unknown',\n",
    "            'benchmarks': {},\n",
    "            'summary': {}\n",
    "        }\n",
    "\n",
    "        for benchmark_name, result in results.items():\n",
    "            if 'error' not in result:\n",
    "                total_benchmarks += 1\n",
    "                if result.get('compliant', False):\n",
    "                    compliant_benchmarks.append(benchmark_name)\n",
    "\n",
    "                # Set model name from first successful result\n",
    "                if report_data['model_name'] == 'unknown':\n",
    "                    report_data['model_name'] = result.get('model_name', 'unknown')\n",
    "\n",
    "                # Store benchmark results\n",
    "                report_data['benchmarks'][benchmark_name] = {\n",
    "                    'accuracy': result['accuracy'],\n",
    "                    'mean_latency_ms': result['mean_latency_ms'],\n",
    "                    'p99_latency_ms': result['p99_latency_ms'],\n",
    "                    'throughput_fps': result['throughput_fps'],\n",
    "                    'target_accuracy': result['target_accuracy'],\n",
    "                    'target_latency_ms': result['target_latency_ms'],\n",
    "                    'accuracy_met': result['accuracy_met'],\n",
    "                    'latency_met': result['latency_met'],\n",
    "                    'compliant': result['compliant']\n",
    "                }\n",
    "\n",
    "        # Summary statistics\n",
    "        if total_benchmarks > 0:\n",
    "            compliance_rate = len(compliant_benchmarks) / total_benchmarks\n",
    "            report_data['summary'] = {\n",
    "                'total_benchmarks': total_benchmarks,\n",
    "                'compliant_benchmarks': len(compliant_benchmarks),\n",
    "                'compliance_rate': compliance_rate,\n",
    "                'overall_compliant': compliance_rate == 1.0,\n",
    "                'compliant_benchmark_names': compliant_benchmarks\n",
    "            }\n",
    "\n",
    "        # Save report\n",
    "        with open(output_path, 'w') as f:\n",
    "            json.dump(report_data, f, indent=2)\n",
    "\n",
    "        # Generate human-readable summary\n",
    "        summary_lines = []\n",
    "        summary_lines.append(\"# TinyMLPerf Compliance Report\")\n",
    "        summary_lines.append(\"=\" * 40)\n",
    "        summary_lines.append(f\"Model: {report_data['model_name']}\")\n",
    "        summary_lines.append(f\"Date: {report_data['timestamp']}\")\n",
    "        summary_lines.append(\"\")\n",
    "\n",
    "        if total_benchmarks > 0:\n",
    "            summary_lines.append(f\"## Overall Result: {'✅ COMPLIANT' if report_data['summary']['overall_compliant'] else '❌ NON-COMPLIANT'}\")\n",
    "            summary_lines.append(f\"Compliance Rate: {compliance_rate:.1%} ({len(compliant_benchmarks)}/{total_benchmarks})\")\n",
    "            summary_lines.append(\"\")\n",
    "\n",
    "            summary_lines.append(\"## Benchmark Details:\")\n",
    "            for benchmark_name, result in report_data['benchmarks'].items():\n",
    "                status = \"✅ PASS\" if result['compliant'] else \"❌ FAIL\"\n",
    "                summary_lines.append(f\"- **{benchmark_name}**: {status}\")\n",
    "                summary_lines.append(f\"  - Accuracy: {result['accuracy']:.1%} (target: {result['target_accuracy']:.1%})\")\n",
    "                summary_lines.append(f\"  - Latency: {result['mean_latency_ms']:.1f}ms (target: <{result['target_latency_ms']}ms)\")\n",
    "                summary_lines.append(\"\")\n",
    "        else:\n",
    "            summary_lines.append(\"No successful benchmark runs.\")\n",
    "\n",
    "        summary_text = \"\\n\".join(summary_lines)\n",
    "\n",
    "        # Save human-readable report\n",
    "        summary_path = output_path.replace('.json', '_summary.md')\n",
    "        with open(summary_path, 'w') as f:\n",
    "            f.write(summary_text)\n",
    "\n",
    "        print(f\"📄 TinyMLPerf report saved to {output_path}\")\n",
    "        print(f\"📄 Summary saved to {summary_path}\")\n",
    "\n",
    "        return summary_text\n",
    "    ### END SOLUTION\n",
    "\n",
    "def test_unit_tinymlperf():\n",
    "    \"\"\"🔬 Test TinyMLPerf standardized benchmarking.\"\"\"\n",
    "    print(\"🔬 Unit Test: TinyMLPerf...\")\n",
    "\n",
    "    # Create mock model for testing\n",
    "    class MockModel:\n",
    "        def __init__(self, name):\n",
    "            self.name = name\n",
    "\n",
    "        def forward(self, x):\n",
    "            time.sleep(0.001)  # Simulate computation\n",
    "            # Return appropriate output shape for different benchmarks\n",
    "            if hasattr(x, 'shape'):\n",
    "                if len(x.shape) == 2:  # Audio/sequence\n",
    "                    return np.random.rand(2)  # Binary classification\n",
    "                else:  # Image\n",
    "                    return np.random.rand(10)  # Multi-class\n",
    "            return np.random.rand(2)\n",
    "\n",
    "    model = MockModel(\"test_model\")\n",
    "    perf = TinyMLPerf(random_seed=42)\n",
    "\n",
    "    # Test individual benchmark\n",
    "    result = perf.run_standard_benchmark(model, 'keyword_spotting', num_runs=5)\n",
    "\n",
    "    # Verify result structure\n",
    "    required_keys = ['accuracy', 'mean_latency_ms', 'throughput_fps', 'compliant']\n",
    "    assert all(key in result for key in required_keys)\n",
    "    assert 0 <= result['accuracy'] <= 1\n",
    "    assert result['mean_latency_ms'] > 0\n",
    "    assert result['throughput_fps'] > 0\n",
    "\n",
    "    # Test full benchmark suite (with fewer runs for speed)\n",
    "    import tempfile\n",
    "    with tempfile.TemporaryDirectory() as tmp_dir:\n",
    "        # Run subset of benchmarks for testing\n",
    "        subset_results = {}\n",
    "        for benchmark in ['keyword_spotting', 'image_classification']:\n",
    "            subset_results[benchmark] = perf.run_standard_benchmark(model, benchmark, num_runs=3)\n",
    "\n",
    "        # Test compliance report generation\n",
    "        report_path = f\"{tmp_dir}/test_report.json\"\n",
    "        summary = perf.generate_compliance_report(subset_results, report_path)\n",
    "\n",
    "        # Verify report was created\n",
    "        assert Path(report_path).exists()\n",
    "        assert \"TinyMLPerf Compliance Report\" in summary\n",
    "        assert \"Compliance Rate\" in summary\n",
    "\n",
    "    print(\"✅ TinyMLPerf works correctly!\")\n",
    "\n",
    "test_unit_tinymlperf()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bce5e722",
   "metadata": {
    "cell_marker": "\"\"\""
   },
   "source": [
    "# 4. Integration - Building Complete Benchmark Workflows\n",
    "\n",
    "Now we'll integrate all our benchmarking components into complete workflows that demonstrate professional ML systems evaluation. This integration shows how to combine statistical rigor with practical insights.\n",
    "\n",
    "The integration layer connects individual measurements into actionable engineering insights. This is where benchmarking becomes a decision-making tool rather than just data collection.\n",
    "\n",
    "## Workflow Architecture\n",
    "\n",
    "```\n",
    "Integration Workflow Pipeline:\n",
    "┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐\n",
    "│ Model Variants  │    │ Optimization    │    │ Use Case        │\n",
    "│ • Base model    │ →  │ Techniques      │ →  │ Analysis        │\n",
    "│ • Quantized     │    │ • Accuracy loss │    │ • Mobile        │\n",
    "│ • Pruned        │    │ • Speed gain    │    │ • Server        │\n",
    "│ • Distilled     │    │ • Memory save   │    │ • Edge          │\n",
    "└─────────────────┘    └─────────────────┘    └─────────────────┘\n",
    "```\n",
    "\n",
    "This workflow helps answer questions like:\n",
    "- \"Which optimization gives the best accuracy/latency trade-off?\"\n",
    "- \"What's the memory budget impact of each technique?\"\n",
    "- \"Which model should I deploy for mobile vs server?\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fceb0478",
   "metadata": {
    "cell_marker": "\"\"\"",
    "lines_to_next_cell": 1
   },
   "source": [
    "## Optimization Comparison Engine\n",
    "\n",
    "Before implementing the comparison function, let's understand what makes optimization comparison challenging and valuable.\n",
    "\n",
    "### Why Optimization Comparison is Complex\n",
    "\n",
    "When you optimize a model, you're making trade-offs across multiple dimensions simultaneously:\n",
    "\n",
    "```\n",
    "Optimization Impact Matrix:\n",
    "                   Accuracy    Latency    Memory    Energy\n",
    "Quantization        -5%        +2.1x      +2.0x     +1.8x\n",
    "Pruning            -2%        +1.4x      +3.2x     +1.3x\n",
    "Knowledge Distill. -8%        +1.9x      +1.5x     +1.7x\n",
    "```\n",
    "\n",
    "The challenge: Which is \"best\"? It depends entirely on your deployment constraints.\n",
    "\n",
    "### Multi-Objective Decision Framework\n",
    "\n",
    "Our comparison engine implements a decision framework that:\n",
    "\n",
    "1. **Measures all dimensions**: Don't optimize in isolation\n",
    "2. **Calculates efficiency ratios**: Accuracy per MB, accuracy per ms\n",
    "3. **Identifies Pareto frontiers**: Models that aren't dominated in all metrics\n",
    "4. **Generates use-case recommendations**: Tailored to specific constraints\n",
    "\n",
    "### Recommendation Algorithm\n",
    "\n",
    "```\n",
    "For each use case:\n",
    "├── Latency-critical (real-time apps)\n",
    "│   └── Optimize: min(latency) subject to accuracy > threshold\n",
    "├── Memory-constrained (mobile/IoT)\n",
    "│   └── Optimize: min(memory) subject to accuracy > threshold\n",
    "├── Accuracy-preservation (quality-critical)\n",
    "│   └── Optimize: max(accuracy) subject to latency < threshold\n",
    "└── Balanced (general deployment)\n",
    "    └── Optimize: weighted combination of all factors\n",
    "```\n",
    "\n",
    "This principled approach ensures recommendations match real deployment needs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0e9d140",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "benchmark-comparison",
     "solution": true
    }
   },
   "outputs": [],
   "source": [
    "def compare_optimization_techniques(base_model: Any, optimized_models: List[Any],\n",
    "                                  datasets: List[Any]) -> Dict[str, Any]:\n",
    "    \"\"\"\n",
    "    Compare base model against various optimization techniques.\n",
    "\n",
    "    TODO: Implement comprehensive comparison of optimization approaches\n",
    "\n",
    "    APPROACH:\n",
    "    1. Run benchmarks on base model and all optimized variants\n",
    "    2. Calculate improvement ratios and trade-offs\n",
    "    3. Generate insights about which optimizations work best\n",
    "    4. Create recommendation matrix for different use cases\n",
    "\n",
    "    EXAMPLE:\n",
    "    >>> models = [base_model, quantized_model, pruned_model, distilled_model]\n",
    "    >>> results = compare_optimization_techniques(base_model, models[1:], datasets)\n",
    "    >>> print(results['recommendations'])\n",
    "\n",
    "    HINTS:\n",
    "    - Compare accuracy retention vs speed/memory improvements\n",
    "    - Calculate efficiency metrics (accuracy per MB, accuracy per ms)\n",
    "    - Identify Pareto-optimal solutions\n",
    "    - Generate actionable recommendations for different scenarios\n",
    "    \"\"\"\n",
    "    ### BEGIN SOLUTION\n",
    "    all_models = [base_model] + optimized_models\n",
    "    suite = BenchmarkSuite(all_models, datasets)\n",
    "\n",
    "    print(\"🔬 Running optimization comparison benchmark...\")\n",
    "    benchmark_results = suite.run_full_benchmark()\n",
    "\n",
    "    # Extract base model performance for comparison\n",
    "    base_name = getattr(base_model, 'name', 'model_0')\n",
    "\n",
    "    base_metrics = {}\n",
    "    for metric_type, results in benchmark_results.items():\n",
    "        for model_name, result in results.items():\n",
    "            if base_name in model_name:\n",
    "                base_metrics[metric_type] = result.mean\n",
    "                break\n",
    "\n",
    "    # Calculate improvement ratios\n",
    "    comparison_results = {\n",
    "        'base_model': base_name,\n",
    "        'base_metrics': base_metrics,\n",
    "        'optimized_results': {},\n",
    "        'improvements': {},\n",
    "        'efficiency_metrics': {},\n",
    "        'recommendations': {}\n",
    "    }\n",
    "\n",
    "    for opt_model in optimized_models:\n",
    "        opt_name = getattr(opt_model, 'name', f'optimized_model_{len(comparison_results[\"optimized_results\"])}')\n",
    "\n",
    "        # Find results for this optimized model\n",
    "        opt_metrics = {}\n",
    "        for metric_type, results in benchmark_results.items():\n",
    "            for model_name, result in results.items():\n",
    "                if opt_name in model_name:\n",
    "                    opt_metrics[metric_type] = result.mean\n",
    "                    break\n",
    "\n",
    "        comparison_results['optimized_results'][opt_name] = opt_metrics\n",
    "\n",
    "        # Calculate improvements\n",
    "        improvements = {}\n",
    "        for metric_type in ['latency', 'memory', 'energy']:\n",
    "            if metric_type in base_metrics and metric_type in opt_metrics:\n",
    "                # For these metrics, lower is better, so improvement = base/optimized\n",
    "                if opt_metrics[metric_type] > 0:\n",
    "                    improvements[f'{metric_type}_speedup'] = base_metrics[metric_type] / opt_metrics[metric_type]\n",
    "                else:\n",
    "                    improvements[f'{metric_type}_speedup'] = 1.0\n",
    "\n",
    "        if 'accuracy' in base_metrics and 'accuracy' in opt_metrics:\n",
    "            # Accuracy retention (higher is better)\n",
    "            improvements['accuracy_retention'] = opt_metrics['accuracy'] / base_metrics['accuracy']\n",
    "\n",
    "        comparison_results['improvements'][opt_name] = improvements\n",
    "\n",
    "        # Calculate efficiency metrics\n",
    "        efficiency = {}\n",
    "        if 'accuracy' in opt_metrics:\n",
    "            if 'memory' in opt_metrics and opt_metrics['memory'] > 0:\n",
    "                efficiency['accuracy_per_mb'] = opt_metrics['accuracy'] / opt_metrics['memory']\n",
    "            if 'latency' in opt_metrics and opt_metrics['latency'] > 0:\n",
    "                efficiency['accuracy_per_ms'] = opt_metrics['accuracy'] / opt_metrics['latency']\n",
    "\n",
    "        comparison_results['efficiency_metrics'][opt_name] = efficiency\n",
    "\n",
    "    # Generate recommendations based on results\n",
    "    recommendations = {}\n",
    "\n",
    "    # Find best performers in each category\n",
    "    best_latency = None\n",
    "    best_memory = None\n",
    "    best_accuracy = None\n",
    "    best_overall = None\n",
    "\n",
    "    best_latency_score = 0\n",
    "    best_memory_score = 0\n",
    "    best_accuracy_score = 0\n",
    "    best_overall_score = 0\n",
    "\n",
    "    for opt_name, improvements in comparison_results['improvements'].items():\n",
    "        # Latency recommendation\n",
    "        if 'latency_speedup' in improvements and improvements['latency_speedup'] > best_latency_score:\n",
    "            best_latency_score = improvements['latency_speedup']\n",
    "            best_latency = opt_name\n",
    "\n",
    "        # Memory recommendation\n",
    "        if 'memory_speedup' in improvements and improvements['memory_speedup'] > best_memory_score:\n",
    "            best_memory_score = improvements['memory_speedup']\n",
    "            best_memory = opt_name\n",
    "\n",
    "        # Accuracy recommendation\n",
    "        if 'accuracy_retention' in improvements and improvements['accuracy_retention'] > best_accuracy_score:\n",
    "            best_accuracy_score = improvements['accuracy_retention']\n",
    "            best_accuracy = opt_name\n",
    "\n",
    "        # Overall balance (considering all factors)\n",
    "        overall_score = 0\n",
    "        count = 0\n",
    "        for key, value in improvements.items():\n",
    "            if 'speedup' in key:\n",
    "                overall_score += min(value, 5.0)  # Cap speedup at 5x to avoid outliers\n",
    "                count += 1\n",
    "            elif 'retention' in key:\n",
    "                overall_score += value * 5  # Weight accuracy retention heavily\n",
    "                count += 1\n",
    "\n",
    "        if count > 0:\n",
    "            overall_score /= count\n",
    "            if overall_score > best_overall_score:\n",
    "                best_overall_score = overall_score\n",
    "                best_overall = opt_name\n",
    "\n",
    "    recommendations = {\n",
    "        'for_latency_critical': {\n",
    "            'model': best_latency,\n",
    "            'reason': f\"Best latency improvement: {best_latency_score:.2f}x faster\",\n",
    "            'use_case': \"Real-time applications, edge devices with strict timing requirements\"\n",
    "        },\n",
    "        'for_memory_constrained': {\n",
    "            'model': best_memory,\n",
    "            'reason': f\"Best memory reduction: {best_memory_score:.2f}x smaller\",\n",
    "            'use_case': \"Mobile devices, IoT sensors, embedded systems\"\n",
    "        },\n",
    "        'for_accuracy_preservation': {\n",
    "            'model': best_accuracy,\n",
    "            'reason': f\"Best accuracy retention: {best_accuracy_score:.1%} of original\",\n",
    "            'use_case': \"Applications where quality cannot be compromised\"\n",
    "        },\n",
    "        'for_balanced_deployment': {\n",
    "            'model': best_overall,\n",
    "            'reason': f\"Best overall trade-off (score: {best_overall_score:.2f})\",\n",
    "            'use_case': \"General production deployment with multiple constraints\"\n",
    "        }\n",
    "    }\n",
    "\n",
    "    comparison_results['recommendations'] = recommendations\n",
    "\n",
    "    # Print summary\n",
    "    print(\"\\n📊 Optimization Comparison Results:\")\n",
    "    print(\"=\" * 50)\n",
    "\n",
    "    for opt_name, improvements in comparison_results['improvements'].items():\n",
    "        print(f\"\\n{opt_name}:\")\n",
    "        for metric, value in improvements.items():\n",
    "            if 'speedup' in metric:\n",
    "                print(f\"  {metric}: {value:.2f}x improvement\")\n",
    "            elif 'retention' in metric:\n",
    "                print(f\"  {metric}: {value:.1%}\")\n",
    "\n",
    "    print(\"\\n🎯 Recommendations:\")\n",
    "    for use_case, rec in recommendations.items():\n",
    "        if rec['model']:\n",
    "            print(f\"  {use_case}: {rec['model']} - {rec['reason']}\")\n",
    "\n",
    "    return comparison_results\n",
    "    ### END SOLUTION\n",
    "\n",
    "def test_unit_optimization_comparison():\n",
    "    \"\"\"🔬 Test optimization comparison functionality.\"\"\"\n",
    "    print(\"🔬 Unit Test: compare_optimization_techniques...\")\n",
    "\n",
    "    # Create mock models with different characteristics\n",
    "    class MockModel:\n",
    "        def __init__(self, name, latency_factor=1.0, accuracy_factor=1.0, memory_factor=1.0):\n",
    "            self.name = name\n",
    "            self.latency_factor = latency_factor\n",
    "            self.accuracy_factor = accuracy_factor\n",
    "            self.memory_factor = memory_factor\n",
    "\n",
    "        def forward(self, x):\n",
    "            time.sleep(0.001 * self.latency_factor)\n",
    "            return x\n",
    "\n",
    "    # Base model and optimized variants\n",
    "    base_model = MockModel(\"base_model\", latency_factor=1.0, accuracy_factor=1.0, memory_factor=1.0)\n",
    "    quantized_model = MockModel(\"quantized_model\", latency_factor=0.7, accuracy_factor=0.95, memory_factor=0.5)\n",
    "    pruned_model = MockModel(\"pruned_model\", latency_factor=0.8, accuracy_factor=0.98, memory_factor=0.3)\n",
    "\n",
    "    datasets = [{\"test\": \"data\"}]\n",
    "\n",
    "    # Run comparison\n",
    "    results = compare_optimization_techniques(base_model, [quantized_model, pruned_model], datasets)\n",
    "\n",
    "    # Verify results structure\n",
    "    assert 'base_model' in results\n",
    "    assert 'optimized_results' in results\n",
    "    assert 'improvements' in results\n",
    "    assert 'recommendations' in results\n",
    "\n",
    "    # Verify improvements were calculated\n",
    "    assert len(results['improvements']) == 2  # Two optimized models\n",
    "\n",
    "    # Verify recommendations were generated\n",
    "    recommendations = results['recommendations']\n",
    "    assert 'for_latency_critical' in recommendations\n",
    "    assert 'for_memory_constrained' in recommendations\n",
    "    assert 'for_accuracy_preservation' in recommendations\n",
    "    assert 'for_balanced_deployment' in recommendations\n",
    "\n",
    "    print(\"✅ compare_optimization_techniques works correctly!\")\n",
    "\n",
    "test_unit_optimization_comparison()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "026dcc7d",
   "metadata": {
    "cell_marker": "\"\"\""
   },
   "source": [
    "## 4.4 MLPerf Principles - Industry-Standard Benchmarking\n",
    "\n",
    "Before we dive into optimization strategies, let's learn from **MLPerf** - the industry-standard ML benchmarking framework. Understanding MLPerf principles will ground your capstone competition in professional ML systems evaluation.\n",
    "\n",
    "### What is MLPerf?\n",
    "\n",
    "MLPerf is the industry-standard benchmark suite for measuring ML system performance. Think of it as the \"Olympics\" of ML systems, but with rigorous scientific methodology:\n",
    "\n",
    "- **Created by:** MLCommons (Google, NVIDIA, Intel, universities)\n",
    "- **Used by:** All major ML hardware/software companies\n",
    "- **Purpose:** Fair, reproducible comparison of ML systems\n",
    "- **Impact:** Drives billions in hardware/software decisions\n",
    "\n",
    "### Core MLPerf Principles\n",
    "\n",
    "**1. Reproducibility**\n",
    "- Exact hardware specifications reported\n",
    "- Software versions documented\n",
    "- Random seeds controlled\n",
    "- Multiple runs required for statistical validity\n",
    "\n",
    "**2. Standardization**\n",
    "- Fixed model architectures (everyone runs the same models)\n",
    "- Fixed datasets (same training/test data)\n",
    "- Fixed quality targets (must achieve X% accuracy)\n",
    "- Fair comparison (apples-to-apples)\n",
    "\n",
    "**3. Divisions for Different Goals**\n",
    "\n",
    "MLPerf has TWO main divisions:\n",
    "\n",
    "**🔒 Closed Division** (Strict Rules):\n",
    "- Use provided model architectures exactly\n",
    "- Use provided datasets exactly\n",
    "- Can optimize: training algorithms, hardware, software stack\n",
    "- **Goal:** Fair comparison of SYSTEMS (not algorithms)\n",
    "- Example: \"Which GPU trains ResNet-50 fastest?\"\n",
    "\n",
    "**🔓 Open Division** (Flexible Rules):\n",
    "- Modify model architectures\n",
    "- Use different datasets\n",
    "- Novel algorithms allowed\n",
    "- **Goal:** Show innovation and new approaches\n",
    "- Example: \"New pruning technique achieves 10x speedup!\"\n",
    "\n",
    "**Why Two Divisions?**\n",
    "- Closed: Answers \"What's the best hardware/software for X?\"\n",
    "- Open: Answers \"What's the best algorithm/innovation for Y?\"\n",
    "\n",
    "### MLPerf Inference Benchmarks\n",
    "\n",
    "MLPerf Inference (what we care about) measures:\n",
    "- **Latency:** Single-stream inference time\n",
    "- **Throughput:** Offline batch processing speed\n",
    "- **Accuracy:** Must meet quality targets\n",
    "- **Power:** Energy efficiency (advanced)\n",
    "\n",
    "Common scenarios:\n",
    "- **Server:** Datacenter deployment (high throughput)\n",
    "- **Edge:** On-device inference (low latency, low power)\n",
    "- **Mobile:** Smartphone deployment (tiny models)\n",
    "\n",
    "### TinyMLPerf - MLPerf for Tiny Systems\n",
    "\n",
    "TinyMLPerf is MLPerf for embedded/edge devices:\n",
    "- Models <1MB\n",
    "- Latency <100ms\n",
    "- Power <10mW\n",
    "- Real deployment constraints\n",
    "\n",
    "**This is what inspires your capstone!**\n",
    "\n",
    "### Key Takeaways for Your Competition\n",
    "\n",
    "1. **Reproducibility Matters:** Document everything\n",
    "2. **Fair Comparison:** Same baseline for everyone\n",
    "3. **Multiple Metrics:** Not just accuracy - latency, memory, energy\n",
    "4. **Real Constraints:** Optimize for actual deployment scenarios\n",
    "5. **Closed vs Open:** Understand the rules of your competition\n",
    "\n",
    "**In Module 20**, you'll participate in **TinyMLPerf-style competition** following these principles!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20aa0b56",
   "metadata": {
    "cell_marker": "\"\"\""
   },
   "source": [
    "## 4.5 Normalized Metrics - Fair Comparison Across Different Hardware\n",
    "\n",
    "### The Hardware Problem\n",
    "\n",
    "Imagine two students submit their optimizations:\n",
    "- **Alice** (M3 Mac, 16GB RAM): \"My model runs at 50ms latency!\"\n",
    "- **Bob** (2015 laptop, 4GB RAM): \"My model runs at 200ms latency!\"\n",
    "\n",
    "Who optimized better? **You can't tell from raw numbers!**\n",
    "\n",
    "Alice's hardware is 4x faster. If Bob achieved 200ms on old hardware, he might have optimized MORE aggressively than Alice. Raw metrics are unfair.\n",
    "\n",
    "### The Solution: Relative Improvement Metrics\n",
    "\n",
    "Instead of absolute performance, measure **relative improvement** from YOUR baseline:\n",
    "\n",
    "```\n",
    "Speedup = Baseline Latency / Optimized Latency\n",
    "Compression Ratio = Baseline Memory / Optimized Memory  \n",
    "Accuracy Delta = Optimized Accuracy - Baseline Accuracy\n",
    "```\n",
    "\n",
    "**Example:**\n",
    "- Alice: 100ms → 50ms = **2.0x speedup** ✓\n",
    "- Bob: 400ms → 200ms = **2.0x speedup** ✓\n",
    "\n",
    "Now they're fairly compared! Both achieved 2x speedup on their hardware.\n",
    "\n",
    "### Key Normalized Metrics for TorchPerf Olympics\n",
    "\n",
    "**1. Speedup (for Latency Sprint)**\n",
    "```python\n",
    "speedup = baseline_latency / optimized_latency\n",
    "# Higher is better: 2.5x means 2.5 times faster\n",
    "```\n",
    "\n",
    "**2. Compression Ratio (for Memory Challenge)**\n",
    "```python\n",
    "compression_ratio = baseline_memory / optimized_memory\n",
    "# Higher is better: 4.0x means 4 times smaller\n",
    "```\n",
    "\n",
    "**3. Accuracy Preservation (for All Events)**\n",
    "```python\n",
    "accuracy_delta = optimized_accuracy - baseline_accuracy\n",
    "# Closer to 0 is better: -0.02 means 2% accuracy drop\n",
    "```\n",
    "\n",
    "**4. Efficiency Score (for All-Around)**\n",
    "```python\n",
    "efficiency = (speedup * compression_ratio) / max(1.0, abs(accuracy_delta))\n",
    "# Balances all metrics\n",
    "```\n",
    "\n",
    "### Why This Matters for Your Competition\n",
    "\n",
    "**Without normalization:**\n",
    "- Newest hardware wins unfairly\n",
    "- Focus shifts to \"who has the best laptop\"\n",
    "- Optimization skill doesn't matter\n",
    "\n",
    "**With normalization:**\n",
    "- Everyone competes on **optimization skill**\n",
    "- Hardware differences are eliminated\n",
    "- Focus is on relative improvement\n",
    "\n",
    "**Real MLPerf Example:**\n",
    "```\n",
    "NVIDIA A100 submission: 2.1ms (absolute) → 3.5x speedup (relative)\n",
    "Google TPU submission: 1.8ms (absolute) → 4.2x speedup (relative)\n",
    "\n",
    "Winner: Google (better speedup despite slower absolute time)\n",
    "```\n",
    "\n",
    "### Implementing Normalized Scoring"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c051c23",
   "metadata": {
    "cell_marker": "\"\"\"",
    "lines_to_next_cell": 1
   },
   "source": [
    "Let's implement a helper function to calculate normalized scores for the competition:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75393e9b",
   "metadata": {
    "lines_to_next_cell": 1,
    "nbgrader": {
     "grade": false,
     "grade_id": "normalized-scoring",
     "locked": false
    }
   },
   "outputs": [],
   "source": [
    "#| export\n",
    "def calculate_normalized_scores(baseline_results: dict, \n",
    "                                optimized_results: dict) -> dict:\n",
    "    \"\"\"\n",
    "    Calculate normalized performance metrics for fair competition comparison.\n",
    "    \n",
    "    This function converts absolute measurements into relative improvements,\n",
    "    enabling fair comparison across different hardware platforms.\n",
    "    \n",
    "    Args:\n",
    "        baseline_results: Dict with keys: 'latency', 'memory', 'accuracy'\n",
    "        optimized_results: Dict with same keys as baseline_results\n",
    "        \n",
    "    Returns:\n",
    "        Dict with normalized metrics:\n",
    "        - speedup: Relative latency improvement (higher is better)\n",
    "        - compression_ratio: Relative memory reduction (higher is better)\n",
    "        - accuracy_delta: Absolute accuracy change (closer to 0 is better)\n",
    "        - efficiency_score: Combined metric balancing all factors\n",
    "        \n",
    "    Example:\n",
    "        >>> baseline = {'latency': 100.0, 'memory': 12.0, 'accuracy': 0.89}\n",
    "        >>> optimized = {'latency': 40.0, 'memory': 3.0, 'accuracy': 0.87}\n",
    "        >>> scores = calculate_normalized_scores(baseline, optimized)\n",
    "        >>> print(f\"Speedup: {scores['speedup']:.2f}x\")\n",
    "        Speedup: 2.50x\n",
    "    \"\"\"\n",
    "    # Calculate speedup (higher is better)\n",
    "    speedup = baseline_results['latency'] / optimized_results['latency']\n",
    "    \n",
    "    # Calculate compression ratio (higher is better)\n",
    "    compression_ratio = baseline_results['memory'] / optimized_results['memory']\n",
    "    \n",
    "    # Calculate accuracy delta (closer to 0 is better, negative means degradation)\n",
    "    accuracy_delta = optimized_results['accuracy'] - baseline_results['accuracy']\n",
    "    \n",
    "    # Calculate efficiency score (combined metric)\n",
    "    # Penalize accuracy loss: the more accuracy you lose, the lower your score\n",
    "    accuracy_penalty = max(1.0, 1.0 - accuracy_delta) if accuracy_delta < 0 else 1.0\n",
    "    efficiency_score = (speedup * compression_ratio) / accuracy_penalty\n",
    "    \n",
    "    return {\n",
    "        'speedup': speedup,\n",
    "        'compression_ratio': compression_ratio,\n",
    "        'accuracy_delta': accuracy_delta,\n",
    "        'efficiency_score': efficiency_score,\n",
    "        'baseline': baseline_results.copy(),\n",
    "        'optimized': optimized_results.copy()\n",
    "    }"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16a7dbfe",
   "metadata": {
    "cell_marker": "\"\"\"",
    "lines_to_next_cell": 1
   },
   "source": [
    "### 🧪 Unit Test: Normalized Scoring\n",
    "\n",
    "**This is a unit test** - it validates that normalized scoring correctly calculates relative improvements."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a76bb43c",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "test-normalized-scoring",
     "locked": true,
     "points": 1
    }
   },
   "outputs": [],
   "source": [
    "def test_unit_normalized_scoring():\n",
    "    \"\"\"Test normalized scoring calculation.\"\"\"\n",
    "    print(\"🔬 Unit Test: Normalized Scoring Calculation...\")\n",
    "    \n",
    "    # Test Case 1: Standard optimization (speedup + compression)\n",
    "    baseline = {'latency': 100.0, 'memory': 12.0, 'accuracy': 0.89}\n",
    "    optimized = {'latency': 40.0, 'memory': 3.0, 'accuracy': 0.87}\n",
    "    \n",
    "    scores = calculate_normalized_scores(baseline, optimized)\n",
    "    \n",
    "    assert abs(scores['speedup'] - 2.5) < 0.01, \"Speedup calculation incorrect\"\n",
    "    assert abs(scores['compression_ratio'] - 4.0) < 0.01, \"Compression ratio incorrect\"\n",
    "    assert abs(scores['accuracy_delta'] - (-0.02)) < 0.001, \"Accuracy delta incorrect\"\n",
    "    print(\"  ✅ Standard optimization scoring works\")\n",
    "    \n",
    "    # Test Case 2: Extreme optimization (high speedup, accuracy loss)\n",
    "    optimized_extreme = {'latency': 20.0, 'memory': 1.5, 'accuracy': 0.75}\n",
    "    scores_extreme = calculate_normalized_scores(baseline, optimized_extreme)\n",
    "    \n",
    "    assert scores_extreme['speedup'] > 4.0, \"Extreme speedup not detected\"\n",
    "    assert scores_extreme['accuracy_delta'] < -0.1, \"Large accuracy loss not detected\"\n",
    "    print(\"  ✅ Extreme optimization scoring works\")\n",
    "    \n",
    "    # Test Case 3: Conservative optimization (minimal changes)\n",
    "    optimized_conservative = {'latency': 90.0, 'memory': 11.0, 'accuracy': 0.89}\n",
    "    scores_conservative = calculate_normalized_scores(baseline, optimized_conservative)\n",
    "    \n",
    "    assert abs(scores_conservative['accuracy_delta']) < 0.01, \"Accuracy preservation not detected\"\n",
    "    print(\"  ✅ Conservative optimization scoring works\")\n",
    "    \n",
    "    # Test Case 4: Accuracy improvement (rare but possible)\n",
    "    optimized_better = {'latency': 80.0, 'memory': 10.0, 'accuracy': 0.91}\n",
    "    scores_better = calculate_normalized_scores(baseline, optimized_better)\n",
    "    \n",
    "    assert scores_better['accuracy_delta'] > 0, \"Accuracy improvement not detected\"\n",
    "    print(\"  ✅ Accuracy improvement scoring works\")\n",
    "    \n",
    "    print(\"📈 Progress: Normalized Scoring ✓\\n\")\n",
    "\n",
    "test_unit_normalized_scoring()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1199666",
   "metadata": {
    "cell_marker": "\"\"\""
   },
   "source": [
    "### Key Takeaways\n",
    "\n",
    "1. **Always report relative improvements, not absolute numbers**\n",
    "2. **Speedup and compression ratio are the primary metrics**\n",
    "3. **Accuracy delta shows the optimization cost**\n",
    "4. **Efficiency score balances all factors for All-Around event**\n",
    "\n",
    "**In Module 20**, you'll use `calculate_normalized_scores()` to generate your competition submission!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3dabdb12",
   "metadata": {
    "cell_marker": "\"\"\""
   },
   "source": [
    "## 4.6 Combination Strategies - Preparing for TorchPerf Olympics\n",
    "\n",
    "You've learned individual optimizations (M14-18). Now it's time to combine them strategically! The order and parameters matter significantly for final performance.\n",
    "\n",
    "### Why Combination Order Matters\n",
    "\n",
    "Consider these two strategies:\n",
    "- **Strategy A**: Quantize INT8 → Prune 70% → Fuse kernels\n",
    "- **Strategy B**: Prune 70% → Quantize INT8 → Fuse kernels\n",
    "\n",
    "Strategy A might preserve more accuracy because quantization happens first (on the full network), while Strategy B might be faster because pruning reduces what needs to be quantized. The \"best\" depends on your Olympic event!\n",
    "\n",
    "### Ablation Studies: Understanding Individual Contributions\n",
    "\n",
    "Professional ML engineers use **ablation studies** to understand what each optimization contributes:\n",
    "\n",
    "```\n",
    "Baseline:           Accuracy: 89%, Latency: 45ms, Memory: 12MB\n",
    "+ Quantization:     Accuracy: 88%, Latency: 30ms, Memory: 3MB   (Δ: -1%, -33%, -75%)\n",
    "+ Pruning:          Accuracy: 87%, Latency: 22ms, Memory: 2MB   (Δ: -1%, -27%, -33%)\n",
    "+ Kernel Fusion:    Accuracy: 87%, Latency: 18ms, Memory: 2MB   (Δ: 0%, -18%, 0%)\n",
    "\n",
    "Conclusion: Quantization provides biggest memory reduction, fusion provides latency boost\n",
    "```\n",
    "\n",
    "This systematic analysis tells you what to prioritize for each Olympic event!\n",
    "\n",
    "### Olympic Event Strategies\n",
    "\n",
    "**🏃 Latency Sprint**: Minimize inference time\n",
    "- Priority: Kernel fusion > KV caching > Quantization > Pruning\n",
    "- Risk: Aggressive optimizations may hurt accuracy\n",
    "- Tip: Start with proven speed techniques, then add memory techniques if needed\n",
    "\n",
    "**🏋️ Memory Challenge**: Minimize model footprint\n",
    "- Priority: Quantization > Pruning > Compression\n",
    "- Risk: Model quality degradation\n",
    "- Tip: Quantize first (4x memory reduction), then prune to meet target\n",
    "\n",
    "**🎯 Accuracy Contest**: Maximize accuracy within constraints\n",
    "- Priority: Minimal optimizations, careful tuning\n",
    "- Risk: Not enough optimization to meet constraints\n",
    "- Tip: Use high-bit quantization (8-bit), light pruning (30-50%)\n",
    "\n",
    "**🏋️‍♂️ All-Around**: Best balanced performance\n",
    "- Priority: Balanced application of all techniques\n",
    "- Risk: Jack of all trades, master of none\n",
    "- Tip: Use moderate settings for each technique (INT8, 60% pruning, selective fusion)\n",
    "\n",
    "**🚀 Extreme Push**: Most aggressive optimization\n",
    "- Priority: Maximum of everything\n",
    "- Risk: Significant accuracy loss\n",
    "- Tip: Start with 4-bit quantization + 90% pruning, verify accuracy threshold\n",
    "\n",
    "### Example: Combining for All-Around Event\n",
    "\n",
    "```python\n",
    "from tinytorch.optimization.quantization import quantize_model\n",
    "from tinytorch.optimization.compression import magnitude_prune\n",
    "from tinytorch.generation.kv_cache import enable_kv_cache\n",
    "\n",
    "# Load baseline\n",
    "baseline_model = load_baseline(\"cifar10_cnn\")\n",
    "\n",
    "# Apply balanced optimization strategy\n",
    "optimized = baseline_model\n",
    "\n",
    "# Step 1: Quantize to INT8 (moderate precision)\n",
    "optimized = quantize_model(optimized, bits=8)\n",
    "\n",
    "# Step 2: Prune 60% (moderate sparsity)\n",
    "optimized = magnitude_prune(optimized, sparsity=0.6)\n",
    "\n",
    "# Step 3: Enable KV cache for transformers (if applicable)\n",
    "if hasattr(optimized, 'transformer_blocks'):\n",
    "    enable_kv_cache(optimized)\n",
    "\n",
    "# Benchmark using TorchPerf\n",
    "from tinytorch.benchmarking.benchmark import Benchmark, OlympicEvent\n",
    "\n",
    "benchmark = Benchmark([baseline_model, optimized], \n",
    "                     [{\"name\": \"baseline\"}, {\"name\": \"optimized\"}])\n",
    "\n",
    "results = benchmark.run_latency_benchmark()\n",
    "# Compare and iterate!\n",
    "```\n",
    "\n",
    "The key: **Start with one technique, measure impact, add next technique, repeat!**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d21ac76",
   "metadata": {
    "cell_marker": "\"\"\"",
    "lines_to_next_cell": 1
   },
   "source": [
    "# 5. Module Integration Test\n",
    "\n",
    "Final validation that our complete benchmarking system works correctly and integrates properly with all TinyTorch components.\n",
    "\n",
    "This comprehensive test validates the entire benchmarking ecosystem and ensures it's ready for production use in the final capstone project."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73f8dc31",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "test-module",
     "locked": true,
     "points": 10
    }
   },
   "outputs": [],
   "source": [
    "def test_module():\n",
    "    \"\"\"\n",
    "    Comprehensive test of entire benchmarking module functionality.\n",
    "\n",
    "    This final test runs before module summary to ensure:\n",
    "    - All benchmarking components work together correctly\n",
    "    - Statistical analysis provides reliable results\n",
    "    - Integration with optimization modules functions properly\n",
    "    - Professional reporting generates actionable insights\n",
    "    \"\"\"\n",
    "    print(\"🧪 RUNNING MODULE INTEGRATION TEST\")\n",
    "    print(\"=\" * 50)\n",
    "\n",
    "    # Run all unit tests\n",
    "    print(\"Running unit tests...\")\n",
    "    test_unit_benchmark_result()\n",
    "    test_unit_precise_timer()\n",
    "    test_unit_benchmark()\n",
    "    test_unit_benchmark_suite()\n",
    "    test_unit_tinymlperf()\n",
    "    test_unit_optimization_comparison()\n",
    "    test_unit_normalized_scoring()\n",
    "\n",
    "    print(\"\\nRunning integration scenarios...\")\n",
    "\n",
    "    # Test realistic benchmarking workflow\n",
    "    print(\"🔬 Integration Test: Complete benchmarking workflow...\")\n",
    "\n",
    "    # Create realistic test models\n",
    "    class RealisticModel:\n",
    "        def __init__(self, name, characteristics):\n",
    "            self.name = name\n",
    "            self.characteristics = characteristics\n",
    "\n",
    "        def forward(self, x):\n",
    "            # Simulate different model behaviors\n",
    "            base_time = self.characteristics.get('base_latency', 0.001)\n",
    "            variance = self.characteristics.get('variance', 0.0001)\n",
    "            memory_factor = self.characteristics.get('memory_factor', 1.0)\n",
    "\n",
    "            # Simulate realistic computation\n",
    "            time.sleep(max(0, base_time + np.random.normal(0, variance)))\n",
    "\n",
    "            # Simulate memory usage\n",
    "            if hasattr(x, 'shape'):\n",
    "                temp_size = int(np.prod(x.shape) * memory_factor)\n",
    "                temp_data = np.random.randn(temp_size)\n",
    "                _ = np.sum(temp_data)  # Use the data\n",
    "\n",
    "            return x\n",
    "\n",
    "        def evaluate(self, dataset):\n",
    "            # Simulate evaluation\n",
    "            base_acc = self.characteristics.get('base_accuracy', 0.85)\n",
    "            return base_acc + np.random.normal(0, 0.02)\n",
    "\n",
    "        def parameters(self):\n",
    "            # Simulate parameter count\n",
    "            param_count = self.characteristics.get('param_count', 1000000)\n",
    "            return [np.random.randn(param_count)]\n",
    "\n",
    "    # Create test model suite\n",
    "    models = [\n",
    "        RealisticModel(\"efficient_model\", {\n",
    "            'base_latency': 0.001,\n",
    "            'base_accuracy': 0.82,\n",
    "            'memory_factor': 0.5,\n",
    "            'param_count': 500000\n",
    "        }),\n",
    "        RealisticModel(\"accurate_model\", {\n",
    "            'base_latency': 0.003,\n",
    "            'base_accuracy': 0.95,\n",
    "            'memory_factor': 2.0,\n",
    "            'param_count': 2000000\n",
    "        }),\n",
    "        RealisticModel(\"balanced_model\", {\n",
    "            'base_latency': 0.002,\n",
    "            'base_accuracy': 0.88,\n",
    "            'memory_factor': 1.0,\n",
    "            'param_count': 1000000\n",
    "        })\n",
    "    ]\n",
    "\n",
    "    datasets = [{\"test_data\": f\"dataset_{i}\"} for i in range(3)]\n",
    "\n",
    "    # Test 1: Comprehensive benchmark suite\n",
    "    print(\"  Testing comprehensive benchmark suite...\")\n",
    "    suite = BenchmarkSuite(models, datasets)\n",
    "    results = suite.run_full_benchmark()\n",
    "\n",
    "    assert 'latency' in results\n",
    "    assert 'accuracy' in results\n",
    "    assert 'memory' in results\n",
    "    assert 'energy' in results\n",
    "\n",
    "    # Verify all models were tested\n",
    "    for result_type in results.values():\n",
    "        assert len(result_type) == len(models)\n",
    "\n",
    "    # Test 2: Statistical analysis\n",
    "    print(\"  Testing statistical analysis...\")\n",
    "    for result_type, model_results in results.items():\n",
    "        for model_name, result in model_results.items():\n",
    "            assert isinstance(result, BenchmarkResult)\n",
    "            assert result.count > 0\n",
    "            assert result.std >= 0\n",
    "            assert result.ci_lower <= result.mean <= result.ci_upper\n",
    "\n",
    "    # Test 3: Report generation\n",
    "    print(\"  Testing report generation...\")\n",
    "    report = suite.generate_report()\n",
    "    assert \"Benchmark Report\" in report\n",
    "    assert \"System Information\" in report\n",
    "    assert \"Recommendations\" in report\n",
    "\n",
    "    # Test 4: TinyMLPerf compliance\n",
    "    print(\"  Testing TinyMLPerf compliance...\")\n",
    "    perf = TinyMLPerf(random_seed=42)\n",
    "    perf_results = perf.run_standard_benchmark(models[0], 'keyword_spotting', num_runs=5)\n",
    "\n",
    "    required_keys = ['accuracy', 'mean_latency_ms', 'compliant', 'target_accuracy']\n",
    "    assert all(key in perf_results for key in required_keys)\n",
    "    assert 0 <= perf_results['accuracy'] <= 1\n",
    "    assert perf_results['mean_latency_ms'] > 0\n",
    "\n",
    "    # Test 5: Optimization comparison\n",
    "    print(\"  Testing optimization comparison...\")\n",
    "    comparison_results = compare_optimization_techniques(\n",
    "        models[0], models[1:], datasets[:1]\n",
    "    )\n",
    "\n",
    "    assert 'base_model' in comparison_results\n",
    "    assert 'improvements' in comparison_results\n",
    "    assert 'recommendations' in comparison_results\n",
    "    assert len(comparison_results['improvements']) == 2\n",
    "\n",
    "    # Test 6: Cross-platform compatibility\n",
    "    print(\"  Testing cross-platform compatibility...\")\n",
    "    system_info = {\n",
    "        'platform': platform.platform(),\n",
    "        'processor': platform.processor(),\n",
    "        'python_version': platform.python_version()\n",
    "    }\n",
    "\n",
    "    # Verify system information is captured\n",
    "    benchmark = Benchmark(models[:1], datasets[:1])\n",
    "    assert all(key in benchmark.system_info for key in system_info.keys())\n",
    "\n",
    "    print(\"✅ End-to-end benchmarking workflow works!\")\n",
    "\n",
    "    print(\"\\n\" + \"=\" * 50)\n",
    "    print(\"🎉 ALL TESTS PASSED! Module ready for export.\")\n",
    "    print(\"Run: tito module complete 19\")\n",
    "\n",
    "test_module()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f526d238",
   "metadata": {},
   "outputs": [],
   "source": [
    "if __name__ == \"__main__\":\n",
    "    print(\"🚀 Running Benchmarking module...\")\n",
    "    test_module()\n",
    "    print(\"✅ Module validation complete!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fea34d89",
   "metadata": {
    "cell_marker": "\"\"\""
   },
   "source": [
    "## 🤔 ML Systems Thinking: Benchmarking and Performance Engineering\n",
    "\n",
    "### Question 1: Statistical Confidence in Measurements\n",
    "You implemented BenchmarkResult with confidence intervals for measurements.\n",
    "If you run 20 trials and get mean latency 5.2ms with std dev 0.8ms:\n",
    "- What's the 95% confidence interval for the true mean? [_____ ms, _____ ms]\n",
    "- How many more trials would you need to halve the confidence interval width? _____ total trials\n",
    "\n",
    "### Question 2: Measurement Overhead Analysis\n",
    "Your precise_timer context manager has microsecond precision, but models run for milliseconds.\n",
    "For a model that takes 1ms to execute:\n",
    "- If timer overhead is 10μs, what's the relative error? _____%\n",
    "- At what model latency does timer overhead become negligible (<1%)? _____ ms\n",
    "\n",
    "### Question 3: Benchmark Configuration Trade-offs\n",
    "Your optimize_benchmark_configuration() function tested different warmup/measurement combinations.\n",
    "For a CI/CD pipeline that runs 100 benchmarks per day:\n",
    "- Fast config (3s each): _____ minutes total daily\n",
    "- Accurate config (15s each): _____ minutes total daily\n",
    "- What's the key trade-off you're making? [accuracy/precision/development velocity]\n",
    "\n",
    "### Question 4: TinyMLPerf Compliance Metrics\n",
    "You implemented TinyMLPerf-style standardized benchmarks with target thresholds.\n",
    "If a model achieves 89% accuracy (target: 90%) and 120ms latency (target: <100ms):\n",
    "- Is it compliant? [Yes/No] _____\n",
    "- Which constraint is more critical for edge deployment? [accuracy/latency]\n",
    "- How would you prioritize optimization? [accuracy first/latency first/balanced]\n",
    "\n",
    "### Question 5: Optimization Comparison Analysis\n",
    "Your compare_optimization_techniques() generates recommendations for different use cases.\n",
    "Given three optimized models:\n",
    "- Quantized: 0.8× memory, 2× speed, 0.95× accuracy\n",
    "- Pruned: 0.3× memory, 1.5× speed, 0.98× accuracy\n",
    "- Distilled: 0.6× memory, 1.8× speed, 0.92× accuracy\n",
    "\n",
    "For a mobile app with 50MB model size limit and <100ms latency requirement:\n",
    "- Which optimization offers best memory reduction? _____\n",
    "- Which balances all constraints best? _____\n",
    "- What's the key insight about optimization trade-offs? [no free lunch/specialization wins/measurement guides decisions]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aadfb85c",
   "metadata": {
    "cell_marker": "\"\"\""
   },
   "source": [
    "## 🎯 MODULE SUMMARY: Benchmarking\n",
    "\n",
    "Congratulations! You've built a professional benchmarking system that rivals industry-standard evaluation frameworks!\n",
    "\n",
    "### Key Accomplishments\n",
    "- Built comprehensive benchmarking infrastructure with BenchmarkResult, Benchmark, and BenchmarkSuite classes\n",
    "- Implemented statistical rigor with confidence intervals, variance analysis, and measurement optimization\n",
    "- Created TinyMLPerf-style standardized benchmarks for reproducible cross-system comparison\n",
    "- Developed optimization comparison workflows that generate actionable recommendations\n",
    "- All tests pass ✅ (validated by `test_module()`)\n",
    "\n",
    "### Systems Engineering Insights Gained\n",
    "- **Measurement Science**: Statistical significance requires proper sample sizes and variance control\n",
    "- **Benchmark Design**: Standardized protocols enable fair comparison across different systems\n",
    "- **Trade-off Analysis**: Pareto frontiers reveal optimization opportunities and constraints\n",
    "- **Production Integration**: Automated reporting transforms measurements into engineering decisions\n",
    "\n",
    "### Ready for Systems Capstone\n",
    "Your benchmarking implementation enables the final milestone: a comprehensive systems evaluation comparing CNN vs TinyGPT with quantization, pruning, and performance analysis. This is where all 19 modules come together!\n",
    "\n",
    "Export with: `tito module complete 19`\n",
    "\n",
    "**Next**: Milestone 5 (Systems Capstone) will demonstrate the complete ML systems engineering workflow!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
