Search

Search papers and lessons

A Simple and Effective Pruning Approach for Large Language Models

Mingjie Sun, Zhuang Liu, Anna Bair et al. · 2023

Wanda prunes a pretrained LLM in one pass by scoring each weight as its magnitude times its input's norm, removing half the weights with no retraining and no weight update.

On this page

  • Colour = topic mix
  • Linear algebra
  • Calculus
  • Discrete math
  • Neural network basics
  • Probability
  • Architectures
  • ReadReady to readNot yet

TL;DR

Wanda is a one-shot method for removing half, or more, of the weights of a trained large language model without retraining it and without changing the weights that remain. It scores each weight by its magnitude times the typical size of the input it multiplies, and it removes the lowest-scoring weights within each output neuron. On LLaMA-7B with half the weights removed, it reaches a WikiText perplexity of 7.26, against 5.68 for the dense model and 17.29 for plain magnitude pruning.

Builds on: Pruning, Language Model, Transformer · Leads to: the LLaMA models it was tested on, Model Compression

Why it mattered

Pruning had been studied for decades, but for large language models the usual options were expensive. Pruning with retraining needs a large training budget, and the strongest one-shot method at the time, SparseGPT, solves a second-order reconstruction problem for every layer. Wanda showed that a much simpler score, with no weight update and no matrix inversion, was enough to make pruning competitive on LLaMA and LLaMA-2 models.

The authors hope it improves understanding of sparsity in LLMs, and they suggest its speed could help sparse training, where pruning has to be repeated. Its speed also makes it a practical tool for producing sparse versions of open models. The paper's own argument is that a pretrained language model contains sparse subnetworks that are exact, in the sense that they work without any change to the surviving weights.

The problem

A pruned model has many weights set to exactly zero, and the question is which ones. The obvious rule is magnitude pruning: remove the smallest weights. That rule works on small image networks, but the paper's own experiments show it failing catastrophically on OPT-13B and Pythia-12B even at 20 percent sparsity. The reason is that large language models contain a few input features whose values are about 100 times larger than typical ones, and these features emerge once models reach roughly 6 billion parameters (LLM.int8() documents them). A small weight that multiplies a large feature can matter far more than a large weight that multiplies a tiny one.

The known fix, SparseGPT, takes this into account by using second-order information about each layer's inputs. That is accurate, but it needs a Hessian-based update of the remaining weights, which costs time that the paper wants to avoid.

How it works

Take one output neuron of a linear layer. Its weights are one row of the layer's weight matrix, and its inputs are the features coming from the previous layer. The walkthrough uses one row with four weights and a target of keeping half of them.

  1. Start with one row. The weights are (0.3,−0.6,0.1,0.4)(0.3, -0.6, 0.1, 0.4). Check: four weights, and the target is to keep two.
  2. Measure the inputs on calibration data. Run a small set of text, which the paper draws from the C4 training data (a large web-text corpus), through the model. For each input feature, record the ℓ2\ell_2 norm (see Vector Norm) of its values across all tokens. Suppose the norms are (2.0,0.5,8.0,1.0)(2.0, 0.5, 8.0, 1.0). Check: input 3 is by far the largest feature, so it is the one with the most influence.
  3. Score each weight. Multiply each weight's magnitude by the norm of its input. The scores are 0.3×2.0=0.60.3 \times 2.0 = 0.6, 0.6×0.5=0.30.6 \times 0.5 = 0.3, 0.1×8.0=0.80.1 \times 8.0 = 0.8, and 0.4×1.0=0.40.4 \times 1.0 = 0.4. Check: the weight 0.10.1 is the smallest magnitude, but its score is the largest.
  4. Compare within the row. Keep the two highest scores, 0.80.8 and 0.60.6, which belong to inputs 3 and 1. Zero the weights for inputs 2 and 4. Check: the kept weights are 0.10.1 (input 3) and 0.30.3 (input 1), and the removed ones are −0.6-0.6 and 0.40.4.
  5. Compare with magnitude pruning. Magnitude pruning would keep the two largest magnitudes, −0.6-0.6 and 0.40.4, and remove 0.30.3 and 0.10.1. Check: the two methods disagree on inputs 1 and 3, and input 3 is the one with the large norm.
  6. Repeat for every row, without changing the survivors. Each output neuron gets its own comparison group, so every row keeps half its weights. The surviving weights keep their original values. Check: no weight is adjusted, which is the point of Wanda.
  7. Move to the next layer. After a layer is pruned, the next layer's input norms are measured on the pruned model, so the statistics reflect the damage already done. Check: pruning runs from the first layer to the last in a single forward pass.
