Pratham Patel
· 26 min read

Attention Residuals: Replacing Fixed Skip Connections with Learned Depth-Wise Attention

Building Attention Residuals from scratch — why standard residuals dilute information, how softmax attention over depth fixes it, the block variant that makes it practical, and the structured-matrix view that unifies everything — all derived step by step with a 4-layer running example

Residual connections are the backbone of every modern deep network. The update rule hl=hl1+fl1(hl1)\boldsymbol{h}_l = \boldsymbol{h}_{l-1} + f_{l-1}(\boldsymbol{h}_{l-1}) is so universal that we rarely question it. But this simplicity hides a rigid design choice: every previous layer’s output is accumulated with a fixed weight of 1. There is no mechanism for a later layer to say “I need the embedding more than I need layer 3’s output” or “layer 7’s contribution is irrelevant to me.”

The Attention Residuals paper (Kimi Team, 2026) proposes a direct fix: replace the fixed accumulation with learned, input-dependent softmax attention over all previous layer outputs. The idea is clean — apply the same attention mechanism that Transformers use over the sequence dimension, but now over the depth dimension.

We will derive everything from scratch using a single running example: a tiny network with L=4L = 4 layers and scalar hidden states (d=1d = 1). By the end, we will have built up to Full Attention Residuals, Block Attention Residuals, and the structured-matrix view that reveals standard residuals, Highway networks, Hyper-Connections, and AttnRes as points on a single spectrum.


1. Standard Residual Connections

1.1 The Recurrence

A residual connection adds the output of a layer to its input, preserving an identity path through the network. The hidden state at layer ll is:

hl=hl1+fl1(hl1)\boldsymbol{h}_l = \boldsymbol{h}_{l-1} + f_{l-1}(\boldsymbol{h}_{l-1})

where fl1f_{l-1} is the transformation applied by layer l1l-1 (an attention sub-layer or an MLP sub-layer in a Transformer), and h1\boldsymbol{h}_1 is the token embedding.

Let us define v0=h1\boldsymbol{v}_0 = \boldsymbol{h}_1 (the embedding) and vi=fi(hi)\boldsymbol{v}_i = f_i(\boldsymbol{h}_i) for i1i \geq 1 (each layer’s output). Then we can unroll the recurrence.

1.2 Unrolling the Recurrence

For our 4-layer network:

h1=v0\boldsymbol{h}_1 = \boldsymbol{v}_0 h2=h1+f1(h1)=v0+v1\boldsymbol{h}_2 = \boldsymbol{h}_1 + f_1(\boldsymbol{h}_1) = \boldsymbol{v}_0 + \boldsymbol{v}_1 h3=h2+f2(h2)=v0+v1+v2\boldsymbol{h}_3 = \boldsymbol{h}_2 + f_2(\boldsymbol{h}_2) = \boldsymbol{v}_0 + \boldsymbol{v}_1 + \boldsymbol{v}_2 h4=h3+f3(h3)=v0+v1+v2+v3\boldsymbol{h}_4 = \boldsymbol{h}_3 + f_3(\boldsymbol{h}_3) = \boldsymbol{v}_0 + \boldsymbol{v}_1 + \boldsymbol{v}_2 + \boldsymbol{v}_3

The general form is:

hl=i=0l1vi=v0+i=1l1fi(hi)\boxed{\boldsymbol{h}_l = \sum_{i=0}^{l-1} \boldsymbol{v}_i = \boldsymbol{v}_0 + \sum_{i=1}^{l-1} f_i(\boldsymbol{h}_i)}

Every layer receives the uniform sum of all previous layer outputs. The coefficient on every term is exactly 1 — no more, no less.

1.3 Numerical Check

Let our scalar example have v0=10\boldsymbol{v}_0 = 10, v1=2\boldsymbol{v}_1 = 2, v2=1\boldsymbol{v}_2 = -1, v3=5\boldsymbol{v}_3 = 5. Then:

  • h1=10\boldsymbol{h}_1 = 10
  • h2=10+2=12\boldsymbol{h}_2 = 10 + 2 = 12
  • h3=10+2+(1)=11\boldsymbol{h}_3 = 10 + 2 + (-1) = 11
  • h4=10+2+(1)+5=16\boldsymbol{h}_4 = 10 + 2 + (-1) + 5 = 16

Each hidden state is a simple running total. Layer 4 has no choice but to accept the sum 10+21+5=1610 + 2 - 1 + 5 = 16. It cannot “turn down” v2=1\boldsymbol{v}_2 = -1 or “amplify” v0=10\boldsymbol{v}_0 = 10.

1.4 The Depth Mixing Matrix

We can write the full system as a matrix equation. Define the depth mixing matrix MRL×L\mathbf{M} \in \mathbb{R}^{L \times L} where Mil\mathbf{M}_{i \to l} is the weight that layer ll assigns to the output of layer ii. For standard residuals, Mil=1\mathbf{M}_{i \to l} = 1 for all i<li < l:

[h1h2h3h4]=[1111111111][v0v1v2v3]\begin{bmatrix} \boldsymbol{h}_1 \\ \boldsymbol{h}_2 \\ \boldsymbol{h}_3 \\ \boldsymbol{h}_4 \end{bmatrix} = \begin{bmatrix} 1 & & & \\ 1 & 1 & & \\ 1 & 1 & 1 & \\ 1 & 1 & 1 & 1 \end{bmatrix} \begin{bmatrix} \boldsymbol{v}_0 \\ \boldsymbol{v}_1 \\ \boldsymbol{v}_2 \\ \boldsymbol{v}_3 \end{bmatrix}

