Skip to main content
QUICK REVIEW

[Paper Review] Fast Attention Requires Bounded Entries

Josh Alman, Zhao Song|arXiv (Cornell University)|Feb 26, 2023
Machine Learning and AlgorithmsComputer Science9 citations
TL;DR

The paper proves a tight transition for approximate attention computation: near-linear time is possible when input entry bound B is o(sqrt(log n)), but subquadratic-time hardness holds for B = Theta(sqrt(log n)) under SETH.

ABSTRACT

In modern machine learning, inner product attention computation is a fundamental task for training large language models such as Transformer, GPT-1, BERT, GPT-2, GPT-3 and ChatGPT. Formally, in this problem, one is given as input three matrices $Q, K, V \in [-B,B]^{n imes d}$, and the goal is to construct the matrix $\mathrm{Att}(Q,K,V) := \mathrm{diag}(A {\bf 1}_n)^{-1} A V \in \mathbb{R}^{n imes d}$, where $A = \exp(QK^ op/d)$ is the `attention matrix', and $\exp$ is applied entry-wise. Straightforward methods for this problem explicitly compute the $n imes n$ attention matrix $A$, and hence require time $Ω(n^2)$ even when $d = n^{o(1)}$ is small. In this paper, we investigate whether faster algorithms are possible by implicitly making use of the matrix $A$. We present two results, showing that there is a sharp transition at $B = Θ(\sqrt{\log n})$. $\bullet$ If $d = O(\log n)$ and $B = o(\sqrt{\log n})$, there is an $n^{1+o(1)}$ time algorithm to approximate $\mathrm{Att}(Q,K,V)$ up to $1/\mathrm{poly}(n)$ additive error. $\bullet$ If $d = O(\log n)$ and $B = Θ(\sqrt{\log n})$, assuming the Strong Exponential Time Hypothesis from fine-grained complexity theory, it is impossible to approximate $\mathrm{Att}(Q,K,V)$ up to $1/\mathrm{poly}(n)$ additive error in truly subquadratic time $n^{2 - Ω(1)}$. This gives a theoretical explanation for the phenomenon observed in practice that attention computation is much more efficient when the input matrices have smaller entries.

Motivation & Objective

  • Motivate speeding up inner-product attention in transformers beyond explicit O(n^2) computation.
  • Investigate how input entry magnitude B affects the possibility of fast approximate attention.
  • Characterize a sharp transition point at B = Theta(sqrt(log n)) for d = O(log n).
  • Provide both an algorithm achieving near-linear time for small B and a conditional lower bound for larger B.

Proposed method

  • Introduce AA ttC(n,d,B,εa) as the approximate attention computation problem with entries bounded by B.
  • Use a polynomial approximation of the exponential to obtain a low-rank approximation of the attention matrix A via the polynomial method.
  • Show that A can be approximated by a low-rank matrix with rank r and constructible factors U1,U2 in O(n r) time (Lemma 3.4).
  • Translate approximation of A into approximation of the attention result with controlled D and V propagation (Lemmas 3.5 and 3.6).
  • Derive a main upper-bound algorithm (Algorithm 1) with running time O(T_mat(n,r,d) + n r g), where g and r depend on B and ε.
  • Choose parameters to achieve n^{1+o(1)} time when B = o(sqrt(log n)) and d = O(log n.

Experimental results

Research questions

  • RQ1When can attention computation be performed in almost linear time in the input size?
  • RQ2When is subquadratic-time attention computation provably impossible under SETH?
  • RQ3How does the input entry bound B influence the complexity of approximate attention computation for d = O(log n)?
  • RQ4Can polynomial-method-based techniques yield practical near-linear algorithms for AA ttC?
  • RQ5What are the precise transitions in complexity around B = Theta(sqrt(log n))?

Key findings

  • There is a tight transition at B = Theta(sqrt(log n)).
  • If d = O(log n) and B = o(sqrt(log n)), there is an n^{1+o(1)}-time algorithm for AA ttC with poly(n) additive error.
  • If d = O(log n) and B = Theta(sqrt(log n)), assuming SETH, no truly subquadratic-time (n^{2−Ω(1)}) algorithm exists for approximating AA ttC.
  • The lower bound for AA ttC leverages a reduction from Approximate Nearest Neighbor search under SETH.
  • The upper-bound algorithm uses a polynomial approximation of exp and a low-rank factorization to speed up computation.
  • The work provides a theoretical explanation for practical observations that attention is faster with bounded or low-bit entries.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.