flowchart LR
  C[Calibration text] --> N[Input norms per feature]
  W[Trained weights] --> S[Score = magnitude times input norm]
  N --> S
  S --> G[Compare scores within each output row]
  G --> M[Zero the lowest scores in each row]
  M --> NEXT[Next layer sees the pruned outputs]

The math

Definition: Wanda pruning score

For a linear layer with weight matrix W\mathbf{W} of shape (Cout,Cin)(C_{out}, C_{in}), the Wanda score of weight Wij\mathbf{W}_{ij} is

Sij=∣Wij∣⋅∥Xj∥2,\mathbf{S}_{ij} = |\mathbf{W}_{ij}| \cdot \lVert \mathbf{X}_j \rVert_2,

where X\mathbf{X} is the matrix of input activations on calibration data, of shape (N×L,Cin)(N \times L, C_{in}), with NN sequences of LL tokens each, and ∥Xj∥2\lVert \mathbf{X}_j \rVert_2 is the ℓ2\ell_2 norm of input feature jj across all of those tokens.

Step 1 in symbols: the input norm

The norm of feature jj is

∥Xj∥2=(∑t=1NL(Xtj)2)1/2,\lVert \mathbf{X}_j \rVert_2 = \Big( \sum_{t=1}^{N L} \big(X_{tj}\big)^2 \Big)^{1/2},

where XtjX_{tj} is the value of feature jj at token tt. It is a single number per input feature, so it can be estimated once from a modest calibration set. The paper finds little gain beyond 128 sequences, and even a single sequence gives a usable estimate. See Vector Norm for the norm itself.

Step 2 in symbols: the comparison group

The paper compares weights within each output row. The comparison group of weight Wij\mathbf{W}_{ij} is

Gi={Wiu:u=1,…,Cin},\mathbf{G}_i = \{\mathbf{W}_{iu} : u = 1, \ldots, C_{in}\},

all the weights that feed output neuron ii. For a sparsity ratio ss, the method zeros the ss fraction of Gi\mathbf{G}_i with the lowest scores. Every row therefore loses the same share of its weights. This is the per-output choice, and it contrasts with per-layer pruning, which compares all weights in the matrix together.

Step 3 in symbols: where the product comes from

Consider one output y=∑jwjxjy = \sum_j w_j x_j. Removing weight jj changes the output by wjxjw_j x_j. If the inputs have very different scales, the sensible choice is to remove the weight whose product ∣wj∣ ∣xj∣|w_j| \, |x_j| is smallest. Wanda uses the norm of each input across the calibration data as a proxy for the size of ∣xj∣|x_j|, which makes the score a product of two numbers that are cheap to compute.

SparseGPT scores weights by [ ∣W∣2/diag((XTX+λI)−1) ]ij[\,|\mathbf{W}|^{2} / \mathrm{diag}\big((\mathbf{X}^{T}\mathbf{X} + \lambda \mathbf{I})^{-1}\big)\,]_{ij}, where XTX+λI\mathbf{X}^{T}\mathbf{X} + \lambda \mathbf{I} is the Hessian of the layer-wise reconstruction problem and λ\lambda is a damping term. Set λ=0\lambda = 0 and keep only the diagonal of the Hessian, and the inverse's diagonal becomes 1/∥Xj∥221/\lVert \mathbf{X}_j \rVert_2^2. The SparseGPT score then becomes (∣Wij∣ ∥Xj∥2)2(|\mathbf{W}_{ij}| \, \lVert \mathbf{X}_j \rVert_2)^2, the square of the Wanda score. Squaring does not change the order of positive numbers, so within a row the two methods rank weights identically under this approximation. Wanda therefore keeps the ranking and drops the inverse.

Step 5 in symbols: structured N:M sparsity

For hardware that supports structured sparsity, the paper keeps at most NN nonzero weights in every block of MM consecutive weights of a row. In a 2:4 pattern, each block of four keeps its two highest scores. The score is the same; only the grouping changes.

Worked examples

Example
1. Tiny: two inputs of very different size (tiny)

