Model Compression Techniques Explained
How pruning, quantization, distillation and other compression techniques shrink neural networks for faster, cheaper inference.
Fundamentals
Motivation
Compression reduces memory and latency while preserving accuracy for edge and production deployments.
Pruning
Unstructured vs structured pruning; magnitude-based and gradient-based approaches; retraining for recovery.
Quantization
Post-training vs quantization-aware training; per-tensor vs per-channel scales; int8, fp16, mixed precision.
Distillation
Teacher-student training; soft labels and intermediate feature matching; task-specific strategies.
Deployment
Backends and toolchains: ONNX Runtime, TensorRT, TFLite; calibration and validation pipelines.
How the Algorithm Works
Pruning Loop
- Train baseline to convergence
- Compute saliencies (magnitude/gradients)
- Prune by mask (structured/unstructured)
- Fine-tune and evaluate
- Iterate until target sparsity or KPI met
Quantization Loop
- Select scheme (PTQ/QAT; per-tensor/per-channel)
- Calibrate scales with representative data
- Apply quantization; optionally QAT
- Validate KPIs; adjust bitwidth/observers
Distillation Loop
- Choose teacher/student and loss (soft labels/features)
- Train student with distillation objective
- Evaluate; iterate with schedule and temperature
Real-World Applications
Edge Deployment
Latency and memory constraints dominate; quantization and structured pruning crucial for ARM/embedded targets.
Cloud Inference
Throughput and cost optimization; distillation to smaller backbones; GPU/CPU mixed serving.
Monitoring
Track accuracy, latency, memory, and drift; rollback on KPI regressions.
Best Practices
Checklist
- Establish KPIs and guardrails before compression
- Sequence: prune → distill → quantize (adjust as needed)
- Benchmark on target hardware
- Track masks, scales, and artifacts
- Validate fairness and calibration
- Govern with audit logs and approvals
Anti-Patterns
- Compressing without a strong baseline
- Relying on proxy latency only
- Skipping QAT where PTQ fails
- No rollback plan
- Untracked changes to toolchains
Evaluation
Metrics
- Accuracy/PR-AUC; calibration (ECE)
- Latency (P50/P95), throughput
- Memory footprint and model size
- Energy (joules/inference) where applicable
Protocols
- Benchmark on target hardware; avoid proxy-only measures
- Golden tests and numerical tolerances
- Fairness and drift checks
Worked Examples
Pruning + Fine-Tuning
# Iterative magnitude pruning with fine-tuning cycles
PTQ + QAT
# Post-training quantization then short QAT refinement
Knowledge Distillation
# Teacher-student with KL loss and intermediate layers
Implementation
PyTorch Pruning
# torch.nn.utils.prune examples for structured/unstructured
Quantization
# PyTorch QAT/PTQ; TFLite converter; TensorRT INT8 calibration
Distillation
# Teacher-student training with KL loss and feature matching
Deployment
- Export ONNX/TFLite; validate with golden tests
- Benchmark latency on target hardware
- Track artifacts and calibration data
The Math Behind It
Sparsity
Sparsity induces regularization; structured sparsity preserves tensor shapes enabling efficient kernels.
Quantization Error
Uniform quantization bounds error by half-step; per-channel scaling reduces variance for convolutional weights.
Distillation Losses
KL divergence with temperature; intermediate feature matching stabilizes training.
Key Parameters
Constraints
Latency, memory, and accuracy guardrails; layer-wise exceptions for sensitive blocks.
Training Strategy
Fine-Tuning after Pruning
- Lower LR, longer patience
- Recover accuracy over 10–30% of baseline epochs
QAT
- Insert fake-quant modules and observers
- Train with gradually reduced LR
Monitoring
- KPIs: accuracy, latency, memory, energy
- Per-layer sensitivity and outliers
Frequently Asked Questions
When to prune vs quantize?
Combine for best trade-offs.
Accuracy loss?
Use QAT and fine-tuning.
Hardware constraints?
Choose formats per target.
KPIs?
Latency, memory, energy, accuracy.
Governance?
Track changes and verify metrics.
Security?
Sanitize exports; validate runtimes.
Reproducibility?
Keep seeds and tool versions.