Linear Attention › The Associative Property Trick

Michael BrenndoerferUpdated June 28, 20254 min read
Language AI HandbookAll chapters
Part 17 of 67Efficient Attention
Chapter 123Linear Attention › The Associative Property Trick
Reading section 2 of 15 · The Associative Property Trick · 4 minSection 2 of 15 · 4 min

Book contents

Section 2 of 15 · 4 min read

Linear Attention › The Associative Property TrickOne section · Sections in this chapter2 / 15 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

To understand linear attention, we need to examine why standard softmax attention requires the n×nn \times n matrix in the first place. The culprit is the softmax normalization, which creates dependencies between all positions.

Standard attention computes:

Attention(Q,K,V)=softmax(QKTdk)V\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V

where:

  • Q∈Rn×dkQ \in \mathbb{R}^{n \times d_k}: the query matrix containing one row per token
  • K∈Rn×dkK \in \mathbb{R}^{n \times d_k}: the key matrix containing one row per token
  • V∈Rn×dvV \in \mathbb{R}^{n \times d_v}: the value matrix containing the information to aggregate
  • dkd_k: the dimension of query and key vectors
  • nn: the sequence length (number of tokens)
  • The softmax is applied row-wise, normalizing each query's attention distribution

For each query position ii, the attention output is:

outputi=∑j=1nexp⁡(qiTkj/dk)⋅vj∑j=1nexp⁡(qiTkj/dk)\text{output}_i = \frac{\sum_{j=1}^{n} \exp(q_i^T k_j / \sqrt{d_k}) \cdot v_j}{\sum_{j=1}^{n} \exp(q_i^T k_j / \sqrt{d_k})}

where:

  • qi∈Rdkq_i \in \mathbb{R}^{d_k}: the query vector at position ii
  • kj∈Rdkk_j \in \mathbb{R}^{d_k}: the key vector at position jj
  • vj∈Rdvv_j \in \mathbb{R}^{d_v}: the value vector at position jj
  • qiTkjq_i^T k_j: the dot product measuring similarity between query ii and key jj
  • exp⁡(⋅)\exp(\cdot): the exponential function ensuring positive weights
  • The denominator sums over all nn positions, creating the normalization constant

The denominator is the normalization term: to compute the attention weight for any single key position jj, we need to know the scores for all positions, because the softmax divides by their sum. This creates a global dependency that forces us to compute all n2n^2 query-key products before we can produce any output.

Breaking the Global Dependency

Linear attention's solution is to replace the exponential-and-normalize pattern with a kernel function that decomposes into separate feature maps for queries and keys. Instead of:

similarity(qi,kj)=exp⁡(qiTkj/dk)\text{similarity}(q_i, k_j) = \exp(q_i^T k_j / \sqrt{d_k})

We use:

similarity(qi,kj)=ϕ(qi)Tϕ(kj)\text{similarity}(q_i, k_j) = \phi(q_i)^T \phi(k_j)

where:

  • ϕ:Rdk→RD\phi: \mathbb{R}^{d_k} \to \mathbb{R}^{D}: a feature map that transforms vectors from the original dkd_k-dimensional space to a DD-dimensional feature space
  • ϕ(qi)∈RD\phi(q_i) \in \mathbb{R}^D: the transformed query vector
  • ϕ(kj)∈RD\phi(k_j) \in \mathbb{R}^D: the transformed key vector
  • DD: the dimension of the feature space (often equal to dkd_k for simple maps, or larger for random feature methods)

The key property is that the similarity decomposes into an inner product of transformed vectors, allowing us to reorder operations using matrix associativity.

With this formulation, the attention output becomes:

outputi=∑j=1nϕ(qi)Tϕ(kj)⋅vj∑j=1nϕ(qi)Tϕ(kj)\text{output}_i = \frac{\sum_{j=1}^{n} \phi(q_i)^T \phi(k_j) \cdot v_j}{\sum_{j=1}^{n} \phi(q_i)^T \phi(k_j)}

Since ϕ(qi)\phi(q_i) does not depend on jj, we can factor it out of the sum:

outputi=ϕ(qi)T∑j=1nϕ(kj)vjTϕ(qi)T∑j=1nϕ(kj)\text{output}_i = \frac{\phi(q_i)^T \sum_{j=1}^{n} \phi(k_j) v_j^T}{\phi(q_i)^T \sum_{j=1}^{n} \phi(k_j)}

This factorization is the key to linear attention. The numerator contains ϕ(qi)T\phi(q_i)^T multiplied by a sum that doesn't depend on ii. The denominator has the same structure. Both sums can be computed once and reused for every query position.

Key Insight: Precomputation

The terms ∑j=1nϕ(kj)vjT\sum_{j=1}^{n} \phi(k_j) v_j^T and ∑j=1nϕ(kj)\sum_{j=1}^{n} \phi(k_j) can be computed once and reused for all query positions. This eliminates the need to compute pairwise query-key interactions.

Let's define:

  • S=∑j=1nϕ(kj)vjTS = \sum_{j=1}^{n} \phi(k_j) v_j^T: a matrix of shape (D×dv)(D \times d_v)
  • Z=∑j=1nϕ(kj)Z = \sum_{j=1}^{n} \phi(k_j): a vector of shape (D,)(D,)

Then the output for each position simplifies to:

outputi=ϕ(qi)TSϕ(qi)TZ\text{output}_i = \frac{\phi(q_i)^T S}{\phi(q_i)^T Z}

where:

  • ϕ(qi)TS\phi(q_i)^T S: a matrix-vector product of shape (1×D)⋅(D×dv)=(1×dv)(1 \times D) \cdot (D \times d_v) = (1 \times d_v), giving the unnormalized output
  • ϕ(qi)TZ\phi(q_i)^T Z: a dot product of shape (1×D)⋅(D×1)=1(1 \times D) \cdot (D \times 1) = 1, giving the normalization scalar

Computing SS requires O(nd⋅D)O(nd \cdot D) operations (summing nn outer products of dimension D×dvD \times d_v). Computing each output requires O(D⋅dv)O(D \cdot d_v) operations. With nn outputs, the total complexity is O(nd2)O(nd^2) assuming DD is proportional to dd.