LLM Architecture

Why Transformers Can Loop: Orthogonal Residual Streams and Circuit Depth

Contents
  1. 1. The Residual Stream: Subspace Addressing Across Shared Steps
  2. 2. Physical Depth vs. Effective Depth, and Why Attention Stays Inside the Loop
  3. 3. Why Constant Depth Stalls on $S_5$, While $\mathcal{O}(\log n)$ Loops Reach $\mathsf{NC}^1$
  4. References

In a standard decoder, layer $l$ transforms activations and hands them off to distinct layer $l+1$ without looking back. Unrolling a plain MLP dozens of times with the exact same weight matrix quickly explodes or collapses hidden states onto top eigenvectors. Yet looped Transformers routinely pass representations through the same shared weights 16, 32, or even 64 times.

Two foundational questions underlie this architecture. First, why does feeding identical weights back into themselves stay well-conditioned instead of overwriting earlier features? Second, can looping fixed weights in latent space actually compute classes of functions inaccessible to constant-depth models? The answers come from residual stream subspace geometry and uniform circuit complexity.


1. The Residual Stream: Subspace Addressing Across Shared Steps#

Pre-Norm Transformers support weight tying naturally because sublayers never replace earlier representations outright; instead, every pass reads from and adds deltas back onto one shared linear residual stream (Elhage et al., 2021):

$$\begin{aligned} H^{(t - \frac{1}{2})} &= H^{(t-1)} + \mathrm{Attn}_{\theta}\!\big(\mathrm{Norm}(H^{(t-1)})\big), \\ H^{(t)} &= H^{(t - \frac{1}{2})} + \mathrm{FFN}_{\theta}\!\big(\mathrm{Norm}(H^{(t - \frac{1}{2})})\big) \end{aligned}$$

Viewed through dynamical systems, the update $h_{t+1} = h_t + f_\theta(h_t)$ is the forward Euler discretization ($\Delta t = 1$) of an autonomous Neural ODE, $\frac{\mathrm{d}h(t)}{\mathrm{d}t} = f_\theta(h(t))$: because the vector field $f_\theta$ is time-independent, each iteration advances the state along a single stationary vector field.

Because every pass operates inside the exact same coordinate basis that ultimately projects into the unembedding matrix $W_U$, different iterations can reuse channels safely. Writing out single attention head $h$ ($d_h \ll d$) makes the mechanism explicit:

$$\Delta H_{\text{attn}, h} = A_h H W_V^h W_O^h, \quad W_V^h \in \mathbb{R}^{d \times d_h},\; W_O^h \in \mathbb{R}^{d_h \times d}$$

Right-multiplying $H$ by $W_V^h$ restricts reads to a low-rank column subspace $\mathrm{col}(W_V^h) \subset \mathbb{R}^d$, while $W_O^h$ adds results back into $\mathrm{row}(W_O^h)$. As long as $d_h \ll d$, high-dimensional residual space supplies plenty of nearly orthogonal slots so intermediate computations can communicate across steps without overwriting prior features.

Calculation

Worked Verification · 4D State Trace (Gated Non-Refiring): Suppose a token starts at $h^{(0)} = [0, 1, 0, 0]$, and the shared block implements two inhibitory-gated rules: $\Delta_3 = 2\,\mathrm{ReLU}(h_2 - h_3)$ and $\Delta_4 = 1.5\,\mathrm{ReLU}(h_3 - h_4)$. In Pass 1, $h_2 = 1,\, h_3 = 0$, giving $\Delta_3 = 2\,\mathrm{ReLU}(1 - 0) = 2$ and $\Delta_4 = 0$, updating the stream to $h^{(1)} = [0, 1, 2, 0]$.

