Course contents 13 / 14

13 / 14 · Robustness and Deployment

Model compression

How to make a trained network smaller and faster: why memory traffic dominates cost, magnitude and structured pruning, the lottery ticket hypothesis, knowledge distillation with temperature and the factor T squared, low-rank factorization, and how to measure a real speed-up.

On this page 8

The previous note made a network robust at the price of more training. At deployment the network meets a different budget. A phone has a few gigabytes of memory shared with everything else and a battery; a server answering millions of requests pays for every multiply-add; a car needs an answer within milliseconds. A network trained for accuracy is usually larger than the function it computes requires. This note shows four ways to remove the excess, pruning weights, distilling the network into a smaller one, factorizing its layers and storing its numbers in fewer bits, and how to check whether the result is really faster, which a smaller parameter count does not guarantee.

Notation. A layer has a weight matrix W∈Rm×nW \in \mathbb R^{m \times n}, with mm outputs and nn inputs; NN is the number of parameters of a network. A mask M∈{0,1}m×nM \in \{0, 1\}^{m \times n} marks the weights that are kept, ⊙\odot is the elementwise product, and the sparsity ss is the fraction of weights set to zero. For distillation, vv are the logits of the teacher, zz those of the student, KK is the number of classes and TT the temperature; q=softmax⁡(v/T)q = \operatorname{softmax}(v / T) are the soft targets and p=softmax⁡(z/T)p = \operatorname{softmax}(z / T) the student’s softened prediction. rr is a rank.

Baseline: deploy the network as trained#

A trained network costs three things at inference. Memory: NN parameters at 4 bytes each in 32-bit floating point, plus the activations. Computation: about one multiply-add per weight per input for a fully connected layer, more for a convolution, which reuses each weight at every position. Time and energy: what the hardware needs to move the weights and activations and to do the arithmetic. A ResNet-50 has 25.6 million parameters, 102 MB in 32-bit floats, and needs about 4.1 billion multiply-adds for one 224×224224 \times 224 image, listed as 4.09 GFLOPS (Convolutional architectures). The teacher network of the Code section, 64→512→512→1064 \to 512 \to 512 \to 10 on the bundled 8×88 \times 8 digits, has 301,066 parameters, 1.2 MB, and classifies 0.976 of the 540 test images correctly.

Why compress: memory, latency and energy#

The three costs are not proportional to each other, and the one that dominates is usually memory traffic. Fetching a number from off-chip memory costs far more than computing with it. For a 45 nm process, a 32-bit floating-point addition takes 0.9 pJ and a multiplication 3.7 pJ, reading 32 bits from an on-chip SRAM cache 5 pJ, and reading them from off-chip DRAM 640 pJ (Han et al., 2015, Figure 1, after Horowitz).

Worked example. If each of the teacher’s 301,066 weights had to come from DRAM for one image, the reads would cost 301,066⋅640 pJ=0.19301{,}066 \cdot 640\ \text{pJ} = 0.19 mJ, and the multiply-adds 301,066⋅(3.7+0.9) pJ=1.4 μ301{,}066 \cdot (3.7 + 0.9)\ \text{pJ} = 1.4\ \muJ: 139 times less. A batch shares one fetch of each weight among all its images, which is why throughput-oriented servers batch requests; an interactive application that classifies one input at a time pays the full fetch for every input. Han et al. make the same point at scale: a network with one billion connections run 20 times per second needs 20⋅109⋅640 pJ=12.820 \cdot 10^9 \cdot 640\ \text{pJ} = 12.8 W for DRAM access alone, beyond the power budget of a phone.

So a layer at batch size 1 is usually memory-bound: its time is set by the bytes it must read, not by its arithmetic. The roofline model of the Computer Architecture and Operating Systems course makes this precise through the arithmetic intensity, the number of operations per byte moved. Fewer and smaller weights help twice: less memory and less traffic.

Pruning: remove the smallest weights#

Magnitude pruning keeps the weights that are large in absolute value and zeroes the rest:

W′=M⊙W,Mij=1[ ∣Wij∣>τ ],\begin{aligned} W' &= M \odot W, \\ M_{ij} &= \mathbf 1\big[\, \lvert W_{ij} \rvert > \tau \,\big], \end{aligned}

with the threshold τ\tau chosen so that a fraction ss of the weights falls below it. A global threshold is one τ\tau for all layers, so layers with many small weights lose more of them; a layerwise threshold prunes every layer to the same sparsity.

Why should the smallest weights matter least? Optimal Brain Damage (LeCun, Denker and Solla, 1990) estimates the increase of the loss when weight ii is deleted, from a Taylor expansion around the trained weights ww:

  ΔLi≈12 Hii wi2  \boxed{\; \Delta L_i \approx \tfrac12\, H_{ii}\, w_i^2 \;}

The steps: deleting weight ii is the change Δw=−wiei\Delta w = -w_i e_i, where eie_i is the ii-th unit vector. To second order,

L(w+Δw)−L(w)≈g⊤Δw+12 Δw⊤H Δw=−giwi+12 Hii wi2≈12 Hii wi2,\begin{aligned} L(w + \Delta w) - L(w) &\approx g^\top \Delta w + \tfrac12\, \Delta w^\top H\, \Delta w \\ &= -g_i w_i + \tfrac12\, H_{ii}\, w_i^2 \\ &\approx \tfrac12\, H_{ii}\, w_i^2 , \end{aligned}

