Constraint-aware and Ranking-distilled Token Pruning for Efficient Transformer Inference

Junyan Li, Li Lyna Zhang, Jiahang Xu, Yujing Wang, Shaoguang Yan, Yunqing Xia, Yuqing Yang, Ting Cao, Hao Sun, Weiwei Deng, Qi Zhang, Mao Yang

Introduction

Transformer-based models, such as BERT and RoBERTa, have revolutionized natural language processing (NLP) but at a significant computational cost. The quadratic growth of self-attention complexity relative to input sequence length poses a major bottleneck for real-time applications and deployment on edge devices. Token pruning—the process of identifying and removing redundant tokens during the forward pass—has emerged as a promising solution to reduce this overhead. However, existing techniques often struggle with a trade-off between pruning accuracy and the latency introduced by the pruning mechanism itself.

This paper introduces ToP (Token Pruning), a novel framework designed to achieve high-efficiency inference without the "predictor tax" associated with auxiliary neural modules. By distilling importance rankings from deep layers to shallow ones and employing a constraint-aware optimization strategy, ToP achieves significant speedups on standard hardware.

The Challenges of Token Pruning

Current token pruning strategies generally fall into two categories:

  1. Attention-based methods: These utilize existing attention scores to determine token importance. While they introduce zero computational overhead, they are often "blind" in the early layers of the model, where the global context has not yet been fully integrated. This leads to the premature removal of tokens that may become critical in later stages.
  2. Prediction-based methods: These employ lightweight auxiliary networks (predictors) to estimate token importance. Although more accurate, these predictors can add up to 30% to the total inference latency, often negating the gains achieved through pruning.

ToP bridges this gap by making the model's inherent attention mechanism "smarter" through distillation, eliminating the need for external predictors.

Methodology: Ranking-aware Distillation and $L_0$ Regularization

The ToP framework is built upon two core innovations: ranking-aware distillation and a coarse-to-fine pruning strategy.

Ranking-aware Token Distillation

The authors observe that while early layers lack the context to rank token importance accurately, the final layers of a Transformer act as "experts." ToP leverages this insight by treating the final layer of a pre-trained teacher model as a reference. Instead of traditional feature matching, ToP employs LambdaLoss, a technique borrowed from information retrieval, to train the early layers of the student model to replicate the importance ranking of the teacher. This ensures that even in the first layer, the most semantically relevant tokens are preserved.

Coarse-to-Fine Pruning

ToP optimizes the pruning process through a two-tier masking system:

  • Gate Masks (Coarse-grained): These act as binary switches to determine which layers in the Transformer should perform pruning at all.
  • Ranking Masks (Fine-grained): These determine the specific number of tokens to be pruned within a selected layer.

The framework utilizes an improved $L_0$ regularization approach during training. This allows users to specify a target computational budget (e.g., a specific FLOPs reduction or speedup factor). The optimization algorithm then automatically identifies the optimal configuration of layers and tokens to prune to meet the constraint while minimizing accuracy loss.

Experimental Evaluation

The researchers evaluated ToP across several benchmarks, including the GLUE suite (SST-2, CoLA, MNLI, QNLI, RTE), SQuAD v2.0 for extractive question answering, and the 20News dataset for long-sequence classification (up to 512 tokens).

Key Results

  • Efficiency and Speed: ToP reduced BERT’s average FLOPs by 8.1x. On an 8-core Intel CPU, this translated to real-world latency speedups between 2.9x and 7.4x.
  • Accuracy Preservation: ToP consistently outperformed state-of-the-art (SOTA) methods like Transkimmer and LTP, reducing FLOPs by an additional 17-20% at comparable accuracy levels.
  • Denoising Effect: In several tasks, such as CoLA and RTE, ToP actually improved accuracy by up to 4.3% compared to the unpruned baseline. This suggests that pruning redundant tokens can act as a form of denoising, allowing the model to focus on the most salient information.
  • Redundancy Patterns: Analysis revealed that under high-sparsity constraints, ToP often prunes over 95% of tokens by the 8th layer, indicating that Transformers possess significant redundancy that can be safely compressed early in the computation.

Limitations and Future Work

Despite its success on CPUs, the speedup on GPUs was more modest (approximately 1.2x). This discrepancy is primarily due to the way GPUs handle "dynamic shapes." Removing tokens mid-computation creates irregular memory access patterns that standard GPU kernels are not currently optimized to handle. Future research is needed to develop specialized high-performance inference engines that can better exploit the dynamic sequence lengths produced by token pruning.

Conclusion

ToP demonstrates that efficient Transformer inference does not require complex auxiliary predictors. By distilling ranking knowledge into early layers and using constraint-aware optimization, ToP provides a robust and scalable solution for deploying large-scale NLP models in resource-constrained environments.

Summary updated

BibTeX
@inproceedings{li2023constraint,
  author = {Junyan Li and Li Lyna Zhang and Jiahang Xu and Yujing Wang and Shaoguang Yan and Yunqing Xia and Yuqing Yang and Ting Cao and Hao Sun and Weiwei Deng and Qi Zhang and Mao Yang},
  title = {{Constraint-aware and Ranking-distilled Token Pruning for Efficient Transformer Inference}},
  booktitle = {ACM SIGKDD Conference on Knowledge Discovery and Data Mining},
  year = {2023},
  url = {https://openreview.net/forum?id=Ftw0CW1JZo},
}
Figure