In Pass 2, because dim 3 now holds $2 > h_2 = 1$, the inhibitory term $h_2 - h_3 = -1$ is clamped to $0$ by ReLU so step 1 does not re-fire; instead, the downstream channel reads $h_3 = 2$ and writes $\Delta_4 = 1.5\,\mathrm{ReLU}(2 - 0) = 3$, yielding $h^{(2)} = [0, 1, 2, 3]$ (settling at a fixed point at Pass 3). Because all steps share one coordinate basis, projecting intermediate states with Logit Lens (nostalgebraist, 2020) ($W_U h^{(t)}$) typically shows next-token confidence sharpening across iterations.

Backpropagation inherits the same identity shortcut:

$$\frac{\partial H^{(K)}}{\partial H^{(0)}} = \prod_{t=1}^{K} \left(I + J_t\right), \quad J_t = \frac{\partial (H^{(t)} - H^{(t-1)})}{\partial H^{(t-1)}}$$

While the $I$ term keeps gradients from decaying exponentially at initialization, it is not unconditional stability: eigenvalues of $J_t$ with $|1 + \lambda(J_t)| > 1$ still explode, and growing residual norms in unnormalized streams shrink Jacobians at subsequent Pre-Norm entrances. Stabilization strategies are analyzed in Keeping Deep Looped Transformers Stable: Norm Growth, Sandwich Norm, and TBPTT.

Residual Stream Bus: Shared Block R_θ and Two-Step Gated Subspace Writes Yes (t < K): iterate shared weights θ H⁽⁰⁾ H⁽ᵗ⁻¹⁾ + H⁽ᵗ⁻¹/²⁾ + t<K No Norm · W_U → Logits Norm Attn_θ Step 1 read & write +[0,0,2,0] Norm FFN_θ Step 2 suppresses s1 & writes +[0,0,0,3] 2-step trace: h⁽⁰⁾ = [0, 1, 0, 0] → h⁽¹⁾ = [0, 1, 2, 0] → h⁽²⁾ = [0, 1, 2, 3]
Figure 1: The residual stream acts as a shared bus enabling gated multi-step orthogonal subspace updates across iterations.

2. Physical Depth vs. Effective Depth, and Why Attention Stays Inside the Loop#

