Title: Fast Attention Requires Bounded Entries
Venue: NeurIPS 2023
Accepted: yes
Keywords: fast attention computation

algorithm

hardness
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 \times 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 \times d}$, where $A = \exp(QK^\top/d)$ is the `attention matrix', and $\exp$ is applied entry-wise. Straightforward methods for this problem explicitly compute the $n \times n$ attention matrix $A$, and hence require time $\Omega(n^2)$ even when $d = n^{o(1)}$ is small. 


In this paper, we investigate whether faster algorithms are possible by \emph{implicitly} making use of the matrix $A$. We present two results, showing that there is a sharp transition at $B = \Theta(\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 = \Theta (\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 - \Omega(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.

Fast Attention Requires Bounded Entries

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 ∈ [ -B,B ] n × d , and the goal is to construct the matrix Att( Q,K,V ) := diag( A 1 n ) -1 AV ∈ R n × d , where A = exp( QK ⊤ /d ) is the 'attention matrix', and exp is applied entry-wise. Straightforward methods for this problem explicitly compute the n × 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 = Θ( √ log n ) .

If d = O (log n ) and B = o ( √ log n ) , there is an n 1+ o (1) time algorithm to approximate Att( Q,K,V ) up to 1 / poly( n ) additive error. √

If d = O (log n ) and B = Θ( log n ) , assuming the Strong Exponential Time Hypothesis from fine-grained complexity theory, it is impossible to approximate Att( Q,K,V ) up to 1 / 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.

1 Introduction

Large language models (LLMs) such as Transformer [VSP + 17], BERT [DCLT18], GPT-3 [BMR + 20], PaLM [CND + 22], and OPT [ZRG + 22] can process natural language more effectively than smaller models or traditional algorithms. This means that they can understand and generate more complex and nuanced language, which can be useful for a variety of tasks such as language translation, question answering, and sentiment analysis. LLMs can also be adapted to multiple purposes without needing to be retained from scratch. Their power is particularly exemplified by the recent success of ChatGPT, a chat software by OpenAI built on top of GPT-3 [Ope22].

The key technical backbone of LLMs is the attention matrix [VSP + 17, RNS + 18, DCLT18, RWC + 19, BMR + 20]. An attention matrix is a square matrix whose rows and columns correspond to words or 'tokens', and whose entries correspond to the correlations between these tokens in natural text. The attention matrix is then used to calculate the importance of each input token in a sequence when producing an output. In an attention mechanism, each input token is given a weight or score, which reflects its importance or relevance to the current output being generated. These scores are calculated based on a comparison between the current output state and the input states, using a similarity function.

More formally, the attention matrix is defined as follows. Let Q ∈ R n × d be the matrix of query tokens, and K ∈ R n × d be the matrix of key tokens. (We focus here on the case when d = n o (1) , so d ≪ n .) The attention matrix is an n × n matrix A where the rows and columns correspond to the input tokens in the sequence. Each entry in the matrix represents the attention weight or score between a particular input token (query token Q ) and a particular output token (key token K ). The diagonal entries of the matrix represent self-attention scores, which measure the importance of each token with respect to itself.

The major bottleneck to speeding up LLM operations (in the case of modeling long sequences with large n ) is the time to perform attention matrix computations [VSP + 17, RNS + 18, DCLT18, RWC + 19, BMR + 20, WLK + 20, KKL20]. These computations ask us to multiply the attention matrix A with another value token matrix V ∈ R n × d .

We formally define Attention computation as follows. Throughout this paper, we write exp to denote the entry-wise exponential for matrices.

Definition 1.1 (Exact Attention Computation EAttC ( n, d ) ) . Given three matrices Q,K,V ∈ R n × d , output the n × d matrix Att( Q,K,V ) defined by

where A ∈ R n × n and diagonal matrix D ∈ R n × n are defined as

The straightforward algorithm for this problem computes the matrix A and then performs the multiplications D -1 AV , in time n 2+ o (1) . Since A is an n × n matrix with n 2 entries, it is impossible to improve on this much while explicitly computing the matrix A . However, the input to the problem is not A , but rather the three matrices Q,K,V which each have only n 1+ o (1) entries. An algorithm which only implicitly makes use of A , without explicitly computing all its entries, could hope to run in almost linear time!

In this paper, we investigate the possibility of accelerating attention computations in this way. The two main questions we address are:

Q1. When can we perform attention computations in almost linear time n 1+ o (1) ?

Q2. When can we prove that subquadratic-time algorithms for attention computations are impossible ?

In most LLMs, it suffices to approximately perform attention computations throughout the inference process as long as there are reasonable precision guarantees [CGRS19, KKL20, WLK + 20, DKOD20, KVPF20, CDW + 21, CDL + 22, LWD + 23, ZSZ + 23]. We therefore focus here on approximate attention computation, which can potentially be performed even faster than exact computation. Mathematically, we define the approximate version of EAttC as follows.

Definition 1.2 (Approximate Attention Computation AAttC ( n, d, B, ϵ a ) ) . Let ϵ a > 0 and B > 0 be parameters. Given three matrices Q,K,V ∈ R n × d , with the guarantees that ∥ Q ∥ ∞ ≤ B , ∥ K ∥ ∞ ≤ B , and ∥ V ∥ ∞ ≤ B , output a matrix T ∈ R n × d which is approximately equal to D -1 AV , meaning,

Here, for a matrix M ∈ R n × n , we write ∥ M ∥ ∞ := max i,j | M i,j | .

Again, the straightforward algorithm for this problem runs in time O ( n 2 d ) ≤ n 2+ o (1) , but the input size is only O ( nd ) ≤ n 1+ o (1) . Our goal is to investigate when faster algorithms are possible in terms of the parameters d, B , and ϵ a .

1.1 Our Results

We focus on the natural setting where d = O (log n ) (the setting where we model long sequences) and ϵ a = 1 / poly( n ) (low enough error so that attention computations over an entire network can be combined). Our main results show that whether or not there is a fast algorithm for AAttC critically depends on B , the magnitudes of the entries in the input matrices.

√

We first show a lower bound, that when B ≥ Ω( log n ) , it is impossible to design a truly subquadratic-time algorithm. Our lower bound makes use of the Strong Exponential Time Hypothesis ( SETH ) [IP01], a popular conjecture [Wil18] from the area of fine-grained complexity regarding the time required to solve k -SAT. (See Section 4 below where we discuss SETH in more detail.)

Theorem 1.3 (Lower bound, informal version of Theorem 4.6) . Assuming SETH , for every q > 0 , there are constants C, C a , C b > 0 such that: there is no O ( n 2 -q ) time algorithm for the problem AAttC ( n, d = C log n, B = C b √ log n, ϵ a = n -C a ) .

Our second complementary result is a new algorithm, showing that when B < o ( √ log n ) , the problem can be solved very efficiently, in almost linear time.

Theorem 1.4 (Upper bound, informal version of Theorem 3.8) . There is an algorithm (Algorithm 1) that solves AAttC ( n, d = O (log n ) , B = o ( √ log n ) , ϵ a = 1 / poly( n )) in time n 1+ o (1) .

Our Theorems 1.3 and 1.4 show that the attention computation problem AAttC exhibits a very tight transition at B = Θ( √ log n ) from almost linear time to trivial quadratic time. When B < o ( √ log n ) is smaller, the problem can be solved in almost linear time n 1+ o (1) in the input size, using our algorithm for Theorem 1.4. When B ≥ Ω( √ log n ) is greater, our algorithm from Theorem 1.4 no longer applies, and furthermore our lower bound from Theorem 1.3 shows that it is impossible to solve the problem in truly subquadratic time, no matter what algorithmic techniques one uses (assuming SETH ).

It has been observed in LLM implementations in practice that computations are much faster when one assumes that the matrix entries are bounded or can be well-approximated using a small number of bits (see, e.g., [ZBIW19, Section 2] and [KVPF20, Section 3.2.1]). Our work can be viewed as giving a theoretical explanation for this phenomenon, and helping to explain why techniques like quantization [ZBIW19] and low-degree polynomial approximation [KVPF20] have been so effective in practice.

Related Work.

A recent work by Zandieh, Han, Daliri, and Karbasi [ZHDK23] was the first to give an algorithm with provable guarantees for attention approximation. Their algorithm makes use of locality sensitive hashing (LSH) techniques [CKNS20] which, as we will discuss next, is quite different from our algorithm for Theorem 1.4 which uses the polynomial method [ACSS20, AA22].

In the case when d = o (log 2 n ) , they achieve a running time of roughly O ( n 1 . 17 · d/ϵ 2 r ) , where ϵ r is a relative error parameter (which is similar, though not exactly the same, as our ϵ a from Definition 1.2). In particular, their algorithm applies for larger d than ours (we require d = O (log n ) ), but we achieve almost linear time n 1+ o (1) (whereas their running time is bounded below by Ω( n 1 . 17 ) ), and our algorithm can handle any polynomial error ϵ a = 1 / poly( n ) (whereas they require ϵ r ≥ 1 /n o (1) to not increase the running time by a polynomial factor).

It is natural to wonder whether further improvements are possible by combining our techniques with those of [ZHDK23]. However, our lower bound of Theorem 1.3 shows that our algorithm of Theorem 1.4 is already essentially tight and cannot be substantially improved.

Another recent work by Keles, Wijewardena, and Hedge [KWH23] was the first to prove a lower bound for attention computation assuming SETH . They prove, among other results, that AAttC cannot be solved in truly subquadratic time in the case when d = ω (log n ) . Our Theorem 1.3 improves their result to also hold for d = Θ(log n ) , and to show how the complexity changes with the magnitude of entries B (which is not studied by [KWH23]). As we discuss more shortly, both our lower bound proof and [KWH23] use the high-level technique of [BIS17], although our more fine-grained analysis of the parameters d, B requires a more intricate analysis and the use of other techniques from fine-grained complexity related to approximate nearest neighbor search [Rub18] and the polynomial method [AA22].

1.2 Technique Overview

Our high-level approach is to make use of similarities between attention computation and other computational problems related to Kernel Density Estimation (KDE). Such a relationship was

investigated by recent work [TBY + 19, ZHDK23]. In particular, [ZHDK23] was inspired to apply LSH techniques to attention computation because of the prevalence of LSH in KDE algorithms [CS17, BCIS18, CS19, CKNS20]. The main conceptual idea behind our results is that different techniques from the KDE literature, other than LSH, can be modified to apply in this setting and yield tight algoriths and lower bounds.

To design our algorithm for Theorem 1.3, we instead build off of a different line of work on KDE which makes use of the 'polynomial method in algorithm design'. Suppose M ∈ R n × n is a matrix, f : R → R is a function, and let f ( M ) denote the matrix one gets by applying f entry-wise to M . The polynomial method is a technique for finding low-rank approximations of f ( M ) . It shows that if M has low rank, and if f can be approximated by a low-degree polynomial, then the matrix f ( M ) is very close to a low-rank matrix whose low-rank decomposition can be computed efficiently.

To use this to solve AAttC , we make use of a recent result which bounds the degree required to approximate the exponential function by a polynomial [AA22] in order to find a low-rank approximation of the attention matrix A . Prior work [ACSS20, ACM + 20, AA22] applied these polynomials in a similar way to solve the Gaussian KDE problem; our main observation is that by an appropriate rescaling, this approach can be modified to apply to AAttC as well.

The proof of our lower bound Theorem 1.3 builds off of another line of work on the fine-grained complexity of KDE problems [BIS17, ACSS20, AA22]. The main idea is to give a fine-grained reduction from the well-studied problem of Approximate Nearest Neighbor search ANN . In ANN , one is given as input n vectors of dimension d , and an error parameter ϵ > 0 , and the goal is to find a pair of vectors whose distance is at most (1 + ϵ ) times the minimum distance between any pair of the vectors. The straightforward algorithm for ANN runs in quadratic time, and it is known that it is impossible to solve ANN in truly subquadratic time assuming SETH [Rub18].

In order to prove our lower bound, we show that AAttC can be used to solve ANN . The key idea is that, if the matrices Q and K from AAttC are formed by concatenating the input vectors to the ANN problem, then the nearest neighbor vectors correspond to the largest entries of the attention matrix A . It is not immediately clear that AAttC can be used to detect large entries of A , since the output is rescaled by the matrix D -1 , but we show that this can be overcome with some modifications to the input vectors which approximately balance the rows of A . Prior work [BIS17, ACSS20, AA22] used a very similar approach to give lower bounds for KDE problems, although KDE doesn't involve any rescaling factors.

Roadmap.

In Section 2, we introduce relevant notation and tools from prior work. In Section 3, we present and analyze our attention algorithm. In Section 4, we prove our fine-grained attention lower bound. In Section 5, we provide a conclusion for this paper.

2 Preliminaries

We work in the standard real-RAM model and assume arithmetic operations on real numbers can be performed in constant time in our algorithms.

We use T mat ( a, b, c ) to denote the time to multiply an a × b matrix with another b × c matrix. In fact, we will only make use of the straightforward, practical bound T mat ( a, b, c ) ≤ O ( abc ) . In principle, fast theoretical matrix multiplication algorithms could be used instead to improve this bound and speed up our algorithms here (in exchange for making them less practical). That said, because of our parameter settings 3 , we will see that faster matrix multiplication could only improve low-order terms in our running times.

For any positive integer, we use [ n ] to denote set { 1 , 2 , · · · , n } .

For a matrix M , we write ∥ M ∥ ∞ to denote its ℓ ∞ norm, i.e., ∥ M ∥ ∞ := max i,j | M i,j | . For a matrix M , we use M ⊤ to denote its transpose.

We use 1 n to denote a lengthn vector whose entries are all 1 s. We use 0 n to denote a lengthn vector whose entries are all 0 s.

For any matrix A ∈ R n × n , we use exp( A ) ∈ R n × n to denote the matrix where exp( A ) i,j = exp( A i,j ) . In other words, all the exp() operators in this paper are applied entry-wise to matrices. In particular, we will not use matrix exponentials in this paper.

For a vector x ∈ R n , we use ∥ x ∥ 0 to denote its number of non-zero entries, we use ∥ x ∥ 1 to denote its ℓ 1 norm, i.e., ∥ x ∥ 1 := ∑ n i =1 | x i | , and we use ∥ x ∥ 2 to denote its ℓ 2 norm, i.e., ∥ x ∥ 2 := ( ∑ n i =1 | x i | 2 ) 1 / 2 . For a vector x , we use x ⊤ to denote its transpose.

2.1 Additive Error for Polynomial Approximation

Our algorithm for attention computation will critically make use of a polynomial approximation for the exponential function. In particular, we use the following tight construction from previous work [AA22].

Lemma 2.1 ([AA22]) . Let B > 1 and let ϵ ∈ (0 , 0 . 1) . There is a polynomial P : R → R of degree g := Θ ( max { log(1 /ϵ ) log(log(1 /ϵ ) /B ) , B }) such that for all x ∈ [0 , B ] , we have

Moreover, P can be computed efficiently: its coefficients are rational numbers with poly( g ) -bit integer numerators and denominators which can be computed in poly( g ) time.

2.2 From Additive Error to Relative Error

We note that in our setting, Lemma 2.1 can be used to give a relative error approximation as well: Corollary 2.2. Let B > 1 and let ϵ ∈ (0 , 0 . 1) . There is a polynomial P : R → R of degree g := Θ(max { log(1 /ϵ ) log(log(1 /ϵ ) /B ) , B } ) such that for all x ∈ [ -B,B ] , we have

Proof. By Lemma 2.1, there is a polynomial Q : R → R of degree g = Θ( { log(1 /ϵ ) log(log(1 /ϵ ) /B ) , B } ) such that, for all y ∈ [0 , 2 B ] we have | Q ( y ) -exp( y ) | ≤ ϵ . Our desired polynomial is the rescaled P ( x ) := Q ( x + B ) / exp( B ) . Indeed, for any x ∈ [ -B,B ] , we have exp( x ) ≥ exp( -B ) , and so

as desired.

3 Attention Algorithm

In this section, we show how to use polynomial approximations for the exponential function in order to approximately perform attention computations. In Section 3.1, we define the type of low-rank matrix approximation which we will use. In Section 3.2, we show how polynomial approximations can give rise to such low-rank matrix approximations. In Section 3.3, we bound the entries of the matrix QK ⊤ ∈ R n × n (before converting it to the attention matrix) to confirm that our polynomial approximation applies. In Section 3.4, we state our main technique for approximating the attention matrix. In Section 3.5, we show how to control the error propagation from A to the rescaling matrix D . In Section 3.6, we further explain how to control the error propagation from D and A to the resulting attention matrix. Finally, in Section 3.7, we conclude our general algorithm, and in Section 3.8, we appropriately select the parameters to achieve almost linear time.

3.1 Matrix Low-Rank Approximation

Definition 3.1. Let r ≥ 1 denote a positive integer. Let ϵ ∈ (0 , 0 . 1) denote an accuracy parameter. Given a matrix A ∈ R n × n ≥ 0 , we say ˜ A ∈ R n × n ≥ 0 is an ( ϵ, r ) -approximation of A if

˜ A = U 1 · U ⊤ 2 for some matrices U 1 , U 2 ∈ R n × r (i.e., ˜ A has rank at most r ), and

| ˜ A i,j -A i,j | ≤ ϵ · A i,j for all ( i, j ) ∈ [ n ] 2 .

3.2 From Low Degree Polynomials to Low Rank Matrices

Lemma 3.2. Let M = XY ⊤ ∈ R n × n denote a matrix with X,Y ∈ R n × d . Let P ( x ) denote a degreeg polynomial, and define r = ( 2( g + d ) 2 g ) .

There is an algorithm that runs in O ( nrg ) time and, given as input the matrix X,Y , constructs matrices U 1 , U 2 ∈ R n × r such that P ( M ) = U 1 U ⊤ 2 . (Here, P ( M ) denotes the entry-wise application of P to M .)

Due to space limitation, we defer the proof of Lemma 3.2 to Appendix A.

3.3 Matrix QK ⊤ Has Bounded Entries

Lemma 3.3 (Bounded entry) . Suppose B ≥ 1 and matrices Q,K ∈ R n × d have ∥ Q ∥ ∞ ≤ B and ∥ K ∥ ∞ ≤ B . Then, we have

Proof. For each ( i, j ) ∈ [ n ] × [ n ] , we have

as desired.

3.4 Key Lemma

Our key lemma shows that, even though the attention matrix A may have full rank, it has a low-rank approximation that is easy to compute:

Lemma 3.4. Suppose Q,K ∈ R n × d , with ∥ Q ∥ ∞ ≤ B , and ∥ K ∥ ∞ ≤ B . Let A := exp( QK ⊤ /d ) ∈ R n × n . For accuracy parameter ϵ ∈ (0 , 1) , there is a positive integer g bounded above by

and a positive integer r bounded above by

such that: There is a matrix ˜ A ∈ R n × n that is an ( ϵ, r ) -approximation (Definition 3.1) of A ∈ R n × n . Furthermore, the matrices U 1 and U 2 defining ˜ A can be computed in O ( n · r ) time.

Proof. Let M := QK ⊤ /d . From Lemma 3.3, we know that ∥ M ∥ ∞ ≤ B 2 . Thus, applying Corollary 2.2 (with bound B 2 on its entries), there is a degreeg polynomial P such that the matrix ˜ A = P ( M ) is an ( ϵ, r ) -approximation to A (See the definition of ( ϵ, r ) -approximation in Definition 3.1.) We can then compute U 1 , U 2 using Lemma 3.2, which gives the bound

This completes the proof.

3.5 From A to D

Lemma 3.5. Let A ∈ R n × n be any matrix whose entires are all positive and ϵ A ∈ (0 , 0 . 1) be any parameter. Let ˜ A ∈ R n × n be any matrix such that, for all ( i, j ) ∈ [ n ] × [ n ] , we have

Define the matrices D, ˜ D ∈ R n × n by D = diag( A 1 n ) and ˜ D = diag( ˜ A 1 n ) . Then, for all i ∈ [ n ] we have

Due to space limitation, we defer the proof of Lemma 3.5 into Appendix A.

3.6 From A and D to Attention Matrix

Lemma 3.6. Let ϵ A , ϵ D ∈ (0 , 0 . 1) and B > 1 be parameters, and let V ∈ R n × d denote a matrix with ∥ V ∥ ∞ ≤ B . Let A ∈ R n × n be any matrix whose entires are all positive, and let ˜ A ∈ R n × n be a matrix such that, for all ( i, j ) ∈ [ n ] × [ n ] we have

Let D, ˜ D ∈ R n × n be any diagonal matrices with positive entries on their diagonals, with the property that, for all i ∈ [ n ] , we have

Due to space limitation, we delay the proof of Lemma 3.6 to Appendix A.

3.7 Main Upper Bound

Theorem 3.7. For positive integers n, d and real parameters ϵ > 0 and B > 1 , there are positive integers g = Θ(max { log(1 /ϵ ) log(log(1 /ϵ ) /B 2 ) , B 2 } ) and r = ( 2( g + d ) 2 d ) such that: There is an algorithm (Algorithm 1) that runs in O ( T mat ( n, r, d ) + nrg ) time to solve AAttC ( n, d, B, ϵ ) (Definition 1.2).

Proof. The running time of each step is shown in Algorithm 1; its running time follows from Lemma 3.4. Its correctness follows from Lemma 3.5 and Lemma 3.6.

Algorithm 1 Our Algorithm

Then, we have

3.8 Proof of Theorem 1.4

Theorem 3.8 (Upper bound, formal statement of Theorem 1.4) . AAttC ( n, d = O (log n ) , B = o ( √ log n ) , ϵ a = 1 / poly( n )) can be solved in time T mat ( n, n o (1) , d ) = n 1+ o (1) .

Proof. If we select the parameters

in Theorem 3.7, then we see that

where the second step follows from ϵ = 1 / poly( n ) and the third step follows from B = o ( √ log n ) Since g = o (log n ) , let us write g = (log n ) /f for some f = ω (1) . We thus have that

.

The second step follows from the generic bound ( a b ) ≤ ( ea/b ) b for 1 ≤ b ≤ a , and the third step uses that d = O (log n ) .

Since d, r, g are all bounded by n o (1) , our final running time is n 1+ o (1) as desired.

4 Hardness

In this section, we prove our fine-grained lower bound for attention computation. In Section 4.1, we state the Strong Exponential Time Hypothesis ( SETH ), the main hardness assumption we will use. In Section 4.2, we define the approximate nearest neighbor search problem, and its known hardness assuming SETH . Finally, in Section 4.3, we give a reduction from approximate nearest neighbor search to attention computation, which implies our hardness result.

4.1 Fine-Grained Hypotheses

The Strong Exponential Time Hypothesis ( SETH ) was introduced by Impagliazzo and Paturi [IP01] over 20 years ago. It is a strengthening of the P = NP conjecture, which asserts that our current best SAT algorithms are roughly optimal:

̸

Hypothesis 4.1 (Strong Exponential Time Hypothesis ( SETH )) . For every ϵ > 0 there is a positive integer k ≥ 3 such that k -SAT on formulas with n variables cannot be solved in O (2 (1 -ϵ ) n ) time, even by a randomized algorithm.

SETH is a popular conjecture which has been used to prove fine-grained lower bounds for a wide variety algorithmic problems. See, for instance, the survey [Wil18].

4.2 Nearest Neighbor Search

We will make use of a known relationship between SETH and approximate nearest neighbor search. Definition 4.2 (Approximate Hamming Nearest Neighbor Search ( ANN ) ) . For a parameter ϵ > 0 , in the (1 + ϵ ) -Approximate Hamming Nearest Neighbor Search problem for n vectors of dimension d , we are given as input two sets A,B ⊂ { 0 , 1 } d with | A | = | B | = n , and our goal is to find an a ∗ ∈ A and b ∗ ∈ B satisfying ∥ a ∗ -b ∗ ∥ 0 ≤ (1 + ϵ ) · min a ∈ A,b ∈ B ∥ a -b ∥ 0 .

(This is sometimes called the 'bichromatic' ANN problem, and a monochromatic version has also been studied; see, for instance, [SM19].) Rubinstein [Rub18] showed that for certain parameters, it is impossible to substantially improve on the straightforward quadratic-time algorithm for ANN assuming SETH :

Lemma 4.3 ([Rub18]) . Assuming SETH , for every q > 0 , there are ϵ ∈ (0 , 1) and C > 0 such that (1 + ϵ ) -Approximate Hamming Nearest Neighbor Search in dimension d = C log n requires Ω( n 2 -q ) time.

Remark 4.4. We may assume that 4.3 holds even in the special case where each input vector from A and B has half its entries equal to 0 and half equal to 1 . Indeed, for any vector a ∈ { 0 , 1 } d , we can construct a new vector ˜ a ∈ { 0 , 1 } 2 d given by ˜ a = [ a ⊤ a ⊤ ] ⊤ . Here a ∈ { 0 , 1 } d is the binary complement of vector a , i.e., a i = 1 -a i for all i ∈ [ d ] . Thus, ∥ ˜ a ∥ 0 = d . We can similarly construct a new vector ˜ b ∈ { 0 , 1 } 2 d for each b ∈ B . After this transformation, for any a ∈ A and b ∈ B , we have ∥ ˜ a -˜ b ∥ 0 = 2 · ∥ a -b ∥ 0 , so it suffices to find an approximate nearest neighbor among these transformed vectors.

For convenience in our the analysis, we define a gap version of approximate nearest neighbor search problem Gap -ANN ( n, d, t, ϵ ) .

Definition 4.5 (Gap approximate nearest neighbor search ( Gap -ANN ( n, d, t, ϵ ) )) . Let n, d denote two positive integers. Let t > 0 denote a threshold parameter. Let ϵ denote a accuracy parameter. Given two sets of points A = { a 1 , · · · , a n } ⊂ { 0 , 1 } d and B = { b 1 , · · · , a n } ⊂ { 0 , 1 } d : For each i ∈ [ n ] , we need to distinguish the following two cases

Case 1. There exists a j ∈ [ n ] such that ∥ a i -b j ∥ 0 < t .

Case 2. For all j ∈ [ n ] we have ∥ a i -b j ∥ 2 2 ≥ (1 + ϵ ) · t .

An algorithm for Gap -ANN ( n, d, t, ϵ ) can be called log( nd ) times to binary search for the answer to ANN , so Lemma 4.3 holds as well for Gap -ANN ( n, d, t, ϵ ) .

4.3 Hardness Result

In the remainder of this section, we prove our lower bound for attention computation:

Theorem 4.6 (Main Result, formal version of Theorem 1.3) . Assuming SETH , for every sufficiently small q > 0 , there are constants C > 0 and C α > 0 and C β > 1 such that Approximate Attention Computation AAttC (Definition 1.2) for parameters ( n, d = C log n, B = C β √ log n, ϵ a = n -C α ) requires Ω( n 2 -q ) time.

Proof. This follows from combining Lemma 4.3 (hardness for approximation nearest neighbor search) and Lemma 4.7 (a reduction from approximate nearest neighbor search to approximate attention computation) which we prove below.

Lemma 4.7. For any constant C γ ∈ (0 , 0 . 1) : For every ϵ > 0 and C > 0 , there exist constants C a > 0 and C b > 0 and such that, if AAttC (Definition 1.2) for parameters (2 n, d = 2 C log n, B = C b √ log n, ϵ a = n -C a ) can be solved in time T , then Gap -ANN ( n, d = C log n, t, ϵ ) (Definition 4.5) can be solved in time O ( T + n 2 -C γ ) .

Due to space limitation, we defer the proof of Lemma 4.7 to Appendix B.

5 Conclusion

In this work, we showed that how quickly one can perform attention computation depends critically on the magnitude, B , of the entries of the input matrices. Our main idea was to make use of similarities between attention computation and KDE, and to show how many known techniques for KDE can also be used in this setting. Since KDE is a very well-studied problem, it would be exciting to see what other techniques can be applied to attention computation as well. One limitation of our lower bound result is, it is a conditional lower bound which is based on a well-known conjecture SETH in the area of complexity. It would be interesting to show unconditional lower bound for future work. As far as we are aware, our work does not have negative societal impacts.

Acknowledgements Josh Alman was partly supported by a grant from the Simons Foundation (Grant Number 825870 JA). The authors would like to thank Beidi Chen for helpful discussions related to LLMs, and Feyza Duman, Chinmay Hegde, and Piotr Indyk for helpful comments on an earlier draft. The authors would like to appreciate very constructable feedbacks for NeurIPS 2023 Reviewers. The authors would like to thanks Lichen Zhang and Ruizhe Zhang for useful and helpful suggestions about proof-reading the paper.

References

[AA22] Amol Aggarwal and Josh Alman. Optimal-degree polynomial approximations for exponentials and gaussian kernel density estimation. In 37th Computational Complexity Conference (CCC 2022) . Schloss Dagstuhl-Leibniz-Zentrum f¨ ur Informatik, 2022.

[ACM + 20] Josh Alman, Timothy Chu, Gary Miller, Shyam Narayanan, Mark Sellke, and Zhao Song. Metric transforms and low rank matrices via representation theory of the real hyperrectangle. arXiv preprint arXiv:2011.11503 , 2020.

[ACSS20] Josh Alman, Timothy Chu, Aaron Schild, and Zhao Song. Algorithms and hardness for linear algebra on geometric graphs. In 2020 IEEE 61st Annual Symposium on Foundations of Computer Science (FOCS) , pages 541-552. IEEE, 2020.

[AS23] Josh Alman and Zhao Song. How to capture higher-order correlations? generalizing matrix softmax attention to kronecker computation. arXiv preprint arXiv:2310.04064 , 2023.

[BCIS18] Arturs Backurs, Moses Charikar, Piotr Indyk, and Paris Siminelakis. Efficient density evaluation for smooth kernels. In 2018 IEEE 59th Annual Symposium on Foundations of Computer Science (FOCS) , pages 615-626. IEEE, 2018.

[BIS17] Arturs Backurs, Piotr Indyk, and Ludwig Schmidt. On the fine-grained complexity of empirical risk minimization: Kernel methods and neural networks. Advances in Neural Information Processing Systems , 30, 2017.

[BMR + 20] Tom Brown, Benjamin Mann, Nick Ryder, Melanie Subbiah, Jared D Kaplan, Prafulla Dhariwal, Arvind Neelakantan, Pranav Shyam, Girish Sastry, Amanda Askell, et al. Language models are few-shot learners. Advances in neural information processing systems (NeurIPS) , 33:1877-1901, 2020.

[CDL + 22] Beidi Chen, Tri Dao, Kaizhao Liang, Jiaming Yang, Zhao Song, Atri Rudra, and Christopher Re. Pixelated butterfly: Simple and efficient sparse training for neural network models. In International Conference on Learning Representations (ICLR) , 2022.

[CDW + 21] Beidi Chen, Tri Dao, Eric Winsor, Zhao Song, Atri Rudra, and Christopher R´ e. Scatterbrain: Unifying sparse and low-rank attention. Advances in Neural Information Processing Systems (NeurIPS) , 34:17413-17426, 2021.

[CGRS19] Rewon Child, Scott Gray, Alec Radford, and Ilya Sutskever. Generating long sequences with sparse transformers. arXiv preprint arXiv:1904.10509 , 2019.

[CKNS20] Moses Charikar, Michael Kapralov, Navid Nouri, and Paris Siminelakis. Kernel density estimation through density constrained near neighbor search. In 2020 IEEE 61st Annual Symposium on Foundations of Computer Science (FOCS) , pages 172-183. IEEE, 2020.

[CND + 22] Aakanksha Chowdhery, Sharan Narang, Jacob Devlin, Maarten Bosma, Gaurav Mishra, Adam Roberts, Paul Barham, Hyung Won Chung, Charles Sutton, Sebastian Gehrmann, et al. Palm: Scaling language modeling with pathways. arXiv preprint arXiv:2204.02311 , 2022.

[CS17] Moses Charikar and Paris Siminelakis. Hashing-based-estimators for kernel density in high dimensions. In 2017 IEEE 58th Annual Symposium on Foundations of Computer Science (FOCS) , pages 1032-1043. IEEE, 2017.

[CS19] Moses Charikar and Paris Siminelakis. Multi-resolution hashing for fast pairwise summations. In 2019 IEEE 60th Annual Symposium on Foundations of Computer Science (FOCS) , pages 769-792. IEEE, 2019.

[DCLT18] Jacob Devlin, Ming-Wei Chang, Kenton Lee, and Kristina Toutanova. Bert: Pretraining of deep bidirectional transformers for language understanding. arXiv preprint arXiv:1810.04805 , 2018.

[DKOD20] Giannis Daras, Nikita Kitaev, Augustus Odena, and Alexandros G Dimakis. Smyrfefficient attention using asymmetric clustering. Advances in Neural Information Processing Systems (NeurIPS) , 33:6476-6489, 2020.

[DLS23] Yichuan Deng, Zhihang Li, and Zhao Song. Attention scheme inspired softmax regression. arXiv preprint arXiv:2304.10411 , 2023.

[DSZ23] Yichuan Deng, Zhao Song, and Tianyi Zhou. Superiority of softmax: Unveiling the performance edge over linear attention. arXiv preprint arXiv:2310.11685 , 2023.

[GSWY23] Yeqi Gao, Zhao Song, Weixin Wang, and Junze Yin. A fast optimization view: Reformulating single layer attention in llm based on tensor and svm trick, and solving it in matrix multiplication time. arXiv preprint arXiv:2309.07418 , 2023.

[GSY23] Yeqi Gao, Zhao Song, and Xin Yang. Differentially private attention computation. arXiv preprint arXiv:2305.04701 , 2023.

[GSYZ23] Yeqi Gao, Zhao Song, Xin Yang, and Ruizhe Zhang. Fast quantum algorithm for attention computation. arXiv preprint arXiv:2307.08045 , 2023.

[HJK + 23] Insu Han, Rajesh Jarayam, Amin Karbasi, Vahab Mirrokni, David P Woodruff, and Amir Zandieh. Hyperattention: Long-context attention in near-linear time. arXiv preprint arXiv:2310.05869 , 2023.

[IP01] Russell Impagliazzo and Ramamohan Paturi. On the complexity of k-sat. Journal of Computer and System Sciences , 62(2):367-375, 2001.

[KKL20] Nikita Kitaev, Łukasz Kaiser, and Anselm Levskaya. Reformer: The efficient transformer. arXiv preprint arXiv:2001.04451 , 2020.

[KMZ23] Praneeth Kacham, Vahab Mirrokni, and Peilin Zhong. Polysketchformer: Fast transformers via sketches for polynomial kernels. arXiv preprint arXiv:2310.01655 , 2023.

[KVPF20] Angelos Katharopoulos, Apoorv Vyas, Nikolaos Pappas, and Franc ¸ois Fleuret. Transformers are rnns: Fast autoregressive transformers with linear attention. In International Conference on Machine Learning (ICLR) , pages 5156-5165. PMLR, 2020.

[KWH23] Feyza Duman Keles, Pruthuvi Mahesakya Wijewardena, and Chinmay Hegde. On the computational complexity of self-attention. In International Conference on Algorithmic Learning Theory , pages 597-619. PMLR, 2023.

[LWD + 23] Zichang Liu, Jue Wang, Tri Dao, Tianyi Zhou, Binhang Yuan, Zhao Song, Anshumali Shrivastava, Ce Zhang, Yuandong Tian, Christopher Re, et al. Deja vu: Contextual sparsity for efficient llms at inference time. In International Conference on Machine Learning , pages 22137-22176. PMLR, 2023.

[Ope22] OpenAI. Chatgpt: Optimizing language models for dialogue. In OpenAI Blog . https://openai.com/blog/chatgpt/, Nov 2022.

[RNS + 18] Alec Radford, Karthik Narasimhan, Tim Salimans, Ilya Sutskever, et al. Improving language understanding by generative pre-training. 2018.

[Rub18] Aviad Rubinstein. Hardness of approximate nearest neighbor search. In Proceedings of the 50th annual ACM SIGACT symposium on theory of computing (STOC) , pages 1260-1268, 2018.

[RWC + 19] Alec Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei, Ilya Sutskever, et al. Language models are unsupervised multitask learners. OpenAI blog , 1(8):9, 2019.

[SM19] Karthik C. S. and Pasin Manurangsi. On closest pair in euclidean metric: Monochromatic is as hard as bichromatic. 10th Innovations in Theoretical Computer Science (ITCS) , 2019.

[TBY + 19] Yao-Hung Hubert Tsai, Shaojie Bai, Makoto Yamada, Louis-Philippe Morency, and Ruslan Salakhutdinov. Transformer dissection: An unified understanding for transformer's attention via the lens of kernel. In Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing and the 9th International Joint Conference on Natural Language Processing (EMNLP-IJCNLP) , pages 4344-4353, 2019.

[VSP + 17] Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Łukasz Kaiser, and Illia Polosukhin. Attention is all you need. Advances in neural information processing systems (NeurIPS) , 30, 2017.

[Wil18] Virginia Vassilevska Williams. On some fine-grained questions in algorithms and complexity. In Proceedings of the international congress of mathematicians: Rio de janeiro 2018 , pages 3447-3487. World Scientific, 2018.

[WLK + 20] Sinong Wang, Belinda Z Li, Madian Khabsa, Han Fang, and Hao Ma. Linformer: Self-attention with linear complexity. arXiv preprint arXiv:2006.04768 , 2020.

[ZBIW19] Ofir Zafrir, Guy Boudoukh, Peter Izsak, and Moshe Wasserblat. Q8bert: Quantized 8bit bert. In 2019 Fifth Workshop on Energy Efficient Machine Learning and Cognitive Computing-NeurIPS Edition (EMC2-NIPS) , pages 36-39. IEEE, 2019.

[ZHDK23] Amir Zandieh, Insu Han, Majid Daliri, and Amin Karbasi. Kdeformer: Accelerating transformers via kernel density estimation. In ICML . arXiv preprint arXiv:2302.02451, 2023.

[ZRG + 22] Susan Zhang, Stephen Roller, Naman Goyal, Mikel Artetxe, Moya Chen, Shuohui Chen, Christopher Dewan, Mona Diab, Xian Li, Xi Victoria Lin, et al. Opt: Open pre-trained transformer language models. arXiv preprint arXiv:2205.01068 , 2022.

[ZSZ + 23] Zhenyu Zhang, Ying Sheng, Tianyi Zhou, Tianlong Chen, Lianmin Zheng, Ruisi Cai, Zhao Song, Yuandong Tian, Christopher R´ e, Clark Barrett, Zhangyang Wang, and Beidi Chen. H 2 o: Heavy-hitter oracle for efficient generative inference of large language models. In NeurIPS . arXiv preprint arXiv:2306.14048, 2023.

Appendix

Roamdap.

We provide the missing proofs of our upper bound (algorithm) result in Section A. We provide the missing proofs of our lower bound (hardness) result in Section B.

A Missing Proofs for Upper Bound

A.1 Proof of Lemma 3.2

Lemma A.1 (Restatement of Lemma 3.2) . Let M = XY ⊤ ∈ R n × n denote a matrix with X,Y ∈ R n × d . Let P ( x ) denote a degreeg polynomial, and define r = ( 2( g + d ) 2 g ) .

There is an algorithm that runs in O ( nrg ) time and, given as input the matrix X,Y , constructs matrices U 1 , U 2 ∈ R n × r such that P ( M ) = U 1 U ⊤ 2 . (Here, P ( M ) denotes the entry-wise application of P to M .)

Proof. Let P ( x ) denote the degreeg polynomial. Expand it in terms of its coefficients as

Consider the function K : R d × R d → R defined by, for u, v ∈ R d ,

K is a degree2 g polynomial in the 2 d entries u 1 , · · · u d , v 1 , · · · , v d of the vectors u, v . Define the set V of its variables,

Let F denote the set of functions

We can count that

Hence, there are coefficients c t ∈ R for each t ∈ F such that

We define ϕ u : R d → R |F| by, for any t ∈ F ,

Similarly, we define ϕ v : R d → R |F| by, for any t ∈ F ,

Define and

Thus, we have

Proof. We calculate that

Then, we have

For i ∈ [ n ] , let X i ∈ R d denote the i -th row of X , and let Y i ∈ R d denote the i -th row of Y . Our algorithm can thus construct

the matrix U 1 ∈ R n ×|F| whose i -th row is the vector ϕ u ( x i ) for i ∈ [ n ] , and

the matrix U 2 ∈ R n ×|F| whose i -th row is the vectors ϕ v ( y i ) for i ∈ [ n ] .

Each entry of these matrices can be constructed by multiplying together at most g variables, so these n × r matrices can be constructed in time O ( nrg ) as desired.

A.2 Proof of Lemma 3.5

LemmaA.2 (Restatement of Lemma 3.5) . Let A ∈ R n × n be any matrix whose entires are all positive and ϵ A ∈ (0 , 0 . 1) be any parameter. Let ˜ A ∈ R n × n be any matrix such that, for all ( i, j ) ∈ [ n ] × [ n ] , we have

Define the matrices D, ˜ D ∈ R n × n by D = diag( A 1 n ) and ˜ D = diag( ˜ A 1 n ) . Then, for all i ∈ [ n ] we have

where the second step follows from triangle inequality.

This completes the proof.

A.3 Proof of Lemma 3.6

Lemma A.3 (Restatement of Lemma 3.6) . Let ϵ A , ϵ D ∈ (0 , 0 . 1) and B > 1 be parameters, and let V ∈ R n × d denote a matrix with ∥ V ∥ ∞ ≤ B . Let A ∈ R n × n be any matrix whose entires are all positive, and let ˜ A ∈ R n × n be a matrix such that, for all ( i, j ) ∈ [ n ] × [ n ] we have

Let D, ˜ D ∈ R n × n be any diagonal matrices with positive entries on their diagonals, with the property that, for all i ∈ [ n ] , we have

Proof. We have

We now bound each of these two terms separately.

First, for each ( i, j ) ∈ [ n ] × [ d ] ,

where the second step follows from the triangle inequality, the forth step follows from | ( D i,i -˜ D i,i ) /D i,i | ≤ ϵ D , the fifth step follows from ˜ D -1 i > 0 and ˜ A i,l > 0 , and the last step follows from our assumption on V .

Second, for each ( i, j ) ∈ [ n ] × [ d ] ,

where the second step follows from triangle inequality, the third step follows from D -1 i,i > 0 , the forth step follows from | ˜ A i,l -A i,l | ≤ ϵ A · A i,l and the last step follows from definition of D i,i . The result follows by combining Eq. (1), and two inequalities (Eq. (2) and Eq. (3)).

B Missing Proofs for Lower Bound

Lemma B.1 (Restatement of Lemma 4.7) . For any constant C γ ∈ (0 , 0 . 1) : For every ϵ > 0 and C > 0 , there exist constants C a > 0 and C b > 0 and such that, if AAttC (Definition 1.2) for parameters (2 n, d = 2 C log n, B = C b √ log n, ϵ a = n -C a ) can be solved in time T , then Gap -ANN ( n, d = C log n, t, ϵ ) (Definition 4.5) can be solved in time O ( T + n 2 -C γ ) .

Proof. We give an algorithm with the stated running time for Gap -ANN ( n, d = C log n, t, ϵ ) . Let c > 0 be a parameter we will choose later (it will be a function of C and C γ ). Our algorithm will proceed to one of two cases depending on the value of t . If t < c log n , then we will use one algorithm

which runs in time O ( n 2 -C γ ) . Otherwise, if t ≥ c log n , we will use another algorithm which runs in time O ( T ) .

Case 1 : t < c log n .

Let a 1 , · · · , a n , b 1 , · · · , b n ∈ { 0 , 1 } d be the input vectors to Gap -ANN , and let t ∈ [0 , d ] denote the target distance. Recall that d = C log n .

In this t < c log n case, we will simply brute-force for the answer in the following way: We first store the vectors b 1 , · · · , b n in a lookup table, then for each i ∈ [ n ] , we iterate over all vectors b ′ ∈ { 0 , 1 } d which have Hamming distance at most t from a i and check whether b ′ is in the lookup table. This determines whether there is a b ∈ B at distance at most t from a i , as desired.

For each i ∈ [ n ] , we need to iterate over ( d t ) choices for the vector b ′ , so the total running time will be O ( n · ( d t ) ) . By standard bounds on binomial coefficients, we know that

for some function f : R > 0 × R > 0 → R > 0 with the property that, for any fixed C > 0 , we have

We can thus pick a sufficiently small constant c > 0 , depending only on C γ and C such that f ( C, c ) < 1 -C γ and this entire brute-force takes O ( n 2 -C γ ) time.

Case 2: t ≥ c log n .

Let a 1 , · · · , a n , b 1 , · · · , b n ∈ { 0 , 1 } d denote the input of Gap -ANN ( n, d, t, ϵ ) (Definition 4.5), and recall from Remark 4.4 that we may assume each has half its entries 0 and half its entries 1 . We will explain how to construct an Attention matrix using this instance.

Let C 0 ≥ c be such that

Furthermore, set

Let β > 0 and ˜ d ≥ d denote parameters we will choose later (see Eq. (9) and Eq. (6), respectively). Define τ > 0 by

Intuitively, our goal in picking these parameters is that τ will be an upper bound on entries of the attention matrix, i.e., we will have:

We will make use of an algorithm for the AAttC ( ˜ n, ˜ d, B, ϵ a ) problem, for the following parameters:

We define Q ∈ R ˜ n × ˜ d and K ∈ R ˜ n × ˜ d as

Since each entry of Q and K is either √ β or 0 , it follows that

Using the above construction of Q ∈ R ˜ n × ˜ d and K ∈ R ˜ n × ˜ d , we note that

is given by

(Note that we do not explicitly compute all the entries of A in our algorithm; we will make use of it only through calling our algorithm for the Attention problem.)

For each ( i, j ) ∈ [ n ] × [ n ] , we know that

where the first step follows from definition of A ∈ R ˜ n × ˜ n , the second step follows from ⟨ a i , b j ⟩ ≤ ∥ a i ∥ ∞ · ∥ b j ∥ ∞ d , the third step follows from d < ˜ d (see Eq. (6)), and the last step follows from definition of τ (see Eq. (5)).

On the other hand, we know that that for each ( i, j ) ∈ [ n ] × [ n ] ,

since it is an exponential of an entry of QK ⊤ / ˜ d .

Using Eq. (10) and Eq. (11), combined with our expression for A , it thus follows that

Since D i,i = ( A 1 ˜ n ) i , thus we know that

Choose the vector v ∈ R ˜ n defined as

We define ˜ t as

We can show that ˜ t ≥ ϵ a as follows:

where the second step follows from simple algebra, the third step follows from t = C 0 log n (Eq. (4)) and d = C log n (assumption in Lemma statement), the second step follows from choice of β (Eq. (7)), and the sixth step follows from choice of C a (Eq. (8)), and the last step follows from Eq. (8).

Since ˜ t ≥ ϵ a , if we run an algorithm for Approximation Attention Computation (Definition 1.2) AAttC ( ˜ n, ˜ d, B, ϵ a ) , where we pick V to be a matrix with one row v and the rest 0 , we can output a vector u ∈ R ˜ n such that, for all i ∈ [ ˜ n ] ,

Note that using Remark 4.4, we have

Therefore, for any ( i, j ) ∈ [ n ] × [ n ] ,

where the second step follows from ∥ a i ∥ 2 2 = ∥ b j ∥ 2 2 = d/ 2 , and the last step follows from simple algebra.

Recall that our goal is to determine, for each i ∈ [ n ] , whether there is a j ∈ [ n ] such that ∥ a i -b j ∥ 2 2 ≤ t , or whether ∥ a i -b j ∥ 2 2 ≥ (1 + ϵ a ) t for all j ∈ [ n ] . We will show next that we can distinguish these two cases by seeing whether u i is greater than or less than the value ˜ t 0 := 2 ˜ t .

Case 2a.

If there exists an ( i, j ) ∈ [ n ] × [ n ] such that ∥ a i -b j ∥ 2 2 ≤ t , then

where the first step follows from 2 d = ˜ d (see Eq. (6)).

This means that

Then, for all i ∈ [ n ] ,

where the third step follows from definition of ˜ t (see Eq. (12)), the forth step follows from the calculation in Eq. (13) below, and the last step follows from ˜ t = ˜ t 0 / 2 .

Finally, b our choice of β and t , we can see that

where the first step follows t = C 0 log n (Eq. (4)), the second step follows from d = C log n , and the third step follows from β = B 2 (Eq. (9)) and the choice of B (Eq. (7)).

Recent Followup Work During the preparation of camera ready, we are aware of a number of new related work [KMZ23, HJK + 23, AS23, DLS23, GSWY23, DSZ23, GSY23] to this paper. This work studies softmax attention, and [KMZ23] shows to speed up the polynomial based attention unit via sketching techniques. [HJK + 23] introduces hyperattention, which is able to handle longcontext attention in near-linear time. This work is mainly focusing classical attention computation, [AS23] consider a more general tensor version computation of attention scheme. This work studies the inference computation, the recent work [DLS23, GSWY23] also considers how to speedup the training process which involves the computation of gradient and hessian. Inspired by hardness result of this work, [DSZ23] shows that there is a binary classification tasks (involves two datasets) that softmax attention can distinguish but linear attention cannot. [GSY23] provides a result for computing the attention matrix differentially privately. [GSYZ23] introduce a quantum algorithm for computing attention matrix.

where the second step follows from the definition of ˜ t (see Eq. (12)), and the last step follows from the definition of ˜ t 0 .

Case 2b.

If for all ( i, j ) ∈ [ n ] × [ n ] , we have ∥ a i -b j ∥ 2 2 > t (1 + ϵ ) , this implies

Peer reviews:
comments: SUMMARY:
This paper shows that whether or not there exists a fast algorithm for approximately computing the attention matrix ($A \in \mathbb{R}^{n \times d}$, where $d = O(\log n)$ depends on the bound of the entry value $B$

1. Particularly, the authors show that when $B = o(\sqrt{\log n)}$, their proposed algorithm can compute the attention matrix within $1/\text{poly}(n)$ error in time $n^{1+o(1)}$.
2. Assuming the Strong Exponential Time Hypothesis (SETH) and the fine-grained complexity conjecture is valid, they show that there is no $O(N^{2-q})$ time algorithm that guarantees $1/\text{poly}(n)$ approximation error.

STRENGTHS:
+ The results are interesting and correct
+ An improved hardness result is a good contribution
+ The observation that the existence of a sublinear algorithm depends on the entry value is interesting.

WEAKNESSES:
The proposed algorithm is a bit incremental, both in terms of the proposed method, and the techniques to establish these results.

Minor typo:
p5: In the proof of Corollary 2.2, it seems like $\max$ operation is missing in the formula $g = \Theta(...)$.

QUESTIONS:
+ Although the paper provides an explicit way of the matrix construction $U_1$ and $U_2$, the reviewer is wondering how complex is the matrix construction in practice.

LIMITATIONS:
+ It would be nice if there were simulation results that demonstrate the practicality for some benchmark datasets on some target tasks.
TITLE: Official Review
SOUNDNESS_CORRECTNESS: 2
DATE: 2023-07-02

{"IS_META_REVIEW": false, "comments": "SUMMARY:\nThe paper is a purely theoretical paper discussing improving the computational time required to solve the attention computation problem which is known to be quadratic in time. The idea hinges upon the use of the boundness of the entries of the matrices involved in such a problem. \nThe authors discuss mainly two cases, where the entries are bounded by a polylogarithmic term in the number of tokens which then the attention computation problem can be solved in time almost linear in the number of tokens. Otherwise, the authors prove via the \"Strong Exponential Time Hypothesis\" from fine-grained complexity theory that the running time is no better than quadratic in the number of tokens.\n\nSTRENGTHS:\n1) The paper is well-written and easy to follow.\n2) The paper proves the existence of almost linear time algorithms for the attention computation problem, as it depends on the boundness of the entries of the matrices. \n3) The paper also shows that in case the entires of the involved matrices in the attention computation problem are \"not bounded\", then the quadratic time associated with the attention computation problem can not be improved.\n\nWEAKNESSES:\nThe only weakness is the lack of practical evaluation. Although the paper sets a theoretical justification for, e.g., the use of quantization or low-degree polynomial approximation of the attention computation problem, it would be great to observe the quality of the proposed algorithm on the quality of transformer-like models which practices the attention computation problem.\n\nQUESTIONS:\n1) On page 14 of the appendix, did you mean to put n instead of D at the top of the summation symbol (see equation 2)?\n2) Also on the same page, did you mean to write $\\tilde{D}_{i, i}$ instead of $\\tilde{D}_i$?\n3) Shouldnt be the $B$ in the denominator of the equation stated at Lemma 3.4 be $B^2$?\n\nLIMITATIONS:\nThe authors adequately addressed the limitations of their work.", "TITLE": "Official Review", "RECOMMENDATION": 7, "REVIEWER_CONFIDENCE": 4, "SOUNDNESS_CORRECTNESS": 3, "DATE": "2023-07-03"}

{"IS_META_REVIEW": false, "comments": "SUMMARY:\nIn this work, the author studied the problem of how to efficiently compute or approximate the attention matrix. The major contribution can be summarized as: \n\n1. they found for the case when $d = O(\\log n)$ and entries of the input matrices are uniformly bounded by $o(\\sqrt{\\log n})$, then an algorithm that approximates the attention matrix can be constructed with sub-quadratic complexity, the key idea of the approximation is based on a polynomial approximation of the $e^x$ that was introduced by another work [AA22].\n\n2. when $d = O(\\log n)$, and the entries of the input matrices are of the order $\\Theta(\\sqrt{\\log n})$, then under the Strong Exponential Time Hypothesis, it is impossible to approximate the attention matrix with small error and sub-quadratic complexity.\n\nSTRENGTHS:\n1. I think in this work, the author did a good job explain the basic intuitions and ideas behind their approach. Especially, breaking down the proof steps into smaller parts improved the readability of this work. \n\n2. On the other hand, by elaborating the related works and contributions, the reviewer can have a better understanding of the contribution of this work.\n\nWEAKNESSES:\nThere are several weakness of this work:\n\n1. The major contribution of this work is not emphasized enough or minor. For example, the technical tools used in this work are developed by previous works to tackle similar problems. As a theory paper, I think the author at least needs to address what technical contribution is made in this work. To me, it seems that the major results are straightforward applications of some closely related recent works.\n\n2. The lack of simulation results is another weakness. As the author is trying to address some practical/computational aspects of the problem, experimental results are essential to validate the theory and claims.\n\n3. Some of the claims in the theorem are ambiguous. For instance, in theorem 4.6, there are many constants $C_a, C_{\\beta}, C, q$ used. What are those constants? Numerical constants? What are the relationships between them? Statements like this is not rigorous at all for theory papers.\n\nQUESTIONS:\nPlease see some questions I mentioned in the weakness section.\n\nAside from that, can the author explain the claim \"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\". Why the results in this work explain this in practice?\n\nLIMITATIONS:\nPlease see the weakness section.", "TITLE": "Official Review", "RECOMMENDATION": 3, "REVIEWER_CONFIDENCE": 4, "SOUNDNESS_CORRECTNESS": 2, "DATE": "2023-07-07"}

{"IS_META_REVIEW": false, "comments": "SUMMARY:\nThe attention mechanism, whose runtime complexity is approximately quadratic in the sequence length $n$, is the main bottleneck to LLM inference. It is thus quite reasonable to study whether this can be *approximated* with sufficient accuracy in subquadratic time. In particular, for this paper the authors define and study the problem of approximate attention computation (AAttC), wherein one seeks a result that is accurate within a pre-specified entrywise error tolerance $\\epsilon_a$.\n\nUnder what I think are reasonable assumptions, the authors find that whether AAttC can run in subquadratic time hinges on the bound $B$ on the magnitudes of the entries of $Q$/$K$/$V$. This is slightly surprising and, in my opinion, a neat story for the paper. This conclusion consists in two results:\n\n1. Theorem 3.8: an algorithm that computes AAttC in near-linear time provided that $B = o(\\sqrt{\\log n})$. The high-level idea is\n   - Approximate the $\\exp$ in $\\exp(QK^T/d)$ with a polynomial $P$ (Lemma 2.1 from [AA22]).\n   - Compute a low-rank decomposition $U_1 U_2^T = P(QK^T/d)$ directly in linear time based on knowledge of $P$ and the entries of $Q$ and $K$ (Lemma 3.2).\n   \n    These lemmas are the main workhorses of Theorem 3.8, but effort is also made stitching everything together. The crucial thread is that $B^2$ determines the degree of $P$, which in turn determines the rank of the decomposition $U_1 U_2^T$. $B$ thus needs to grow slowly in $n$ to make the algorithm subquadratic.\n2. Theorem 4.6, which shows that if $B = \\Theta(\\log n)$, there is no subquadratic-time algorithm for AAttC (assuming the strong exponential time hypothesis, SETH). This result is based on a reduction of approximate nearest neighbor search to AAttC.\n\nSTRENGTHS:\n- Although past work has produced some closely related results (ZHDK23, KWH23), the authors' focus on $B$, the entrywise bound, is original and insightful. Furthermore, the proof techniques in this paper bring up interesting connections to the KDE literature which may be interesting for the community.\n- They provide a near-linear time attention algorithm based on $B = o(\\sqrt{\\log n})$, and show that the rate at which $B$ scales cannot be improved upon. In some sense their work is a complete description of the problem, conditional on their (reasonable) assumptions on how other quantities scale with $n$ ($d = O(\\log n), \\epsilon_a = 1/\\text{poly}(n)$). This is quite satisfying.\n- For a theoretical paper, this is well-written and easy to understand. The authors do a great job of motivating, roadmapping, and explaining their work.\n- This work offers a key insight about an important problem.\n\nWEAKNESSES:\nThis is a pair of complexity results. It is not clear to me how the insights provided here can be operationalized to improve AAttC in a practical setting. (The authors do highlight practical examples in ZBIW19 and KVPF20, but the connections between those works and the theory developed here do not seem particularly deep).\n\nTypos\n- L117: Hedge -> Hegde\n- L133: algoriths -> algorithms\n- L134: I believe should read \"To design our algorithm for *Theorem 1.4*\" not Theorem 1.3.\n- L242: entires -> entries\n- L232, L257, Algorithm 1 L4: I believe $B$ in $\\frac{\\log(1/\\epsilon)}{\\log(\\log(1/\\epsilon)/B)}$ was meant to be $B^2$\n- Appendix L434: roamdap -> roadmap\n- Appendix L453: $v_i$ -> $u_i$ under the product sign\n\nQUESTIONS:\nI am curious to hear how you think your results might lead to practical speedups in attention computation for future work.\n\nLIMITATIONS:\nThe authors do highlight their main limitation: Theorem 4.6's reliance on the strong exponential time hypothesis (SETH). As far as I can tell, no important limitations were omitted.", "TITLE": "Official Review", "RECOMMENDATION": 6, "REVIEWER_CONFIDENCE": 2, "SOUNDNESS_CORRECTNESS": 4, "DATE": "2023-07-26"}