How do you make a model smaller and faster without losing much accuracy?
Written by the mlprep team · Updated
You have a large model with great accuracy, but it's too slow and memory-intensive for production. How do you make it smaller and faster without losing much accuracy?
Model compression is a core MLOps skill — the model that wins in research rarely fits production latency and memory budgets. The three main tools are distillation, quantization, and pruning. They're complementary and often combined.
Knowledge Distillation#
Train a smaller "student" model to mimic the outputs of the larger "teacher" model. Instead of training on hard labels (0 or 1), the student trains on the teacher's soft probability distributions — which carry more information. A teacher that predicts 0.85 cat / 0.10 dog / 0.05 other tells the student something about feature similarity that a hard label doesn't.
The loss combines task loss and distillation loss:
L = α · L_task(student, labels) + (1-α) · L_distill(student_logits, teacher_logits / T)
Temperature T > 1 softens the teacher's distribution, amplifying the signal in small probabilities. α controls the blend.
Results: DistilBERT is 40% smaller, 60% faster, and retains 97% of BERT's performance on GLUE. The student can be a completely different architecture — you're not constrained to the teacher's design.
Quantization#
Reduce weight precision from float32 to float16 or int8. Float16 is almost free (1-2% accuracy drop, 1.5-2x speedup, half the memory). Int8 requires calibration (running representative data to set activation scales) and may need quantization-aware training for sensitive models.
Works best for models where memory bandwidth is the bottleneck (large transformers on GPU) and less well for models with wide dynamic ranges in activations (some RNNs, models with unusual activation functions).
Pruning#
Remove weights below a threshold — set them to zero. Unstructured pruning (individual weights) can achieve 90%+ sparsity with minimal accuracy loss, but sparse matrix operations on modern hardware don't actually speed up unless you use specialized sparse kernels (e.g., NVIDIA's cuSPARSE).
Structured pruning removes entire components: attention heads, neurons, or transformer layers. This maps directly to speedup because you're actually shrinking the computation graph. Removing 30% of attention heads from BERT can produce a 1.3x speedup with 3-5% accuracy drop.
Combining techniques#
The standard playbook for compressing a large transformer:
- Distill to a smaller architecture (halve depth or width)
- Quantize the student to float16
- Prune redundant attention heads if latency target isn't met
Each step compounds: a 2x distillation + 2x quantization + 20% pruning can realistically give 5-8x speedup with acceptable accuracy loss.
The measurement discipline#
Always measure on your target hardware with your serving framework. A 2x PyTorch speedup doesn't always translate to a 2x Triton serving speedup. And always evaluate accuracy on a held-out test set after each compression step — compression can have surprising interactions with specific input distributions.
You mentioned structured pruning removes attention heads — how do you decide which heads to prune, and is there a principled way to measure head importance?
What head importance means#
An attention head is important if removing it significantly degrades performance on your task. You want to identify heads whose contribution is small, redundant, or task-irrelevant.
Gradient-based importance (most principled)#
Compute the gradient of the loss with respect to each head's output, weighted by the activation magnitude. The "importance score" for head h:
I_h = |∂L/∂h_out · h_out|
Run this over a calibration set and average. Heads with low importance scores contribute little to the loss — pruning them changes the output minimally.
This is the approach used in "Are Sixteen Heads Really Better than One?" (Michel et al., 2019), which showed that in BERT, most heads can be pruned at test time with minimal accuracy drop, and some heads are redundant even at training time.
Taylor expansion approximation#
More computationally efficient: approximate the change in loss from zeroing a head using a first-order Taylor expansion:
ΔL ≈ (∂L/∂h) · (-h)
Score = |ΔL| — how much does zeroing this head change the loss? Sort heads by score, prune the lowest-scoring ones iteratively, fine-tune between pruning steps.
Iterative pruning + fine-tuning#
Don't prune all at once. Prune the bottom N% of heads, fine-tune for a few steps to recover, then prune the next N%. This iterative process consistently outperforms one-shot pruning because the model adapts to each removal before the next one.
Task dependence#
Head importance is not universal — a head important for sentiment classification might be irrelevant for NER. Always measure importance on a dataset representative of your deployment task, not the original pre-training task. This is a common mistake: pruning based on importance on a generic benchmark and deploying on a specialized task where the pruned heads actually mattered.
Go deeper: Quantization for LLM serving covers INT8, INT4 (AWQ/GPTQ), and FP8 trade-offs in depth.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
Distillation trains a smaller model on soft teacher outputs — biggest accuracy-preserving compression. Quantization reduces numerical precision (float16 is nearly free; int8 needs calibration). Structured pruning removes components (heads, layers) that map to real speedup. Combine all three for maximum compression. Measure head importance via gradient magnitude or Taylor expansion on your specific task — importance is task-dependent and you should prune iteratively with fine-tuning between steps.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- What is the lottery ticket hypothesis and what does it imply about how neural networks store information?
- How would you compress a model for mobile deployment where you have no GPU?
- When does distillation fail — what kinds of tasks or architectures make the teacher-student transfer difficult?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions
How do you serve ML models at low latency and high throughput?
What is model quantization and why does it matter for deployment?
Walk me through post-training quantization for LLM serving: INT8, INT4 (AWQ/GPTQ), and FP8 trade-offs
How would you implement CI/CD for ML pipelines?