Dynamic Context Pruning for Efficient and Interpretable Autoregressive Transformers

Sotiris Anagnostidis, Dario Pavllo, Luca Biggio, Lorenzo Noci, Aurelien Lucchi, Thomas Hofmann

Abstract

Autoregressive Transformers face significant scaling challenges due to the quadratic complexity of the self-attention mechanism and the linear growth of the Key-Value (KV) cache. This paper introduces Adaptively Sparse Attention, a mechanism that enables models to dynamically prune uninformative tokens from their context during inference. By learning to identify redundant information, the proposed method achieves up to a 2x increase in throughput and a 50% reduction in wall-time latency with minimal impact on model accuracy. Furthermore, the pruning patterns provide novel insights into the internal logic of Transformers, revealing how models prioritize information across different layers.

1. Introduction

The "Achilles' heel" of the Transformer architecture is its quadratic complexity. As sequences grow longer, the computational cost and memory requirements for the Key-Value (KV) cache balloon, limiting the efficiency of long-context generation. While previous attempts to mitigate this—such as local attention windows or static sparsity patterns—have provided some relief, they are inherently rigid. These methods treat all tokens as equally important, failing to distinguish between a crucial noun and a redundant punctuation mark.

This research addresses a fundamental question: Can we dynamically prune the context based on actual content, retaining only what is necessary for the model’s expressivity? We propose a modular approach that allows Transformers to "forget" unnecessary tokens, optimizing both memory and compute.

2. Methodology

The authors propose a "modular building block" designed to be integrated into existing pre-trained models, such as the GPT-2 family, through a lightweight fine-tuning process. The system consists of several key technical innovations:

2.1 Learnable Interaction Parameters

To determine which tokens to prune, the model utilizes small, additional weight matrices ($W_{Qint}$ and $W_{Kint}$). These calculate "interaction" scores between tokens, separate from the standard attention weights, to evaluate the long-term utility of a token in the context.

2.2 The $\alpha$-sigmoid Function

To facilitate binary "keep or drop" decisions in a differentiable way, the method employs a sparse sigmoid function. During training, a "temperature" parameter ($\alpha$) is adjusted using a cosine scheduler. This allows the model to transition from a soft distribution (enabling gradient flow) to a hard, sparse distribution that produces exact zeros for pruning during inference.

2.3 Irreversible Pruning and Hardware Optimization

The mechanism is designed for irreversible pruning: once a token is dropped at a specific layer, it remains excluded for all subsequent tokens in the generation process. This is critical for hardware efficiency, as it allows the KV cache to be physically cleared. To realize these gains on a GPU, the authors developed a custom KV cache data structure that recycles memory slots from pruned tokens, ensuring the processor works with contiguous, "packed" memory blocks.

3. Experimental Setup

The researchers fine-tuned GPT-2 models (Small, Medium, Large, and XL) on a subset of English Wikipedia and the BookCorpus. Performance was evaluated using perplexity and zero-shot accuracy on standard benchmarks, including WinoGrande, HellaSwag, PIQA, and LAMBADA.

4. Key Insights and Results

4.1 Efficiency and Throughput

The experimental results demonstrate that the model can prune up to 80% of the context with almost no increase in perplexity. This reduction in the KV cache footprint translates directly to performance: the implementation achieved a 2x increase in token throughput and a 50% reduction in wall-time latency for long sequences. Notably, a pruned GPT-2 Medium model provided higher throughput than a standard GPT-2 Small, while maintaining the superior accuracy of the Medium architecture.

4.2 Layer-wise Pruning Logic

Pruning behavior is not uniform across the model. The authors observed that middle layers are typically the most aggressive in dropping tokens. In contrast, the earliest layers retain more context for initial feature extraction, and the final layers maintain context to support precise token prediction.

4.3 Interpretability: Punctuation as a "Reset"

One of the most striking qualitative findings is the role of punctuation. The model frequently triggers pruning after a sentence ends. This suggests the model performs a "contextual reset," recognizing that specific word choices in a previous sentence are less relevant to future predictions than the high-level semantic summary.

5. Discussion and Limitations

While Dynamic Context Pruning offers a significant leap in efficiency, it is not a "plug-and-play" solution for frozen models; it requires a specialized fine-tuning stage. Additionally, while the logic is expected to generalize to modern Large Language Models (LLMs) like Llama or Mistral, this study focused primarily on the GPT-2 family.

Future work may involve developing custom CUDA kernels to further optimize sparse operations and combining context pruning with orthogonal techniques like quantization and weight pruning to achieve even greater compounding efficiency.

6. Conclusion

Dynamic Context Pruning transforms the Transformer's attention mechanism from a dense, exhaustive process into a selective, efficient one. By teaching models to forget, we can significantly reduce the memory and computational overhead of long-form text generation without sacrificing the reasoning capabilities that make Transformers so powerful.

Summary updated

BibTeX
@inproceedings{anagnostidis2023dynamic,
  author = {Sotiris Anagnostidis and Dario Pavllo and Luca Biggio and Lorenzo Noci and Aurelien Lucchi and Thomas Hofmann},
  title = {{Dynamic Context Pruning for Efficient and Interpretable Autoregressive Transformers}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2023},
  url = {https://openreview.net/forum?id=uvdJgFFzby},
}
Figure