Skip to content

2300: Framework Engineering - Practice Exercises

⏱ 2 min read·📅 Last updated: 2026-10-09

Hands-on exercises to reinforce your learning.


Contents


Exercise 1: Model Abstraction (30 minutes)

Task

Implement a complete model abstraction layer that supports both PyTorch and TensorFlow.

Requirements

  1. Create BaseModel abstract class
  2. Implement PyTorchModel
  3. Implement TensorFlowModel
  4. Write framework-agnostic training loop

Prerequisite: the solution imports both torch and tensorflow. Install TensorFlow with uv pip install tensorflow if you only have PyTorch.

Starter Code

from abc import ABC, abstractmethod
from typing import Any

class BaseModel(ABC):
    def __init__(self, config: dict[str, Any]):
        self.config = config

    @abstractmethod
    def forward(self, x):
        pass

    @abstractmethod
    def train_step(self, batch):
        pass

# SOLUTION PROVIDED BELOW - See complete implementation
# The PyTorchModel, TensorFlowModel, and train_model implementations
# are provided in the Solution section with full code

Solution

# The solution block is standalone-complete (it re-imports torch/tf/np),
# so it also re-imports the abc names the starter block had - BaseModel
# below subclasses ABC and uses @abstractmethod
from abc import ABC, abstractmethod
import torch
import torch.nn as nn
import tensorflow as tf
import numpy as np
from typing import Any

# Base Model (Complete)
class BaseModel(ABC):
    """Abstract base class for framework-agnostic models."""

    def __init__(self, config: dict[str, Any]):
        self.config = config
        self.metrics_history = []

    @abstractmethod
    def forward(self, x):
        """Forward pass through the model."""
        pass

    @abstractmethod
    def train_step(self, batch):
        """Single training step."""
        pass

    def save(self, path: str):
        """Save model checkpoint."""
        raise NotImplementedError

    def load(self, path: str):
        """Load model checkpoint."""
        raise NotImplementedError

