Learn To be Efficient: Build Structured Sparsity in Large Language Models

Haizhong Zheng, Xiaoyan Bai, Xueshen Liu, Zhuoqing Mao, Beidi Chen, Fan Lai, Atul Prakash

TL;DR

The "Learn-To-be-Efficient" (LTE) framework introduces a novel training algorithm that transforms standard Large Language Models (LLMs) into efficiency-aware versions by inducing structured activation sparsity. By training models to activate only a fraction of their neurons through a custom Mixture-of-Experts (MoE) approach, LTE achieves a 25% wall-clock speedup on LLaMA2-7B with minimal performance loss.

LTE Framework

The Problem: Inefficient FFNs and Softmax Instability

Large Language Models are computationally expensive, primarily due to their massive Feed-Forward Network (FFN) layers, which account for over 60% of total operations. While older models using ReLU activations exhibit "natural" sparsity (many neurons output zero), modern high-performance models like LLaMA use "soft" activations (like SwiGLU) that rarely result in actual zeros.

Existing methods like "MoEfication" try to convert these dense FFNs into Mixture-of-Experts (MoE) modules after training. However, they often suffer from two major issues:

  1. Training Instability: Standard "Top-k Softmax" routing leads to biased expert selection and severe accuracy drops during fine-tuning.
  2. Hardware Inefficiency: Theoretical FLOP reductions often don't translate to real-world speedups because the resulting sparsity is too fragmented for GPUs to handle efficiently.

The Solution: Learn-To-be-Efficient (LTE)

The authors propose LTE, which moves beyond just utilizing existing sparsity to actively training the model to be sparse in a structured, hardware-friendly way.

  1. Expert Grouping: Neurons are grouped into "experts" (sets of 32 neurons) using balanced K-Means clustering based on the model's weights. This ensures that neurons that "fire together" are stored together.
  2. Sigmoid-based Routing: Instead of the common Top-k Softmax (which forces a fixed number of experts), LTE uses a Sigmoid routing function. This allows each expert to be selected independently based on a threshold, enabling the model to adaptively choose more or fewer experts depending on the complexity of the input.
  3. Two-Stage Training:
    • Stage 1 (Model-Router Training): The model is trained with two new loss functions: an efficiency loss (penalizing high activation scores to encourage sparsity) and a separability loss (pushing scores away from the threshold to make selection more decisive).
  4. Stage 2 (Model Adaptation): The routers are frozen, and the model is fine-tuned to adapt to the discrete "on/off" nature of the experts.
Effect of Separability Loss
  1. Custom Triton Kernel: To turn theoretical sparsity into actual speed, the authors developed a custom CUDA kernel using Triton that avoids slow, non-coalesced memory accesses by loading only the necessary "expert" rows and columns.

Key Insights

  • Sparsity is a Learnable Trait: Rather than treating sparsity as a byproduct of ReLU, LTE treats it as a training objective. This allows models with soft activations (like LLaMA) to become just as efficient as ReLU models without the performance hit of switching activation functions.
  • Softmax is the Enemy of Stability: The paper identifies that Softmax routing creates a "winner-take-all" bias where only one expert gets a high score, leading to performance collapse. Sigmoid routing solves this by treating expert selection as a set of independent binary decisions.
  • Inter-layer Orchestration: LTE doesn't force the same sparsity on every layer. The efficiency loss allows the model to naturally keep "important" layers dense while making "redundant" layers extremely sparse. For example, in RoBERTa, deeper layers were found to be much sparser than earlier ones.
  • Structured vs. Unstructured: By grouping neurons into blocks of 32, the sparsity becomes "structured." This is crucial because modern GPUs are much faster at processing blocks of data than individual, scattered values.

Results & Performance

The researchers evaluated LTE across a wide range of tasks, including Natural Language Understanding (GLUE), Natural Language Generation (E2E, XSum), Instruction Tuning (Tulu), and Generalization (MMLU).

  • Real-world Speedup: LTE reduces LLaMA2-7B inference latency by 25% at 50% sparsity on a single NVIDIA 3090Ti.
  • FLOPs Reduction: On generation tasks (NLG), LTE achieved a 1.83x to 2.59x reduction in FLOPs without compromising text quality.
  • Superiority over Baselines: LTE consistently outperformed SOTA methods like MoEfication and Deja Vu. On NLU tasks, LTE maintained high accuracy even at 80-95% sparsity, whereas baselines began to fail at 30-70%.
  • Instruction Tuning: LTE-trained LLaMA2-7B outperformed other sparse baselines on the MMLU benchmark across all sparsity levels.

Limitations and Future Work

While highly effective, LTE requires a two-stage fine-tuning process. While this is a one-time cost, it is more intensive than post-training quantization or pruning methods. Additionally, on very small datasets (like CoLA), the model struggled to learn optimal routers, suggesting that a minimum amount of data is needed for the "efficiency-aware" training to be effective.

The authors suggest that incorporating LTE directly into the massive-scale pre-training phase could yield even better efficiency-performance trade-offs in the future.

Summary updated

BibTeX
@inproceedings{zheng2024learn,
  author = {Haizhong Zheng and Xiaoyan Bai and Xueshen Liu and Zhuoqing Mao and Beidi Chen and Fan Lai and Atul Prakash},
  title = {{Learn To be Efficient: Build Structured Sparsity in Large Language Models}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=iSfCWhvEGA},
}
Figure