where gg and HH are the gradient and the Hessian of the loss at ww. The second line substitutes Δw=−wiei\Delta w = -w_i e_i, which picks out one entry of gg and one diagonal entry of HH. The third uses that training has ended near a minimum, where g≈0g \approx 0. The saliency 12Hiiwi2\tfrac12 H_{ii} w_i^2 ranks the weights; if the curvatures HiiH_{ii} are similar, it ranks them by ∣wi∣\lvert w_i \rvert, which is magnitude pruning. Deleting several weights at once adds the off-diagonal terms of HH, which the estimate ignores, so pruning is done in rounds with retraining between them.

Worked example. Six weights w=(0.8,−0.05,0.3,−0.6,0.02,0.1)w = (0.8, -0.05, 0.3, -0.6, 0.02, 0.1) pruned to s=0.5s = 0.5. The three smallest magnitudes are 0.020.02, 0.050.05 and 0.10.1, so any τ\tau between 0.10.1 and 0.30.3 gives w′=(0.8,0,0.3,−0.6,0,0)w' = (0.8, 0, 0.3, -0.6, 0, 0). Half the weights are gone, but only 0.022+0.052+0.12=0.01290.02^2 + 0.05^2 + 0.1^2 = 0.0129 of the squared norm 1.10291.1029, 1.2% of it.

Han et al. (2015) prune, retrain the remaining weights with the mask fixed, and repeat. Without retraining, accuracy drops sharply; with it, AlexNet lost 9 times its parameters, from 61 to 6.7 million, and VGG-16 13 times, from 138 to 10.3 million, without loss of ImageNet accuracy. The Code section prunes the teacher globally and fine-tunes it for 10 epochs with the mask fixed (one run, test accuracy on the 540 digits):

sparsity ss0.50.80.90.950.98
after pruning0.9780.9670.8870.4440.126
after 10 epochs of fine-tuning0.9800.9810.9810.9630.700

Nine weights in ten can go without any loss, once the survivors are retrained; past 95% the accuracy falls quickly.

Unstructured and structured sparsity#

Zeroing individual weights gives unstructured sparsity: the zeros are scattered irregularly. It saves less than it seems.

  • Memory. A sparse format must store where each nonzero is. Compressed sparse row (CSR) stores a 4-byte value and a 4-byte column index per nonzero, plus one pointer per row: about 8(1−s) mn8 (1 - s)\, mn bytes against 4mn4mn dense. At s=0.5s = 0.5 it saves nothing (the Code section measures 1.003 of the dense size), at s=0.9s = 0.9 it needs 0.203.
  • Speed. A dense matrix product does the same work whether a weight is zero or not. Sparse kernels skip the zeros but access memory irregularly, and on general-purpose hardware they pay off only at very high sparsity.

Structured pruning removes whole units: rows of WW for a fully connected layer, filters or channels of a convolution, heads of attention. What remains is a smaller dense network, which every library and every processor runs faster. Li et al. (2017) rank the filters of each convolutional layer by the sum of their absolute weights and remove the smallest; with retraining, this cut the inference cost of VGG-16 by up to 34% and of ResNet-110 by up to 38% on CIFAR-10, with close to the original accuracy. Between the two lies semi-structured sparsity: in the 2:4 pattern, two of every four consecutive weights are zero, a pattern that the sparse tensor cores of NVIDIA’s Ampere GPUs execute at twice the throughput of dense ones (Mishra et al., 2021).

Figure

Three ways to remove half of the weights

Three ways to remove half of the weightsFour 8 × 8 matrices; the shade of a cell is the magnitude of its weight, dashed cells are zeroed weights. Each pattern keeps 32 of 64 weights: unstructured keeps 89% of the squared norm, 2:4 keeps 83%, removing four rows 59%.dense64 weights100% of ‖W‖²1unstructured, 50%32 kept, anywhere89% of ‖W‖²22:432 kept, 2 in each 483% of ‖W‖²3structured: 4 rows32 kept as 4 × 859% of ‖W‖²4Three ways to remove half of the weightsFour 8 × 8 matrices; the shade of a cell is the magnitude of its weight, dashed cells are zeroed weights. Each pattern keeps 32 of 64 weights: unstructured keeps 89% of the squared norm, 2:4 keeps 83%, removing four rows 59%.dense64 weights100% of ‖W‖²1unstructured, 50%32 kept, anywhere89% of ‖W‖²22:432 kept, 2 in each 483% of ‖W‖²3structured: 4 rows32 kept as 4 × 859% of ‖W‖²4
An 8 × 8 matrix of standard normal weights; the shade of a cell is proportional to the magnitude of its weight, dashed cells are zeros. All three patterns keep 32 weights, but different shares of the squared norm: the unconstrained choice keeps the most (89%), 2:4 slightly less (83%), whole rows the least (59%). In exchange, only the structured version is an ordinary smaller dense matrix, which any hardware multiplies faster; 2:4 is accelerated by the sparse tensor cores of GPUs, and scattered zeros do not speed up a dense product at all.
  1. The dense weight matrix of a layer, 8 outputs by 8 inputs.
  2. Unstructured: the 32 smallest weights of the whole matrix are zeroed, wherever they are.
  3. 2:4: in every group of four consecutive weights of a row, the two smallest are zeroed.
  4. Structured: the four rows with the smallest norms are removed; what is left is a dense 4×84 \times 8 matrix.

