← Back to Table of Contents

Chapter 26 β€” Model Optimization Overview

β€œThe best model is not the most accurate one β€” it’s the one that runs on your hardware within your latency budget with acceptable accuracy.”

Why Optimize?

Planned content: Research vs. deployment requirements. A ViT-H has 632M parameters β€” fine for GPU inference, impractical for a smartphone. Optimization bridges this gap. The three axes: accuracy, latency, model size. The tradeoff triangle.

πŸ“Š Planned diagram: The optimization tradeoff triangle β€” accuracy vs. latency vs. model size, with different techniques’ positions labeled.

The Optimization Landscape

Planned content: Four main families: (1) Quantization β€” reduce numerical precision. (2) Pruning/Sparsification β€” remove weights or structure. (3) Knowledge Distillation β€” compress knowledge into smaller model. (4) Neural Architecture Search β€” find efficient architectures. (5) Inference optimization β€” graph compilation, operator fusion.

πŸ“Š Planned diagram: Optimization family tree β€” taxonomy with representative techniques per family.

Hardware Targets

Planned content: Server GPU (A100, H100): FP16/BF16, INT8, FP8. Edge GPU (Jetson Orin): INT8, INT4. Mobile GPU (Adreno, Apple Neural Engine): FP16, INT8. CPU (x86, ARM): INT8, sparse. FPGA: custom bitwidth. Each target has different optimal optimization strategy.

πŸ“Š Planned table: Hardware targets Γ— optimization compatibility matrix β€” which techniques work well on which hardware.

Profiling and Bottleneck Analysis

Planned content: Before optimizing, profile! Latency breakdown: memory-bound vs. compute-bound ops. PyTorch Profiler. Roofline model for CV models. Memory bandwidth vs. arithmetic intensity. FLOPs vs. latency (why more FLOPs β‰  more latency). Batch size effects. Layer-wise latency analysis.

πŸ“Š Planned diagram: Roofline model for a ResNet-50 layer β€” plotting arithmetic intensity vs. performance, roof = max compute or max bandwidth.

Optimization Pipeline

Planned content: Typical pipeline: train full model β†’ profile β†’ quantize PTQ β†’ evaluate accuracy β†’ if not enough: QAT β†’ prune β†’ distill smaller model β†’ export to ONNX/TensorRT β†’ benchmark on target hardware.

πŸ“Š Planned diagram (flowchart): Model optimization decision tree β€” start with PTQ, evaluate accuracy drop, if too large β†’ try QAT β†’ if still not enough β†’ distillation or architecture replacement.

FLOP Counting

Planned content: FLOPs (Floating Point Operations): 2 Γ— M Γ— N Γ— K for matmul [M, K] Γ— [K, N]. FLOPs for Conv2d: 2 Γ— C_in Γ— C_out Γ— kΒ² Γ— H_out Γ— W_out. Tools: thop, fvcore, torchinfo. MACs vs. FLOPs (1 MAC = 2 FLOPs). Why FLOPs are a poor proxy for latency.

1
2
3
4
5
6
7
8
9
from thop import profile
import torch
from torchvision.models import resnet50

model = resnet50()
x = torch.randn(1, 3, 224, 224)  # [1, 3, 224, 224]
macs, params = profile(model, inputs=(x,))
print(f"MACs: {macs/1e9:.2f}G, Params: {params/1e6:.2f}M")
# MACs: 4.09G, Params: 25.56M

Next: Chapter 27 β€” Quantization β†’


Last updated: May 2026