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 Re

TL;DR

Researchers at Stanford have developed Hedgehog, a learnable linear attention mechanism that bridges the performance gap between efficient linear Transformers and standard "quadratic" Transformers. By using a small MLP to mimic the "spiky" (low-entropy) and monotonic properties of softmax attention, Hedgehog recovers over 99% of standard model quality while offering up to 6x faster inference at long sequence lengths.

What Problem Does This Paper Address?

The "Quadratic Bottleneck" is the bane of modern NLP. Standard Transformer attention (Softmax) has a computational complexity of $O(n^2)$, meaning if you double the sequence length, the computational cost quadruples. This makes processing long documents or large contexts prohibitively expensive.

"Linear attentions" were proposed as a solution, reducing complexity to $O(n)$ by using the "kernel trick" to swap the order of operations. However, there’s a catch: linear Transformers almost always perform worse than their softmax cousins. They struggle to "focus" on specific tokens, often producing "blurry" or uniform attention weights.

The authors identify two specific reasons for this failure:

  1. Lack of Spikiness: Softmax attention is "spiky" (low entropy), meaning it focuses intensely on a few relevant tokens. Prior linear attentions are too "flat."
  2. Lack of Monotonicity: In softmax, as the dot product of a query and key increases, the attention weight always increases. Many linear attention feature maps actually see weights decrease as similarity increases, leading to unstable training.
Comparison of attention weight vs. dot product. Unlike prior linear methods, Hedgehog successfully mimics the smooth, monotonic curve of Softmax.

Methods

The authors introduce Hedgehog, which replaces the fixed feature maps used in prior work (like Performer or 1+ELU) with a learnable MLP.

The Hedgehog methodology: (1) a trainable feature map, (2) supervised to mimic standard attention, (3) resulting in an expressive and efficient linear attention layer.
  • The Feature Map: Hedgehog uses a single-layer MLP with an exponential activation function. To improve expressivity, they use a "negation mapping" ($R^d \to R^{2d}$), concatenating the exponential of the hidden state with the exponential of its negation. This allows the model to better factor in negative correlations.
  • Softmax Mimicry (Distillation): To ensure the MLP behaves like softmax, the authors use a distillation loss. They take a pretrained Transformer, freeze its weights, and train only the small Hedgehog MLP layers to minimize the KL-divergence between the Hedgehog attention weights and the original "ground-truth" softmax weights.
  • Three Training Regimes: They test Hedgehog in three ways:
  • Training from scratch: Initializing the MLP as an identity matrix and training the whole model.
  • Finetuned-conversion: Swapping attention in a task-specific model (like BERT) and finetuning.
  • Pretrained-conversion: Linearizing a massive model (like Llama-2) using LoRA (Low-Rank Adaptation).

Datasets

The authors evaluated Hedgehog across a diverse set of benchmarks:

  • Language Modeling: WikiText-103 (using GPT-2 architectures).
  • General Language Understanding: The GLUE benchmark (using BERT).
  • Long-Context Tasks: Long Range Arena (LRA).
  • Computer Vision: ImageNet-1K (using Vision Transformers/ViT).
  • Summarization: SAMSum (for the Llama-2 7B experiments).

Key Insights and Ideas

  • The "Aha" Moment on Taylor Expansions: The authors found that a 2nd-degree Taylor approximation of the exponential function actually recovers "spikiness" and monotonicity perfectly. However, it is computationally heavy ($O(nd^3)$). Hedgehog is designed to get the benefits of the Taylor expansion's shape but at the cost of standard linear attention ($O(nd^2)$).
  • Monotonicity Matters for Stability: The paper demonstrates that if an attention mechanism isn't monotonic, gradients become unstable during finetuning. By forcing the feature map to be monotonic, Hedgehog allows for much smoother "conversion" of pretrained models.
  • Generalization: Surprisingly, a Hedgehog layer trained to mimic softmax on one dataset (like WikiText) generalizes well to other tasks (like GLUE) without needing to be retrained from scratch.

Key Results

  • Efficiency: At a sequence length of 32K tokens, Hedgehog is 6x faster than FlashAttention and uses significantly less memory.
  • Quality Recovery: In BERT-base conversion, Hedgehog recovered 99.3% of the original model's GLUE performance, whereas prior methods like Performer lagged significantly behind.
  • State-of-the-Art (SOTA): On WikiText-103, a 125M parameter Hedgehog-GPT2 achieved a perplexity of 16.7, a new SOTA for subquadratic models of that size.
  • Scaling to Llama-2: They successfully converted Llama-2 7B into a linear version. Using LoRA, Hedgehog-Llama2 achieved 47.4 ROUGE-1 points on summarization, while previous linear conversion methods essentially collapsed (scoring as low as 2.8).

Limitations and Future Work

  • The Distillation Step: While the distillation process is relatively fast, it still requires a "teacher" model and a pass over some data to learn the MLP weights.
  • Fixed Head Dimension: The current implementation assumes a fixed head dimension; exploring how this scales with extremely large head dimensions in "ultra-wide" Transformers remains for future work.
  • Hardware Optimization: While theoretically faster, the authors note that custom CUDA kernels (similar to FlashAttention) could further widen the real-world speed gap between Hedgehog and standard attention.

Summary updated

BibTeX
@inproceedings{zhang2024hedgehog,
  author = {Michael Zhang and Kush Bhatia and Hermann Kumbong and Christopher Re},
  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