The Code section prunes the teacher’s hidden units by the norm of their incoming weights and builds the smaller dense network that remains. Keeping 128 of the 512 units in each hidden layer leaves 26,122 parameters, 8.7% of the teacher, and 0.970 accuracy after fine-tuning; unstructured pruning keeps 0.981 with 10% of the weights. Near a tenth of the parameters, structured pruning loses a little more accuracy than unstructured pruning, but its smaller network is faster on any hardware. At the extreme the order reverses in this run: 32 units per layer, 1.2% of the parameters, keep 0.922 after fine-tuning, while unstructured pruning to 2% of the weights keeps 0.700.

Figure

Accuracy after pruning: unstructured and structured

Accuracy after pruning: unstructured and structuredTest accuracy against the fraction of parameters kept, from 1% to 100%. Without fine-tuning, accuracy drops already at 10% of the weights (0.887); after 10 epochs of fine-tuning, 10% of the weights give 0.981. Structured pruning to 128 units per layer (8.7% of the parameters) gives 0.970 after fine-tuning; 32 units (1.2%) give 0.922.1%2%5%10%20%50%100%00.51.0fraction of parameters kept (log scale)test accuracyunstructured, prunedunstructured, fine-tunedstructured, prunedstructured, fine-tunedteacher 0.976Accuracy after pruning: unstructured and structuredTest accuracy against the fraction of parameters kept, from 1% to 100%. Without fine-tuning, accuracy drops already at 10% of the weights (0.887); after 10 epochs of fine-tuning, 10% of the weights give 0.981. Structured pruning to 128 units per layer (8.7% of the parameters) gives 0.970 after fine-tuning; 32 units (1.2%) give 0.922.1%2%5%10%20%50%100%00.51.0fraction of parameters kept (log scale)test accuracyunstructured, prunedunstructured, fine-tunedstructured, prunedstructured, fine-tunedteacher 0.976
The teacher 64 → 512 → 512 → 10 (301,066 parameters, accuracy 0.976) on 540 test digits, one run of the Code section. Blue: global magnitude pruning (horizontal axis: the fraction of weights kept); red: removing hidden units (the fraction of parameters of the smaller dense network). Dashed: right after pruning; solid: after 10 epochs of fine-tuning. Fine-tuning recovers almost all the accuracy until more than 95% of the weights are gone.

The lottery ticket hypothesis#

If 90% of the weights can be removed after training, could the small network have been trained alone from the start? Trained from a fresh random initialization, pruned architectures usually do worse. Frankle and Carbin (2019) found a way that works: train the dense network, prune the smallest weights, reset the survivors to their original initial values, and retrain; repeat for several rounds (iterative magnitude pruning). The subnetworks found this way, at less than 10–20% of the size of fully connected and convolutional networks on MNIST and CIFAR-10, trained to the accuracy of the full network, often faster. The same masks with fresh random initial values trained far worse. Their hypothesis: a dense randomly initialized network contains such winning tickets, subnetworks whose initial weights happen to suit training. For large networks such as ResNet-50 on ImageNet, the survivors must be reset not to their initial values but to their values from early in training (Frankle et al., 2020). Finding a ticket costs several full trainings, so the result explains why pruning works more than it saves training time.

Knowledge distillation#

A pruned network keeps the architecture of the original. Knowledge distillation (Hinton, Vinyals and Dean, 2015) trains a different, smaller student to imitate a trained teacher. The labels say only which class is correct. The teacher’s output says more: how much an image of a 2 resembles a 3 or a 7. Training the student to reproduce that distribution, not only the label, passes this information on.

A trained teacher is usually very confident, so its probabilities for the wrong classes are tiny. A temperature T>1T > 1 divides the logits before the softmax and brings them out:

qk=evk/T∑j=1Kevj/T,pk=ezk/T∑j=1Kezj/T.\begin{aligned} q_k &= \frac{e^{v_k / T}}{\sum_{j=1}^K e^{v_j / T}}, \\ p_k &= \frac{e^{z_k / T}}{\sum_{j=1}^K e^{z_j / T}} . \end{aligned}

Dividing by TT divides every log-ratio by TT: log⁡(qk/qj)=(vk−vj)/T\log(q_k / q_j) = (v_k - v_j)/T. The student is trained on a weighted sum of the usual cross-entropy with the label, at temperature 1, and a soft loss at temperature TT:

  L=α ℓCE(y,softmax⁡(z))+(1−α) T2 DKL(q ∥ p)  \boxed{\; \mathcal L = \alpha\, \ell_{\text{CE}}\big(y, \operatorname{softmax}(z)\big) + (1 - \alpha)\, T^2\, D_{\mathrm{KL}}(q \,\|\, p) \;}

The KL divergence equals the cross-entropy −∑kqklog⁡pk-\sum_k q_k \log p_k minus the entropy of qq, which does not depend on the student, so the two give the same gradient (KL divergence). After training, the student is used at T=1T = 1.

