Model Compression Can Go Beyond Quantization
Beyond quantization, pruning, structured pruning, and distillation compress models, preserve behavior, and keep runtime efficiency.
Key Points Summary
- Model compression is not only about reducing numeric precision; pruning and knowledge distillation also require preserving model behavior and runtime compatibility.
- Magnitude pruning creates sparse weights, but sparse storage does not automatically improve speed and may add complexity if hardware lacks efficient sparse kernels.
- Structured pruning removes whole model units such as channels, heads, or layers to keep dense tensor shapes, but requires coordinated architecture edits and hardware-friendly dimensions.
- Knowledge distillation trains a smaller student model to imitate a teacher’s output distribution or intermediate representations, using losses such as temperature-scaled KL divergence.
- Practical compression benefits depend on implementation details, including sparse matrix operations, valid channel/head selection, masked loss computation, and access to a pretrained teacher.
Meta description: Learn how pruning, structured pruning, and knowledge distillation compress language models beyond quantization, preserving serving behavior and runtime support.

Tags: model compression, pruning, structured pruning, knowledge distillation, language models
Featured image: abstract, friendly illustration of a dense weight grid becoming sparse and a smaller multilayer network learning from a larger one, no real people, no logos, no text. Source photo credit: Kutan Ural, some rights reserved.
For teams choosing model compression beyond quantization, the main serving question is not only how values are stored, but whether the model still behaves correctly after weights or structures are changed. Quantization changes the numeric representation of model values. Some compression methods reduce how many values remain or how large model structures are. Pruning removes selected weights or larger structures, while distillation trains a smaller student to imitate a larger teacher’s behavior.
A smaller served model is often an effective inference optimization. It reads fewer weights, and its cache use can depend on layer count, key-value heads, and head dimension. It may also make larger batches or cheaper hardware possible. The hard part is preserving the behavior an application needs and making sure the runtime can execute the compressed structure efficiently.
Pruning and Sparsity: Zeros Do Not Automatically Speed Things Up
Magnitude pruning is based on the idea that weights with small absolute values may have less effect on outputs. The `sparsity` argument is the fraction of weights that pruning aims to remove. A value of `sparsity=0.0` retains all weights, while the practical upper bound excludes `sparsity=1.0` because that would remove every weight and make the layer unusable.
After pruning, zeros may appear anywhere in `pruned_weight`, but the tensor can still be stored densely. Calling `to_sparse_csr()` creates a compressed sparse row tensor that stores nonzero values together with row and column metadata. The function `torch.sparse.mm()` then multiplies that sparse matrix by a dense matrix.
This distinction matters because sparse and dense matrix multiplication can accumulate terms in a different order, so outputs may need to be compared with a numerical tolerance. A sparse representation saves storage only when metadata costs less than the removed data. It improves speed only when the matrix shape, sparsity level, data type, and hardware have an efficient sparse kernel. Otherwise, unstructured pruning may add complexity without a serving win.
Structured Pruning: Remove Whole Units and Keep Dense Shapes Compatible
Structured pruning removes complete units such as attention heads, multilayer perceptron channels, layers, or blocks. The result can often be represented as a smaller dense model, which is easier to accelerate across hardware.
In a transformer multilayer perceptron, one projection expands each hidden vector into an intermediate representation, and another projects it back to the model’s hidden size. Each intermediate channel is produced by one row of the up projection and consumed by the matching column of the down projection. Removing a row from the up projection therefore requires removing the corresponding column from the down projection.
A common implementation ranks rows of the up projection weight by their norms, keeps the `keep_channels` highest-scoring rows, and keeps the matching columns of the down projection weight. The `keep_channels` parameter is a count, not a sparsity fraction. It must be an integer from 1 through the up projection’s output features, and the original projections must satisfy `up_proj.out_features == down_proj.in_features`. For example, setting `keep_channels=2048` reduces an original intermediate width of 8192 to 2048 channels.
This preserves compatible tensor shapes, but shape compatibility alone does not prove the chosen channels are unimportant. A real pruning method evaluates layer outputs, interactions, and recovery fine-tuning. Removing attention heads requires coordinated edits to query, key, value, and output projections. In grouped-query attention, query heads share key-value heads, so arbitrary head removal can break grouping. Removing transformer layers changes residual depth and may require learning mappings between the retained layers.
Structured pruning can reduce parameter count and dense compute, but awkward hidden sizes may perform worse than slightly larger hardware-friendly sizes. Channel counts should align with the tile requirements of the target matrix multiplication kernels.
Knowledge Distillation: Transfer Teacher Behavior Into a Smaller Student
Knowledge distillation trains a smaller student using signals from a larger teacher. The student may learn from the teacher’s output distribution, not just the highest-scoring token, as well as intermediate representations, generated data, or a combination of these signals.
For teacher logits `z_t` and student logits `z_s`, a common distillation loss uses a temperature `T`. The loss can be written as:
`L_KD = T^2 * KL(softmax(z_t / T) || softmax(z_s / T))`
Here, `KL` is the Kullback-Leibler divergence from the teacher distribution to the student distribution. It is zero when the two probability distributions match and grows as their probabilities differ.
In PyTorch training code, padded batches require replacing `.mean()` with a masked average over valid token positions. The loss also assumes teacher and student logits refer to the same vocabulary and token positions. The teacher runs without gradients, but producing its targets still costs time.
A local example using two small randomly initialized GPT-2 style models can demonstrate one training step, but it does not produce a useful language model. A real distillation experiment needs a pretrained teacher and representative text.
For a fair comparison, train on a representative data split and report held-out task quality, parameter bytes, peak memory, prefill latency, and decode throughput for the same prompts and hardware. Parameter count can fall substantially while behavior still changes, so serving metrics and quality metrics should be measured together.
Further Reading
- Jonathan Frankle and Michael Carbin, 2018, The Lottery Ticket Hypothesis: studies sparse subnetworks that can train effectively within larger networks.
- Elias Frantar and Dan Alistarh, 2023, SparseGPT: Massive Language Models Can Be Accurately Pruned in One-Shot: presents a post-training pruning method for large language models.
- Geoffrey Hinton, Oriol Vinyals, and Jeff Dean, 2015, Distilling the Knowledge in a Neural Network: introduces the commonly used soft-target distillation formulation.
- Victor Sanh et al., 2019, DistilBERT: applies knowledge distillation while pretraining a smaller transformer.
Common Mistakes When Compressing Served Models
- Treating zeros after unstructured pruning as an automatic runtime win. Sparse storage and an efficient sparse kernel are required before speed or memory improvements are likely.
- Forgetting paired edits in structured pruning. Removing an up-projection row without removing the matching down-projection column breaks tensor compatibility.
- Confusing `keep_channels` with sparsity. `keep_channels` is an integer channel count, not a fraction of weights to remove.
- Pruning attention heads without checking grouping. In grouped-query attention, query heads share key-value heads, so arbitrary removal can break the grouping.
- Reporting only parameter count. Held-out task quality, peak memory, prefill latency, and decode throughput are needed to understand serving tradeoffs.
Comments (0)