Education
7 min read

Weight Pruning in Large Language Models: Mathematical Foundations of Magnitude Pruning, Wanda, SparseGPT, and Semi-Structured 2:4 Sparsity

Weight Pruning in Large Language Models: Mathematical Foundations of Magnitude Pruning, Wanda, SparseGPT, and Semi-Structured 2:4 Sparsity As frontier transformer models expand past hundreds of billions of parameters, model serving is constrained by memory capacity and memory bandwidth. In autoregressive token generation, memory bandwidth dominates latency because memory-bound matrix-vector operations require streaming entire weight matrices from High Bandwidth Memory (HBM) to on-chip SRAM for

Weight Pruning in Large Language Models: Mathematical Foundations of Magnitude Pruning, Wanda, SparseGPT, and Semi-Structured 2:4 Sparsity

Weight Pruning in Large Language Models: Mathematical Foundations of Magnitude Pruning, Wanda, SparseGPT, and Semi-Structured 2:4 Sparsity

As frontier transformer models expand past hundreds of billions of parameters, model serving is constrained by memory capacity and memory bandwidth. In autoregressive token generation, memory bandwidth dominates latency because memory-bound matrix-vector operations require streaming entire weight matrices from High Bandwidth Memory (HBM) to on-chip SRAM for every single generated token.

Weight pruning reduces model size and memory bandwidth pressure by setting a fraction of weight parameters to zero. While classical pruning methods relied on iterative magnitude thresholding followed by extensive retraining, trillion-parameter foundation models cannot easily undergo compute-intensive fine-tuning cycles after compression. Modern post-training pruning techniques focus on one-shot, calibration-guided algorithms that sparsify models without backpropagation or parameter retraining.

Mathematical comparison of weight pruning metrics: magnitude, input activations, and second-order Hessian curvature

The Mathematical Framework of Weight Pruning

Consider a single linear layer in a transformer network parameterized by weight matrix W∈Rdout×dinW \in \mathbb{R}^{d_{\text{out}} \times d_{\text{in}}}. Given an input activation tensor X∈Rdin×NX \in \mathbb{R}^{d_{\text{in}} \times N} collected over NN calibration tokens, the layer computes output Y=WXY = W X.

The goal of post-training weight pruning is to find a sparse weight matrix W^\widehat{W} subject to a target sparsity constraint s∈(0,1)s \in (0, 1) such that the layer output reconstruction error is minimized:

min⁡W^∥WX−W^X∥F2subject to∥W^∥0≤(1−s)⋅∥W∥0\min_{\widehat{W}} \| W X - \widehat{W} X \|_F^2 \quad \text{subject to} \quad \|\widehat{W}\|_0 \le (1 - s) \cdot \|W\|_0

where ∥⋅∥F\|\cdot\|_F denotes the Frobenius norm and ∥⋅∥0\|\cdot\|_0 counts non-zero entries.

Second-Order Taylor Approximations: OBD and OBS

The foundation of analytical pruning traces to Optimal Brain Damage (LeCun et al., 1989) and Optimal Brain Surgeon (Hassibi and Stork, 1992). Expanding the loss function L(w)\mathcal{L}(w) around an optimized parameter vector w∗w^* via Taylor expansion:

ΔL≈gTΔw+12ΔwTHΔw\Delta \mathcal{L} \approx g^T \Delta w + \frac{1}{2} \Delta w^T H \Delta w

where $g = \nabla_w \mathcal{L}(w^)$ is the gradient vector and $H = \nabla_w^2 \mathcal{L}(w^)$ is the Hessian matrix of second derivatives. Because pre-trained models reside near a local minimum, the first-order gradient g≈0g \approx 0, leaving the quadratic term 12ΔwTHΔw\frac{1}{2} \Delta w^T H \Delta w.

When pruning parameter wqw_q to zero (Δwq=−wq\Delta w_q = -w_q), Optimal Brain Damage assumes a diagonal Hessian, yielding the saliency score:

SqOBD=12Hqqwq2S_q^{\text{OBD}} = \frac{1}{2} H_{qq} w_q^2

Optimal Brain Surgeon removes the diagonal assumption. By solving the constrained optimization problem min⁡Δw12ΔwTHΔw\min_{\Delta w} \frac{1}{2} \Delta w^T H \Delta w subject to the constraint eqTΔw+wq=0e_q^T \Delta w + w_q = 0, OBS derives both the optimal parameter removal score and the compensatory weight update for remaining unpruned parameters:

