STAT: Shrinking Transformers After Training

Megan Flynn, Alexander Wang, Dean Edward Alvarez, Christopher De Sa, Anil DamlearXiv 2024

TL;DR

STAT (Shrinking Transformers After Training) is a structured pruning algorithm that removes attention heads and neurons from transformer models without requiring any expensive fine-tuning. By leveraging principled matrix factorizations to calculate weight corrections for subsequent layers, STAT can compress a 7B parameter model like Llama-2 in under three hours on a single GPU while maintaining competitive accuracy.

What Problem Does This Paper Address?

Large-scale transformer models have become the backbone of modern NLP, but their massive size makes deployment expensive and slow. While compression techniques like quantization and pruning exist, they come with significant trade-offs. Unstructured pruning (removing individual weights) requires specialized hardware to see real-world speedups. Structured pruning (removing entire neurons or attention heads) is hardware-friendly but typically requires extensive re-training or "fine-tuning" to recover the accuracy lost during the process.

Existing "training-free" structured pruning methods often struggle to maintain accuracy at high compression ratios or fail to scale efficiently to modern Large Language Models (LLMs) with billions of parameters. The authors of STAT aimed to bridge this gap by creating a method that is fast, requires no fine-tuning, and scales to the latest generative architectures.

Methods

The core of STAT is a mathematical technique called Interpolative Decomposition (ID).

Unlike the more common Singular Value Decomposition (SVD), which creates abstract "singular vectors," an ID approximates a matrix using a subset of its actual columns. In the context of a neural network, these columns correspond directly to physical structures like neurons or attention heads.

The STAT pipeline works as follows:

  1. Calibration: A small set of unlabeled data (e.g., 512 examples) is passed through the model to collect activation outputs at each layer.
  2. Selection: Using a column-pivoted QR factorization on these activations, the algorithm identifies which heads or neurons are most "important" for reconstructing the original output.
  3. Correction: This is the critical step. Instead of just deleting the unimportant parts, STAT calculates a "correction matrix" that is fused into the weights of the next layer. This correction compensates for the information lost by the pruned neurons, effectively allowing the next layer to "mimic" the behavior of the original, larger block.
  4. Scaling: To handle massive models like Llama-2, the authors introduced randomized dimension reduction (sketching) and neuron grouping to ensure the matrix factorizations fit within GPU memory and execute in a reasonable timeframe.

Datasets

The authors evaluated STAT across a variety of standard benchmarks:

  • Encoder Models (BERT, DistilBERT): Evaluated on the GLUE benchmark (including tasks like QNLI, QQP, MRPC, MNLI, SST-2, and STS-B) and SQuAD 1.1/2.0 for question answering.
  • Decoder Models (Llama-2 7B): Evaluated on WikiText2 to measure perplexity.
  • Calibration Data: Pruning was performed using small subsets of the C4 training set for Llama and task-specific training sets for BERT.

Key Insights and Ideas

  • Correction vs. Deletion: The primary "aha" moment is that structured pruning doesn't have to be a zero-sum game. By treating the subsequent layer as a compensator, you can maintain the network's output distribution much more effectively than simple magnitude-based pruning.
  • ID is the Right Tool for the Job: Because ID selects actual columns, it provides a mathematically principled way to perform structured pruning that SVD cannot offer directly.
  • Layer Sensitivity Matters: The authors found that errors induced in early layers of a transformer have a disproportionately large impact on the final output. They introduced a weighting function to prune early layers more conservatively, which significantly stabilized the compressed models.
  • Efficiency is King: STAT is a "one-shot" method. Unlike iterative pruning methods that require multiple passes and potential re-training, STAT computes its corrections once and finishes.

Key Results

STAT demonstrates a superior trade-off between model size (FLOPS) and accuracy compared to previous training-free methods.

As shown in Figure 1, STAT consistently outperforms the previous state-of-the-art training-free method (Kwon et al.) across almost all GLUE tasks, especially as the compression ratio increases (lower FLOPS ratio).

Figure 1: Pruning results for BERT and DistilBERT models. We begin with the exact same base models as Kwon et al. [2022]. Error bars are reported as standard deviations across 10 trials of random pruning set selections. We see that for modest pruning levels, our method performs within error bounds of the baseline method, and at higher compression ratios we substantially outperform Kwon et al. [2022].

Furthermore, Figure 2 highlights that STAT is often competitive with methods that involve substantial fine-tuning (represented by stars), despite STAT requiring zero training epochs.

Figure 2: Pruning results for BERT model, compared against methods Wang et al. [2020c], Lin et al. [2020], Sajjad et al. [2023], Hou et al. [2020], Liu et al. [2021b], Lagunas et al. [2021] which include substantial (at least 4 epochs and 5 hours of) fine tuning (stars) and Kwon et al. [2022], which does not include fine tuning.

For Llama-2 7B, STAT compressed the model to ~6B parameters in roughly 2.5 hours on a single A6000 GPU, achieving better perplexity than SliceGPT, another recent training-free competitor.

Limitations and Future Work

While STAT is highly effective, the authors noted a few areas for improvement:

  • Architecture Specifics: The weighting function used to protect sensitive layers currently requires slight manual adjustment depending on whether the model uses "pre-layer norm" (like Llama) or "post-layer norm" (like BERT).
  • Data Scaling: While STAT works well with just a few hundred examples, the authors suspect that using larger calibration sets might further improve results for massive 70B+ parameter models, though this would increase the compression time.
  • Layer Norm Sensitivity: A more systematic study is needed to understand exactly how layer normalization placement affects the propagation of pruning errors through the network.

Summary updated

BibTeX
@misc{flynn2024stat,
  author = {Megan Flynn and Alexander Wang and Dean Edward Alvarez and Christopher De Sa and Anil Damle},
  title = {{STAT: Shrinking Transformers After Training}},
  year = {2024},
  eprint = {2406.00061},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2406.00061},
}
Figure