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.
- Start with one row. The weights are . Check: four weights, and the target is to keep two.
- 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 norm (see Vector Norm) of its values across all tokens. Suppose the norms are . Check: input 3 is by far the largest feature, so it is the one with the most influence.
- Score each weight. Multiply each weight's magnitude by the norm of its input. The scores are , , , and . Check: the weight is the smallest magnitude, but its score is the largest.
- Compare within the row. Keep the two highest scores, and , which belong to inputs 3 and 1. Zero the weights for inputs 2 and 4. Check: the kept weights are (input 3) and (input 1), and the removed ones are and .
- Compare with magnitude pruning. Magnitude pruning would keep the two largest magnitudes, and , and remove and . Check: the two methods disagree on inputs 1 and 3, and input 3 is the one with the large norm.
- 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.
- 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
For a linear layer with weight matrix of shape , the Wanda score of weight is
where is the matrix of input activations on calibration data, of shape , with sequences of tokens each, and is the norm of input feature across all of those tokens.
Step 1 in symbols: the input norm
The norm of feature is
where is the value of feature at token . 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 is
all the weights that feed output neuron . For a sparsity ratio , the method zeros the fraction of 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 . Removing weight changes the output by . If the inputs have very different scales, the sensible choice is to remove the weight whose product is smallest. Wanda uses the norm of each input across the calibration data as a proxy for the size of , which makes the score a product of two numbers that are cheap to compute.
Step 4 in symbols: the link to second-order pruning
SparseGPT scores weights by , where is the Hessian of the layer-wise reconstruction problem and is a damping term. Set and keep only the diagonal of the Hessian, and the inverse's diagonal becomes . The SparseGPT score then becomes , 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 nonzero weights in every block of 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
This is the motivating neuron of the paper: , with . Take and , and suppose a typical input has and .
- Magnitude pruning compares with and removes .
- The contribution lost is .
- Wanda scores and . It removes .
- The contribution lost is .
- 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.
Use the row and the input norms from the walkthrough. Keep two of four.
- Magnitudes: . The top two are inputs 2 and 4, so magnitude pruning keeps and .
- Wanda scores: . The top two are inputs 3 and 1, so Wanda keeps and .
- The two methods remove different weights in the same row. Magnitude removes and . Wanda removes and .
- 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 and , a total of . Wanda removes inputs 2 and 4, whose scores are and , a total of . Wanda removes less.
- 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: .
The paper reports WikiText perplexity for LLaMA-7B. Lower is better. The dense model scores 5.68.
- At 50 percent unstructured sparsity, magnitude pruning scores 17.29, Wanda scores 7.26, and SparseGPT scores 7.22.
- The increase over dense is for magnitude and for Wanda.
- At 2:4 structured sparsity, magnitude scores 42.13, Wanda scores 11.53, and SparseGPT scores 11.00.
- The increase over dense at 2:4 is for magnitude and for Wanda.
- 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: .
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 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 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 . With norm 0.5 it is . 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