Linear Attention Delta Attention: Derivation and Analysis
This article explains Delta Attention, an incremental linear attention mechanism designed for long-context models. It covers the limitations of standard and naive linear attention, the matrix form of memory states, the "erase-then-write" update rule, and two equivalent perspectives: online gradient descent and orthogonal projection. It also summarizes practical engineering considerations for training, inference, and deployment.
Introduction: Why Attention Needs a Rethink
The compute and memory cost of standard attention grows quadratically with sequence length, which makes it impractical for very long contexts. To address this, the field has developed linear attention methods: instead of applying softmax first and then a weighted sum, they maintain a state matrix that accumulates historical information, reducing complexity to linear scale.
Naive linear attention, however, has an obvious flaw: it can only add to the state, never remove from it. Historical information piles up indefinitely, the state matrix is quickly flooded with irrelevant content, and long-range retrieval degrades sharply. Delta Attention was proposed against this backdrop. Its core idea is to erase old memories related to the incoming information before writing new information, and then perform an incremental update.
1. Matrix Representation of the Memory State
Given an input sequence $x_1, x_2, \dots, x_T$, each step produces three vectors: query $q_t$, key $k_t$, and value $v_t$. Linear attention maintains a state matrix $S_t \in \mathbb{R}^{d_k \times d_v}$, and the output is:
S_t = S_{t-1} + k_t v_t^To_t = q_t^T S_t
Here $k_t v_t^T$ is a rank-1 outer product, meaning “write the value $v_t$ along the direction indicated by key $k_t$.” $S_0$ is usually initialized as a zero matrix. The advantage of this form is that the state size is fixed during inference and independent of sequence length; during training it can also be written as parallel matrix multiplications, preserving efficiency.
The problem is that $\text{rank}(S_t)$ is at most $\min(d_k, d_v)$, so capacity is inherently limited. When the number of writes far exceeds this capacity, new writes inevitably overwrite old information, and naive additive updates perform poorly because they overwrite indiscriminately.
2. Incremental Update Rule: From Adding to Erase-then-Write
The Delta Attention update rule introduces a decay term:
S_t = S_{t-1} (I - β_t k_t k_t^T) + β_t k_t v_t^T
where $\beta_t \in (0, 1)$ is the write strength. Breaking it down:
- $I - \betat k_t k_t^T$ is a transformation on the key space that attenuates the component of $S{t-1}$ along the $k_t$ direction by a factor of $\beta_t$;
- The new key-value pair is then written with strength $\beta_t$.
When $\beta_t = 1$, the old memory along that direction is completely cleared, equivalent to an overwrite. When $\beta_t \to 0$, it degenerates to the original additive update. So $\beta_t$ is a continuous interpolation knob that lets the model learn whether to remember firmly or forget as needed.
In practice, to avoid explicitly constructing the $d_k \times d_k$ matrix, an equivalent vectorized form is commonly used:
# Pseudocode: single-step delta updatepred = k @ S # read old memory with the current keydelta = v - pred # difference between new value and old predictionS = S + beta * outer(k, delta)
This code is the key to the whole article: what gets written is not $v$ itself, but the residual between the new value and the old prediction.
3. Appendix Perspective 1: Memory Update as Online Gradient Descent
Treat the state matrix as a learnable parameter $S$ and define a single-step squared loss:
L_t(S) = 0.5 * || S^T k_t - v_t ||^2
The gradient with respect to $S$ is $\nabla_S L_t = k_t (S^T k_t - v_t)^T$. Taking one gradient descent step with learning rate $\beta_t$:
S_t = S_{t-1} - β_t * k_t (S_{t-1}^T k_t - v_t)^T= S_{t-1} (I - β_t k_t k_t^T) + β_t k_t v_t^T
This matches the previous section exactly. It shows that Delta Attention is not a heuristic trick but a natural result of “online regression on the state matrix”: at every step the model fine-tunes its memory with the current sample so that the memory’s prediction for the current key is closer to the current value. This view also explains the meaning of $\beta_t$: it is the learning rate and needs to be adaptive—larger when information is reliable, smaller when noise is high.
4. Appendix Perspective 2: Orthogonal Projection and Interference Suppression
Consider the geometric meaning of $I - k k^T$ (assuming $|k|=1$): it projects any vector onto the hyperplane orthogonal to $k$. The update rule can then be rewritten as:
S_t = S_{t-1} - β_t k (k^T S_{t-1}) + β_t k v_t^T
That is, first project out the component of the state that lies along the $k$ direction, then inject the new value along $k$. Since different keys are usually not strictly orthogonal, practical implementations normalize keys first and approximately maintain near-orthogonality of the key set over multiple accumulated steps, so that memories associated with different keys do not interfere with each other. This is the fundamental reason Delta Attention is more stable than naive additive updates on long sequences: it actively performs subtraction, suppressing memory crosstalk.
5. Engineering Practice
- Numerical stability: long-term accumulation in the state matrix can cause magnitude drift. In practice, keys are normalized, the range of $\beta$ is constrained, and higher precision is used for state accumulation under mixed precision.
- Parallel training: during training, the recurrence can be unrolled into masked matrix operations to avoid step-by-step sequential computation; during inference, it reduces to a fixed-size state recurrence with constant memory usage.
- Hybrid architectures: a common approach is to interleave linear attention layers with a small number of global attention layers, balancing linear complexity and precise retrieval.
- Cloud deployment: these models have small state and high throughput, making them suitable for elastic scaling on container platforms, with object storage for checkpoints, and logging and monitoring services to observe latency and memory usage under long contexts.
Conclusion
Delta Attention turns “memory” from a passive accumulation container into an object of active online optimization: the update rule can be derived from one step of gradient descent, and geometrically it is equivalent to orthogonal projection followed by residual injection. Understanding these two equivalent perspectives captures the main design thread of efficient long-sequence modeling—trading controllable forgetting for sustainable memory capacity.