SqOBS=wq22[H−1]qqS_q^{\text{OBS}} = \frac{w_q^2}{2 [H^{-1}]_{qq}}

Δw=−wq[H−1]qqH−1eq\Delta w = -\frac{w_q}{[H^{-1}]_{qq}} H^{-1} e_q

where eqe_q is the standard basis vector with 1 at index qq and [H−1]qq[H^{-1}]_{qq} is the qq-th diagonal element of the inverted Hessian matrix.

Magnitude Pruning and Its Breakdown in LLMs

Classical magnitude pruning ranks parameter importance purely by absolute magnitude:

SijMag=∣Wij∣S_{ij}^{\text{Mag}} = |W_{ij}|

In vision networks and smaller language models, magnitude pruning paired with iterative fine-tuning achieved high sparsity ratios. However, in modern decoder-only transformers (such as LLaMA, OPT, and BLOOM), one-shot magnitude pruning without retraining collapses perplexity at 20% to 30% sparsity.

The breakdown of magnitude pruning in large language models stems from emergent activation outliers. Research by Dettmers et al. (2022) revealed that once transformer models scale beyond 6.7 billion parameters, specific hidden dimensions develop activation magnitudes up to 100 times larger than average features. In linear transformations Yi,:=∑jWijXj,:Y_{i,:} = \sum_j W_{ij} X_{j,:}, a weight WijW_{ij} with small magnitude ∣Wij∣|W_{ij}| paired with an outlier input channel Xj,:X_{j,:} contributes heavily to output activations. Magnitude pruning drops these small weights, destroying critical signal pathways.

Wanda: Pruning by Weights and Activations

To address the activation outlier bottleneck without computing expensive second-order derivatives, Sun et al. (2023) proposed Wanda (Pruning by Weights and activations).

Wanda evaluates weight saliency by scaling the absolute weight magnitude by the ℓ2\ell_2-norm of the corresponding input activation column across calibration tokens:

SijWanda=∣Wij∣⋅∥Xj,:∥2=∣Wij∣⋅∑k=1NXjk2S_{ij}^{\text{Wanda}} = |W_{ij}| \cdot \|X_{j,:}\|_2 = |W_{ij}| \cdot \sqrt{\sum_{k=1}^N X_{jk}^2}

Per-Output Row-Wise Comparison

Unlike global or whole-matrix sorting, Wanda evaluates pruning scores independently for each output channel (each row of WW):

  1. Compute input activation norms ∥Xj,:∥2\|X_{j,:}\|_2 for all input features j∈{1,…,din}j \in \{1, \dots, d_{\text{in}}\} over calibration samples.
  2. Form the saliency matrix S∈Rdout×dinS \in \mathbb{R}^{d_{\text{out}} \times d_{\text{in}}} where Sij=∣Wij∣⋅∥Xj,:∥2S_{ij} = |W_{ij}| \cdot \|X_{j,:}\|_2.
  3. For each row i∈{1,…,dout}i \in \{1, \dots, d_{\text{out}}\}, sort entries Si,:S_{i,:} and set the lowest s×dins \times d_{\text{in}} elements to zero.

Wanda requires zero second-order Hessian inversions and zero parameter updates on remaining weights. Despite its simplicity, Wanda achieves 50% unstructured sparsity on LLaMA models with near-baseline perplexity, executing up to 300 times faster than full Hessian reconstruction methods.

SparseGPT: Large-Scale Second-Order Reconstruction

Frantar and Alistarh (2023) introduced SparseGPT, demonstrating that massive language models up to 175 billion parameters can be pruned to 50%–60% sparsity in one shot using exact second-order error compensation.

SparseGPT casts layer-wise pruning into a constrained least-squares problem over empirical activation covariance. For layer weights WW and calibration inputs XX, the empirical Hessian is given by:

H=2XXT+λIH = 2 X X^T + \lambda I

where λ\lambda acts as a dampening factor to ensure numerical stability during matrix inversion.

Column-Wise Elimination and Batched Updates

