TaylorShift: Shifting the Complexity of Self-attention from Squared to Linear (and Back) Using Taylor-Softmax

Tobias Christian Nauen, Sebastian Palacio, Andreas Dengel

Introduction: The Attention Bottleneck

The primary hurdle in scaling modern Transformer models is the "Attention Bottleneck." Standard self-attention mechanisms require comparing every token in a sequence to every other token, resulting in a computational and memory complexity that scales quadratically ($O(N^2)$) with the sequence length $N$. This quadratic growth makes processing long-form content—such as extended documents, high-resolution images, or genomic data—prohibitively expensive.

While various "Linear Transformers" have been proposed, they often sacrifice expressivity. Sparse attention methods limit the model's field of view, while low-rank approximations can lose the ability to capture the intricate, fine-grained relationships between distant tokens that define the Transformer's success. TaylorShift introduces a novel mechanism that achieves linear scaling without compromising this modeling power.

The Taylor-Softmax Innovation

The core of TaylorShift is the Taylor-Softmax approximation. In standard attention, the exponential function ($\exp(x)$) creates the similarity matrix. TaylorShift replaces this with a second-order Taylor polynomial: $$1 + x + \frac{1}{2}x^2$$

This polynomial approximation allows for a mathematical "disentanglement" of queries and keys using a tensor-product-based operator ($\boxtimes$). By changing the order of operations, the model can combine keys and values into a global context buffer before applying queries, effectively shifting the complexity from quadratic to linear.

Methods: Dual Implementations

TaylorShift is unique because it offers two mathematically interchangeable implementations that allow a model to "shift" its computation strategy based on input length:

  1. Direct-TaylorShift: A quadratic version optimized for shorter sequences, maintaining the traditional attention structure.
  2. Efficient-TaylorShift: A linear version ($O(Nd^3)$) optimized for long sequences.
Overview of (a) standard softmax attention, (b) the quadratic direct-TaylorShift, and (c) the linear efficient-TaylorShift.

To ensure numerical stability—a common issue with Taylor expansions—the authors implemented a specialized normalization scheme. This involves mapping queries and keys to a unit sphere and utilizing a per-head temperature parameter to prevent values from exploding during training.

Efficiency and the $d^3$ Trade-off

While TaylorShift achieves linear scaling with respect to sequence length $N$, it introduces a cubic complexity relative to the head dimension $d$ ($O(d^3)$). This creates a specific design trade-off: the model is most efficient when configured with many "heads" of smaller dimensions rather than a few heads with large dimensions.

Sequence length crossover points ($N_0, N_1$) for different head dimensions $d$.

As shown in the table above, the "crossover point" where TaylorShift becomes more efficient than standard attention depends heavily on $d$. For a head dimension of $d=8$, the linear version becomes more efficient at sequences as short as 73 tokens. For $d=128$, the crossover point moves to over 16,000 tokens.

Experimental Results

The authors evaluated TaylorShift on the Long Range Arena (LRA) benchmark and ImageNet-1k.

  • Performance: On LRA, TaylorShift achieved an average accuracy of 62.8%, outperforming the vanilla Transformer (62.2%) and significantly exceeding other linear approximations like Performer (52.9%) and Linformer (57.0%).
  • Resource Efficiency: For a 2000-token sequence, TaylorShift requires only 35% of the memory of a standard Transformer. In terms of speed, the practical crossover point for inference is approximately 1700–1800 tokens.
  • Stability: The proposed normalization allowed TaylorShift to converge on complex tasks where other linear methods, such as Random Fourier Features (RFA), often failed.

Conclusion and Future Work

TaylorShift provides a flexible framework for scaling Transformers, allowing for a seamless transition between quadratic and linear complexity. However, the $O(d^3)$ scaling remains a bottleneck for models requiring large per-head dimensions. Future research may focus on low-level CUDA optimizations to lower the efficiency crossover point further and exploring methods to improve the model's ability to extrapolate to sequence lengths significantly longer than those seen during training.

Summary updated

BibTeX
@inproceedings{nauen2024taylorshift,
  author = {Tobias Christian Nauen and Sebastian Palacio and Andreas Dengel},
  title = {{TaylorShift: Shifting the Complexity of Self-attention from Squared to Linear (and Back) Using Taylor-Softmax}},
  booktitle = {ICPR},
  year = {2024},
  doi = {10.1007/978-3-031-78172-8\_1},
}
Figure