[Paper Review] Self-attention Does Not Need $O(n^2)$ Memory
This paper presents a memory-efficient attention mechanism that reduces memory complexity from $O(n^2)$ to $O(1)$ for single-query attention and $O(/log n)$ for self-attention by deferring the softmax normalization until the end of computation. The method enables significant memory savings—up to 59X for inference and 32X for backpropagation at sequence length 16384—while maintaining numerical stability and exact computation, making it a drop-in replacement for standard attention in Transformers.
We present a very simple algorithm for attention that requires $O(1)$ memory with respect to sequence length and an extension to self-attention that requires $O(\log n)$ memory. This is in contrast with the frequently stated belief that self-attention requires $O(n^2)$ memory. While the time complexity is still $O(n^2)$, device memory rather than compute capability is often the limiting factor on modern accelerators. Thus, reducing the memory requirements of attention allows processing of longer sequences than might otherwise be feasible. We provide a practical implementation for accelerators that requires $O(\sqrt{n})$ memory, is numerically stable, and is within a few percent of the runtime of the standard implementation of attention. We also demonstrate how to differentiate the function while remaining memory-efficient. For sequence length 16384, the memory overhead of self-attention is reduced by 59X for inference and by 32X for differentiation.
Motivation & Objective
- To challenge the widely held belief that self-attention inherently requires $O(n^2)$ memory.
- To develop a memory-efficient attention algorithm that computes the exact same result as standard attention without approximation.
- To enable practical deployment of long-sequence attention on memory-constrained accelerators like TPUs and GPUs.
- To provide a numerically stable, differentiable, and hardware-efficient implementation for use in modern Transformer architectures.
Proposed method
- Deferring the softmax normalization until the end of the computation by expressing attention as a ratio of sums: $\mathrm{attention}(q,k,v) = \frac{\sum_i v_i e^{s_i}}{\sum_j e^{s_j}}$, where $s_i = \mathrm{dot}(q,k_i)$.
- Processing keys and values sequentially, maintaining running sums of $v_i e^{s_i}$ and $e^{s_i}$, requiring only $O(1)$ memory per query.
- Extending the method to self-attention by sequentially computing outputs for each query, using $O(\log n)$ memory for indexing.
- Applying the standard numerical stability trick—subtracting the maximum score from all scores—while preserving the deferred normalization structure.
- Implementing the algorithm in JAX for efficient execution on TPUs, with optional key and query chunking to further reduce memory usage.
- Ensuring differentiability by maintaining gradient computation through the same forward pass structure, enabling use in backpropagation.
Experimental results
Research questions
- RQ1Can self-attention be computed with sub-quadratic memory complexity without approximation?
- RQ2Is it possible to maintain numerical stability in a memory-efficient attention mechanism that defers normalization?
- RQ3Can such a method be efficiently implemented on modern accelerators like TPUs and GPUs?
- RQ4Does the memory-efficient attention mechanism preserve model accuracy when used as a drop-in replacement in Transformers?
- RQ5How does the performance of this method compare to query chunking and other memory-reduction techniques?
Key findings
- The proposed method reduces memory overhead for self-attention to $O(\sqrt{n})$ in practice on accelerators, achieving 59X less memory for inference and 32X less for differentiation at sequence length 16384.
- The algorithm is numerically stable when the standard score-centering trick is applied, preventing overflow in floating-point arithmetic.
- The implementation runs within a few percent of the standard attention runtime and is fully differentiable, enabling use in end-to-end training.
- When integrated into a Flax-based Transformer, the memory-efficient attention achieved 62.69 BLEU on WMT en-de translation, compared to 62.59 for standard attention, showing near-identical performance.
- Compared to query chunking alone, the method provides better performance at scale because it also chunks keys, avoiding the need to reduce query chunk size below 64 for memory constraints.
- The method is a drop-in replacement for standard attention, requiring no architectural changes and preserving exact computation.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.