Model Compression Techniques Explained

How pruning, quantization, distillation and other compression techniques shrink neural networks for faster, cheaper inference.

▶ Open the simulation

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

  1. Train baseline to convergence
  2. Compute saliencies (magnitude/gradients)
  3. Prune by mask (structured/unstructured)
  4. Fine-tune and evaluate
  5. Iterate until target sparsity or KPI met

Quantization Loop

  1. Select scheme (PTQ/QAT; per-tensor/per-channel)
  2. Calibrate scales with representative data
  3. Apply quantization; optionally QAT
  4. Validate KPIs; adjust bitwidth/observers

Distillation Loop

  1. Choose teacher/student and loss (soft labels/features)
  2. Train student with distillation objective
  3. 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.

What did you find?

Add reproduction steps (optional)