This is the motivating neuron of the paper: y=w1x1+w2x2y = w_1 x_1 + w_2 x_2, with ∣w1∣≤∣w2∣|w_1| \le |w_2|. Take w1=0.5w_1 = 0.5 and w2=0.8w_2 = 0.8, and suppose a typical input has x1=10x_1 = 10 and x2=0.1x_2 = 0.1.

  1. Magnitude pruning compares ∣w1∣=0.5|w_1| = 0.5 with ∣w2∣=0.8|w_2| = 0.8 and removes w1w_1.
  2. The contribution lost is w1x1=0.5×10=5w_1 x_1 = 0.5 \times 10 = 5.
  3. Wanda scores ∣w1∣⋅∣x1∣=0.5×10=5|w_1| \cdot |x_1| = 0.5 \times 10 = 5 and ∣w2∣⋅∣x2∣=0.8×0.1=0.08|w_2| \cdot |x_2| = 0.8 \times 0.1 = 0.08. It removes w2w_2.
  4. The contribution lost is w2x2=0.8×0.1=0.08w_2 x_2 = 0.8 \times 0.1 = 0.08.
  5. Interpretation: removing the weight with the smaller magnitude cost 5 units of output, while removing the weight with the larger magnitude cost 0.08. The input scale decided which weight was unimportant.
Example
2. Standard: one row, two selections (standard)

Use the row (0.3,−0.6,0.1,0.4)(0.3, -0.6, 0.1, 0.4) and the input norms (2.0,0.5,8.0,1.0)(2.0, 0.5, 8.0, 1.0) from the walkthrough. Keep two of four.

  1. Magnitudes: (0.3,0.6,0.1,0.4)(0.3, 0.6, 0.1, 0.4). The top two are inputs 2 and 4, so magnitude pruning keeps −0.6-0.6 and 0.40.4.
  2. Wanda scores: (0.6,0.3,0.8,0.4)(0.6, 0.3, 0.8, 0.4). The top two are inputs 3 and 1, so Wanda keeps 0.10.1 and 0.30.3.
  3. The two methods remove different weights in the same row. Magnitude removes 0.30.3 and 0.10.1. Wanda removes −0.6-0.6 and 0.40.4.
  4. Measure what each choice throws away, using the Wanda score as a rough guide to each weight's share of the output. Magnitude removes inputs 1 and 3, whose scores are 0.60.6 and 0.80.8, a total of 1.41.4. Wanda removes inputs 2 and 4, whose scores are 0.30.3 and 0.40.4, a total of 0.70.7. Wanda removes less.
  5. Interpretation: the row contains one weight with a small magnitude attached to a large feature. Wanda keeps it; magnitude discards it.

Try it: in the same row, Wanda removes inputs 2 and 4. What is the sum of the removed Wanda scores? Answer: 0.3+0.4=0.70.3 + 0.4 = 0.7.

Example
3. Realistic: LLaMA-7B at half sparsity and at 2:4 (realistic)

The paper reports WikiText perplexity for LLaMA-7B. Lower is better. The dense model scores 5.68.

  1. At 50 percent unstructured sparsity, magnitude pruning scores 17.29, Wanda scores 7.26, and SparseGPT scores 7.22.
  2. The increase over dense is 17.29−5.68=11.6117.29 - 5.68 = 11.61 for magnitude and 7.26−5.68=1.587.26 - 5.68 = 1.58 for Wanda.
  3. At 2:4 structured sparsity, magnitude scores 42.13, Wanda scores 11.53, and SparseGPT scores 11.00.
  4. The increase over dense at 2:4 is 42.13−5.68=36.4542.13 - 5.68 = 36.45 for magnitude and 11.53−5.68=5.8511.53 - 5.68 = 5.85 for Wanda.
  5. Interpretation: under 2:4 sparsity magnitude pruning loses more than six times as much perplexity as Wanda does, and Wanda comes within about half a point of SparseGPT, which does a second-order update.

Try it: under 2:4 sparsity, how many times larger is magnitude's increase over dense than Wanda's? Answer: 36.45/5.85≈6.236.45 / 5.85 \approx 6.2.

Results

Main results on LLaMA and LLaMA-2. On zero-shot tasks, averaged over seven benchmarks, Wanda at 50 percent unstructured sparsity scores 54.21 on LLaMA-7B, against 46.94 for magnitude pruning, 54.94 for SparseGPT and 59.99 for the dense model. The paper reports that the gap to dense narrows as models grow, and that 50 percent sparse LLaMA-65B and LLaMA-2-70B match the zero-shot accuracy of their dense versions.

Language modeling. Perplexity results on WikiText follow the same pattern. At 50 percent sparsity Wanda is on par with SparseGPT for most models, and it is far ahead of magnitude pruning. For structured 2:4 sparsity, SparseGPT is better on the smallest LLaMA model, while Wanda is better on the larger ones, for example LLaMA-30B and LLaMA-2-70B.

