With the help of DeepSeek, this post helps me understand three model compression techniques for mobile AI: Knowledge Distillation, Quantization, and Pruning.
1. Knowledge Distillation
Core Idea
Train a lightweight “student model” to mimic the output behavior of a complex “teacher model”, thereby transferring the teacher model’s knowledge into the student model.
- Source of knowledge: the teacher model’s output probability distribution (soft labels), intermediate-layer features, or attention mechanisms.
- Goal: the student model reaches performance close to the teacher model while staying small.
Example
Scenario: an image classification task (e.g. the ImageNet dataset)
- Teacher model: a large model (e.g. ResNet-50, 76% accuracy).
- Student model: a lightweight model (e.g. MobileNetV3, only 70% accuracy when trained directly).
- Distillation process:
- The teacher model generates “soft labels” for the training data (i.e. a probability distribution over classes, such as
[0.7, 0.2, 0.1]). - The student model learns from both the ground-truth labels (hard labels) and the soft labels.
The loss combines the hard-label loss with a KL-divergence loss on the soft labels:where is the cross-entropy loss and is the KL-divergence loss.
- The teacher model generates “soft labels” for the training data (i.e. a probability distribution over classes, such as
Deeper Thoughts
1.Why can the student model, with fewer parameters, get close to the teacher’s performance?
By analogy with a “derived result”, the student model already has the teacher model’s prior knowledge (the probability distribution) and does not need to learn everything from the ground up. We call this the ability to abstract decision boundaries.
From an information-theoretic perspective: the “useful information” in the teacher model (decision boundaries, feature correlations) is encoded into the student model’s parameters, rather than copying all of the parameters.
2.How is the soft-label probability distribution generated?
Core method: Temperature Scaling
Soft labels are not the teacher model’s raw output used directly; instead, a temperature parameter (Temperature, T) is introduced to smooth the probability distribution so that it conveys the relationships between classes.
Formula:
where
The effect of T:
- When
, the soft labels are equivalent to the hard labels. - When
, the probability distribution is smoother and carries richer information about the relationships between classes. - When
, the probability distribution approaches a uniform distribution.
3.Why not use soft labels when training the teacher model?
Root cause: the teacher model has a different training objective
- The teacher model’s mission: pursue the highest accuracy, not transfer knowledge
- The teacher model needs to fit the details in the data as closely as possible; hard labels (definite answers) are a more direct supervision signal.
- Soft labels would introduce unnecessary “uncertainty” and lower the model’s confidence in the correct class.
- The contradiction in where soft labels come from:
- In knowledge distillation, the soft labels are generated by a more powerful teacher model (e.g. ResNet-50 teaching MobileNet).
- If soft labels were used to train the teacher model, another, even stronger model would be needed to generate them, which leads to an infinite regress (who generates the soft labels for that stronger model?)
4.Limitations of knowledge distillation
- Requires a high-quality teacher model: the teacher model must be large enough to provide high-quality soft labels.
- Requires a lot of compute: the teacher model needs substantial computing resources to generate high-quality soft labels.
- Requires a lot of data: the teacher model needs a large amount of data to generate high-quality soft labels.
2. Quantization
Core Idea
Convert the model parameters (weights) and activations from high-precision floating point (e.g. 32-bit) to low-precision values (e.g. 8-bit integers), reducing model size and compute cost.
- Types:
- Post-training Quantization: quantize an already-trained model directly.
- Quantization-aware Training: simulate quantization error during training to improve the accuracy of the final quantized model.
Example
Scenario: a speech recognition model on a phone
- Original model: an LSTM-based speech recognition model in FP32 precision, 120MB in size, 50ms latency.
- Quantization steps:
- Quantize the weights and activations from FP32 to INT8 (mapping the range to -128~127).
- Insert dequantization layers to restore precision at critical compute nodes.
- Result: model size shrinks to 30MB, latency drops to 15ms, accuracy loss under 1%.
Below, using PyTorch as an example, are implementations of post-training quantization and quantization-aware training.
Post-training Quantization
1 | import torch |
Quantization-aware Training
1 | import torch |
Deeper Thoughts
Implementing the quantized computation by hand:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17# 原始FP32计算
W_fp32 = torch.tensor([2.5, -1.3, 0.8], dtype=torch.float32)
x_fp32 = torch.tensor([0.4, 1.2, -0.5], dtype=torch.float32)
y_fp32 = torch.dot(W_fp32, x_fp32) # 输出:2.5*0.4 + (-1.3)*1.2 + 0.8*(-0.5) = -1.56
# 量化到INT8(范围假设为[-5, 5])
scale_W = 5 / 127 # 对称量化,scale = max(abs(W)) / 127
W_int8 = torch.clamp((W_fp32 / scale_W).round(), min=-128, max=127).to(torch.int8)
scale_x = 5 / 127
x_int8 = torch.clamp((x_fp32 / scale_x).round(), min=-128, max=127).to(torch.int8)
# 整数计算
y_int32 = torch.dot(W_int8.float(), x_int8.float()) # 转为float避免溢出
y_dequant = y_int32 * (scale_W * scale_x) # 反量化
print("FP32结果:", y_fp32.item()) # -1.56
print("量化结果:", y_dequant.item()) # 约-1.55(存在微小误差)
The full quantization pipeline:
- Preparation stage
- Insert observers into the model to collect the distribution of weights and activations in each layer.
- Code: model_prepared = prepare(model)
- Calibration stage
- Run the model on representative data; the observers record each layer’s min/max values.
- Code: model_prepared(input_data)
- Conversion stage
- Compute the quantization parameters from the calibration results and replace the floating-point operators with quantized operators.
- Code: model_quantized = convert(model_prepared)
Core formulas:
- Quantization formula:
- clamp: restrict the result to the range between min and max
- round: round to the nearest integer
- zero_point: the quantization offset, used for calibration, usually 0
- Dequantization formula:
Practical Applications
- TensorFlow Lite: supports post-training quantization by default and can compress an object detection model (e.g. SSD MobileNet) from 16MB down to 4MB.
- Apple Core ML: runs a quantized StyleGAN model on the iPhone for real-time portrait style transfer.
3. Pruning
Core Idea
Reduce model complexity by removing unimportant parameters (e.g. weights close to zero) or structures (e.g. redundant neurons) from the model.
- Types:
- Unstructured pruning: remove individual weights (sparsification).
- Structured pruning: remove entire neurons or channels (better suited to hardware acceleration).
Example
Scenario: compressing a BERT model in natural language processing
- Original model: BERT-base (110 million parameters, 400MB model size).
- Pruning process:
- During fine-tuning, prune 30% of the attention heads based on weight magnitude or gradient-based importance scores.
- Retrain the remaining parameters to recover accuracy.
- Result: model size reduced to 280MB, inference speed up 1.5x, accuracy on the GLUE benchmark drops by only 0.5%.
Here is an example of implementing pruning in PyTorch:
Unstructured Pruning
1 | import torch |
Why prune with the L1 norm?:
The L1 norm naturally induces sparsity: by minimizing the L1 norm, the model tends to push some weights toward 0 to achieve sparsification, and it is simple to compute.
Structured Pruning
1 | from torch.nn.utils.prune import ln_structured, remove_structured |
Why prune with the L2 norm?:
- Avoids extreme values: shrinks uniformly
- Computational efficiency: the L2 norm has relatively low computational complexity
Practical Applications
- NVIDIA’s Nemo framework: applies structured pruning to speech recognition models (e.g. QuartzNet), doubling GPU inference speed.
- Drone obstacle avoidance: a pruned YOLOv5 model detects obstacles in real time on edge devices, with 40% lower power consumption.
Comparison and Combined Use of the Three
| Technique | Core goal | Advantages | Limitations | Typical compression ratio |
|---|---|---|---|---|
| Knowledge distillation | Transfer knowledge to a small model | Accuracy close to the teacher model | Depends on a high-quality teacher model | 2-5x |
| Quantization | Reduce numerical precision | Significantly reduces size and compute cost | May lose accuracy (needs calibration) | 4x+ |
| Pruning | Remove redundant parameters or structures | Faster inference, lower memory footprint | May break the structural integrity of the model | 2-10x |
A combined-use example:
Google’s MobileNetV4 model combines all three:
- Use knowledge distillation to transfer knowledge from EfficientNet;
- Apply mixed-precision quantization to the model (INT8 for some layers, FP16 for critical layers);
- Prune away 80% of the redundant channels; the final model is 6x smaller and 3x faster, with only a 2% drop in accuracy.
Summary
Knowledge distillation, quantization, and pruning are the three core techniques for compressing AI models on mobile devices:
- Knowledge distillation: transfers knowledge through “teacher-student learning”, suited to transferring model capabilities;
- Quantization: lowers numerical precision, directly shrinking size and accelerating computation;
- Pruning: eliminates redundant parameters, improving hardware execution efficiency.
In practice, the three are often used together (e.g. a “distillation + quantization + pruning” pipeline) to achieve the most aggressive optimization of mobile AI models while preserving accuracy.
Translated from the Chinese original.

