Pruning & Structured Sparsity

Removing redundant parameters: magnitude pruning, movement pruning, structured vs unstructured, the lottery ticket hypothesis, SparseGPT, and Wanda — achieving 50-70% sparsity without quality loss.

Advanced

Prerequisites

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. Unstructured Pruning
  5. Structured Pruning
  6. The Lottery Ticket Hypothesis
  7. SparseGPT: One-Shot LLM Pruning
  8. Wanda: Pruning by Weights and Activations
  9. N:M Sparsity (Hardware Support)
  10. Common Pitfalls
  11. Summary
  12. Exercises

Learning Objectives

  1. Distinguish unstructured and structured pruning and their hardware implications.
  2. State the lottery ticket hypothesis and its implications.
  3. Derive SparseGPT's one-shot pruning algorithm.
  4. Explain N:M sparsity and NVIDIA's hardware support.
  5. Analyze the sparsity-quality tradeoff for LLMs.

Notation

  • M{0,1}d×k\mathbf{M} \in \{0, 1\}^{d \times k} — binary mask
  • ss — sparsity level (fraction of zeros)
  • N:MN:M — structured sparsity pattern (NN zeros per MM elements)

Core Intuition

Neural networks are massively over-parameterized — many weights contribute negligibly to the output. Pruning removes these redundant weights, creating a sparse model that's smaller and potentially faster. The challenge: identifying which weights to remove without degrading accuracy, and making the resulting sparsity pattern hardware-friendly.

Interactive: Network Pruning

Weight Matrix (8×8)

-1.5
-2.8
-2.1
-1.0
-1.2
-2.0
-2.3
-2.4
-1.1
-1.1
-2.7
-1.6
-2.8
-1.0
-1.0
-2.3
-2.5
-1.4
-1.0
-2.5
-2.8
-1.9
-2.2
-2.7
-2.5
-1.1
-1.8
-1.7
-1.3
-2.3
-1.3
-1.2

Sparsity

50.0%

Remaining

32/64

Compression

2.0×

Pruning method:

Compare: Magnitude pruning removes smallest weights (often best accuracy). Random is a baseline. Structured pruning removes entire rows (better for hardware acceleration but less precise).

Unstructured Pruning

Remove individual weights based on importance:

W^=WM,Mij={1wij>τ0wijτ.(1)\hat{\mathbf{W}} = \mathbf{W} \odot \mathbf{M}, \quad M_{ij} = \begin{cases}1 & |w_{ij}| > \tau \\ 0 & |w_{ij}| \leq \tau\end{cases}. \tag{1}

Magnitude pruning: Remove smallest-magnitude weights. Simple, effective baseline.

Movement pruning: Remove weights whose magnitude is DECREASING during training (they're being trained toward zero):

importance(w)=wLw.(2)\text{importance}(w) = -w \cdot \frac{\partial\mathcal{L}}{\partial w}. \tag{2}

Limitation: Random sparsity patterns don't map to hardware acceleration. A 90% sparse matrix stored naively is just as large; need sparse formats (CSR, CSC) which have overhead.


Structured Pruning

Remove entire structural units:

  • Neuron pruning: Remove entire rows/columns of weight matrices.
  • Head pruning: Remove entire attention heads.
  • Layer pruning: Remove entire transformer layers.
  • Channel pruning: Remove feature map channels (for CNNs).

Advantage: Result is a smaller dense model — no special sparse hardware needed.

Disadvantage: Coarser granularity; can't achieve high sparsity without significant quality loss.


The Lottery Ticket Hypothesis

Frankle & Carlin (2019): A randomly-initialized dense network contains a sparse subnetwork (the "winning ticket") that, when trained in isolation from the same initialization, reaches comparable accuracy.

Implication: The full model is needed for finding the right architecture (which weights matter), but not for the final trained model.

Practical issue: Finding the winning ticket requires training the full model first (iterative pruning + rewinding), making it impractical for LLMs.


SparseGPT: One-Shot LLM Pruning

Problem: Prune a pre-trained LLM to 50-60% sparsity without any retraining.

Method (Frantar & Alistarh, 2023): Apply OBS-style (Optimal Brain Surgeon) reasoning column by column (same framework as GPTQ):

  1. For each column: identify the weight to prune (smallest importance-weighted magnitude).
  2. Set it to zero.
  3. Compensate remaining weights using Hessian information:
Δwj+1:=wj[H1]jjHj,j+1:1.(3)\Delta\mathbf{w}_{j+1:} = -\frac{w_j}{[\mathbf{H}^{-1}]_{jj}} \cdot \mathbf{H}^{-1}_{j, j+1:}. \tag{3}

Result: 50% unstructured sparsity on GPT-175B with negligible perplexity increase. 60% with minor degradation.


Wanda: Pruning by Weights and Activations

Simpler alternative (Sun et al., 2023): Importance = weight magnitude × input activation magnitude:

score(wij)=wijX:,j2.(4)\text{score}(w_{ij}) = |w_{ij}| \cdot \|\mathbf{X}_{:,j}\|_2. \tag{4}

Prune weights with lowest score. No Hessian computation needed.

Key insight: A large weight on a channel that's always near-zero is unimportant. A small weight on a highly active channel matters more.

Quality: Comparable to SparseGPT at 50% sparsity; slightly worse at higher sparsity. Much faster to compute.


N:M Sparsity (Hardware Support)

NVIDIA Ampere/Hopper: Hardware support for 2:4 sparsity — exactly 2 zeros per group of 4 elements.

2:4 pattern: 50% sparsity with structured layout that maps to sparse tensor cores. 2x speedup with dedicated hardware.

Training with N:M:

  1. Train dense model normally.
  2. Apply 2:4 mask based on magnitude within each group of 4.
  3. Fine-tune the remaining weights.

Result: Consistent 2x inference speedup with less than 1% accuracy loss for most models.


Common Pitfalls

Pitfall 1. Expecting unstructured sparsity to provide speedup without special hardware/software. Standard CUDA kernels don't benefit from random zeros; need sparse libraries (cuSPARSE) or N:M hardware.

Pitfall 2. Pruning uniformly across layers. Earlier layers (embeddings) and final layers (LM head) are much more sensitive. Use per-layer sparsity targets.

Pitfall 3. Pruning and quantization simultaneously without care. Both introduce error; naive combination can be catastrophic. Apply sequentially with compensation.


Summary

  • Unstructured: Flexible, high sparsity possible, but needs sparse hardware.
  • Structured: Dense result (any hardware), but coarser → lower achievable sparsity.
  • Lottery Ticket: Sparse winning tickets exist but are expensive to find.
  • SparseGPT/Wanda: One-shot LLM pruning to 50-60% without retraining.
  • 2:4 sparsity: Hardware-supported 50% sparsity with 2x speedup on NVIDIA GPUs.

Exercises

Exercise 1. For a 7B model pruned to 50% unstructured sparsity: compute the model size in CSR format vs dense FP16.

Exercise 2. Derive the Wanda score for a weight w=0.01w=0.01 with input activation norm 100 vs w=0.5w=0.5 with activation norm 0.1. Which gets pruned?

Exercise 3. For 2:4 sparsity in a layer with 4096 input features: how many possible sparsity patterns exist per group?

Exercise 4. Explain why structured pruning (removing entire heads) often hurts less than expected (hint: redundancy across heads).

Exercise 5. Design a combined pruning + quantization pipeline for a 70B model targeting 4x size reduction with minimal quality loss.