NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

The Hedgehog & the Porcupine: Expressive Linear Attentions with Softmax Mimicry

Michael Zhang, Kush Bhatia, Hermann Kumbong, Christopher Ré

Abstract

Linear attentions have shown potential for improving Transformer efficiency, reducing attention's quadratic complexity to linear in sequence length. This holds exciting promise for (1) training linear Transformers from scratch, (2) "finetuned-conversion" of task-specific Transformers into linear versions that recover task performance, and (3) "pretrained-conversion" of Transformers such as large language models into linear versions finetunable on downstream tasks. However, linear attentions often underperform standard softmax attention in quality. To close this performance gap, we find prior linear attentions lack key properties of softmax attention tied to good performance: low-entropy (or "spiky") weights and dot-product monotonicity. We further observe surprisingly simple feature maps that retain these properties and match softmax performance, but are inefficient to compute in linear attention. We thus propose Hedgehog, a learnable linear attention that retains the spiky and monotonic properties of softmax attention while maintaining linear complexity. Hedgehog uses simple trainable MLPs to produce attention weights mimicking softmax attention. Experiments show Hedgehog recovers over 99% of standard Transformer quality in train-from-scratch and finetuned-conversion settings, outperforming prior linear attentions up to 6 perplexity points on WikiText-103 with causal GPTs, and up to 8.7 GLUE score points on finetuned bidirectional BERTs. Hedgehog also enables pretrained-conversion. Converting a pretrained GPT-2 into a linear attention variant achieves state-of-the-art 16.7 perplexity on WikiText-103 for 125M subquadratic decoder models. We finally turn a pretrained Llama-2 7B into a viable linear attention Llama. With low-rank adaptation, Hedgehog-Llama2 7B achieves 28.1 higher ROUGE-1 points over the base standard attention model, where prior linear attentions lead to 16.5 point drops.

Introduction

The "Quadratic Bottleneck" remains a primary obstacle in scaling Transformers to long sequences. Standard softmax attention requires $O(n^2)$ computation and memory, where $n$ is the sequence length. While various linear attention mechanisms ($O(n)$) have been proposed to mitigate this, they historically suffer from a significant "quality gap" compared to their quadratic counterparts. This research identifies the root causes of this performance degradation and introduces Hedgehog, a learnable linear attention mechanism designed to bridge this gap through softmax mimicry.

The Quality Gap: Spikiness and Monotonicity

Why do linear attentions underperform? The authors identify two critical properties of standard softmax attention that are often lost in linear approximations:

  1. Low-Entropy ("Spikiness"): Softmax attention is highly effective at concentrating weight on a few highly relevant tokens. Most linear attention kernels produce "flat" distributions, which dilute the signal across the sequence.
  2. Dot-Product Monotonicity: In softmax, as the dot product between a query and key increases, the resulting attention weight increases monotonically. Many linear approximations fail to maintain this relationship, leading to training instability and poor performance during model conversion.

While some complex feature maps (like 2nd-order Taylor expansions) can preserve these properties, they often introduce high computational overhead ($O(nd^3)$), defeating the purpose of linear attention.

The Hedgehog Architecture

Hedgehog addresses these issues by using a learnable feature map. Instead of relying on a fixed mathematical kernel, Hedgehog employs a simple, single-layer MLP with an exponential activation function to produce attention weights.

Softmax Mimicry

To ensure the MLP captures the essential behavior of softmax, the authors utilize a distillation process called Softmax Mimicry. During training, the attention weights from a standard pretrained Transformer serve as "soft labels." The Hedgehog layers are trained to minimize the cross-entropy loss between their linear attention weights and the original softmax weights. This allows the model to learn the "spiky" nature of the original attention distribution.

Negation Mapping

To further improve expressivity, Hedgehog utilizes a Negation Mapping trick. By mapping the hidden dimension $d$ to $2d$ (concatenating $exp(x)$ and $exp(-x)$), the model can effectively capture negative correlations—a feature often ignored by standard ReLU-based linear attentions.

Experimental Results

Train-from-Scratch and BERT Recovery

In "train-from-scratch" scenarios on WikiText-103, Hedgehog outperformed prior linear attentions by up to 6 perplexity points. In "finetuned-conversion" tasks, Hedgehog recovered 99.3% of the original BERT-base performance across the GLUE benchmark, significantly outperforming methods like Performer or cosFormer, which often saw drops of over 8 GLUE points.

Linearizing Large Language Models

The most striking results involve the conversion of pretrained LLMs. The authors successfully converted a Llama-2 7B model into a linear version. Using Low-Rank Adaptation (LoRA), Hedgehog-Llama2 achieved a ROUGE-1 score of 47.4 on the SAMSum dialogue summarization task. In contrast, existing linear conversion methods caused the model to collapse, with scores dropping as low as 2.8.

Efficiency and Scaling

Hedgehog maintains the efficiency benefits of linear attention, achieving nearly 6x faster inference and significantly lower memory usage than FlashAttention at sequence lengths of 32K. For 125M-parameter subquadratic models, Hedgehog set a new state-of-the-art perplexity of 16.7 on WikiText-103.

Conclusion

Hedgehog demonstrates that the performance gap in linear attention is not an inherent limitation of $O(n)$ complexity, but rather a result of losing specific structural properties of the softmax function. By prioritizing "spikiness" and monotonicity through learnable MLPs and softmax mimicry, Hedgehog enables the efficient conversion and training of high-performance Transformers, including large-scale models like Llama-2.

Summary updated

BibTeX
@inproceedings{zhang2024hedgehog,
  author = {Michael Zhang and Kush Bhatia and Hermann Kumbong and Christopher Ré},
  title = {{The Hedgehog \& the Porcupine: Expressive Linear Attentions with Softmax Mimicry}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=4g02l2N2Nx},
}
Figure