Worked example. Teacher logits v=(5,2,0)v = (5, 2, 0). At T=1T = 1 the softmax gives (0.9465,0.0471,0.0064)(0.9465, 0.0471, 0.0064): class 1 takes almost everything, and the difference between classes 2 and 3 is buried in the last two numbers. At T=4T = 4 the logits become (1.25,0.5,0)(1.25, 0.5, 0) and the soft targets (0.5685,0.2686,0.1629)(0.5685, 0.2686, 0.1629). The ratio of the second to the third probability falls from e2=7.39e^{2} = 7.39 to e0.5=1.65e^{0.5} = 1.65, and the student now gets a gradient of useful size from both wrong classes.

Figure

Distillation: the teacher’s soft targets and the student’s loss

Distillation: the teacher’s soft targets and the student’s lossTop, the teacher: logits (5, 2, 0) through a softmax at temperature 4 give the soft targets (0.569, 0.269, 0.163). Below, the student: logits (3, 0, 1) give (0.481, 0.227, 0.292). The KL divergence between them times 16 is 0.720; the student’s cross-entropy with the label is 0.170; their average is 0.445.xteacherfrozen520vsoftmax(·/4)0.5690.2690.163q1studenttrained301zsoftmax(·/4)0.4810.2270.292p2KL · T²soft 0.7203CE with yhard 0.1704α·hard + (1−α)·soft= 0.4455inputstateweights, gatesoutputDistillation: the teacher’s soft targets and the student’s lossTop, the teacher: logits (5, 2, 0) through a softmax at temperature 4 give the soft targets (0.569, 0.269, 0.163). Below, the student: logits (3, 0, 1) give (0.481, 0.227, 0.292). The KL divergence between them times 16 is 0.720; the student’s cross-entropy with the label is 0.170; their average is 0.445.xteacherfrozen520vsoftmax(·/4)0.5690.2690.163q1studenttrained301zsoftmax(·/4)0.4810.2270.292p2KL · T²soft 0.7203CE with yhard 0.1704α·hard + (1−α)·soft= 0.4455inputstateweights, gatesoutput
Three classes, the first is correct. Teacher logits v = (5, 2, 0) from the worked example, student logits z = (3, 0, 1), T = 4, α = 0.5. The student is confident in the correct class, so the hard loss is small (0.170), but it ranks the third class above the second where the teacher does the opposite; only the soft loss sees this (0.720). Both branches divide the logits by T; the soft loss is multiplied by T² so that its gradient does not shrink like 1/T². After training the student runs at T = 1.
  1. The teacher’s logits are divided by TT and passed through a softmax: the soft targets qq.
  2. The student’s logits are divided by the same TT: its softened prediction pp.
  3. The soft loss is DKL(q ∥ p)D_{\mathrm{KL}}(q \,\|\, p), multiplied by T2T^2.
  4. The hard loss is the cross-entropy of the student’s logits at T=1T = 1 with the label.
  5. The total loss mixes the two with weight α\alpha; only the student is trained.

Why the soft loss is multiplied by T squared#

The gradient of the soft loss with respect to the student’s logits shrinks like 1/T21/T^2. Start from the derivative of the cross-entropy with respect to the logits of a softmax, p−qp - q (backpropagation). Here the softmax acts on z/Tz / T, so the chain rule adds a factor 1/T1/T:

∂∂zi(−∑kqklog⁡pk)=1T(pi−qi).\frac{\partial}{\partial z_i} \Big( -\sum_k q_k \log p_k \Big) = \frac1T \big( p_i - q_i \big) .

The difference pi−qip_i - q_i is itself small when TT is large compared with the logits. Expand the exponentials to first order, ezi/T≈1+zi/Te^{z_i / T} \approx 1 + z_i / T, and assume the logits of each example have mean zero, ∑jzj=∑jvj=0\sum_j z_j = \sum_j v_j = 0 (adding a constant to all logits does not change a softmax, so this loses nothing):

pi≈1+zi/TK+∑jzj/T=1K+ziKT,qi≈1K+viKT,1T(pi−qi)≈zi−viKT2.\begin{aligned} p_i &\approx \frac{1 + z_i / T}{K + \sum_j z_j / T} = \frac{1}{K} + \frac{z_i}{K T}, \qquad q_i \approx \frac{1}{K} + \frac{v_i}{K T}, \\ \frac1T \big( p_i - q_i \big) &\approx \frac{z_i - v_i}{K T^2} . \end{aligned}

The first line uses the expansion in numerator and denominator and the zero mean. The second subtracts the two and multiplies by 1/T1/T. So the soft gradient falls like 1/T21/T^2, and multiplying the soft loss by T2T^2 keeps its contribution comparable with the hard loss whatever temperature is chosen (Hinton, Vinyals and Dean, 2015, section 2). The second line also shows what distillation becomes at high temperature: after the factor T2T^2, the gradient (zi−vi)/K(z_i - v_i)/K is that of 12K∑i(zi−vi)2\frac{1}{2K} \sum_i (z_i - v_i)^2, matching the logits directly.

