Linear Attention › The Associative Property Trick
Language AI HandbookAll chapters 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 matrix in the first place. The culprit is the softmax normalization, which creates dependencies between all positions.
Standard attention computes:
where:
- : the query matrix containing one row per token
- : the key matrix containing one row per token
- : the value matrix containing the information to aggregate
- : the dimension of query and key vectors
- : the sequence length (number of tokens)
- The softmax is applied row-wise, normalizing each query's attention distribution
For each query position , the attention output is:
where:
- : the query vector at position
- : the key vector at position
- : the value vector at position
- : the dot product measuring similarity between query and key
- : the exponential function ensuring positive weights
- The denominator sums over all positions, creating the normalization constant
The denominator is the normalization term: to compute the attention weight for any single key position , 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 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:
We use:
where:
- : a feature map that transforms vectors from the original -dimensional space to a -dimensional feature space
- : the transformed query vector
- : the transformed key vector
- : the dimension of the feature space (often equal to 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:
Since does not depend on , we can factor it out of the sum:
This factorization is the key to linear attention. The numerator contains multiplied by a sum that doesn't depend on . The denominator has the same structure. Both sums can be computed once and reused for every query position.
The terms and can be computed once and reused for all query positions. This eliminates the need to compute pairwise query-key interactions.
Let's define:
- : a matrix of shape
- : a vector of shape
Then the output for each position simplifies to:
where:
- : a matrix-vector product of shape , giving the unnormalized output
- : a dot product of shape , giving the normalization scalar
Computing requires operations (summing outer products of dimension ). Computing each output requires operations. With outputs, the total complexity is assuming is proportional to .