Introduction
The self-attention mechanism in standard Transformers faces a significant bottleneck: its computational and memory costs scale quadratically ($O(n^2)$) with sequence length $n$. This limitation hinders the processing of long-context inputs such as entire books or large codebases. While existing solutions like sparse attention or sliding windows offer some relief, they often sacrifice global dependency capture or implementation efficiency.
This paper introduces Tensorized Attention, a novel approach that reshapes 1D sequences into multi-dimensional tensors to perform hierarchical attention. By decomposing the massive $O(n^2)$ attention matrix into smaller, dimension-wise operations, the authors achieve substantial speedups while maintaining high performance on long-context tasks.
Methods: Attention Tensorization
The core innovation lies in "folding" a sequence of $n$ tokens into an $m$-order tensor. For example, a 1,000-token sequence can be represented as a $10 \times 10 \times 10$ cube.
Sequential Dimension-wise Attention
Instead of a single global attention operation, the model performs attention sequentially along each dimension of the tensor. This process is mathematically equivalent to a Kronecker decomposition of the full attention matrix. Attention on lower dimensions captures local context, while higher dimensions facilitate "multi-hop" interactions, allowing tokens to communicate across long distances in the original sequence.
Technical Implementation
The architecture utilizes mode-$i$ flattening and matrix multiplication to compute attention scores for each dimension. These are combined using element-wise operations and Einstein summation (Einsum) to produce the final tensorized output, which is then vectorized back to the original sequence format.
To ensure efficiency on modern hardware, the authors developed custom Triton kernels. These optimizations allow the theoretical complexity gains—reducing $O(n^2)$ to $O(n \log n)$ or even $O(n)$—to translate into significant wall-clock speedups. Additionally, the authors adapted Rotary Positional Embeddings (RoPE) to the multi-dimensional tensor space, enhancing the model's ability to extrapolate to sequences longer than those seen during training.
Key Insights
A critical theoretical finding is that while standard attention matrices are often high-rank, they are naturally low-rank in tensor space. This allows for the recovery of nearly all information using significantly fewer parameters. Furthermore, the hierarchical structure enables exponential extrapolation; adding a single element to a higher tensor dimension can expand the effective context window by thousands of tokens.
Evaluation and Results
The method was evaluated across language modeling (RedPajama), long-context benchmarks (LongBench), general NLP (GLUE), and time series classification (UEA Archive).
- Efficiency: At a 128k context length, Tensorized Attention is 11x faster than FlashAttention-2.
- Scalability: The authors successfully trained a Llama-3-8B model with a 32,768 context length on A6000 GPUs, which is typically memory-prohibitive for standard attention.
- Extrapolation: Perplexity remained stable up to 128k tokens, outperforming standard models using positional interpolation.
- Versatility: In time series tasks, the method reduced running time by 30% while maintaining state-of-the-art accuracy.
Conclusion and Future Work
Tensorized Attention provides a scalable path toward "infinite" context windows by leveraging the inherent hierarchical structure of long sequences. While the current work focuses on continued pre-training and sequences up to 128k, future research will explore training large-scale models from scratch, reaching the million-token regime, and integrating these custom kernels into mainstream deep learning libraries.
Summary updated