Worked example. The Code section takes random logits zz and vv for K=10K = 10 classes and computes the gradient of the soft loss exactly. Its norm is 0.3850.385 at T=1T = 1, 0.08890.0889 at T=2T = 2, 0.02150.0215 at T=4T = 4 and 0.001320.00132 at T=16T = 16, falling by about 4 for each doubling of TT. The high-temperature formula gives 0.08460.0846, 0.02120.0212 and 0.001320.00132 at the last three. Multiplied by T2T^2, the norms are 0.3850.385, 0.3560.356, 0.3440.344 and 0.3390.339: almost constant.

What the soft targets carry#

Hinton et al. trained a large, strongly regularized network on MNIST that made 67 test errors; a smaller network with two hidden layers of 800 units trained on the labels made 146; the same small network trained also to match the large one’s soft targets at T=20T = 20 made 74 (section 3). They then removed every 3 from the student’s training data: the distilled student had never seen a 3, and yet, after a correction of the bias of class 3, it recognized 98.6% of the test 3s.

The Code section repeats both experiments on the digits with a student 64→32→1064 \to 32 \to 10 of 2,410 parameters, 125 times fewer than the teacher, trained for 150 epochs with T=20T = 20 and the soft loss alone (α=0\alpha = 0; one run each):

student trained ontest accuracytest 3s recognized
labels, all training digits0.9740.982
soft targets, all training digits0.9650.982
labels, training digits without the 3s0.8780.000
soft targets, training digits without the 3s0.9650.927

With all the labels, this small student does as well on its own as with the teacher: the problem is easy enough that distillation adds nothing here. Without the 3s, the student trained on labels cannot output a class it never saw. The distilled student recognizes 92.7% of the test 3s, without any bias correction, from what the teacher’s soft targets on other digits say about how much each of them resembles a 3. That information is what the hard labels lack.

Distillation is used at every scale. The DistilBERT model of the NLP course is a BERT distilled to half its depth. A teacher trained with label smoothing is a worse teacher: smoothing pulls the wrong-class probabilities towards uniform and erases part of the similarity structure (Müller, Kornblith and Hinton, 2019).

Low-rank factorization#

A fully connected layer computes WxWx. If WW is close to a matrix of rank rr, it can be replaced by two thin layers, W≈ABW \approx AB with A∈Rm×rA \in \mathbb R^{m \times r} and B∈Rr×nB \in \mathbb R^{r \times n}, and Wx≈A(Bx)Wx \approx A(Bx) costs r(m+n)r(m + n) multiply-adds instead of mnmn. The best choice of AA and BB comes from the singular value decomposition W=UΣV⊤W = U \Sigma V^\top with singular values σ1≥σ2≥…\sigma_1 \ge \sigma_2 \ge \dots: keep the first rr of them,

Wr=U:,1:r Σ1:r V:,1:r⊤,A=U:,1:r Σ1:r,B=V:,1:r⊤,∥W−Wr∥F∥W∥F=∑i>rσi2∑iσi2.\begin{aligned} W_r &= U_{:, 1:r}\, \Sigma_{1:r}\, V_{:, 1:r}^\top, \\ A &= U_{:, 1:r}\, \Sigma_{1:r}, \quad B = V_{:, 1:r}^\top, \\ \frac{\lVert W - W_r \rVert_F}{\lVert W \rVert_F} &= \sqrt{\frac{\sum_{i > r} \sigma_i^2}{\sum_i \sigma_i^2}} . \end{aligned}

By the Eckart–Young theorem, no matrix of rank rr is closer to WW in the Frobenius norm. The factorization saves work only when r(m+n)<mnr(m + n) < mn, that is, r<mn/(m+n)r < mn/(m + n).

Worked example. The teacher’s middle layer is 512×512512 \times 512: 262,144 weights, and the break-even rank is 512⋅512/1024=256512 \cdot 512 / 1024 = 256. At r=8r = 8 the two factors have 8⋅1024=8,1928 \cdot 1024 = 8{,}192 weights, 32 times fewer. The relative error of the approximation is large, 0.6280.628, and yet the network’s test accuracy stays at 0.9760.976, unchanged; at r=4r = 4 it drops to 0.7170.717. The weights are dominated by their random initialization, which the network does not rely on, while what training added to them is nearly low-rank: 91.1% of the norm of the change from the initial to the trained weights lies in its top 8 singular directions. The same observation, that useful weight updates are low-rank, is the basis of LoRA for fine-tuning large language models.

Convolutions can be factorized in the same way, across their input channels, output channels and kernel dimensions. Denton et al. (2014) sped up the convolutional layers of large ImageNet networks by a factor of 2 on CPU and GPU, keeping the accuracy within 1% of the original. Depthwise-separable convolutions, the building block of MobileNet, are a factorization built into the architecture from the start.

Quantization, briefly#

The fourth route keeps the architecture and every weight, and stores each number in fewer bits: 8-bit integers instead of 32-bit floats take 4 times less memory and traffic, and integer arithmetic is cheaper still. The affine map from floats to integers, what it does to a matrix product, and the methods that quantize large language models are the subject of the NLP course’s quantization note; the floating-point formats themselves, including fp16 and bf16, are in the Computer Architecture and Operating Systems course. The routes combine: pruning, then quantization of the remaining weights and an entropy code for their storage, made AlexNet 35 times smaller, from 240 MB to 6.9 MB, and VGG-16 49 times smaller, from 552 MB to 11.3 MB, without loss of accuracy (Han, Mao and Dally, 2016).