# PyTorch Model (Complete)
class PyTorchModel(BaseModel):
    """PyTorch implementation of BaseModel."""

    def __init__(self, config: dict[str, Any]):
        super().__init__(config)

        # Build model
        self.model = nn.Sequential(
            nn.Linear(config["input_size"], config["hidden_size"]),
            nn.ReLU(),
            nn.Dropout(config.get("dropout", 0.1)),
            nn.Linear(config["hidden_size"], config["hidden_size"] // 2),
            nn.ReLU(),
            nn.Linear(config["hidden_size"] // 2, config["output_size"])
        )

        # Setup optimizer
        self.optimizer = torch.optim.Adam(
            self.model.parameters(),
            lr=config.get("lr", 0.001)
        )

        # Setup loss
        self.criterion = nn.MSELoss()

        # Device
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        self.model.to(self.device)

    def forward(self, x):
        """Forward pass."""
        if isinstance(x, np.ndarray):
            x = torch.from_numpy(x).float()
        return self.model(x.to(self.device))

    def train_step(self, batch):
        """Single training step."""
        self.model.train()

        # Get data
        x = batch["x"]
        y = batch["y"]

        # Convert to tensors if needed
        if isinstance(x, np.ndarray):
            x = torch.from_numpy(x).float()
        if isinstance(y, np.ndarray):
            y = torch.from_numpy(y).float()

        x, y = x.to(self.device), y.to(self.device)

        # Forward pass
        self.optimizer.zero_grad()
        predictions = self.model(x)
        loss = self.criterion(predictions, y)

        # Backward pass
        loss.backward()
        self.optimizer.step()

        return {"loss": loss.item()}

    def save(self, path: str):
        """Save model."""
        torch.save({
            'model_state_dict': self.model.state_dict(),
            'optimizer_state_dict': self.optimizer.state_dict(),
            'config': self.config
        }, path)

    def load(self, path: str):
        """Load model."""
        # weights_only=True is the torch >= 2.6 default; passing it
        # explicitly keeps the checkpoint load safe and version-stable
        checkpoint = torch.load(path, weights_only=True)
        self.model.load_state_dict(checkpoint['model_state_dict'])
        self.optimizer.load_state_dict(checkpoint['optimizer_state_dict'])

# TensorFlow Model (Complete)
class TensorFlowModel(BaseModel):
    """TensorFlow implementation of BaseModel."""

    def __init__(self, config: dict[str, Any]):
        super().__init__(config)

        # Build model
        self.model = tf.keras.Sequential([
            tf.keras.layers.Dense(
                config["hidden_size"],
                activation="relu",
                input_shape=(config["input_size"],)
            ),
            tf.keras.layers.Dropout(config.get("dropout", 0.1)),
            tf.keras.layers.Dense(config["hidden_size"] // 2, activation="relu"),
            tf.keras.layers.Dense(config["output_size"])
        ])

        # Compile model
        self.model.compile(
            optimizer=tf.keras.optimizers.Adam(learning_rate=config.get("lr", 0.001)),
            loss="mse"
        )

    def forward(self, x):
        """Forward pass."""
        return self.model(x, training=False)

    def train_step(self, batch):
        """Single training step."""
        x = batch["x"]
        y = batch["y"]

        # Train on batch
        result = self.model.fit(x, y, verbose=0, epochs=1)

        return {"loss": result.history["loss"][0]}

    def save(self, path: str):
        """Save model."""
        self.model.save(path)

    def load(self, path: str):
        """Load model."""
        self.model = tf.keras.models.load_model(path)

# Framework-agnostic training function (Complete)
def train_model(model: BaseModel, dataloader, epochs: int, validation_data=None):
    """
    Train a model framework-agnostically.

    Args:
        model: BaseModel instance
        dataloader: Data loader
        epochs: Number of training epochs
        validation_data: The validation data, or None

    Returns:
        Training history
    """
    print(f"Training model for {epochs} epochs...")

    for epoch in range(epochs):
        total_loss = 0
        num_batches = 0

        for batch in dataloader:
            result = model.train_step(batch)
            total_loss += result["loss"]
            num_batches += 1

        avg_loss = total_loss / num_batches
        model.metrics_history.append({"epoch": epoch, "loss": avg_loss})

        print(f"Epoch {epoch+1}/{epochs}: Loss = {avg_loss:.4f}")

    return model.metrics_history

# Verification (Complete)
if __name__ == "__main__":
    print("=== Testing Model Abstraction ===\n")

    # Create dummy data
    config = {
        "input_size": 10,
        "hidden_size": 32,
        "output_size": 1,
        "lr": 0.001
    }

    # Create dummy batches
    batches = [
        {"x": np.random.randn(16, 10), "y": np.random.randn(16, 1)}
        for _ in range(10)
    ]

    # Test PyTorch model
    print("Testing PyTorch Model...")
    pytorch_model = PyTorchModel(config)
    history = train_model(pytorch_model, batches, epochs=3)
    print("✓ PyTorch model trained successfully\n")

    # Test TensorFlow model
    print("Testing TensorFlow Model...")
    tf_model = TensorFlowModel(config)
    history = train_model(tf_model, batches, epochs=3)
    print("✓ TensorFlow model trained successfully\n")

    # Round-trip a checkpoint through save/load
    pytorch_model.save("pytorch_model.pt")
    fresh = PyTorchModel(config)
    fresh.load("pytorch_model.pt")
    print("✓ Checkpoint save/load round-trip works\n")

    print("All tests passed!")

# Expected output:
# Both PyTorch and TensorFlow models train successfully and the
# checkpoint round-trips through save/load.
# Per-epoch loss decreases (PyTorch side reaches a far lower loss than
# TensorFlow's single-epoch-per-fit first steps). Exact values are not
# reproducible as printed - the batches come from unseeded
# np.random.randn - so treat any specific numbers as run-dependent.

Verification

# Test with PyTorch
config = {"input_size": 10, "hidden_size": 32, "output_size": 1, "lr": 0.001}
model = PyTorchModel(config)
# Should work!

# Test with TensorFlow
model = TensorFlowModel(config)
# Should also work!

Exercise 2: Plugin System (45 minutes)

Task

Create a plugin system for custom metrics with registration, discovery, and dynamic loading.

Requirements

  1. Create MetricRegistry class
  2. Implement @metric decorator
  3. Register 3 built-in metrics (Accuracy, Precision, F1)
  4. Support dynamic loading from file

Solution

from typing import Any
import importlib.util
import os
import json
import torch

# Complete MetricRegistry Implementation
class MetricRegistry:
    """Complete metric registry system with plugin support."""

    def __init__(self, name: str):
        self.name = name
        self._metrics = {}
        self._metadata = {}
        self._hooks = {"before_compute": [], "after_compute": []}

    def register(self, name: str | None = None, **metadata):
        """
        Decorator to register a metric.

        Args:
            name: Metric name (defaults to class name)
            **metadata: Additional metadata

        Returns:
            Decorator function
        """
        def decorator(cls):
            metric_name = name or cls.__name__
            self._metrics[metric_name] = cls
            self._metadata[metric_name] = {
                "class": cls.__name__,
                "module": cls.__module__,
                **metadata
            }
            print(f"✓ Registered metric: {metric_name}")
            return cls
        return decorator

    def get(self, name: str):
        """Get a metric class by name."""
        return self._metrics.get(name)

    def create(self, name: str, **kwargs):
        """Create a metric instance."""
        metric_class = self.get(name)
        if metric_class is None:
            raise ValueError(f"Metric '{name}' not found")
        return metric_class(**kwargs)

    def list_all(self) -> list[str]:
        """List all registered metrics."""
        return list(self._metrics.keys())

    def get_metadata(self, name: str) -> dict[str, Any]:
        """Get metadata for a metric."""
        return self._metadata.get(name, {})

    def load_from_file(self, filepath: str, metric_names: list[str] | None = None):
        """
        Dynamically load metrics from a Python file.

        Args:
            filepath: Path to Python file
            metric_names: The metric names to load (None = all)

        Note:
            Plugins loaded this way enter the registry directly, so they
            carry no decorator metadata - get_metadata() returns {} for
            them. Only the @register decorator path records metadata.
        """
        spec = importlib.util.spec_from_file_location("metrics_module", filepath)
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)

        # Find metric classes
        loaded = []
        for attr_name in dir(module):
            attr = getattr(module, attr_name)
            if isinstance(attr, type) and hasattr(attr, '__metric_name__'):
                metric_name = attr.__metric_name__
                if metric_names is None or metric_name in metric_names:
                    self._metrics[metric_name] = attr
                    loaded.append(metric_name)

        return loaded

    def register_hook(self, hook_type: str, func):
        """Register a callback hook."""
        if hook_type in self._hooks:
            self._hooks[hook_type].append(func)

    def save_registry(self, path: str):
        """Save registry state to JSON."""
        state = {
            "metrics": {
                name: meta
                for name, meta in self._metadata.items()
            }
        }
        with open(path, 'w', encoding="utf-8") as f:
            json.dump(state, f, indent=2)

# Create registry instance
METRIC_REGISTRY = MetricRegistry("metrics")

# Register 3 built-in metrics
# torch is imported at module top level on purpose: the metric classes'
# update() methods reference torch.Tensor. Importing it only inside the
# __main__ demo would work when run as a script but crash with NameError
# the moment another module imports this block and calls update().
@METRIC_REGISTRY.register("accuracy", description="Classification accuracy")
class Accuracy:
    """Accuracy metric."""

    __metric_name__ = "accuracy"

    def __init__(self):
        self.correct = 0
        self.total = 0

    def update(self, predictions, targets):
        """Update metric with new batch."""
        if isinstance(predictions, torch.Tensor):
            predictions = predictions.cpu().numpy()
        if isinstance(targets, torch.Tensor):
            targets = targets.cpu().numpy()

        self.correct += (predictions == targets).sum()
        self.total += len(targets)

    def compute(self):
        """Compute final metric value."""
        if self.total == 0:
            return 0.0
        return float(self.correct) / self.total

    def reset(self):
        """Reset metric state."""
        self.correct = 0
        self.total = 0

@METRIC_REGISTRY.register("precision", description="Precision score")
class Precision:
    """Precision metric."""

    __metric_name__ = "precision"

    def __init__(self, positive_label=1):
        self.true_positives = 0
        self.false_positives = 0
        self.positive_label = positive_label

    def update(self, predictions, targets):
        """Update metric with new batch."""
        if isinstance(predictions, torch.Tensor):
            predictions = predictions.cpu().numpy()
        if isinstance(targets, torch.Tensor):
            targets = targets.cpu().numpy()

        self.true_positives += ((predictions == self.positive_label) & (targets == self.positive_label)).sum()
        self.false_positives += ((predictions == self.positive_label) & (targets != self.positive_label)).sum()

    def compute(self):
        """Compute final metric value."""
        if self.true_positives + self.false_positives == 0:
            return 0.0
        return self.true_positives / (self.true_positives + self.false_positives)

    def reset(self):
        """Reset metric state."""
        self.true_positives = 0
        self.false_positives = 0

@METRIC_REGISTRY.register("f1", description="F1 score")
class F1Score:
    """F1 Score metric."""

    __metric_name__ = "f1"

    def __init__(self, positive_label=1):
        self.precision = Precision(positive_label)
        self.recall = Recall(positive_label)
        self.positive_label = positive_label

    def update(self, predictions, targets):
        """Update metric with new batch."""
        self.precision.update(predictions, targets)
        self.recall.update(predictions, targets)

    def compute(self):
        """Compute final metric value."""
        p = self.precision.compute()
        r = self.recall.compute()
        if p + r == 0:
            return 0.0
        return 2 * p * r / (p + r)

    def reset(self):
        """Reset metric state."""
        self.precision.reset()
        self.recall.reset()

# Helper: Recall metric (needed for F1)
# Not registered - F1 composes it internally. The @register path is for
# metrics users create by name; internal collaborators stay private.
class Recall:
    """Recall metric."""

    __metric_name__ = "recall"

    def __init__(self, positive_label=1):
        self.true_positives = 0
        self.false_negatives = 0
        self.positive_label = positive_label

    def update(self, predictions, targets):
        """Update metric with new batch."""
        if isinstance(predictions, torch.Tensor):
            predictions = predictions.cpu().numpy()
        if isinstance(targets, torch.Tensor):
            targets = targets.cpu().numpy()

        self.true_positives += ((predictions == self.positive_label) & (targets == self.positive_label)).sum()
        self.false_negatives += ((predictions != self.positive_label) & (targets == self.positive_label)).sum()

    def compute(self):
        """Compute final metric value."""
        if self.true_positives + self.false_negatives == 0:
            return 0.0
        return self.true_positives / (self.true_positives + self.false_negatives)

    def reset(self):
        """Reset metric state."""
        self.true_positives = 0
        self.false_negatives = 0

# Verification (Complete)
if __name__ == "__main__":
    import tempfile

    print("=== Testing Metric Registry ===\n")

    # Test registry
    print("Available metrics:", METRIC_REGISTRY.list_all())

    # Test metric usage
    predictions = torch.tensor([1, 0, 1, 1, 0, 1])
    targets = torch.tensor([1, 0, 0, 1, 0, 1])

    # Test accuracy
    accuracy = METRIC_REGISTRY.create("accuracy")
    accuracy.update(predictions, targets)
    print(f"\nAccuracy: {accuracy.compute():.4f}")

    # Test precision
    precision = METRIC_REGISTRY.create("precision", positive_label=1)
    precision.update(predictions, targets)
    print(f"Precision: {precision.compute():.4f}")

    # Test F1
    f1 = METRIC_REGISTRY.create("f1", positive_label=1)
    f1.update(predictions, targets)
    print(f"F1 Score: {f1.compute():.4f}")

    # Requirement 4 demonstrated: dynamic loading from file.
    # Write a plugin file, then load its metric classes into the registry.
    plugin_src = '''
import torch

class Specificity:
    """True negative rate - loaded dynamically from a plugin file."""

    __metric_name__ = "specificity"

    def __init__(self, positive_label=1):
        self.positive_label = positive_label
        self.true_negatives = 0
        self.false_positives = 0

    def update(self, predictions, targets):
        if isinstance(predictions, torch.Tensor):
            predictions = predictions.cpu().numpy()
        if isinstance(targets, torch.Tensor):
            targets = targets.cpu().numpy()
        self.true_negatives += ((predictions != self.positive_label) & (targets != self.positive_label)).sum()
        self.false_positives += ((predictions == self.positive_label) & (targets != self.positive_label)).sum()

    def compute(self):
        denom = self.true_negatives + self.false_positives
        if denom == 0:
            return 0.0
        return float(self.true_negatives) / denom

    def reset(self):
        self.true_negatives = 0
        self.false_positives = 0
'''
    plugin_path = os.path.join(tempfile.mkdtemp(), "custom_metrics.py")
    with open(plugin_path, "w", encoding="utf-8") as f:
        f.write(plugin_src)

    loaded = METRIC_REGISTRY.load_from_file(plugin_path)
    print(f"\nDynamically loaded from file: {loaded}")

    specificity = METRIC_REGISTRY.create("specificity", positive_label=1)
    specificity.update(predictions, targets)
    print(f"Specificity: {specificity.compute():.4f}")

    # Get metadata
    print("\nMetric metadata:")
    for name in METRIC_REGISTRY.list_all():
        meta = METRIC_REGISTRY.get_metadata(name)
        print(f"  {name}: {meta.get('description', 'N/A')}")

    # Persist registry state to JSON
    state_path = os.path.join(tempfile.mkdtemp(), "registry.json")
    METRIC_REGISTRY.save_registry(state_path)
    print(f"\n✓ All tests passed! (registry state written to {os.path.basename(state_path)})")

# Expected output (verified by running the block):
# ✓ Registered metric: accuracy
# ✓ Registered metric: precision
# ✓ Registered metric: f1                (printed at decoration time)
# Available metrics: ['accuracy', 'precision', 'f1']
# Accuracy: 0.8333     (5 of 6 predictions match)
# Precision: 0.7500    (3 true positives, 1 false positive)
# F1 Score: 0.8571     (recall is 1.0 - no positive was missed)
# Dynamically loaded from file: ['specificity']
# Specificity: 0.6667  (2 true negatives, 1 false positive)
# Metric metadata:
#   accuracy: Classification accuracy
#   precision: Precision score
#   f1: F1 score
#   specificity: N/A     (loaded directly, no decorator metadata)
# ✓ All tests passed! (registry state written to registry.json)

Exercise 3: Batching Server (60 minutes)

Task

Implement a production-ready batching server with dynamic batching, priority support, and statistics.

Solution

import time
import threading
import uuid
from typing import Any
from queue import PriorityQueue
from dataclasses import dataclass, field
from enum import Enum

class Priority(Enum):
    """Request priority levels."""
    HIGH = 1
    MEDIUM = 2
    LOW = 3

@dataclass(order=True)
class Request:
    """Request data structure.

    order=True makes PriorityQueue sort instances as (priority, timestamp)
    tuples and pop the LOWEST first: HIGH (1) jumps MEDIUM (2), and equal
    priorities keep FIFO order because earlier monotonic timestamps are
    smaller.
    """
    priority: int
    timestamp: float
    id: str = field(compare=False)
    input_data: Any = field(compare=False, default=None)
    result: Any = field(compare=False, default=None)
    completed: bool = field(compare=False, default=False)

class BatchingServer:
    """Production-ready batching server."""

    def __init__(self, model, max_batch_size=32, timeout_ms=50):
        """
        Initialize batching server.

        Args:
            model: Model to run inference on
            max_batch_size: Maximum batch size
            timeout_ms: Timeout in milliseconds
        """
        self.model = model
        self.max_batch_size = max_batch_size
        self.timeout_ms = timeout_ms
        self.timeout_sec = timeout_ms / 1000.0

        # Request queue
        self.queue = PriorityQueue()
        self.pending_requests: dict[str, Request] = {}
        self.results: dict[str, Any] = {}

        # Statistics
        self.stats = {
            "total_requests": 0,
            "total_batches": 0,
            "avg_batch_size": 0.0,
            "avg_latency": 0.0,
            "total_latency": 0.0
        }

        # Control
        self.running = False
        self.worker_thread = None
        self.lock = threading.Lock()

    def add_request(self, input_data, priority=Priority.MEDIUM) -> str:
        """
        Add a request to the queue.

        Args:
            input_data: Input data for the model
            priority: Request priority

        Returns:
            Request ID
        """
        request_id = str(uuid.uuid4())
        request = Request(
            priority=priority.value,
            # monotonic clock, not wall clock: an NTP correction or manual
            # clock change mid-run would otherwise corrupt both the queue
            # ordering and the deadline arithmetic below
            timestamp=time.monotonic(),
            id=request_id,
            input_data=input_data
        )

        with self.lock:
            self.queue.put(request)
            self.pending_requests[request_id] = request
            self.stats["total_requests"] += 1

        return request_id

    def get_result(self, request_id: str, timeout=5.0) -> Any:
        """
        Get result for a request.

        Args:
            request_id: Request ID
            timeout: Timeout in seconds

        Returns:
            Request result
        """
        start_time = time.monotonic()
        while time.monotonic() - start_time < timeout:
            with self.lock:
                if request_id in self.results:
                    return self.results.pop(request_id)
            time.sleep(0.01)

        raise TimeoutError(f"Request {request_id} timed out")

    def _check_and_process(self):
        """Check if batch is ready and process it."""
        batch = []
        current_time = time.monotonic()

        with self.lock:
            # Collect requests for batch
            while not self.queue.empty() and len(batch) < self.max_batch_size:
                request = self.queue.get()
                if request.id in self.pending_requests:
                    batch.append(request)

                    # Check timeout
                    if current_time - request.timestamp > self.timeout_sec:
                        break

        # Process batch if we have requests
        if batch:
            self._process_batch(batch)

    def _process_batch(self, batch: list[Request]):
        """
        Process a batch of requests.

        Args:
            batch: The requests to process
        """
        start_time = time.monotonic()

        # Sort by priority
        batch.sort(key=lambda r: r.priority)

        # Prepare batch data
        batch_inputs = [r.input_data for r in batch]

        # Run inference (mock)
        batch_results = self._run_inference(batch_inputs)

        # Store results
        with self.lock:
            for request, result in zip(batch, batch_results):
                request.result = result
                request.completed = True
                self.results[request.id] = result
                del self.pending_requests[request.id]

            # Update statistics
            self.stats["total_batches"] += 1
            batch_size = len(batch)
            # Running mean over batches: dividing by total_requests
            # instead of total_batches is not an average of anything -
            # 10 requests in batches of 4/4/2 would print 0.71 instead
            # of the true mean batch size 3.33
            self.stats["avg_batch_size"] = (
                (self.stats["avg_batch_size"] * (self.stats["total_batches"] - 1) + batch_size)
                / self.stats["total_batches"]
            )

            latency = time.monotonic() - start_time
            self.stats["total_latency"] += latency
            self.stats["avg_latency"] = self.stats["total_latency"] / self.stats["total_batches"]

    def _run_inference(self, batch_inputs: list[Any]) -> list[Any]:
        """
        Run inference on batch (mock implementation).

        Args:
            batch_inputs: The inputs

        Returns:
            List of results
        """
        # Mock inference - replace with actual model call
        time.sleep(0.01)
        # Result labels carry the BATCH position, not the original input
        # number: input_9 served in the first batch receives result_0.
        return [f"result_{i}" for i in range(len(batch_inputs))]

    def start(self):
        """Start the batching server."""
        self.running = True
        self.worker_thread = threading.Thread(target=self._worker_loop, daemon=True)
        self.worker_thread.start()

    def _worker_loop(self):
        """Worker thread loop."""
        while self.running:
            self._check_and_process()
            time.sleep(0.001)  # 1ms

    def shutdown(self):
        """Shutdown the server gracefully."""
        print("Shutting down batching server...")
        self.running = False

        # Process remaining requests
        while not self.queue.empty():
            self._check_and_process()

        # Wait for worker thread
        if self.worker_thread:
            self.worker_thread.join(timeout=5.0)

        print(f"Shutdown complete. Processed {self.stats['total_requests']} requests.")

    def get_stats(self) -> dict[str, Any]:
        """Get server statistics."""
        with self.lock:
            return self.stats.copy()

# Verification (Complete)
if __name__ == "__main__":
    print("=== Testing Batching Server ===\n")

    # Create server
    server = BatchingServer(model=None, max_batch_size=4, timeout_ms=50)
    server.start()

    # Add requests
    request_ids = []
    for i in range(10):
        priority = Priority.HIGH if i % 3 == 0 else Priority.MEDIUM
        req_id = server.add_request(f"input_{i}", priority=priority)
        request_ids.append(req_id)

    # Get results
    results = []
    for req_id in request_ids:
        result = server.get_result(req_id, timeout=2.0)
        results.append(result)
        print(f"Request {req_id[:8]}: {result}")

    # Get statistics
    stats = server.get_stats()
    print(f"\nStatistics:")
    print(f"  Total requests: {stats['total_requests']}")
    print(f"  Total batches: {stats['total_batches']}")
    print(f"  Avg batch size: {stats['avg_batch_size']:.2f}")
    print(f"  Avg latency: {stats['avg_latency']*1000:.2f}ms")

    # Shutdown
    server.shutdown()

    print("\n✓ All tests passed!")

# Expected output (structure verified by running the block; the
# 8-character request-id prefixes are uuid4 fragments and differ every run):
# === Testing Batching Server ===
#
# Request 3f2a81bc: result_0      <- HIGH-priority requests (i = 0, 3, 6, 9)
# Request 9d1c04e7: result_1         form the first batch of 4
# Request 51b7e2aa: result_2
# Request c8e93f10: result_3
# Request ...: result_0           <- MEDIUM requests batch as 4 + 2
# ...
#
# Statistics:
#   Total requests: 10
#   Total batches: 3
#   Avg batch size: 3.33          (4 + 4 + 2 requests across 3 batches)
#   Avg latency: 10.x xms         (~10ms mock inference per batch)
# Shutting down batching server...
# Shutdown complete. Processed 10 requests.
#
# ✓ All tests passed!
#
# Timing caveat: batch boundaries depend on when the 1ms worker loop
# runs relative to the request arrivals. With all 10 requests queued
# within a few milliseconds the 4/4/2 split above is the typical
# outcome, but a slow machine can produce different splits (e.g. 4/3/3)
# and correspondingly different avg_batch_size. The invariants that
# always hold: every request gets a result, total_requests == 10, and
# avg_batch_size equals 10 / total_batches.

Summary

This practice guide provides complete, production-ready implementations for:

  1. Model Abstraction: Framework-agnostic model interface (PyTorch + TensorFlow) with checkpoint save/load round-trip
  2. Plugin System: Extensible metrics registry with decorators, metadata, JSON persistence, and true dynamic loading from file
  3. Batching Server: Production inference server with priority queuing, monotonic-clock deadlines, and honest running-average statistics

Expected Learning Outcomes:

  • Build framework-agnostic ML systems
  • Implement plugin architectures — including dynamic loading, not just decoration
  • Create production inference servers with correct timeout and priority semantics

References

External References


Next Steps

  1. Check yourself: take the 2300: Framework Engineering - Quiz — the coding questions there mirror these exercises at smaller scale.
  2. Apply it: LAB-007: Production RAG System and LAB-009: Production Deployment use the batching and deployment patterns from Exercises 1 and 3.
  3. Next Module: 2400: LLM Pretraining

Related: 2300: Framework Engineering · 2306: Building a Production Framework · 1402: vLLM and TGI High-Concurrency Inference

Experiment: No EXP_23xx exists yet — nearest relevant: EXP_1404: vLLM Production Tuning Experiments (dynamic batching and throughput tuning in a real serving engine, Exercise 3's themes at production scale).