Skip to content
Inference Academy

Sparsity, pruning and distillation: making the model itself smaller

The techniques that cut work by removing weights or whole parts of a model rather than shrinking each number: unstructured and 2:4 structured sparsity, magnitude versus activation-aware (Wanda) pruning, activation sparsity, layer and width pruning, and distillation into a smaller student. A runnable experiment compares their error and their bytes per weight with quantisation, and shows why quantisation usually wins for decode while pruning plus distillation produces the best small models.

By

16 min · verified 2026-111 animated diagramPro
Share

Quantisation keeps every weight and stores each in fewer bits. The techniques in this lesson take the other route: remove weights, neurons, heads or whole layers, or train a smaller model to imitate the big one. They are less common in day-to-day LLM serving than quantisation, and this lesson shows why with numbers. They still matter: structured sparsity has hardware support, and pruning plus distillation is how many of the best small open models are made.

You need Python 3 and numpy.

#The menu

Technique What is removed Needs retraining? Hardware speed-up
Unstructured pruning Individual weights anywhere Light or none Little on GPUs (irregular memory access)
2:4 structured sparsity 2 of every 4 consecutive weights Usually some Up to 2× matmul throughput on NVIDIA tensor cores (Ampere and later)
Activation sparsity Neurons that output zero for this token Sometimes (ReLU-style activations) Only if the zeros are predicted in advance
Width pruning Whole neurons, heads or channels Yes (distillation) Full: the model is simply smaller
Depth pruning Whole layers Yes (distillation) Full
Distillation Everything: a new, smaller student Yes, it is training Full

The pattern: the less regular the removal, the less hardware can turn it into speed. Removing whole layers or neurons gives a normal, smaller dense model that every engine runs fast. Removing scattered weights gives a model the same shape full of zeros, which a GPU reads and multiplies just as before unless the pattern is one it knows how to skip.

#Which weights to remove

The classic rule is to remove the smallest weights. LLMs break that rule: a handful of hidden channels carry values tens of times larger than the rest (outliers), so a small weight on an outlier channel can matter more than a large weight elsewhere. Wanda (Sun et al., 2023) scores each weight by ∣weight∣×typical size of its input| \text{weight} | \times \text{typical size of its input}, measured on a little calibration data, with no retraining.

The experiment below builds a layer whose inputs have 8 outlier channels, removes half its weights in four ways, and compares output error with INT4 quantisation:

python
import numpy as np

rng = np.random.default_rng(0)
d_in, d_out, n = 1024, 1024, 2048
W = rng.normal(size=(d_out, d_in)) / np.sqrt(d_in)
scale = np.ones(d_in); scale[rng.choice(d_in, 8, replace=False)] = 30   # a few outlier input channels, as LLMs have
X = rng.normal(size=(n, d_in)) * scale
Y = X @ W.T

def err(Wp):
    return np.linalg.norm(X @ Wp.T - Y) / np.linalg.norm(Y)

def prune_unstructured(score, frac=0.5):          # zero the lowest-scoring half in each output row
    k = int(d_in * frac)
    idx = np.argsort(score, axis=1)[:, :k]
    M = np.ones_like(W); np.put_along_axis(M, idx, 0, axis=1)
    return W * M

def prune_2_4(score):                              # in every group of 4 inputs keep the 2 best
    s = score.reshape(d_out, d_in // 4, 4)
    drop = np.argsort(s, axis=2)[:, :, :2]
    M = np.ones_like(s); np.put_along_axis(M, drop, 0, axis=2)
    return W * M.reshape(d_out, d_in)

magnitude = np.abs(W)
wanda = np.abs(W) * np.linalg.norm(X, axis=0)     # weight size times how big its input usually is
print("1. Relative output error after removing 50% of weights")
print(f"  unstructured, by magnitude: {err(prune_unstructured(magnitude)):.3f}")
print(f"  unstructured, by Wanda:     {err(prune_unstructured(wanda)):.3f}")
print(f"  2:4, by magnitude:          {err(prune_2_4(magnitude)):.3f}")
print(f"  2:4, by Wanda:              {err(prune_2_4(wanda)):.3f}")
print(f"  INT4 per-group-128 (for comparison): ", end="")
Wg = W.reshape(d_out, -1, 128); s = np.abs(Wg).max(2, keepdims=True) / 7
print(f"{err((np.clip(np.round(Wg / s), -8, 7) * s).reshape(d_out, d_in)):.3f}")
text
1. Relative output error after removing 50% of weights
  unstructured, by magnitude: 0.273
  unstructured, by Wanda:     0.096
  2:4, by magnitude:          0.363
  2:4, by Wanda:              0.130
  INT4 per-group-128 (for comparison): 0.118

Three lessons in five lines:

  1. Scoring matters more than the pattern. Wanda cut the error by about two thirds at the same sparsity.
  2. The 2:4 constraint costs accuracy. It cannot remove two weights from the same group of four even when both are unimportant, so it is worse than unstructured pruning by either score.
  3. INT4 quantisation is about as accurate as good 2:4 pruning on this layer. The difference is in what each saves.
Ways to remove weights
Animated

Weights kept

100%

GPU speed-up

1×

A dense block of weights

Darker cells are larger weights. A GPU reads and multiplies every one of them.

Each cell is a weight; colour depth = magnitude. Dashed boxes mark groups of four.

Pro content

Keep reading with Inference Academy Pro

Unlock the rest of this lesson, plus every advanced lesson, hands-on lab, the larger playground models and completion certificates.

Still to come in this lesson

  1. What each saves at decode time
  2. Activation sparsity
  3. Removing layers and width
  4. Distillation
  5. Choosing
  6. What you can now do
  7. Common mistakes
  8. Exercise
  9. Going deeper

You will be able to answer

  • Why did activation-aware (Wanda) pruning beat magnitude pruning in the lesson's experiment?
  • What is 2:4 structured sparsity?
  • For memory-bound decode, how do 2:4 sparsity and INT4 quantisation compare in the lesson's byte count?
  • Why do pruned small models (such as NVIDIA's Minitron family) use distillation afterwards?