Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Packing for a small suitcase
A trained network is like luggage packed for a long trip, and a small device is a small suitcase. You can fold things tighter (store numbers with fewer bits), leave out what you rarely use (remove weights or whole neurons), or ask an expert to pack a smaller bag for you (train a small network to copy a big one). Each trick saves space differently, and the best result often combines them. After packing, you check that nothing important was left behind, not only on average but for every kind of input.
Best within 4,000 bytes: 48 units (structured), int4. M4-2 study, seed 0. Pruned models need sparse kernels to turn fewer non-zero weights into fewer operations.
Storing weights as 8-bit integers instead of 32-bit floats makes a network almost four times smaller, usually with no measurable loss of accuracy.
- 1. Quantization saves memory by…
- 2. Distillation trains…
- 3. After compression you should check accuracy…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Post-training quantization (PTQ): round trained weights (and activations) to b-bit integers with a scale per tensor or per output channel.
- Quantization-aware training (QAT): train through the quantizer, passing gradients straight through the rounding.
- Mixed precision: different word lengths per layer, assigned by sensitivity and size.
- Unstructured pruning: remove individual small weights; needs sparse storage (CSR, bitmasks) and sparse kernels to save memory and time.
- Structured pruning: remove whole neurons, channels or heads; the result is a smaller dense model that runs faster everywhere.
- Low-rank factorization: replace a weight matrix by a product of two thin matrices from its SVD.
- Knowledge distillation: train a small student on the teacher’s softened outputs as well as the labels.
- Deep Compression: pruning, quantization and entropy coding combined (Han et al. 2016).
- 1. A smaller dense model results from…
- 2. QAT passes gradients through rounding using…
- 3. CSR storage keeps, for each non-zero weight…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Compressing to a budget
Exercise 1.
- A 16-96-96-8 teacher (11,720 parameters, 46,880 bytes in fp32) on an 8-class task whose classes are mixtures of clusters.
- Symmetric quantization per tensor or per channel, global magnitude pruning with fine-tuning, CSR storage, structured pruning by unit norm, SVD low-rank factorization, and distillation with temperature.
- Storage counted honestly: packed weights, scales, biases, indices and row pointers.
Practice.
- Count real bytes: scales, biases, indices and alignment can dominate small models.
- Prefer structured compression (fewer units, channels or layers) unless the target has sparse kernels.
- Quantize first: int8 is nearly free; go lower with per-channel scales, mixed precision and quantization-aware training.
- Distil when training a small model from scratch falls short, and tune temperature on validation data.
- Re-run slice and per-class tests after every compression step (topic M3-4), and test on the target runtime (topic M4-4).
def quantize(W, bits, per_channel=False):
q = 2 ** (bits - 1) - 1
scale = abs(W).max(axis=0) / q if per_channel else abs(W).max() / q
return round(W / scale) * scale
def distill_gradient(z_student, z_teacher, y, T=4, alpha=0.9):
return alpha * T * (softmax(z_student / T) - softmax(z_teacher / T)) + (1 - alpha) * (softmax(z_student) - onehot(y))- 1. Without sparse kernels, unstructured pruning saves…
- 2. Which compression step is nearly free?
- 3. After compression, tests should check…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Quantization error. Rounding to a grid of step Δ adds an error uniform on [−Δ/2, Δ/2] with variance Δ²/12. With symmetric b-bit quantization Δ = 2·max|w| / (2^b − 2), so every bit removed doubles the step and quadruples the noise variance. A per-channel scale uses each output channel’s own maximum, so channels with small weights are not crushed by one large weight elsewhere. Quantization-aware training lets the remaining weights move to compensate, which is why it rescues very low word lengths.
Sparse storage. A matrix with r rows, c columns and density ρ needs rc·v bytes dense and about rcρ(v + i) + 4(r + 1) bytes in CSR, with v bytes per value and i per index. CSR is smaller only when ρ < v/(v + i): about 0.5 for int8 values with 8-bit indices, 0.8 for fp32 values with 8-bit indices. A bitmask costs rc/8 bytes plus rcρv, cheaper than CSR until ρ is very small. Moderate pruning of an int8 model therefore saves little memory, and without sparse kernels no time.
Low rank. The best rank-r approximation of W in Frobenius norm is the truncated SVD (Eckart–Young), with error equal to the root sum of squares of the discarded singular values. A layer a × b becomes r(a + b) parameters, a saving when r < ab/(a + b).
Distillation. At temperature T the teacher’s softened probabilities reveal which wrong classes it considers similar; the gradient of T²·KL(p_T^teacher ‖ p_T^student) with respect to the student’s logits is T(p_T^student − p_T^teacher), matching the cross-entropy gradient’s scale. The gain depends on how much the soft labels add to the hard ones.
- 1. Removing one bit multiplies quantization noise variance by…
- 2. CSR beats dense for int8 values with 8-bit indices when density is below…
- 3. The best rank-r approximation in Frobenius norm is…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.At equal memory, which compression methods keep the most accuracy for a small deployed network?
History.
- Y. LeCun, J. Denker and S. Solla (1990) pruned networks with Optimal Brain Damage; B. Hassibi and D. Stork (1993) with Optimal Brain Surgeon.
- C. Buciluǎ, R. Caruana and A. Niculescu-Mizil (2006) compressed ensembles into single models; G. Hinton, O. Vinyals and J. Dean (2015) introduced distillation with temperature.
- E. Denton and colleagues (2014) used low-rank factorization to speed up CNNs; S. Han, H. Mao and W. Dally (2016) combined pruning, quantization and Huffman coding in Deep Compression.
- B. Jacob and colleagues (2018) defined the integer-only quantization used by mobile runtimes; H. Li and colleagues (2017) pruned whole filters.
- S. Hooker and colleagues (2019) showed that compression damages some classes and examples much more than the average.
Exercise 2.
- 3,000 training points and 2,000 test points; the teacher is trained with mini-batch Adam (600 steps).
- Pruned and structurally pruned models are fine-tuned for 200 steps; students of 24 units per layer are trained for 600 steps on labels only or by distillation (T = 4, alpha = 0.9).
Seed 0:
| model | bytes | MACs per input | accuracy |
|---|---|---|---|
| teacher (fp32) | 46,880 | 11,520 | 0.9640 |
| int8 | 12,332 | 11,520 | 0.9635 |
| int4 per channel | 7,360 | 11,520 | 0.9615 |
| 80% pruned, int8 CSR | 6,246 | 2,301 | 0.9620 |
| 90% pruned, int8 CSR | 3,942 | 1,149 | 0.9135 |
| 48 units (structured), int4 | 2,560 | 3,456 | 0.9605 |
| 24 units (structured), int8 | 1,388 | 1,152 | 0.9425 |
| 24 units trained directly, int8 | 1,388 | 1,152 | 0.9445 |
| 24 units distilled, int8 | 1,388 | 1,152 | 0.9560 |
Seeds 1 and 2 (teacher 0.9670 and 0.9715): int8 0.9665 and 0.9715; int4 per channel 0.9655 and 0.9710; 90% pruned 0.9155 and 0.9205; 48 units int4 0.9610 and 0.9575; 24 units directly 0.9435 and 0.9485, distilled 0.9500 and 0.9575.
Findings.
- int8 cost at most 0.0005 accuracy in every seed and int4 per channel at most 0.0025.
- 48 units with int4 weights (2,560 bytes, 18 times smaller) beat 90% unstructured pruning (3,942 bytes) by 3.7 to 4.7 points in every seed.
- Distillation improved the 24-unit student over training it directly by 0.65 to 1.15 points in all seeds; structured pruning plus fine-tuning matched direct training (within 0.003).
- Index overhead limits unstructured pruning: 80% sparsity saved only half the bytes of the int8 dense model.
Compression in practice
- Mobile and embedded runtimes (TensorFlow Lite, ONNX Runtime, TensorRT) provide int8 kernels and quantization tools; structured sparsity patterns such as 2:4 are accelerated on recent GPUs (topic M4-4).
- Large language models are quantized to 4 bits with methods such as GPTQ and AWQ, and distilled into smaller models for deployment.
- Spacecraft and drones combine compression with radiation-aware choices (topic M3-2) and on-board power limits (topics M4-3 and M4-5).
References
- Y. LeCun, J. Denker and S. Solla (1990). “Optimal brain damage.” NeurIPS.
- G. Hinton, O. Vinyals and J. Dean (2015). “Distilling the knowledge in a neural network.” NeurIPS Deep Learning Workshop.
- S. Han, H. Mao and W. Dally (2016). “Deep compression: compressing deep neural networks with pruning, trained quantization and Huffman coding.” ICLR.
- B. Jacob and colleagues (2018). “Quantization and training of neural networks for efficient integer-arithmetic-only inference.” CVPR.
- H. Li, A. Kadav, I. Durdanovic, H. Samet and H. P. Graf (2017). “Pruning filters for efficient ConvNets.” ICLR.
- S. Hooker, A. Courville, G. Clark, Y. Dauphin and A. Frome (2019). “What do compressed deep neural networks forget?” arXiv:1911.05248.
- E. Denton, W. Zaremba, J. Bruna, Y. LeCun and R. Fergus (2014). “Exploiting linear structure within convolutional networks for efficient evaluation.” NeurIPS.