Evaluating looped models requires separating stored layer count from serial computation depth: a standard decoder locks execution to a single pass through $L$ distinct layers, whereas a looped core reuses one shared block across $K$ passes (see also David Louapre's interactive Looped Transformers Space). Moving from the 2018 Universal Transformer to modern looped language models required clearing four practical walls: (1) replacing brittle ACT halting (which suffers from zero gradients and step jumps) with fixed/randomized depth or KL convergence, (2) replacing full BPTT memory growth with shallow checkpointing or TBPTT($k$), (3) removing additive sinusoidal step encodings that drift when unrolling past training depth, and (4) buffering token embeddings and logit projections outside the loop via a Prelude + Core + Coda stack.

Original figure (Dehghani et al., 2018, Fig. 2): Universal Transformer recurrent encoder and decoder blocks refining token states over T shared steps (arXiv:1807.03819).
Figure 2: Universal Transformer recurrent encoder and decoder blocks refining token states over T shared steps (arXiv:1807.03819). (Source: Dehghani et al., 2018, Fig. 2)

As the diagram shows, Embed Input Symbols feeds directly into the Recurrent Encoder Block (Multihead Self-Attention then Transition Function looped For T steps), passes to the Recurrent Decoder Block (For T steps), and exits straight into Softmax and Output Probabilities—with no untied Prelude or Coda layers buffering token embeddings or logit projections outside the recurrence. Denote untied prelude layers $L_{\text{pre}}$, shared core layers $L_{\text{core}}$ looped $K$ times, and untied coda layers $L_{\text{coda}}$:

Architecture VariantPhysical Layers $L_{\text{phys}}$Effective Depth $L_{\text{eff}}$Design Profile
LLaMA-3 8B · $K=1$3232Standard untied baseline, $L_{\text{phys}} = L_{\text{eff}}$
Universal Transformer · 201818 · $1\times 8$Single-block recurrence; maximal tying, tight capacity
Huginn-0125 · 3.5B8 · $2+4+2$132 · $r=32$Lightweight ends paired with a deep autonomous core
Inkling 2 Mini · 26.4B24 · $6+12+6$48 · $K=3$Pass 1 (incl. ends) is 3.4B; 2 extra core passes add 2×1B → 5.4B
NVIDIA Dory 1.0-4B36 · $8+20+8$36 / 56 / 96 · $K=1/2/4$Thick 20L core; latency increases in 20-layer steps
GLM-5.4 · ~750B78 · $26+28+24$106 · $2\times$ middle 28LFrontier-scale partial middle-stack looping

On Inkling 2 Mini, note the active-parameter arithmetic carefully: 3.4B already includes pass 1 ($K=1$, spanning the Prelude, the first Core pass, and the Coda), so running $K=3$ middle passes adds 2 × 1B = +2B → 5.4B active compute. Empirically, Saunshi et al. (2025) establish a sharp capability split under equal compute: a 12L × 6 looped model matches a 72L untied baseline on multi-hop reasoning and math while lagging on closed-book factual memorization such as TriviaQA.

Original figure (Saunshi et al., 2025, Fig. 1): Weight-tied looping (k ⊗ L) compared against iso-param (k ⊗ 1), iso-FLOP (kL ⊗ 1), and middle-looping baselines (arXiv:2502.17416).
Figure 3: Weight-tied looping (k ⊗ L) compared against iso-param (k ⊗ 1), iso-FLOP (kL ⊗ 1), and middle-looping baselines (arXiv:2502.17416). (Source: Saunshi et al., 2025, Fig. 1)

Two details help map this diagram onto our notation: first, the rightmost Middle Looping (1 → 2...2 → 3) panel—bottom peach block 1, repeated blue core blocks 2...2, and top lavender block 3—corresponds directly to Prelude–Core–Coda; second, Saunshi et al. swap k and L relative to our equations, using k for physical block layers and L for loop count in (k ⊗ L) (the reverse of our $L_{\text{core}}$ layers looped $K$ times).

A critical architectural invariant cuts across every one of those models: the looped core must contain Attention (or another sequence-mixing operator). An FFN transforms each position completely independently with zero communication diameter across tokens. Trace a 2-hop composition A → B → C stored across different context tokens: in Round 1, Attention lets token A read B and write that bridge into A's residual slot; in Round 2, Attention reads the newly written B from A to retrieve C, resolving A → C. Looping FFNs alone cuts step 2 off from intermediate values computed at other positions during step 1, halting cross-token multi-hop composition immediately after pass 1.

Physical Layers L_phys vs. Effective Compute Depth L_eff (Proportional Scale) Stored Layers L_phys Effective Applied Depth L_eff LLaMA-3 8B (K=1)phys 32 / eff 32Huginn-0125 (r=32)phys 8 / eff 132Inkling 2 Mini (K=3)phys 24 / eff 48Dory 1.0-4B (K=4)phys 36 / eff 96GLM-5.4 (middle ×2)phys 78 / eff 106
Figure 4: Proportional comparison of physical layers L_phys versus effective depth L_eff across architectures.

3. Why Constant Depth Stalls on $S_5$, While $\mathcal{O}(\log n)$ Loops Reach $\mathsf{NC}^1$#

Early Turing-completeness arguments for recurrent networks relied on unbounded loops or infinite scalar precision—neither of which holds on BF16 hardware. To model realistic finite-precision execution, Merrill & Sabharwal (2023, 2025) and Saunshi et al. (2025) analyze Transformers restricted to $\mathcal{O}(\log n)$-bit precision and $\mathrm{DLOGTIME}$-uniform circuits, positioning architectures inside standard complexity classes:

$$\mathsf{NC}^0 \subsetneq \mathsf{AC}^0 \subsetneq \mathsf{TC}^0 \subseteq \mathsf{NC}^1 \subseteq \mathsf{L} \subseteq \mathsf{NL} \subseteq \mathsf{TC}^1$$

The notation decodes straightforwardly: C denotes Boolean circuit families; superscript 0 means $\mathcal{O}(\log^0 n) = \mathcal{O}(1)$ constant depth, while superscript 1 means $\mathcal{O}(\log^1 n) = \mathcal{O}(\log n)$ logarithmic depth (and $\mathsf{L} \subseteq \mathsf{NL}$ denote deterministic and nondeterministic log-space), while N, A, T, and modular-gate ACC specify gate fan-in and primitives. In particular, $\mathsf{NC}^0$ uses bounded fan-in gates and suffers from $2^d$ myopia: a depth-3 $\mathsf{NC}^0$ circuit can inspect at most $2^3 = 8$ input bits even out of a million-token input.

Circuit ClassDepth in $n$Gate Type & Fan-inCharacteristic Power & Limits
$\mathsf{NC}^0$$\mathcal{O}(1)$ constantBounded fan-in AND/OR/NOTSees at most $2^d$ inputs; cannot compute global parity
$\mathsf{AC}^0$$\mathcal{O}(1)$ constantUnbounded fan-in AND/ORGlobal parallel lookup; still misses Parity and majority
$\mathsf{TC}^0$$\mathcal{O}(1)$ constantUnbounded fan-in threshold gatesBounds fixed-depth Transformers; solves counting and Dyck-1
$\mathsf{NC}^1$$\mathcal{O}(\log n)$ log depthBounded fan-in Boolean gatesSolves nested Boolean formulas and $S_5$ multiplication

Any Transformer whose depth remains constant as context length $n$ grows stays confined inside $\mathrm{DLOGTIME}$-uniform $\mathsf{TC}^0$. Somewhat surprisingly, checking balanced single-type parentheses (Dyck-1) is already solvable in $\mathsf{TC}^0$: map ( to $+1$ and ) to $-1$; layer 1 uses uniform causal attention to compute every prefix average $s_i / (i+1)$ and emit a per-position violation flag $\mathbf{1}[s_i < 0]$, and layer 2 applies uniform attention over those violation flags $\mathbf{1}[s_i < 0]$ across $i \le n$ while verifying $s_n = 0$—strictly $\mathcal{O}(1)$ constant depth.

What exposes $\mathsf{TC}^0$'s structural ceiling is non-commutative function composition and nested evaluation, canonically represented by nested Boolean formula evaluation and iterated multiplication in the symmetric group $S_5$ ($f_n \circ \dots \circ f_2 \circ f_1$). Why does the boundary sit right between $S_4$ and $S_5$? $S_4$ is a solvable group (its derived series terminates at the identity through abelian quotients), keeping iterated $S_4$ word multiplication inside $\mathsf{ACC}^0 \subseteq \mathsf{TC}^0$. Starting at $S_5$, however, the alternating subgroup $A_5$ is a non-abelian simple group, which by Barrington's theorem (1989) makes $S_5$ word multiplication $\mathsf{NC}^1$-complete. Under the standard conjecture $\mathsf{TC}^0 \subsetneq \mathsf{NC}^1$, no constant-depth Transformer can reliably compose arbitrarily long $S_5$ chains.

Why does $\mathcal{O}(\log n)$ recurrence break through? Because associative composition permits parallel tree reduction. Consider a 4-cup shell game executing four sequential swaps 1↔2, 2↔3, 3↔4, 2↔4. Tracking one ball serially takes 4 steps, yet two workers can reduce each half independently into a start-to-end permutation table:

Saunshi et al. (2025) prove that a single Transformer block looped $\lceil \log_2 n \rceil$ rounds can evaluate arbitrary finite-group word problems such as $S_5$. A natural question is how a stepless weight-tied block doubles its lookback stride $2^{k-1}$ automatically on iteration $k$ without explicit step embeddings: the same orthogonal residual-bus mechanism stores a self-updating binary/phase counter slot that the FFN advances after each pass, allowing identical $W_Q/W_K$ weights to match offset $2^{k-1}$ and execute a causal Hillis–Steele prefix scan on elements $f_1, f_2, f_3, f_4$:

This short runnable Python script verifies the Hillis–Steele prefix scan over eight non-commuting $S_5$ permutations: $\lceil \log_2 8 \rceil = 3$ parallel rounds match the 8-step serial product exactly:

PYTHON
def comp(a, b):
    return tuple(a[i] for i in b)

swaps = [
    (1, 0, 2, 3, 4), (0, 2, 1, 3, 4),
    (0, 1, 3, 2, 4), (0, 1, 2, 4, 3),
    (4, 1, 2, 3, 0), (0, 3, 2, 1, 4),
    (2, 1, 0, 3, 4), (0, 1, 4, 3, 2),
]
serial = [swaps[0]]
for x in swaps[1:]:
    serial.append(comp(x, serial[-1]))

cur, stride, rnd = list(swaps), 1, 0
while stride < len(swaps):
    cur = [comp(cur[i], cur[i - stride]) if i >= stride
           else cur[i] for i in range(len(swaps))]
    stride *= 2
    rnd += 1

assert cur == serial
print("rounds:", rnd, "final:", cur[-1])  # 3 rounds, (4,2,3,0,1)

Doubling the effective reach on every iteration evaluates every causal prefix in $\lceil \log_2 n \rceil$ passes. Extending the same mechanism to iterated boolean matrix multiplication lifts $\mathcal{O}(\log n)$-step looped Transformers into $\mathsf{TC}^1 \supseteq \mathsf{NC}^1$, capturing directed graph reachability ($\mathsf{NL}$). Meanwhile, explicit autoregressive Chain-of-Thought (CoT) acts as a manual loop unrolled across output tokens, trading $\mathrm{poly}(n)$ serial token steps to reach $\mathrm{P}$.

Scaling AxisSerial Steps in $n$Parallelism / StepComplexity ClassCharacteristic Tasks
Fixed-Depth Transformer$\mathcal{O}(1)$Full sequenceuniform $\mathsf{TC}^0$Counting, Dyck-1 matching
$\mathcal{O}(\log n)$-Step Loop$\mathcal{O}(\log n)$Full sequenceCovers $\mathsf{NC}^1$ / $\mathsf{TC}^1$Nested Boolean formulas, $S_5$, $\mathsf{NL}$ reachability
Autoregressive CoT$\mathrm{poly}(n)$ tokens1 token / stepReaches $\mathrm{P}$ · Merrill & Sabharwal 2024Explicit Turing-machine traces
Original figure (Merrill & Sabharwal, 2025, Fig. 1): Resource scaling (depth vs. width vs. CoT steps) required for regular-language recognition as context length grows (arXiv:2503.03961).
Figure 5: Resource scaling (depth vs. width vs. CoT steps) required for regular-language recognition as context length grows (arXiv:2503.03961). (Source: Merrill & Sabharwal, 2025, Fig. 1)

Tracing the regular-language recognition curves in the plot shows the separation directly: the orange width curve explodes vertically near context length ~100, the grey CoT steps line grows linearly at $\mathcal{O}(n)$ (500 steps at length 500), whereas the blue depth curve remains nearly flat on a logarithmic trajectory (~20–25 out to length 512). For problems beyond regular languages, extending linear $\mathcal{O}(n)$ CoT steps to $\mathrm{poly}(n)$ explicit tokens bridges the same unrolling mechanism to the complexity class $\mathrm{P}$.

Caveat

Caveat · Three Theoretical Bounds: First, $\mathsf{TC}^0 \subsetneq \mathsf{NC}^1$ remains an unproven separation conjecture in complexity theory. Second, classical Turing-completeness proofs assume unbounded runtime or infinite precision, whereas real inference clips both step count and floating-point mantissas. Third, circuit classes describe asymptotic scaling as $n \to \infty$; on short bounded inputs, wide untied networks can still memorize and match many iterative functions.

Circuit complexity marks a clear theoretical boundary: fixed-depth networks hit an algebraic ceiling at $\mathsf{TC}^0$ on deep non-commutative state tracking, whereas a logarithmic number of tied iterations breaks through inside a shared linear basis. Practically, though, real prompts rarely multiply 512-element $S_5$ chains—and 12L×6 still trails an untied 72L baseline on closed-book TriviaQA—so the tangible payoff in systems like Huginn or GLM-5.4 comes more from iterative latent refinement and compositional inductive bias than from asymptotic $\mathsf{NC}^1$ separation alone.

Constant-Depth TC⁰ Threshold Gate vs. O(log n) Hillis–Steele Causal Prefix Scan 1. Uniform TC⁰ (Unbounded Fan-In) Σ xᵢ ≥ θ (O(1) Depth) 1-layer Majority counts balance: Dyck-1 passes Non-Solvable Limit (S₅ ⊂ NC¹) Stalls on non-commutative S₅ word chains 2. O(log n) Causal Prefix Scan (offset 2^(k−1)) Step k=1 Step k=2 f₁f₁f₁f₂f₂∘f₁f₂∘f₁f₃f₃∘f₂f₃∘f₂∘f₁f₄f₄∘f₃f₄∘f₃∘f₂∘f₁ Produces exact causal prefixes f₁, f₂∘f₁, f₃∘f₂∘f₁, f₄∘f₃∘f₂∘f₁
Figure 6: Constant-depth TC⁰ limits versus O(log n) causal Hillis–Steele prefix composition.

References#

  1. Nelson Elhage et al. (2021). A Mathematical Framework for Transformer Circuits. Transformer Circuits Thread
  2. nostalgebraist (2020). interpreting GPT: the logit lens. LessWrong
  3. David A. Mix Barrington (1989). Bounded-Width Polynomial-Size Branching Programs Recognize Exactly Those Languages in $\mathsf{NC}^1$. Journal of Computer and System Sciences, 38(1): 150–164.
  4. Mostafa Dehghani et al. (2018). Universal Transformers. arXiv:1807.03819
  5. William Merrill & Ashish Sabharwal (2023). The Parallelism Tradeoff: Limitations of Log-Precision Transformers. TACL 11, arXiv:2207.00729
  6. William Merrill & Ashish Sabharwal (2024). The Expressive Power of Transformers with Chain of Thought. ICLR 2024, arXiv:2310.07923
  7. Nikunj Saunshi et al. (2025). Reasoning with Latent Thoughts: On the Power of Looped Transformers. arXiv:2502.17416
  8. William Merrill & Ashish Sabharwal (2025). A Little Depth Goes a Long Way: The Expressive Power of Log-Depth Transformers. arXiv:2503.03961
  9. Jonas Geiping et al. (2025). Scaling up Test-Time Compute with Latent Reasoning: A Recurrent Depth Approach (Huginn-0125). arXiv:2502.05171
  10. Thinking Machines Lab (2026). Introducing Inkling 2 Mini. thinkingmachines.ai/news/inkling-2-mini/
  11. NVIDIA Nemotron-Labs (2026). NVIDIA-Nemotron-Labs-Dory1.0-4B-BF16. Hugging Face
  12. Z.ai (2026). GLM-5.4: Toward Generalized Coding with Our First Loop Transformer. z.ai/blog/glm-5.4
  13. David Louapre. Looped Transformers (Interactive Exploration Space). huggingface.co/spaces/dlouapre/looped-transformers
Cite this note
@misc{liu2026whytransformerscan,
  title  = {{Why Transformers Can Loop: Orthogonal Residual Streams and Circuit Depth}},
  author = {Feiyang Liu},
  year   = {2026},
  month  = {nov},
  url    = {https://zaychikliu.github.io/blog/2026/why-transformers-can-loop/}
}