Measuring the real speed-up#

Parameters, multiply-adds and time are three different quantities, and a compression method can shrink one without the others. A review of 81 pruning papers found that they rarely reported comparable measurements: different architectures, datasets and metrics, and often only parameter counts or FLOPs, a “theoretical speedup” that says nothing about wall-clock time (Blalock et al., 2020).

The Code section times one layer, the teacher’s 512×512512 \times 512 middle layer applied to a batch of 256 activations, on one CPU thread, as the median of 200 repetitions. The dense layer takes a fraction of a millisecond on a laptop CPU. Relative to it:

variant of the layerweights storedtime relative to dense
dense262,1441
90% of the weights zeroed, still a dense matrix262,1441.0
the same zeros in CSR format26,215 nonzerosabout 40
structured: 51 of the 512 output units kept26,112about 0.2

Zeros in a dense matrix change nothing. The sparse format is about 40 times slower than the dense product at this size and sparsity, although it does a tenth of the multiply-adds. Removing 90% of the output units makes the layer about 5 times faster, not 10 times: a small product has fixed overheads that do not shrink with it. The exact ratios depend on the machine, the library and the batch size; the pattern does not.

A measurement that means something follows a protocol:

  • Measure the deployment setting. The same hardware, batch size, numerical precision and library as in production. A model that is faster at batch 256 on a GPU can be slower at batch 1 on a phone.
  • Time correctly. Warm up first, repeat many times and report the median; fix the number of threads. On a GPU, operations run asynchronously, so the clock must stop only after the device has finished (Zhang et al., section 13.2).
  • Report all three. Parameters or bytes, multiply-adds, and measured latency or throughput, each with its setup, and the accuracy at that size.
  • Compare against a smaller dense model. A network with fewer units trained from scratch, or distilled, is the baseline any compressed model must beat at the same size and speed.

What changed#

Network as trainedAfter compression (digits)
Parameters301,066 (1.2 MB in fp32)about 30,000 nonzero weights at 90% sparsity; 26,122 after structured pruning; 2,410 in the student
Test accuracy0.9760.981 pruned and fine-tuned; 0.970 structured; 0.974 student on labels
Middle layer512×512512 \times 512, 262,144 weightsrank 8, 8,192 weights, accuracy 0.976
Faster?only the smaller dense networks; unstructured zeros are not faster here

Limitations#

  • Smaller is not faster. Unstructured sparsity reduces parameters but needs sparse kernels or special hardware, and at moderate sparsity those are often slower than dense products. Only structured changes speed up standard hardware.
  • Retraining is part of the method. Most compression loses accuracy until the network is fine-tuned, which needs the training data or a transfer set, and compute.
  • Distillation needs a good teacher and a gap to close. When a small network already matches the teacher on the labels, as here, soft targets add little; a teacher trained with label smoothing transfers less.
  • The redundancy depends on the task. The 8 × 8 digits are easy, so a tenth of the weights or rank 8 suffice; on harder tasks the same ratios cost accuracy.
  • Accuracy is not all that changes. A compressed model can keep its average accuracy and lose it on rare classes or inputs, and its robustness can change; it must be evaluated on what matters, not only on the average.

What you should remember#

  • Memory traffic dominates the energy and often the time of inference: 640 pJ for a 32-bit DRAM read against 3.7 pJ for a multiplication.
  • Magnitude pruning zeroes the weights with the smallest ∣w∣\lvert w \rvert, justified by the saliency 12Hiiwi2\tfrac12 H_{ii} w_i^2; with retraining, 90% of the weights of the digit network went with no loss.
  • Unstructured sparsity saves memory only below a density of about one half and gives no speed-up on dense hardware; structured pruning gives a smaller dense network that is faster everywhere; 2:4 sparsity needs hardware support.
  • Lottery tickets: sparse subnetworks that train to full accuracy from the original initialization, found by pruning and resetting.
  • Distillation: α ℓCE+(1−α) T2DKL(softmax⁡(v/T) ∥ softmax⁡(z/T))\alpha\, \ell_{\text{CE}} + (1 - \alpha)\, T^2 D_{\mathrm{KL}}(\operatorname{softmax}(v/T) \,\|\, \operatorname{softmax}(z/T)); the soft gradient scales as 1/T21/T^2, hence the T2T^2; at high TT it matches logits.
  • Low-rank factorization replaces mnmn weights by r(m+n)r(m + n), worthwhile for r<mn/(m+n)r < mn/(m + n); truncated SVD is the best rank-rr approximation.
  • Measure the real speed-up in the deployment setting, and report parameters, multiply-adds and time.

Code#

Soft targets and the T2T^2 scaling, a teacher on the bundled digits, magnitude and structured pruning with fine-tuning, low-rank factorization of a layer, distillation into a small student with and without the 3s, and timings of a pruned layer. Seeds are fixed; exact numbers can differ slightly across platforms and library versions, and the timings vary between runs and machines. The script runs in about 20 seconds on a laptop CPU.

Data. Optical Recognition of Handwritten Digits, E. Alpaydin and C. Kaynak (1998), UCI Machine Learning Repository, CC BY 4.0. Loaded through scikit-learn; the transformations used in this example are specified in the code.