This is an all-ones lower-triangular matrix. Every entry below and including the diagonal is 1. There is zero selectivity.

1.5 Numerical Check (Matrix Form)

Using our values v=[10,2,1,5]\boldsymbol{v} = [10, 2, -1, 5]^\top:

[1000110011101111][10215]=[10121116]\begin{bmatrix} 1 & 0 & 0 & 0 \\ 1 & 1 & 0 & 0 \\ 1 & 1 & 1 & 0 \\ 1 & 1 & 1 & 1 \end{bmatrix} \begin{bmatrix} 10 \\ 2 \\ -1 \\ 5 \end{bmatrix} = \begin{bmatrix} 10 \\ 12 \\ 11 \\ 16 \end{bmatrix}

Checking row 4: 1×10+1×2+1×(1)+1×5=161 \times 10 + 1 \times 2 + 1 \times (-1) + 1 \times 5 = 16. Matches.


2. Why Fixed Accumulation Is a Problem

2.1 The PreNorm Dilution Problem

In practice, modern LLMs use PreNorm — applying layer normalization before each sub-layer rather than after. PreNorm restores a clean identity path and stabilizes gradients, making it the dominant paradigm.

But PreNorm introduces a subtle problem. Since hl=i=0l1vi\|\boldsymbol{h}_l\| = \|\sum_{i=0}^{l-1} \boldsymbol{v}_i\| grows as O(L)O(L) with depth (by the unrolled recurrence above), and PreNorm normalizes before the transformation, each layer’s relative contribution gets progressively diluted. The embedding v0\boldsymbol{v}_0 that was 100% of h1\boldsymbol{h}_1 is only a fraction 1/L\sim 1/L of hL\boldsymbol{h}_L.

For our example with L=4L = 4: h4=16\boldsymbol{h}_4 = 16, and v0=10\boldsymbol{v}_0 = 10 contributes 10/16=62.5%10/16 = 62.5\%. With L=100L = 100, the embedding would contribute roughly 1%\sim 1\% of the hidden state magnitude.

This has a concrete consequence: deeper layers must learn increasingly larger outputs just to remain influential. Empirically, the paper shows that output magnitudes grow monotonically with depth in baseline models — a direct symptom of this dilution.

2.2 Three Limitations of Single-State Recurrence

Whether we use fixed weights (standard residuals) or learned gates (Highway networks), every approach that conditions only on hl1\boldsymbol{h}_{l-1} shares three limitations:

  1. No selective access. Different layer types (attention vs. MLP) receive the same aggregated state, despite potentially benefiting from different weightings of past layers.

  2. Irreversible loss. Once information is mixed into the running sum, it cannot be selectively recovered. If v3\boldsymbol{v}_3 partially cancels v1\boldsymbol{v}_1 in the sum, layer 5 cannot “undo” this.

  3. Output growth. Later layers must learn increasingly larger outputs to influence the accumulated residual, which can destabilize training.


3. The Time-Depth Duality

This is the central insight of the paper. It is worth slowing down for.

3.1 RNNs Compress Over Time

A recurrent neural network (RNN) processes a sequence by maintaining a single hidden state st\boldsymbol{s}_t that compresses all past tokens:

st=st1+f(st1,xt)\boldsymbol{s}_t = \boldsymbol{s}_{t-1} + f(\boldsymbol{s}_{t-1}, \boldsymbol{x}_t)

This is structurally identical to the residual update hl=hl1+fl1(hl1)\boldsymbol{h}_l = \boldsymbol{h}_{l-1} + f_{l-1}(\boldsymbol{h}_{l-1}). The RNN compresses over time steps tt; the residual connection compresses over layers ll. Both maintain a single state that accumulates all prior contributions with fixed weights.

3.2 Attention Replaced RNNs Over Time

The Transformer solved the RNN bottleneck by replacing the fixed recurrence with attention: each position can selectively access all previous positions with learned, data-dependent weights. This was the linear-to-softmax transition for the sequence dimension.

3.3 AttnRes Applies the Same Fix Over Depth

Attention Residuals propose the exact same transition for depth. Instead of compressing all previous layers into a single running sum, each layer selectively attends to all previous layer outputs with learned, input-dependent weights via softmax.

The analogy is precise:

Sequence (RNN → Transformer)Depth (Residual → AttnRes)
Statest\boldsymbol{s}_t (hidden state)hl\boldsymbol{h}_l (hidden state)
SourcesPast tokens x1,,xt1\boldsymbol{x}_1, \ldots, \boldsymbol{x}_{t-1}Past layer outputs v0,,vl1\boldsymbol{v}_0, \ldots, \boldsymbol{v}_{l-1}
Fixed mixingRNN recurrenceResidual sum
Selective mixingSequence attentionDepth attention (AttnRes)

Standard residuals and prior recurrence-based variants can all be shown to perform depth-wise linear attention. AttnRes generalizes them to depth-wise softmax attention — completing for depth the same linear-to-softmax transition that proved transformative for sequences.


4. Full Attention Residuals

4.1 The Attention Weights

We now define the attention mechanism over depth. For each layer ll, the attention weight that layer ll assigns to source ii is:

αil=ϕ(ql,ki)j=0l1ϕ(ql,kj)\alpha_{i \to l} = \frac{\phi(\boldsymbol{q}_l, \boldsymbol{k}_i)}{\sum_{j=0}^{l-1} \phi(\boldsymbol{q}_l, \boldsymbol{k}_j)}

where ϕ:Rd×RdR0\phi: \mathbb{R}^d \times \mathbb{R}^d \to \mathbb{R}_{\geq 0} is a kernel function. The paper uses:

ϕ(q,k)=exp ⁣(qRMSNorm(k))\phi(\boldsymbol{q}, \boldsymbol{k}) = \exp\!\bigl(\boldsymbol{q}^\top \text{RMSNorm}(\boldsymbol{k})\bigr)

This is the standard softmax attention formula — the softmax function ensures the weights sum to 1 across all sources. The RMSNorm inside ϕ\phi prevents layers with naturally larger outputs from dominating the attention weights.

4.2 Queries, Keys, and Values

For each layer ll, we define:

Query: ql=wl\boldsymbol{q}_l = \boldsymbol{w}_l, a learned dd-dimensional vector specific to layer ll. This is a pseudo-query — it is a parameter, not a function of the hidden state.

Keys and Values:

ki=vi={h1i=0fi(hi)1il1\boldsymbol{k}_i = \boldsymbol{v}_i = \begin{cases} \boldsymbol{h}_1 & i = 0 \\ f_i(\boldsymbol{h}_i) & 1 \leq i \leq l-1 \end{cases}

The keys and values are identical — both are the individual layer outputs. This is a deliberate design choice: the pseudo-query wl\boldsymbol{w}_l is decoupled from the forward computation, meaning attention weights for all layers in a group can be computed in parallel.

4.3 The Full AttnRes Formula

The input to layer ll is then:

hl=i=0l1αilvi\boxed{\boldsymbol{h}_l = \sum_{i=0}^{l-1} \alpha_{i \to l} \cdot \boldsymbol{v}_i}

Compare this with the standard residual: hl=i=0l11vi\boldsymbol{h}_l = \sum_{i=0}^{l-1} 1 \cdot \boldsymbol{v}_i. The only change is replacing the fixed coefficient 1 with the learned attention weight αil\alpha_{i \to l}. But because these weights are softmax-normalized and input-dependent, each layer can now selectively emphasize or suppress any previous layer’s contribution.

4.4 Numerical Example

Let us work through Full AttnRes for our 4-layer scalar example. Since d=1d = 1, each query wl\boldsymbol{w}_l and each key ki\boldsymbol{k}_i is a scalar. Suppose:

  • Layer outputs: v0=10\boldsymbol{v}_0 = 10, v1=2\boldsymbol{v}_1 = 2, v2=1\boldsymbol{v}_2 = -1, v3=5\boldsymbol{v}_3 = 5
  • Pseudo-queries: w1=0.5w_1 = 0.5, w2=0.3w_2 = 0.3, w3=0.8w_3 = 0.8, w4=0.1w_4 = 0.1

For simplicity, let us skip the RMSNorm (in d=1d = 1 it just normalizes to ±1\pm 1) and compute raw ϕ(q,k)=exp(qk)\phi(q, k) = \exp(q \cdot k) directly on unnormalized values for illustration.

Layer 2 attends over sources {0,1}\{0, 1\} with query w2=0.3w_2 = 0.3:

ϕ(0.3,10)=e3.0=20.09,ϕ(0.3,2)=e0.6=1.82\phi(0.3, 10) = e^{3.0} = 20.09, \quad \phi(0.3, 2) = e^{0.6} = 1.82 α02=20.0920.09+1.82=20.0921.91=0.917\alpha_{0 \to 2} = \frac{20.09}{20.09 + 1.82} = \frac{20.09}{21.91} = 0.917 α12=1.8221.91=0.083\alpha_{1 \to 2} = \frac{1.82}{21.91} = 0.083 h2=0.917×10+0.083×2=9.17+0.17=9.34\boldsymbol{h}_2 = 0.917 \times 10 + 0.083 \times 2 = 9.17 + 0.17 = 9.34

Compare with the standard residual: h2=10+2=12\boldsymbol{h}_2 = 10 + 2 = 12. AttnRes produces a weighted combination that sums to a different value — and crucially, the weights are normalized so the magnitude does not grow uncontrollably.

4.5 The Depth Mixing Matrix for Full AttnRes

For Full AttnRes, the mixing matrix M\mathbf{M} has entries Mil=αil\mathbf{M}_{i \to l} = \alpha_{i \to l}:

MFull=[ϕ(w1,k0)ϕ(w2,k0)ϕ(w2,k1)ϕ(w3,k0)ϕ(w3,k1)ϕ(w3,k2)ϕ(w4,k0)ϕ(w4,k1)ϕ(w4,k2)ϕ(w4,k3)]\mathbf{M}_{\text{Full}} = \begin{bmatrix} \phi(\boldsymbol{w}_1, \boldsymbol{k}_0) & & & \\ \phi(\boldsymbol{w}_2, \boldsymbol{k}_0) & \phi(\boldsymbol{w}_2, \boldsymbol{k}_1) & & \\ \phi(\boldsymbol{w}_3, \boldsymbol{k}_0) & \phi(\boldsymbol{w}_3, \boldsymbol{k}_1) & \phi(\boldsymbol{w}_3, \boldsymbol{k}_2) & \\ \phi(\boldsymbol{w}_4, \boldsymbol{k}_0) & \phi(\boldsymbol{w}_4, \boldsymbol{k}_1) & \phi(\boldsymbol{w}_4, \boldsymbol{k}_2) & \phi(\boldsymbol{w}_4, \boldsymbol{k}_3) \end{bmatrix}

(Each row is normalized by its row sum.) This is a dense, input-dependent, lower-triangular matrix with rank LL — the maximum possible. Contrast this with the all-ones matrix of standard residuals.

4.6 Overhead and Feasibility

Full AttnRes requires O(L2d)O(L^2 d) computation and O(Ld)O(Ld) memory to store all layer outputs. The computation cost is modest because L<1000L < 1000 in practice (unlike sequence length which can reach millions). The memory overlaps entirely with activations already retained for backpropagation in standard training.

However, at scale with pipeline parallelism and activation recomputation, every layer output must be kept alive and transmitted across pipeline stages, making the O(Ld)O(Ld) memory and communication prohibitive. This motivates the Block variant.


5. Block Attention Residuals

5.1 The Idea: Compress Within Blocks, Attend Across Blocks

Block Attention Residuals (Block AttnRes) partition the LL layers into NN blocks of S=L/NS = L/N layers each. Within each block, layer outputs are accumulated via standard summation. Across blocks, we apply full attention over the NN block-level representations.

This reduces memory from O(Ld)O(Ld) to O(Nd)O(Nd) and computation from O(L2)O(L^2) to O(N2)O(N^2).

5.2 Intra-Block Accumulation

We divide our L=4L = 4 layers into N=2N = 2 blocks of S=2S = 2 layers each. Let B1={1,2}\mathcal{B}_1 = \{1, 2\} and B2={3,4}\mathcal{B}_2 = \{3, 4\}. The block representation is the sum of layer outputs within the block:

bn=jBnfj(hj)\boldsymbol{b}_n = \sum_{j \in \mathcal{B}_n} f_j(\boldsymbol{h}_j)

For our example:

b1=v1+v2=2+(1)=1\boldsymbol{b}_1 = \boldsymbol{v}_1 + \boldsymbol{v}_2 = 2 + (-1) = 1 b2=v3+v4\boldsymbol{b}_2 = \boldsymbol{v}_3 + \boldsymbol{v}_4

We also track partial sums within each block. Define bni\boldsymbol{b}_n^i as the partial sum over the first ii layers in block nn:

b11=v1=2,b12=v1+v2=1=b1\boldsymbol{b}_1^1 = \boldsymbol{v}_1 = 2, \quad \boldsymbol{b}_1^2 = \boldsymbol{v}_1 + \boldsymbol{v}_2 = 1 = \boldsymbol{b}_1

5.3 Inter-Block Attention

For the first layer in block nn, the value matrix consists of all previous block representations plus the embedding b0=h1\boldsymbol{b}_0 = \boldsymbol{h}_1:

V=[b0,b1,,bn1]\mathbf{V} = [\boldsymbol{b}_0, \boldsymbol{b}_1, \ldots, \boldsymbol{b}_{n-1}]^\top

For subsequent layers within block nn, we additionally include the current block’s partial sum bni1\boldsymbol{b}_n^{i-1}:

V=[b0,b1,,bn1,bni1]\mathbf{V} = [\boldsymbol{b}_0, \boldsymbol{b}_1, \ldots, \boldsymbol{b}_{n-1}, \boldsymbol{b}_n^{i-1}]^\top

Keys and attention weights follow the same kernel ϕ\phi from Eq. 2 and Eq. 3 (Section 4.1–4.2), with the block representations serving as both keys and values.

5.4 Walking Through Block AttnRes

For our L=4L = 4, N=2N = 2 example with b0=v0=10\boldsymbol{b}_0 = \boldsymbol{v}_0 = 10 and b1=1\boldsymbol{b}_1 = 1:

Layer 3 (first layer of block 2) attends over {b0,b1}={10,1}\{\boldsymbol{b}_0, \boldsymbol{b}_1\} = \{10, 1\}:

h3=α0310+α131\boldsymbol{h}_3 = \alpha_{0 \to 3} \cdot 10 + \alpha_{1 \to 3} \cdot 1

Layer 4 (second layer of block 2) attends over {b0,b1,b21}\{\boldsymbol{b}_0, \boldsymbol{b}_1, \boldsymbol{b}_2^1\} where b21=v3=f3(h3)\boldsymbol{b}_2^1 = \boldsymbol{v}_3 = f_3(\boldsymbol{h}_3). So it sees N+1=3N + 1 = 3 sources instead of N=2N = 2, gaining one extra source for the intra-block partial sum.

5.5 The Block AttnRes Mixing Matrix

For our L=4L = 4, N=2N = 2 example, the mixing matrix is:

MBlock=[ϕ(w1,k0)ϕ(w2,k0)ϕ(w2,k1)ϕ(w3,k0)ϕ(w3,k1+k2)ϕ(w4,k0)ϕ(w4,k1+k2)ϕ(w4,k3)]\mathbf{M}_{\text{Block}} = \begin{bmatrix} \phi(\boldsymbol{w}_1, \boldsymbol{k}_0) & & & \\ \phi(\boldsymbol{w}_2, \boldsymbol{k}_0) & \phi(\boldsymbol{w}_2, \boldsymbol{k}_1) & & \\ \phi(\boldsymbol{w}_3, \boldsymbol{k}_0) & \phi(\boldsymbol{w}_3, \boldsymbol{k}_1 + \boldsymbol{k}_2) & & \\ \phi(\boldsymbol{w}_4, \boldsymbol{k}_0) & \phi(\boldsymbol{w}_4, \boldsymbol{k}_1 + \boldsymbol{k}_2) & \phi(\boldsymbol{w}_4, \boldsymbol{k}_3) & \end{bmatrix}

Notice the key difference from Full AttnRes: layers within a completed block share the same combined key k1+k2\boldsymbol{k}_1 + \boldsymbol{k}_2. The individual layer-level granularity is lost in exchange for a dramatic reduction in the number of sources from LL to approximately N+SN + S.

5.6 Interpolating Between Extremes

The block count NN controls a smooth interpolation:

  • N=LN = L: Each block has one layer (S=1S = 1). Every layer output is its own block. This recovers Full AttnRes.
  • N=1N = 1: All layers are in one block. Intra-block summation reduces to standard addition. This recovers standard residual connections with the embedding isolated as b0\boldsymbol{b}_0.

Empirically, N8N \approx 8 recovers most of the gain of Full AttnRes. For the 48B-parameter Kimi Linear model with 54 layers, Block AttnRes uses 6 layers per block, producing 9 blocks plus the token embedding for a total of 10 depth-wise sources.


6. The Two-Phase Computation Strategy

A naive implementation of Block AttnRes would compute attention at every layer, each requiring a full pass over all preceding blocks — resulting in O(LN)O(L \cdot N) total memory accesses. The paper introduces a two-phase strategy that exploits a key property: the pseudo-queries wl\boldsymbol{w}_l are learned parameters decoupled from the forward computation.

6.1 Phase 1: Parallel Inter-Block Attention

Because the pseudo-queries are parameters (not functions of hl\boldsymbol{h}_l), we can batch all SS queries within a block and compute their attention over all previous blocks simultaneously:

Q=[wl]lBnRS×d,K=V=[b0;;bn1]Rn×d\mathbf{Q} = [\boldsymbol{w}_l]_{l \in \mathcal{B}_n} \in \mathbb{R}^{S \times d}, \quad \mathbf{K} = \mathbf{V} = [\boldsymbol{b}_0; \ldots; \boldsymbol{b}_{n-1}] \in \mathbb{R}^{n \times d}

This single batched attention call returns, for each layer ll in the block, the inter-block attention output ol(1)\boldsymbol{o}_l^{(1)} along with its softmax statistics (the max ml(1)m_l^{(1)} and log-sum-exp l(1)\ell_l^{(1)}). This amortizes the memory reads from SS reads down to 1 read per block.

6.2 Phase 2: Sequential Intra-Block Attention with Online Softmax Merge

Phase 2 processes layers sequentially within the block. For each layer ll (after the first in the block), it computes intra-block attention over the evolving partial sum bni1\boldsymbol{b}_n^{i-1}, obtaining output ol(2)\boldsymbol{o}_l^{(2)} with statistics ml(2)m_l^{(2)} and l(2)\ell_l^{(2)}.

The two sets of attention outputs are then merged using the online softmax algorithm. This is a numerically stable method for combining two softmax computations that were performed independently. The merge computes:

ml=max(ml(1),ml(2))m_l = \max(m_l^{(1)}, m_l^{(2)}) hl=eml(1)mlol(1)+eml(2)mlol(2)eml(1)mll(1)+eml(2)mll(2)\boldsymbol{h}_l = \frac{e^{m_l^{(1)} - m_l} \cdot \boldsymbol{o}_l^{(1)} + e^{m_l^{(2)} - m_l} \cdot \boldsymbol{o}_l^{(2)}}{e^{m_l^{(1)} - m_l} \cdot \ell_l^{(1)} + e^{m_l^{(2)} - m_l} \cdot \ell_l^{(2)}}

This is mathematically equivalent to computing softmax over all sources jointly, but avoids materializing the full attention matrix. The subtraction of mlm_l from both exponents prevents numerical overflow — this is the same log-sum-exp trick used in FlashAttention and standard stable softmax implementations.

6.3 Numerical Check of Online Softmax Merge

Suppose Phase 1 gives o(1)=8.5\boldsymbol{o}^{(1)} = 8.5 with m(1)=3.0m^{(1)} = 3.0, (1)=25.0\ell^{(1)} = 25.0, and Phase 2 gives o(2)=2.0\boldsymbol{o}^{(2)} = 2.0 with m(2)=1.5m^{(2)} = 1.5, (2)=5.0\ell^{(2)} = 5.0.

m=max(3.0,1.5)=3.0m = \max(3.0, 1.5) = 3.0 h=e3.03.0×8.5+e1.53.0×2.0e3.03.0×25.0+e1.53.0×5.0=1.0×8.5+0.223×2.01.0×25.0+0.223×5.0=8.5+0.44625.0+1.115=8.94626.115=0.3425\boldsymbol{h} = \frac{e^{3.0 - 3.0} \times 8.5 + e^{1.5 - 3.0} \times 2.0}{e^{3.0 - 3.0} \times 25.0 + e^{1.5 - 3.0} \times 5.0} = \frac{1.0 \times 8.5 + 0.223 \times 2.0}{1.0 \times 25.0 + 0.223 \times 5.0} = \frac{8.5 + 0.446}{25.0 + 1.115} = \frac{8.946}{26.115} = 0.3425

The key property: this gives the same result as if we had computed softmax attention over all sources jointly. The online merge introduces zero approximation error.

6.4 Memory Access Cost

The total per-layer memory access cost for Block AttnRes with the two-phase strategy is:

ReadWrite
Phase 1 (amortized)NSd\frac{N}{S}ddd
Phase 23d3ddd
Total(NS+3)d(\frac{N}{S} + 3)d2d2d

With typical values L=128L = 128, N=8N = 8, S=16S = 16: total reads = (816+3)d=3.5d(\frac{8}{16} + 3)d = 3.5d, total writes = 2d2d, for a grand total of 5.5d5.5d. Compare this with standard residuals at 3d3d — the overhead is modest. The end-to-end inference latency overhead is less than 2% on typical workloads.


7. The Structured-Matrix View: Unifying All Residual Variants

This is the part that ties everything together. We have seen that standard residuals, Highway networks, Hyper-Connections, and AttnRes all compute hl=i=0l1Milvi\boldsymbol{h}_l = \sum_{i=0}^{l-1} \mathbf{M}_{i \to l} \cdot \boldsymbol{v}_i for different choices of the depth mixing matrix M\mathbf{M}. The paper formalizes this and shows that the variants differ in three properties: whether the weights are fixed or learned, whether they are input-dependent, and the semiseparable rank of M\mathbf{M}.

7.1 Standard Residuals: All-Ones Matrix

As derived in Section 1.4:

MResidual=[1111111111]\mathbf{M}_{\text{Residual}} = \begin{bmatrix} 1 \\ 1 & 1 \\ 1 & 1 & 1 \\ 1 & 1 & 1 & 1 \end{bmatrix}

Weights: fixed. Input-dependent: no. The matrix has the simplest possible structure.

7.2 Highway Networks: Gated Carry Products

Highway networks introduce element-wise gates gl[0,1]dg_l \in [0, 1]^d that interpolate between the identity path and the transformation:

hl=(1gl)hl1+glfl1(hl1)\boldsymbol{h}_l = (1 - g_l) \odot \boldsymbol{h}_{l-1} + g_l \odot f_{l-1}(\boldsymbol{h}_{l-1})

For scalar clarity, define the carry product γil×:=j=i+1l(1gj)\gamma_{i \to l}^\times := \prod_{j=i+1}^{l} (1 - g_j). This represents how much of source ii‘s output survives through all subsequent gates to reach layer ll. The mixing matrix entries are:

M0l=γ1l×,Mil=gi+1γi+1l× for i1\mathbf{M}_{0 \to l} = \gamma_{1 \to l}^\times, \quad \mathbf{M}_{i \to l} = g_{i+1} \cdot \gamma_{i+1 \to l}^\times \text{ for } i \geq 1

7.3 Numerical Check of Highway Carry Products

Let g1=0.3g_1 = 0.3, g2=0.5g_2 = 0.5, g3=0.2g_3 = 0.2, g4=0.6g_4 = 0.6. Compute the carry products for layer 4:

γ14×=(10.2)(10.5)(10.6)=0.8×0.5×0.4=0.16\gamma_{1 \to 4}^\times = (1 - 0.2)(1 - 0.5)(1 - 0.6) = 0.8 \times 0.5 \times 0.4 = 0.16

Wait — we need to be careful about indexing. Let us redo this with the paper’s convention. The gate glg_l is applied at layer ll. The carry product from source ii to layer ll is:

γil×=j=i+1l(1gj)\gamma_{i \to l}^\times = \prod_{j=i+1}^{l} (1 - g_j)

For M04\mathbf{M}_{0 \to 4} (embedding reaching layer 4):

γ14×=(1g2)(1g3)(1g4)=0.5×0.8×0.4=0.16\gamma_{1 \to 4}^\times = (1 - g_2)(1 - g_3)(1 - g_4) = 0.5 \times 0.8 \times 0.4 = 0.16

The Highway mixing matrix for our L=4L = 4 example:

MHighway=[1γ12×g2γ13×g2γ23×g3γ14×g2γ24×g3γ34×g4]\mathbf{M}_{\text{Highway}} = \begin{bmatrix} 1 \\ \gamma_{1\to2}^\times & g_2 \\ \gamma_{1\to3}^\times & g_2 \cdot \gamma_{2\to3}^\times & g_3 \\ \gamma_{1\to4}^\times & g_2 \cdot \gamma_{2\to4}^\times & g_3 \cdot \gamma_{3\to4}^\times & g_4 \end{bmatrix}

The key structural property: since the cumulative products factor through scalar gates, M\mathbf{M} is 1-semiseparable — the same rank as the standard residual, but with input-dependent weights. The weights sum to 1 by construction (each row partitions probability mass between “carry” and “transform”), making Highway a softmax-free, depth-wise instance of stick-breaking attention.

7.4 (m)Hyper-Connections: Multi-Stream Matrices

Hyper-Connections (HC) and their manifold-constrained variant mHC widen the recurrence to mm parallel streams. The update is:

Hl=Hl1Al+fl1(Hl1αl1)βl1\mathbf{H}_l = \mathbf{H}_{l-1} \mathbf{A}_l + f_{l-1}(\mathbf{H}_{l-1} \boldsymbol{\alpha}_{l-1}) \boldsymbol{\beta}_{l-1}^\top

where AlRm×m\mathbf{A}_l \in \mathbb{R}^{m \times m} is a learned transition matrix, αl1Rm\boldsymbol{\alpha}_{l-1} \in \mathbb{R}^m mixes streams into a single input for fl1f_{l-1}, and βl1Rm\boldsymbol{\beta}_{l-1} \in \mathbb{R}^m distributes the output back across streams.

Unrolling this recurrence gives:

Mil=βiAi+1l×αl\mathbf{M}_{i \to l} = \boldsymbol{\beta}_i^\top \mathbf{A}_{i+1 \to l}^\times \boldsymbol{\alpha}_l

where Ai+1l×:=k=i+1lAk\mathbf{A}_{i+1 \to l}^\times := \prod_{k=i+1}^{l} \mathbf{A}_k is the cumulative matrix product of transitions. The m×mm \times m transitions render M\mathbf{M} mm-semiseparable. mHC further constrains each Al\mathbf{A}_l to be doubly stochastic (by the Birkhoff–von Neumann theorem, every doubly stochastic matrix is a convex combination of permutation matrices), stabilizing the cumulative products across depth.

7.5 Full AttnRes: Dense, Input-Dependent

Full AttnRes computes:

Mil=αil=ϕ(wl,ki)jϕ(wl,kj)\mathbf{M}_{i \to l} = \alpha_{i \to l} = \frac{\phi(\boldsymbol{w}_l, \boldsymbol{k}_i)}{\sum_j \phi(\boldsymbol{w}_l, \boldsymbol{k}_j)}

where ki=vi\boldsymbol{k}_i = \boldsymbol{v}_i are the layer outputs. This yields a dense, rank-LL lower-triangular matrix. Every entry is input-dependent (through the keys) and the weights are softmax-normalized.

7.6 Block AttnRes: Controlled Rank

Block AttnRes shares weights within completed blocks: for all ii in a completed block Bn\mathcal{B}_n, Mil=αnl\mathbf{M}_{i \to l} = \alpha_{n \to l} (the same weight for all sources in the block). Within the current block, each layer additionally attends to the evolving partial sum bni1\boldsymbol{b}_n^{i-1}. The effective rank of M\mathbf{M} lies between NN and N+SN + S, interpolating between standard residuals (N=1N = 1) and Full AttnRes (N=LN = L).

7.7 The Spectrum

We can now arrange all variants along a spectrum of increasing expressiveness:

MethodWeight typeInput-dependent?Rank of M\mathbf{M}
Standard ResidualFixed (all 1s)No1
HighwayLearned (gates)Yes1
(m)HCLearned (matrices)Yesmm
Block AttnResLearned (softmax)YesNN to N+SN+S
Full AttnResLearned (softmax)YesLL

The insight is that these are not separate inventions — they are points on a single axis of increasing rank in the depth mixing matrix, with AttnRes at the maximum.


8. Prior Residuals as Depth-Wise Linear Attention

This section makes the time-depth duality from Section 3 mathematically precise.

8.1 The (m)HC Weight as Linear Attention

Recall the unrolled (m)HC weight from Section 7.4:

Mil=βiAi+1l×αl\mathbf{M}_{i \to l} = \boldsymbol{\beta}_i^\top \mathbf{A}_{i+1 \to l}^\times \boldsymbol{\alpha}_l

This admits a natural interpretation as linear attention over depth. The vector αl\boldsymbol{\alpha}_l plays the role of a query issued by layer ll. The vector βi\boldsymbol{\beta}_i serves as a key summarizing the contribution of layer ii. The cumulative transition Ai+1l×\mathbf{A}_{i+1 \to l}^\times acts as a depth-relative positional operator governing the query-key interaction across intervening layers.

The mm parallel streams correspond to state expansion along the depth axis, expanding the recurrent state from dd to d×md \times m. This is directly analogous to how multi-head attention expands representation capacity along the sequence axis.

8.2 From Linear to Softmax Attention Over Depth

Standard residuals and Highway networks perform depth-wise attention with rank-1 matrices — the simplest case. (m)HC extends this to rank-mm linear attention. AttnRes goes further and replaces the linear kernel with softmax normalization via the kernel ϕ(q,k)=exp(qRMSNorm(k))\phi(\boldsymbol{q}, \boldsymbol{k}) = \exp(\boldsymbol{q}^\top \text{RMSNorm}(\boldsymbol{k})).

This is the same transition that took RNNs (linear attention with state compression) to Transformers (softmax attention with direct access) — but applied to the depth dimension rather than the sequence dimension.


9. Initialization and Training Dynamics

9.1 Zero Initialization of Pseudo-Queries

A critical implementation detail: all pseudo-query vectors wl\boldsymbol{w}_l must be initialized to zero. When wl=0\boldsymbol{w}_l = \boldsymbol{0} for all ll:

ϕ(0,ki)=exp(0RMSNorm(ki))=exp(0)=1\phi(\boldsymbol{0}, \boldsymbol{k}_i) = \exp(\boldsymbol{0}^\top \text{RMSNorm}(\boldsymbol{k}_i)) = \exp(0) = 1

for all sources ii. This means:

αil=1lfor all i\alpha_{i \to l} = \frac{1}{l} \quad \text{for all } i

At initialization, every layer assigns equal weight to all previous sources — AttnRes starts as a uniform average, then learns to specialize during training. This prevents training volatility from random initial attention patterns.

9.2 How AttnRes Fixes PreNorm Dilution

Recall the dilution problem from Section 2.1: in standard residuals, hl\|\boldsymbol{h}_l\| grows as O(L)O(L) because every layer output is added with weight 1. With AttnRes, the weights αil\alpha_{i \to l} sum to 1 by the definition of softmax. The hidden state is a convex combination of previous outputs rather than their sum:

hl=i=0l1αilvii=0l1αilvimaxivi\|\boldsymbol{h}_l\| = \left\|\sum_{i=0}^{l-1} \alpha_{i \to l} \cdot \boldsymbol{v}_i\right\| \leq \sum_{i=0}^{l-1} \alpha_{i \to l} \|\boldsymbol{v}_i\| \leq \max_i \|\boldsymbol{v}_i\|

The last inequality follows from the triangle inequality and the fact that iαil=1\sum_i \alpha_{i \to l} = 1 (this is the convexity of the weighted average). The hidden state magnitude is bounded by the largest individual layer output, not their cumulative sum.

For Block AttnRes, the selective aggregation resets at block boundaries, confining the growth within each block. The paper shows empirically that this yields a bounded periodic pattern in output magnitudes — a dramatic improvement over the monotonic growth of the baseline.

9.3 Gradient Distribution

With standard residuals, the gradient with respect to an intermediate hidden state is:

Lhl=LhLj=lL1(I+fjhj)\frac{\partial \mathcal{L}}{\partial \boldsymbol{h}_l} = \frac{\partial \mathcal{L}}{\partial \boldsymbol{h}_L} \cdot \prod_{j=l}^{L-1} \left(\mathbf{I} + \frac{\partial f_j}{\partial \boldsymbol{h}_j}\right)

All residual weights are fixed at 1, so there is no mechanism to regulate gradient flow across depth. This leads to disproportionately large gradients in the earliest layers.

With AttnRes, the learnable softmax weights introduce competition among sources for probability mass. This naturally distributes gradients more uniformly across depth — the paper shows substantially more uniform gradient magnitudes across transformer blocks compared to the baseline.


10. Learned Attention Patterns

10.1 What the Network Actually Learns

The paper visualizes the learned weights αil\alpha_{i \to l} for a 16-head model with both Full and Block AttnRes. Three patterns emerge:

Preserved locality. Each layer attends most strongly to its immediate predecessor — the diagonal of the attention matrix dominates. This makes sense: the standard residual (which is purely local) works well, so the learned weights should stay close to it unless there is reason to deviate.

Selective long-range connections. Despite the diagonal dominance, selective off-diagonal concentrations emerge. For example, layer 4 attending to early sources, or layers 15–16 reaching back to the first few layers. These are learned skip connections beyond the standard residual path.

Embedding persistence. The token embedding h1\boldsymbol{h}_1 (source 0) retains non-trivial weight throughout the network, especially in pre-attention layers. This is consistent with the embedding carrying fundamental token identity information that remains relevant at all depths.

10.2 Attention vs. MLP Specialization

Pre-attention inputs show broader receptive fields (attending to sources across a wider range of depths), while pre-MLP inputs show sharper diagonal reliance on recent representations. This specialization is consistent with attention layers operating more globally across depth while MLPs refine locally — a distinction that standard residuals cannot express.


11. Experimental Results

11.1 Scaling Laws

The paper trains five model sizes (194M to 528M activated parameters) with three variants each: PreNorm baseline, Full AttnRes, and Block AttnRes (N8N \approx 8).

Fitting power-law curves L=A×Cα\mathcal{L} = A \times C^{-\alpha} (where CC is compute in PFLOP/s-days):

  • Baseline: L=1.891×C0.057\mathcal{L} = 1.891 \times C^{-0.057}
  • Block AttnRes: L=1.870×C0.058\mathcal{L} = 1.870 \times C^{-0.058}
  • Full AttnRes: L=1.865×C0.057\mathcal{L} = 1.865 \times C^{-0.057}

All three variants share similar scaling exponents, but AttnRes achieves consistently lower loss across the entire compute range. At the largest scale (5.6 PFLOP/s-days), Block AttnRes reaches 1.692 versus the baseline’s 1.714 — equivalent to a 1.25×1.25\times compute advantage.

11.2 48B Parameter Model

The full-scale model uses Block AttnRes on the Kimi Linear architecture (48B total / 3B activated parameters), pre-trained on 1.4T tokens. Results across 16 benchmarks:

  • General knowledge: MMLU +1.1, BBH +1.7, GPQA-Diamond +7.5
  • Math and code: GSM8K +0.7, Math +3.6, CMath +0.4, HumanEval +3.1, MBPP +1.9
  • Chinese: CMMLU +0.9, C-Eval +2.9

Block AttnRes matches or outperforms the baseline on every benchmark. The improvements are particularly pronounced on multi-step reasoning tasks (GPQA-Diamond, Math), consistent with the hypothesis that improved depth-wise information flow benefits compositional tasks where later layers need to selectively retrieve and build upon earlier representations.

11.3 Ablation Highlights

Key ablation findings on a 16-layer model:

VariantLoss
Baseline (PreNorm)1.766
DenseFormer (fixed cross-layer weights)1.767
mHC (mm streams)1.747
Full AttnRes1.737
Block AttnRes (S=4S = 4)1.746
w/ input-dependent query1.731
w/ input-independent mixing1.749
w/ sigmoid instead of softmax1.741
w/o RMSNorm on keys1.743

DenseFormer grants cross-layer access but with fixed, input-independent coefficients — it shows no gain over the baseline, highlighting the importance of input-dependent weighting. Replacing softmax with sigmoid degrades performance, which the paper attributes to softmax’s competitive normalization forcing sharper selection among sources. Removing RMSNorm on keys degrades both Full and Block AttnRes, confirming that preventing large-magnitude layers from dominating the attention weights is essential.


12. Summary

Standard residual connections accumulate all previous layer outputs with fixed unit weights, producing an all-ones depth mixing matrix that offers zero selectivity and causes hidden-state magnitudes to grow as O(L)O(L) under PreNorm — the dilution problem. Attention Residuals replace this fixed accumulation with learned softmax attention over depth, where each layer uses a single pseudo-query vector to selectively weight all previous layer outputs, yielding a dense, input-dependent mixing matrix with maximum rank. Block AttnRes makes this practical at scale by compressing layers into NN blocks with standard summation within blocks and full attention across block representations, reducing memory from O(Ld)O(Ld) to O(Nd)O(Nd) while recovering most of the gain with N8N \approx 8 — a two-phase computation strategy with online softmax merging keeps inference overhead below 2%.


Previous: Mixture of Experts from Scratch — Part 2
Next: Mathematical Prerequisites for Mixture of Experts — Part 3

Enjoyed this post?

Subscribe to get notified when I publish new posts. No spam, unsubscribe anytime.