MaskLLM: Learnable Semi-Structured Sparsity for Large Language Models

Gongfan Fang, Hongxu Yin, Saurav Muralidharan, Greg Heinrich, Jeff Pool, Jan Kautz, Pavlo Molchanov, Xinchao Wang · OpenReview

Abstract

Large Language Models (LLMs) are distinguished by their massive parameter counts, which typically result in significant redundancy. This work introduces MaskLLM, a learnable pruning method that establishes Semi-structured (or "N:M") Sparsity in LLMs, aimed at reducing computational overhead during inference. Instead of developing a new importance criterion, MaskLLM explicitly models N:M patterns as a learnable distribution through Gumbel Softmax sampling. This approach facilitates end-to-end training on large-scale datasets and offers two notable advantages: 1) High-quality Masks - our method effectively scales to large datasets and learns accurate masks; 2) Transferability - the probabilistic modeling of mask distribution enables the transfer learning of sparsity across domains or tasks. We assessed MaskLLM using 2:4 sparsity on various LLMs, including LLaMA-2, Nemotron-4, and GPT-3, with sizes ranging from 843M to 15B parameters, and our empirical results show substantial improvements over state-of-the-art methods. For instance, leading approaches achieve a perplexity (PPL) of 10 or greater on Wikitext compared to the dense model's 5.12 PPL, but MaskLLM achieves a significantly lower 6.72 PPL solely by learning the masks with frozen weights. Furthermore, MaskLLM's learnable nature allows customized masks for lossless application of 2:4 sparsity to downstream tasks or domains.

Introduction

Large Language Models (LLMs) have revolutionized natural language processing but remain notoriously resource-intensive, complicating deployment on consumer-grade hardware. Pruning—the systematic removal of redundant parameters—offers a path toward efficiency. Among various pruning strategies, N:M semi-structured sparsity (e.g., 2:4 sparsity, where two out of every four consecutive weights are zeroed) is particularly valuable. This specific pattern is natively supported by modern GPU architectures, such as NVIDIA’s Ampere and Hopper, providing direct hardware acceleration for sparse computations.

Despite its potential, existing N:M pruning methods like SparseGPT or Wanda often fall short. These "one-shot" approaches rely on handcrafted importance heuristics and tiny calibration datasets (typically around 128 samples). Because these proxy metrics do not perfectly align with actual model performance, they often result in a significant "sparsity gap," where the pruned model's accuracy drops sharply compared to the original dense version. MaskLLM addresses this by treating mask selection as a differentiable learning problem, optimized over large-scale data.

Methodology: Learnable Masking via Gumbel Softmax

The fundamental challenge in pruning is its discrete nature: a weight is either kept (1) or removed (0). This lack of differentiability prevents standard gradient-based optimization. MaskLLM overcomes this by transforming the combinatorial problem into a continuous distribution learning task.

Differentiable Masking

MaskLLM models the selection of N:M patterns as a categorical distribution. By employing the Gumbel Softmax trick, the researchers can sample masks during training while allowing gradients to flow back to the underlying mask probabilities (logits). This enables the model to "learn" which weights are most critical for performance across a broad dataset.

Frozen Weights and Efficiency

Unlike traditional sparse fine-tuning, which updates the model's weights, MaskLLM keeps the original LLM weights frozen. Only the parameters defining the mask distribution are updated. This approach significantly reduces the computational burden of training and ensures that the core knowledge of the pre-trained model remains intact.

Mask Prior and Regularization

To accelerate convergence, MaskLLM utilizes a Mask Prior. Rather than starting from a random configuration, the distribution is initialized using masks generated by one-shot methods like SparseGPT. Additionally, a sparse weight regularization term is introduced to maintain stability by encouraging the remaining weights to retain sufficient magnitude, preventing gradient vanishing during the learning process.

Experimental Setup

The researchers evaluated MaskLLM across a diverse range of model architectures and sizes:

  • Models: LLaMA-2 (7B, 13B), LLaMA-3 (8B), Nemotron-4 (15B), and GPT-3 variants (843M, 2B).
  • Training: The masks were learned using a blended dataset covering 69 domains, including C4, GitHub, and Wikipedia, to ensure generalizability.
  • Evaluation: Performance was benchmarked using Wikitext-2 for perplexity (PPL) and the LM-Eval-Harness for zero-shot tasks such as HellaSwag, PIQA, and WinoGrande.

Key Insights

Data-Driven Scaling

One of the most significant findings is that MaskLLM scales effectively with data. While one-shot methods plateau quickly regardless of the calibration set size, MaskLLM’s performance improves consistently as it sees more samples (up to 512k in testing). This allows the model to find much more accurate masks than heuristic-based approaches.

Transferability and Domain Specialization

Because the mask is represented as a learned distribution, it can be adapted for specific domains. A general-purpose mask can serve as a starting point to quickly learn a specialized mask for tasks like coding (HTML) or specific languages (French). This transferability allows for highly optimized, task-specific sparse models.

Achieving Lossless Sparsity

Perhaps most strikingly, the research demonstrates that for specific downstream tasks, MaskLLM can identify 2:4 sparsity patterns that result in zero performance loss compared to the dense model. This suggests that LLMs possess substantial task-specific redundancy that can be exploited without sacrificing accuracy.

Results and Performance

MaskLLM significantly outperforms existing state-of-the-art (SOTA) methods in maintaining model quality:

  • Accuracy: On LLaMA-2 7B, MaskLLM achieved a Wikitext PPL of 6.72, a dramatic improvement over SparseGPT’s 10.42 (the dense baseline is 5.12).
  • Hardware Speedup: Implementing the learned 2:4 sparsity resulted in a 1.4x wall-clock speedup on an NVIDIA A6000 GPU.
  • Memory Efficiency: The approach reduced the memory footprint to 73% of the original model. Furthermore, by using arithmetic coding, task-specific masks can be stored at only 0.65 bits per parameter, making it feasible to store multiple "expert" masks for a single model.

Conclusion

MaskLLM represents a shift from heuristic-based pruning to a principled, learnable approach for LLM compression. By bridging the gap between dense and sparse performance, it makes 2:4 semi-structured sparsity a viable option for production environments. While the training process is more intensive than one-shot methods—requiring approximately 2,000 training steps—the resulting gains in inference speed and accuracy preservation provide a compelling case for learnable sparsity in the next generation of efficient AI deployment.

Summary updated

BibTeX
@inproceedings{fang2024maskllm,
  author = {Gongfan Fang and Hongxu Yin and Saurav Muralidharan and Greg Heinrich and Jeff Pool and Jan Kautz and Pavlo Molchanov and Xinchao Wang},
  title = {{MaskLLM: Learnable Semi-Structured Sparsity for Large Language Models}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=Llu9nJal7b},
}
Figure