Calibration data. Wanda is robust to how much calibration data it sees. A single sequence gives a perplexity of about 7.5 on LLaMA-7B at 50 percent sparsity, against 7.26 with the full set of 128 sequences. The paper suggests this is because input norms are much easier to estimate than the inverse Hessian that SparseGPT needs.

Speed. The paper reports that computing the Wanda metric can be 300 times faster than SparseGPT's pruning step for LLMs. For inference, the 2:4 structured pattern gives about a 1.6 times speedup on the linear layers in a simulation of the matrix kernel, and an end-to-end latency of 251 milliseconds against 312 milliseconds for LLaMA-7B. The paper states that the speedup comes from the sparsity pattern in general, not from Wanda in particular.

Other model families. In the appendix, the paper extends the results to OPT, BLOOM and Pythia. Magnitude pruning fails badly on some of them, for example OPT-13B at 20 percent sparsity, while Wanda stays within about 0.3 perplexity points of dense at 10 to 30 percent sparsity.

Reading guide

The paper is short, and each section maps onto the lessons it needs.

  • Abstract and Section 1. Needs Pruning for the idea of zeroing weights, and Quantization for the alternative of shrinking each number instead.
  • Section 2, Preliminaries. Needs Language Model and Transformer for the models being pruned, and LLM.int8() for the outlier features that motivate the method. The background on magnitude pruning cites the lottery ticket work on extremely sparse trainable networks.
  • Section 3, the method. Needs Vector Norm for the ℓ2\ell_2 norm, and Hessian Matrix for the remark that relates Wanda to SparseGPT. Algorithm 1 is a few lines of PyTorch and is worth reading directly.
  • Section 4, experiments. Needs Perplexity for the language-modeling metric, and the zero-shot tables are averages over tasks. Read the tables with the sparsity column in mind, because the rows change meaning across sparsity patterns.
  • Section 5, analysis. Needs Fine-Tuning and LoRA for the recovery experiments, and the inverse Hessian for the explanation of why Wanda needs less calibration data.
  • Appendices. The image classifier comparison shows that per-output grouping is specific to language models.

Check yourself

QuestionRecall: what is the Wanda score of a weight, and why does Wanda compare weights within each output row?Show answer

The score is the absolute weight multiplied by the ℓ2\ell_2 norm of its input feature, measured on calibration data. Each output neuron gets its own comparison group, so every row keeps the same share of its weights, which the paper finds works better for LLMs than comparing across a whole layer.

QuestionComputation: a weight has magnitude 0.2 and its input feature has a norm of 6. What is its Wanda score, and what is its score if the feature's norm is 0.5?Show answer

With norm 6 the score is 0.2×6=1.20.2 \times 6 = 1.2. With norm 0.5 it is 0.2×0.5=0.10.2 \times 0.5 = 0.1. The same weight is worth twelve times more when its input is large.

QuestionWhy: why does the magnitude rule fail for LLMs even when it works on small networks?Show answer

LLMs contain a few input features with values far larger than the rest, and these emerge at scale. A weight with a small magnitude that multiplies such a feature can matter more than a large weight that multiplies a small one. Magnitude ignores the input, so it removes the wrong weights in these rows.

QuestionApplied: you want to halve the weights of an open LLM on a single GPU, without a training budget, and the target hardware has 2:4 sparse tensor cores. Which approach would you try first, and what would you check?Show answer

Wanda is a reasonable first try, because it needs only a small calibration set and a single forward pass, and it supports the 2:4 pattern. Check the perplexity of the pruned model on a held-out text set against the dense model, not only the zero-shot scores. Then measure the actual latency on the target hardware, since the speedups in the paper come from a simulation. If the gap is too large, a short fine-tuning pass or a second-order method such as SparseGPT is the next option to compare.

Sources

  • Sun, M., Liu, Z., Bair, A., and Kolter, J. Z. (2023), A Simple and Effective Pruning Approach for Large Language Models, arXiv:2306.11695. https://arxiv.org/abs/2306.11695
  • Han, S., Pool, J., Tran, J., and Dally, W. (2015), Learning both Weights and Connections for Efficient Neural Networks, arXiv:1506.02626. https://arxiv.org/abs/1506.02626
  • Dettmers, T., Lewis, M., Belkada, Y., and Zettlemoyer, L. (2022), LLM.int8(): 8-bit Matrix Multiplication for Transformers at Scale, arXiv:2208.07339. https://arxiv.org/abs/2208.07339

Read the full paper

Mingjie Sun, Zhuang Liu, Anna Bair et al., 2023. arXiv:2306.11695