Linear attention aggregates feature-mapped keys and values before reading them with a query. See why this saves sequence-length work and when it changes softmax attention.
Linear attention
Linear attention is a family of attention mechanisms that factor query–key similarities through finite feature vectors, allowing keys and values to be aggregated before each query reads them. For a quant model processing long news histories or market-event sequences, this can reduce the attention operation's growth with sequence length. The word linear describes that computational scaling; it does not make the whole neural network a linear function.
Aggregate first, query second
Let queries and keys be column vectors, values vj∈Rdv, and the feature map ϕ have width r. Choose similarities sij=ϕ(qi)⊤ϕ(kj) that are nonnegative, with a strictly positive sum for each query. Componentwise nonnegative features are a sufficient way to obtain nonnegative similarities, but an all-zero query or incompatible feature supports can still give a zero denominator.
Define the shared summaries
S=j=1∑Nϕ(kj)vj⊤,z=j=1∑Nϕ(kj).
Then the output is
yi⊤=∑j=1Nsij∑j=1Nsijvj⊤=ϕ(qi)⊤zϕ(qi)⊤S.
This is the associative computation developed by Katharopoulos et al.. The summaries have shapes r×dv and r, independent of the number of keys. For N queries and keys, aggregation and reading cost O(Nrdv), apart from feature construction and projections. Calling this linear in N holds r and dv fixed. It avoids explicitly constructing an N×N attention matrix; it does not remove all sequence activations or training memory.
An equality within one kernel
Take dimensionless toy vectors q=(2,1)⊤, k1=(1,0)⊤, k2=(0,1)⊤, and scalar values v1=2, v2=4. Use the identity feature map on these nonnegative vectors. Similarities are 2 and 1, so normalised weights are 2/3 and 1/3:
y=2+12⋅2+1⋅4=38≈2.6667.
Alternatively, aggregate S=(2,4)⊤ and z=(1,1)⊤ first. The query produces numerator q⊤S=8 and denominator q⊤z=3, giving the same result. Signed raw embeddings would not generally permit this identity-map construction with nonnegative weights.
Dimensionless toy attention: query (2, 1), keys (1, 0) and (0, 1), values 2 and 4. Direct linear-kernel weights (2/3, 1/3) and aggregate state S=(2, 4), z=(1, 1) both yield 8/3. Softmax of logits (2, 1) instead yields 2.5379.
Original toy calculation. Reordering the products preserves the chosen feature-kernel attention exactly; changing the kernel changes the output.
Here the logit scale is explicitly one; no dimension-based scaling is applied. The different answer comes from exponentiating the dot products. Associativity alone cannot move the rowwise softmax through the matrix products.
One approach chooses a different kernel, such as the positive feature construction in the original linear Transformer. Another approximates the softmax kernel using kernel feature approximation. Performer uses positive orthogonal random features for that second approach. Finite random features introduce approximation error; an unbiased estimate of a kernel entry does not automatically make the normalised ratio unbiased. These distinctions matter when replacing a trained softmax attention layer. Choromanski et al.
A running state must respect the forecast clock
For causal attention including the current position, maintain prefix summaries:
Start with zero summaries. If the task permits only strictly earlier positions, read the previous state before adding the current key and value. A full-sequence summary would let future events affect earlier forecasts. The prefix mask also cannot repair embeddings built from information unavailable at the forecast time.
The attention state needs O(rdv+r) storage per head during sequential inference. Check finite values and positive denominators: small denominators and accumulated floating-point error can make the ratio fragile. A denominator floor changes the formula and should be treated as part of the implemented model.
For financial evaluation, compare forecast quality and actual latency on the same dated information set. A smaller attention state can make a longer history feasible, but it does not establish that older events improve a forecast. The fixed-width state also does not retain a separately addressable value vector for every past event.
The SelfMadeQuant QKV lesson develops the underlying weighted-context mechanism and query/key/value roles. Its public page is a preview; the full lesson requires membership.