import time, warnings, torch
from torch import nn
import torch.nn.functional as F
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split

# 1. soft targets: teacher logits v = (5, 2, 0) at temperatures 1 and 4
v = torch.tensor([5.0, 2.0, 0.0])
print([round(p, 4) for p in F.softmax(v, 0).tolist()], [round(p, 4) for p in F.softmax(v / 4, 0).tolist()])
# [0.9465, 0.0471, 0.0064] [0.5685, 0.2686, 0.1629]

# 2. the soft loss's gradient shrinks like 1/T^2; multiplying by T^2 restores it
torch.manual_seed(0)
z, v10 = torch.randn(10, requires_grad=True), torch.randn(10)
for T in (1, 2, 4, 8, 16):
    soft = -(F.softmax(v10 / T, 0) * F.log_softmax(z / T, 0)).sum()
    g, = torch.autograd.grad(soft, z)
    approx = (z - v10 - (z - v10).mean()).norm() / (10 * T * T)   # the high-temperature formula
    print(T, round(g.norm().item(), 5), round(approx.item(), 5), round(T * T * g.norm().item(), 4))
# 1 0.38541 0.33842 0.3854 | 2 0.0889 0.08461 0.3556 | 4 0.0215 0.02115 0.3439
# 8 0.00532 0.00529 0.3403 | 16 0.00132 0.00132 0.3391   (T, |grad|, high-T formula, T^2 |grad|)

# 3. digits: 1257 training and 540 test images; a teacher 64 -> 512 -> 512 -> 10 with dropout
X, y = load_digits(return_X_y=True)
Xtr, Xte, ytr, yte = train_test_split(X / 16.0, y, test_size=0.3, random_state=0, stratify=y)
Xtr, Xte = torch.tensor(Xtr, dtype=torch.float32), torch.tensor(Xte, dtype=torch.float32)
ytr, yte = torch.tensor(ytr), torch.tensor(yte)

def acc(m, x=Xte, y=yte):
    with torch.no_grad():
        return round((m(x).argmax(1) == y).float().mean().item(), 3)

def train(model, loss_fn, epochs, rows=None, masks=None, seed=0):
    torch.manual_seed(seed)
    rows = torch.arange(len(Xtr)) if rows is None else rows
    opt = torch.optim.Adam(model.parameters(), lr=1e-3)
    for _ in range(epochs):
        model.train()
        for idx in rows[torch.randperm(len(rows))].split(64):
            opt.zero_grad()
            loss_fn(model(Xtr[idx]), idx).backward()
            opt.step()
            if masks:                                            # keep pruned weights at zero while fine-tuning
                with torch.no_grad():
                    for w, m in masks:
                        w.mul_(m)
    return model.eval()

hard = lambda out, idx: F.cross_entropy(out, ytr[idx])
torch.manual_seed(0)
teacher = train(nn.Sequential(nn.Linear(64, 512), nn.ReLU(), nn.Dropout(0.3), nn.Linear(512, 512), nn.ReLU(),
                              nn.Dropout(0.3), nn.Linear(512, 10)), hard, 60)
weights = [teacher[0].weight, teacher[3].weight, teacher[6].weight]
n_par = sum(p.numel() for p in teacher.parameters())
print(n_par, acc(teacher))                                       # 301066 0.976

# 4. global magnitude pruning: zero the smallest |w| over all weight matrices, then fine-tune 10 epochs
state = {k: t.clone() for k, t in teacher.state_dict().items()}
print('sparsity  pruned  fine-tuned  CSR bytes / dense bytes')
for s in (0.5, 0.8, 0.9, 0.95, 0.98):
    teacher.load_state_dict(state)
    allw = torch.cat([w.detach().abs().flatten() for w in weights])
    thr = allw.kthvalue(int(s * len(allw))).values
    masks = [(w, (w.detach().abs() > thr).float()) for w in weights]
    with torch.no_grad():
        for w, m in masks:
            w.mul_(m)
    a0 = acc(teacher)
    a1 = acc(train(teacher, hard, 10, masks=masks))
    csr = sum(8 * int(m.sum()) + 4 * (m.shape[0] + 1) for _, m in masks)   # fp32 value + int32 column per nonzero, row pointers
    print(f'{s:8.2f} {a0:7.3f} {a1:11.3f} {csr / (4 * len(allw)):10.3f}')
teacher.load_state_dict(state)
# sparsity  pruned  fine-tuned  CSR bytes / dense bytes
#     0.50   0.978       0.980      1.003
#     0.80   0.967       0.981      0.403
#     0.90   0.887       0.981      0.203
#     0.95   0.444       0.963      0.103
#     0.98   0.126       0.700      0.043

# 5. structured pruning: keep the hidden units with the largest incoming-weight norms; the result is a smaller dense network
def shrink(net, keep):
    i1 = net[0].weight.norm(dim=1).topk(keep).indices
    i2 = net[3].weight.norm(dim=1).topk(keep).indices
    small = nn.Sequential(nn.Linear(64, keep), nn.ReLU(), nn.Linear(keep, keep), nn.ReLU(), nn.Linear(keep, 10))
    with torch.no_grad():
        small[0].weight.copy_(net[0].weight[i1]); small[0].bias.copy_(net[0].bias[i1])
        small[2].weight.copy_(net[3].weight[i2][:, i1]); small[2].bias.copy_(net[3].bias[i2])
        small[4].weight.copy_(net[6].weight[:, i2]); small[4].bias.copy_(net[6].bias)
    return small.eval()