Direct application of Optimal Brain Surgeon requires O(din3)\mathcal{O}(d_{\text{in}}^3) operations per pruned parameter, which is computationally intractable for matrices with din=8192d_{\text{in}} = 8192 or larger. SparseGPT reduces complexity through three architectural mechanics:

  1. Cholesky Inversion: SparseGPT computes the inverse Hessian H−1H^{-1} once per layer using Cholesky factorization H−1=L−TL−1H^{-1} = L^{-T} L^{-1}.
  2. Fixed Pruning Sequence: Parameters are evaluated column by column rather than searching globally across all remaining matrix entries.
  3. Lazy Batched Updates: Weight updates are accumulated across blocks of columns (e.g., block size B=128B = 128) and applied as matrix-matrix multiplications rather than sequential rank-1 updates.

When pruning column jj, the optimal weight adjustment for unpruned columns j′>jj' > j is computed as:

W:,j′:din←W:,j′:din−W:,j−W^:,j[H−1]jj⋅(H−1)j,j′:dinW_{:, j':d_{\text{in}}} \leftarrow W_{:, j':d_{\text{in}}} - \frac{W_{:, j} - \widehat{W}_{:, j}}{[H^{-1}]_{jj}} \cdot (H^{-1})_{j, j':d_{\text{in}}}

SparseGPT prunes 175B parameter models in under 4.5 hours on a single GPU while preserving downstream zero-shot accuracy.

Schematic illustrating hardware semi-structured 2:4 sparsity matrix compression and 2-bit indexing on sparse tensor cores

Semi-Structured 2:4 Sparsity and Hardware Acceleration

While unstructured sparsity zeroes out arbitrary weights across the matrix, it produces random non-zero patterns that standard dense matrix multiplication hardware cannot accelerate efficiently without specialized sparse storage formats and sparse kernel overhead.

To bridge theoretical compression and real-world execution speed, modern GPU architectures (NVIDIA Ampere, Hopper, and Blackwell) implement native hardware support for semi-structured N:M sparsity, specifically 2:4 sparsity.

The 2:4 Sparsity Constraint

The 2:4 structured sparsity pattern mandates that for every contiguous block of 4 values along a row, exactly 2 values must be zero and 2 values remain non-zero (50% uniform sparsity):

∀k∈{1,…,din4},∥Wi,4k−3:4k∥0≤2\forall k \in \left\{1, \dots, \frac{d_{\text{in}}}{4}\right\}, \quad \|W_{i, 4k-3 : 4k}\|_0 \le 2

On Sparse Tensor Cores:

  • The non-zero values are packed into a dense tensor of half the original width (din/2d_{\text{in}} / 2).
  • A compact metadata tensor stores 2-bit column indices per non-zero element to map positions during hardware execution.
  • Matrix multiply units read half the memory bandwidth and execute double the arithmetic throughput, delivering up to a theoretical 2x speedup over dense operations.

Pruning Under N:M Constraints

Both Wanda and SparseGPT adapt directly to 2:4 structured constraints:

  • 2:4 Wanda: In each 4-element block Wi,4k−3:4kW_{i, 4k-3:4k}, evaluate scores Si,4k−3:4k=∣Wi,4k−3:4k∣⊙∥X4k−3:4k,:∥2S_{i, 4k-3:4k} = |W_{i, 4k-3:4k}| \odot \|X_{4k-3:4k,:}\|_2 and prune the two elements with smallest scores.
  • 2:4 SparseGPT: In each 4-element block, select the two weights whose elimination minimizes the second-order reconstruction error, then apply OBS compensatory updates to all trailing columns.

Comparative Trade-Offs and Architectural Analysis

Understanding the trade-offs across pruning strategies guides deployment decisions in production inference pipelines:

  • Unstructured Magnitude Pruning: Lowest compute overhead (O(1)\mathcal{O}(1) per weight), but high perplexity degradation past 20% sparsity in LLMs due to activation outlier neglect.
  • Wanda: Fast calibration pass (O(din⋅N)\mathcal{O}(d_{\text{in}} \cdot N)), accounts for activation outlier channels without matrix inversions, retains baseline accuracy up to 50% unstructured sparsity.
  • SparseGPT: Second-order Hessian inversion (O(din3+doutdin2)\mathcal{O}(d_{\text{in}}^3 + d_{\text{out}} d_{\text{in}}^2)), compensates remaining weights, achieves superior accuracy at higher sparsity ratios (60%+) and under strict 2:4 structured constraints.
  • Hardware Acceleration: Unstructured sparsity primarily saves disk storage unless paired with specialized CPU/GPU sparse runtimes. In contrast, 2:4 semi-structured sparsity delivers direct inference latency speedups on NVIDIA Tensor Cores.

Sources

EducationPruningsparsitycompressionLLM