Magic Pyramid: Accelerating Inference with Early Exiting and Token Pruning

Xuanli He, Iman Keivanloo, Yi Xu, Xiang He, Belinda Zeng, Santosh Rajagopalan, Trishul ChilimbiarXiv 2021

Abstract

Pre-training and then fine-tuning large language models is commonly used to achieve state-of-the-art performance in natural language processing (NLP) tasks. However, most pre-trained models suffer from low inference speed. Deploying such large models to applications with latency constraints is challenging. In this work, we focus on accelerating the inference via conditional computations. To achieve this, we propose a novel idea, Magic Pyramid (MP), to reduce both width-wise and depth-wise computation via token pruning and early exiting for Transformer-based models, particularly BERT. The former manages to save the computation via removing non-salient tokens, while the latter can fulfill the computation reduction by terminating the inference early before reaching the final layer, if the exiting condition is met. Our empirical studies demonstrate that compared to previous state of arts, MP is not only able to achieve a speed-adjustable inference but also to surpass token pruning and early exiting by reducing up to 70% giga floating point operations (GFLOPs) with less than 0.5% accuracy drop. Token pruning and early exiting express distinctive preferences to sequences with different lengths. However, MP is capable of achieving an average of 8.06x speedup on two popular text classification tasks, regardless of the sizes of the inputs.

Introduction

Large-scale Transformer models like BERT have revolutionized NLP, but they are notoriously "heavy." In production environments—such as online stores handling millions of search queries per second—the latency of a 12-layer BERT model is often a dealbreaker. Existing solutions usually focus on one dimension: depth-wise reduction (Early Exiting) or width-wise reduction (Token Pruning).

The authors observed a crucial gap: Early exiting works best on short, simple sequences, while token pruning is most effective on long, complex ones. No single method handled the diversity of real-world input lengths efficiently. Magic Pyramid (MP) is a novel framework that slashes Transformer inference latency by attacking computational waste from both angles simultaneously.

Methods

The Magic Pyramid framework superimposes these two techniques to create a "pyramid" of computation that narrows in both width and depth as the data flows through the model.

  • Early Exiting (The Depth Axis): MP attaches "sub-classifiers" to every layer of the Transformer. At each layer, the model calculates the "uncertainty" of its current prediction. If the uncertainty is below a specific threshold ($\tau$), the model stops immediately and returns the result, skipping all remaining layers.
  • Token Pruning (The Width Axis): Not all words in a sentence contribute equally to the final classification. MP uses attention scores to identify "non-salient" tokens. Using a learnable threshold ($\Delta$), the model progressively discards these tokens at each layer.
The Magic Pyramid framework combining token pruning and early exiting.

By combining these, the model processes fewer tokens as it goes deeper, and it often doesn't even need to reach the final layer. This creates a highly flexible, "speed-adjustable" inference engine where the number of active tokens decreases (width) and the probability of exiting increases (depth).

Datasets

The researchers evaluated MP on five diverse text classification and natural language inference tasks from the GLUE benchmark and beyond: AG News (topic identification), Yelp (sentiment classification), QQP and MRPC (paraphrase detection), and RTE (language inference).

Results and Discussion

The most striking insight is that token pruning and early exiting are "orthogonal" solutions. Early exiting struggles with long sequences because they are inherently more complex and require more depth to reach confidence. Conversely, token pruning thrives on long sequences because there is more redundant information to trim. MP bridges this divide, achieving an average of 8.06x speedup across the Yelp and AG News datasets.

Speedup comparison across different sequence lengths for Yelp and AG News datasets.

MP consistently outperformed specialized models like DistilBERT, FastBERT (early exit only), and LTP (token pruning only) in the speed-accuracy trade-off. For example, on the Yelp dataset, MP achieved an 8.25x speedup, significantly higher than FastBERT's 6.18x at similar accuracy levels. The model can reduce Giga Floating Point Operations (GFLOPs) by up to 70% while maintaining an accuracy drop of less than 0.5% compared to a full BERT-base model.

Limitations and Future Work

While promising, the current study focuses primarily on classification tasks. It remains to be seen how well this pyramid structure holds up for generative tasks (like summarization) where every token might be necessary for the decoder. Additionally, MP requires a multi-stage training process involving soft and hard pruning followed by sub-classifier training. Simplifying this pipeline and optimizing hardware kernels for the resulting sparse computation are key areas for future research.

Summary updated

BibTeX
@misc{he2021magic,
  author = {Xuanli He and Iman Keivanloo and Yi Xu and Xiang He and Belinda Zeng and Santosh Rajagopalan and Trishul Chilimbi},
  title = {{Magic Pyramid: Accelerating Inference with Early Exiting and Token Pruning}},
  year = {2021},
  eprint = {2111.00230},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2111.00230},
}
Figure