Self-attention Does Not Need $O(n^2)$ Memory

By Markus Norman Rabe, Charles Staats. 2021

205 citations recorded by the source corpus.

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.

Subfields: Artificial Intelligence, Hardware and Architecture, Efficiency & Compression

How researchers see this paper

The researchers whose trusted networks rank it highest, strongest advocate first. Every researcher gets a different order.

In Artificial Intelligence

  1. Luke S. Zettlemoyer
  2. Mohit Bansal
  3. Jianfeng Gao
  4. Quoc V. Le
  5. Percy Liang
  6. Ruslan Salakhutdinov
  7. Yoshua Bengio
  8. Sergey Levine

In Hardware and Architecture

  1. Tri Dao
  2. Christopher R'e
  3. Atri Rudra
  4. Stefano Ermon
  5. Edouard Grave
  6. Daniel Y. Fu
  7. Armand Joulin
  8. Fan Yang

In Efficiency & Compression

  1. Tri Dao
  2. Armand Joulin
  3. Edouard Grave
  4. Stefano Ermon
  5. Christopher R'e
  6. Hugo Touvron
  7. Naman Goyal
  8. Mostafa Dehghani

Citations in the corpus