Abstract
Standard Transformer architectures suffer from a quadratic computational bottleneck, where the cost of attention scales with the square of the sequence length ($O(n^2)$). This paper introduces Luna (Linear Unified Nested Attention), a novel attention mechanism that decouples standard attention into two nested linear operations. By utilizing a fixed-length auxiliary sequence to "pack" and "unpack" information, Luna achieves linear time and memory complexity ($O(n)$) while outperforming existing efficient Transformer variants on long-sequence benchmarks.
The Quadratic Bottleneck in Transformers
The primary limitation of the standard Transformer is its attention mechanism. To calculate attention, every token in a input sequence must attend to every other token. While effective, this scaling makes Transformers prohibitively expensive for long-context tasks, such as processing entire books, high-resolution images, or long legal documents.
While various "efficient Transformers" (xformers) have been proposed to mitigate this, they often face trade-offs: some sacrifice accuracy, others struggle with variable-length sequences, and many cannot be easily adapted for autoregressive (causal) generation.
Methods: Linear Unified Nested Attention
Luna approximates standard softmax attention using two nested attention functions. The core innovation is the introduction of an additional input sequence $P$ with a fixed, constant length $l$ (e.g., 128 or 256 tokens), which serves as a bottleneck for information flow.
The Pack and Unpack Mechanism
- Pack Attention: The fixed-length sequence $P$ acts as a set of queries that attend to the actual input sequence $X$. This "packs" the raw input into a compressed, fixed-size representation. Because the length of $P$ is constant, this operation scales linearly relative to the input length.
- Unpack Attention: The original query sequence then attends to this compressed representation. This "unpacks" the relevant contextual information back to the original sequence length, again maintaining linear complexity.
Key Architectural Innovations
- Contextual Memory: Unlike models that use static projection matrices, $P$ is updated at each layer and passed to the next. This allows $P$ to function as a "shared workspace" or global memory that evolves as it processes the sequence.
- Flexibility: Luna’s packing mechanism is dynamic. Unlike models like Linformer, which are tied to specific sequence lengths, Luna can handle sequences of any length without changing model parameters.
- Linear Causal Attention: The authors also developed a variant supporting autoregressive decoding (GPT-style) using a cumulative summation technique, preserving linear efficiency for generative tasks.
Experimental Results
Luna was evaluated across diverse benchmarks, including the Long Range Arena (LRA), WMT’14 English-German translation, and large-scale pretraining (RoBERTa-base configuration).
Performance on Long Range Arena (LRA)
Luna-256 achieved an average score of 61.95 on the LRA benchmark, significantly outperforming the standard Transformer (54.39) and other efficient models like BigBird (55.01) and Performer (51.41). As shown in the figure below, Luna occupies the ideal "top-right" quadrant of the performance-efficiency trade-off.
Efficiency Gains
The efficiency benefits of Luna are most apparent at scale. At a sequence length of 4,000 tokens, Luna-16 uses only 10% of the memory required by a standard Transformer and operates roughly 5.5x faster during training. Even with a projection length ($l$) as small as 16, Luna remains remarkably competitive, suggesting that a massive bottleneck can still capture essential contextual information.
Conclusion and Future Work
Luna demonstrates that it is possible to achieve linear efficiency without sacrificing the representational power needed for complex long-sequence tasks. While it slightly lagged behind vanilla RoBERTa in extremely high-resource settings (160GB pretraining), its performance on LRA and translation tasks establishes it as a leading architecture for long-context modeling. Future work includes exploring Luna for document-level summarization and integrating it with recurrence to process potentially infinite data streams.
Summary updated