print('units  parameters  pruned  fine-tuned')
for keep in (256, 128, 64, 32):
    small = shrink(teacher, keep)
    print(f'{keep:5d} {sum(p.numel() for p in small.parameters()):11d} {acc(small):7.3f} {acc(train(small, hard, 10)):11.3f}')
# units  parameters  pruned  fine-tuned
#   256       85002   0.956       0.978
#   128       26122   0.869       0.970
#    64        8970   0.656       0.957
#    32        3466   0.369       0.922

# 6. low-rank factorization of the middle 512 x 512 layer by truncated SVD, no fine-tuning
W = teacher[3].weight.detach().clone()
U, S, Vh = torch.linalg.svd(W)
for r in (4, 8, 16, 32):
    with torch.no_grad():
        teacher[3].weight.copy_(U[:, :r] * S[:r] @ Vh[:r])
    err = (S[r:].square().sum() / S.square().sum()).sqrt().item()
    print(r, 2 * 512 * r, round(err, 3), acc(teacher))         # rank, parameters of the factors, relative error, accuracy
with torch.no_grad():
    teacher[3].weight.copy_(W)
# 4 4096 0.784 0.717 | 8 8192 0.628 0.976 | 16 16384 0.562 0.976 | 32 32768 0.524 0.976
torch.manual_seed(0)                                             # the teacher's initial weights, drawn again from the same seed
W0 = nn.Sequential(nn.Linear(64, 512), nn.Linear(512, 512))[1].weight.detach()
s_upd = torch.linalg.svdvals(W - W0)
print(round((s_upd[:8].square().sum() / s_upd.square().sum()).sqrt().item(), 3))   # share of the training update in 8 directions
# 0.911

# 7. distillation: a student 64 -> 32 -> 10 (2410 parameters), 150 epochs
with torch.no_grad():
    v_tr = teacher(Xtr)
def distill(T):
    return lambda out, idx: T * T * F.kl_div(F.log_softmax(out / T, 1), F.log_softmax(v_tr[idx] / T, 1),
                                             log_target=True, reduction='batchmean')
student = lambda: nn.Sequential(nn.Linear(64, 32), nn.ReLU(), nn.Linear(32, 10))
no3 = (ytr != 3).nonzero().squeeze(1)                            # the training images without a single 3
three = yte == 3
for name, loss_fn, rows in (('labels, all digits', hard, None), ('soft targets T=20, all digits', distill(20), None),
                            ('labels, no 3s', hard, no3), ('soft targets T=20, no 3s', distill(20), no3)):
    torch.manual_seed(0)
    s = train(student(), loss_fn, 150, rows=rows)
    with torch.no_grad():
        pred = s(Xte).argmax(1)
    print(f'{name:30} {acc(s):.3f}  3s recognized: {(pred[three] == 3).float().mean().item():.3f}')
# labels, all digits             0.974  3s recognized: 0.982
# soft targets T=20, all digits  0.965  3s recognized: 0.982
# labels, no 3s                  0.878  3s recognized: 0.000
# soft targets T=20, no 3s       0.965  3s recognized: 0.927

# 8. what the speed-up really is: the middle layer on a batch of 256, one thread, median of 200 runs
torch.set_num_threads(1)
xb = Xte[:256]
def ms(f):
    times = []
    for _ in range(200):
        t = time.perf_counter(); f(); times.append(time.perf_counter() - t)
    return 1000 * sorted(times)[100]
Wd = teacher[3].weight.detach()
W90 = Wd * (Wd.abs() > Wd.abs().flatten().kthvalue(int(0.9 * Wd.numel())).values)
warnings.filterwarnings('ignore', 'Sparse CSR')               # CSR support is marked beta
Wcsr = W90.to_sparse_csr()
h = torch.relu(teacher[0](xb)).detach()
with torch.no_grad():
    t_dense = ms(lambda: h @ Wd.T)
    t_zeros = ms(lambda: h @ W90.T)
    t_csr = ms(lambda: (Wcsr @ h.T).T)
    W51 = Wd[:51].contiguous()
    t_small = ms(lambda: h @ W51.T)
print(round(t_dense, 3), round(t_zeros / t_dense, 2), round(t_csr / t_dense, 2), round(t_small / t_dense, 2))
# e.g. 0.124 1.0 38.21 0.2: dense ms, then relative times for 90% zeros kept dense, the same in CSR, 51 of 512 rows kept

Next#

Every note of the course has ended with a network that trains: its loss falls, its accuracy rises, its numbers come out as printed. Getting there on a new problem is rarely that smooth. A bug in the data pipeline, a learning rate ten times too large or a missing call to model.eval() all produce a network that trains, just badly, without any error message. The last note is a recipe for training a network from scratch that catches these failures early: the checks to run before the first real experiment, how to choose the learning rate, how to read the curves, and how to keep runs reproducible and within a compute budget.

References#