Full text not available for this paper

Rewirable Circuits in Transformers with Loom Attention

Authors: Da Xiao, Qingye Meng, Shengping Li, Xingyuan Yuan
Affiliations: Beijing University of Posts and Telecommunications, ColorfulClouds Technology


Summary (Overview)

  • Novel architecture (LoomFormer): Proposes loom attention, which interweaves vertical attention over earlier heads' outputs (to obtain query, key, and value) with standard horizontal attention over tokens, enabling per-token circuit rewiring.
  • Dual-stream design: Introduces a per-token matrix memory serving as a second residual stream (matrix stream) alongside the vector stream, splitting control (vector) from content (matrix).
  • Efficiency: Adds only 2.0–3.7% extra FLOPs while matching Transformers trained with >3× compute and recent residual-stream baselines (HC/mHC, MUDDFormer, AttnRes) trained with ~1.7–2.8× compute.
  • Key empirical result: LoomPythia-2.8B trained on 300B tokens achieves ~1.6× the downstream gain of mHC-Pythia-2.8B over Pythia-2.8B, matching (0-shot) or surpassing (5-shot) Pythia-12B.
  • Mechanistic insights: Trained LoomFormer keeps 3.3–4.2× more deep-layer heads off attention sinks compared to Pythia and mHC, with dynamic wiring rank up to 64 vs. rank ≤1 for baselines.

Introduction and Theoretical Foundation

Background and Motivation

Transformers solve tasks by composing attention heads into circuits (Elhage et al., 2020; Olsson et al., 2022): an earlier head writes a feature into the residual stream, and a later head reads it. However, the composition between two heads is fixed after training—determined by the product of their output and input projections (a virtual weight). While horizontal attention makes token-to-token flow input-dependent, the vertical flow across layers is static: a head cannot choose which heads to read from.

The paper asks: Can circuits be made rewirable per token?

Theoretical Framework

Writers, readers, and circuits: A head's output is a writer WiW_i; its query, key, and value are readers RjR_j. Writer WiW_i reaches reader RjR_j through the virtual weight Cˉij=WiOWjs∈Rd×d\bar{C}_{ij} = W_i^O W_j^s \in \mathbb{R}^{d \times d}.

Dynamic wiring matrix: At a token, reader RjR_j receives:

uj=∑ioicijCˉiju_j = \sum_i o_i c_{ij} \bar{C}_{ij}

where per-token scalars cijc_{ij} form the wiring matrix C∈Rw×rC \in \mathbb{R}^{w \times r}.

Key definitions:

Definition 2.1. An architecture with fixed virtual weights is rewirable w.r.t. a set of wiring patterns if every pattern is realized by some wiring matrix CC that it admits at a token.

Definition 2.2. An architecture is node-gated if its wiring matrix has the form C=g g′⊤C = g\,g'^{\top} (rank ≤ 1), with gate vectors marking which writers/readers are on.

Theorem 2.4 (node-gating cost): To realize all independent wiring patterns, a node-gated architecture needs at least N(w,r−1)N(w, r-1) heads per reader, where N(w,1)≈log⁡2wN(w, 1) \approx \log_2 w, N(w,t)=Ω(t2log⁡w/log⁡t)N(w, t) = \Omega(t^2 \log w / \log t) for 2≤t≤w2 \leq t \leq \sqrt{w}, and N(w,t)=Θ(w)N(w, t) = \Theta(w) for t>wt > \sqrt{w}. At practical sizes, node gating needs Θ(w)\Theta(w) heads per reader.

Naive loom attention: The direct solution—letting every reader attend over all earlier heads' outputs with per-token attention weights:

uj=∑iαij(x)oi,α⋅j(x)=softmax(xWjα)u_j = \sum_i \alpha_{ij}(x) o_i, \quad \alpha_{\cdot j}(x) = \text{softmax}(x W_j^{\alpha})

—is rewirable but expensive: 32n2L2D\frac{3}{2} n^2 L^2 D parameters, growing quadratically in total heads nLnL.


Methodology

From Naive to Affordable Loom Attention

The key insight: compress head outputs along depth into a matrix memory M∈Rdk×dM \in \mathbb{R}^{d_k \times d} with dkd_k slots of width dd, analogous to how linear attention compresses a KV cache.

Dual ports (Eq. 4):

vector stream: uj=xWjs,Δx=oiWiO\text{vector stream: } u_j = x W_j^s, \quad \Delta x = o_i W_i^O matrix stream: uj=qjmM,ΔM=kim⊗oi\text{matrix stream: } u_j = q_j^m M, \quad \Delta M = k_i^m \otimes o_i

Architecture Components

MRead module (Eq. 5): Generates query qm∈Rdkq^m \in \mathbb{R}^{d_k} to read from MM:

qm=Norm(xrW2R)⊙σ(xWgR)+qˉ⊙σ(xWgˉR)q^m = \text{Norm}(x_r W_2^R) \odot \sigma(x W_g^R) + \bar{q} \odot \sigma(x W_{\bar{g}}^R) u=qmMu = q^m M

MWrite module (Eq. 6): Generates key kmk^m and writes output oo as outer product:

km=Norm(xwW2W)⊙σ(xWgW)k^m = \text{Norm}(x_w W_2^W) \odot \sigma(x W_g^W) ΔM=km⊗o\Delta M = k^m \otimes o

LoomMHA (Eq. 7–8): Each head reads four vectors from memory (query, key, value, output bypass omo^m), applies RoPE to a d/4d/4-dimensional projection, and writes outputs back:

q~,k~,v,om=MReadQ,K,V,O(M~,x,xr)\tilde{q}, \tilde{k}, v, o^m = \text{MRead}^{Q,K,V,O}(\tilde{M}, x, x_r) q,k=RoPE(xWQ,K)∥(q~,k~)q, k = \text{RoPE}(x W^{Q,K}) \| (\tilde{q}, \tilde{k}) oi=oim+Attention(qi,ki,vi)o_i = o_i^m + \text{Attention}(q_i, k_i, v_i) ΔM=∑i=1nMWritei(Norm(oi),x,xw)\Delta M = \sum_{i=1}^{n} \text{MWrite}_i(\text{Norm}(o_i), x, x_w)

FFN with MWrite (Eq. 9): A second, smaller FFN writes content into MM:

ΔM=∑i=1nMWritei(spliti(FFNM(x)),x,xw)\Delta M = \sum_{i=1}^{n} \text{MWrite}_i(\text{split}_i(\text{FFN}_M(x)), x, x_w)

Residual updates (Eq. 11):

(xl′,Ml′)=(xl,Ml)+LoomMHA(LN1(xl),Ml)(x'_l, M'_l) = (x_l, M_l) + \text{LoomMHA}(\text{LN}_1(x_l), M_l) (xl+1,Ml+1)=(xl′,Ml′)+FFN(LN2(xl′))(x_{l+1}, M_{l+1}) = (x'_l, M'_l) + \text{FFN}(\text{LN}_2(x'_l))

Default settings: dk=2nd_k = 2n slots (memory size 2D2D), Dr=ndk/2=n2D_r = nd_k/2 = n^2 (2× compression bottleneck).

Rewiring Capacity (Proposition 3.1)

  • (i) With fixed ports, LoomFormer reduces to a standard Transformer (node-gated).
  • (ii) With per-token generated ports, LoomFormer is rewirable with one head per reader for patterns with at most dkd_k writers, replacing the N(w,r−1)N(w, r-1) heads per reader needed by node gating.

The wiring matrix is C=KQ⊤C = KQ^{\top} with rank at most dkd_k, versus rank 1 for Transformers/HC/mHC and rank 3 for MUDDFormer.


Empirical Validation / Results

Scaling Laws (Section 5.1)

Across Medium (432M), Large (778M), and XL (1.4B) scales:

  • LoomFormer achieves the lowest validation loss at every scale.
  • Matches Transformer++ trained with 3.19–3.25× compute (3.10× net after FLOP overhead).
  • Matches residual-stream baselines at 1.73–2.76× compute.
  • At XL: loss reduced from 2.101 to 1.979 (ΔL = 0.122, 1.8× MUDDFormer's gain of 0.068) at 2.8% extra FLOPs.

Large-Scale Pretraining (300B tokens, Section 5.2)

Table 2 (key results):

ModelPile ppl ↓FLAN ppl ↓0-shot Avg5-shot AvgΔ acc (mean)
Pythia-2.8B6.638.1656.357.3—
Pythia-6.9B6.297.8558.159.3—
mHC-Pythia-2.8B6.247.3558.859.9+2.55
Pythia-12B6.017.2659.960.8—
LoomPythia-2.8B6.026.9459.961.6+3.95

LoomPythia-2.8B:

  • 1.6× the average downstream gain of mHC-Pythia-2.8B.
  • Matches Pythia-12B in 0-shot, surpasses in 5-shot.
  • Best FLAN perplexity (6.94), beating Pythia-12B (7.26) and mHC (7.35).
  • 0/5-shot mean matches ~4.9× compute extrapolation of Pythia.

Mechanistic Analysis (Section 5.3)

  • Dynamic wiring rank: LoomPythia reaches effective rank 41.0 at layer 32 (mean 26.4), with 71.0% of dynamic wiring energy outside rank 1.
  • Port rewiring: Cross-token cosine distance of ports averages 0.33–0.46 (up to 0.68), vs. zero for static/gated baselines.
  • Head utilization: In layers 17–32, LoomPythia keeps 74.4% of heads on vs. 22.9% (Pythia) and 17.7% (mHC)—a 3.3× and 4.2× improvement.
  • Within-head specialization: Per-writer correlation within the same head drops to 0.71–0.84, with top-32 writer overlap of only 0.38–0.49.

Computational Efficiency (Section 5.4)

Model+FLOPs (%)Train (% v5p)Prefill (% bs1/8)Decode (% bs1/16/64)
Medium3.7090.587.4/90.491.7/89.2/92.3
Large3.1389.285.3/86.285.4/87.8/84.9
XL2.7792.383.8/85.386.9/84.6/86.6
2.8B1.9592.487.5/90.586.4/87.6/88.9

Ablation Studies (Section 5.5)

Key findings (Medium scale, gain relative to full LoomFormer):

AblationLossRetained Gain
Full LoomFormer2.266100%
Static read & write (qᵐ, kᵐ)2.34456.0%
Static read queries only2.31075.4%
Static write keys only2.28390.3%
Remove FFN_M write2.30080.7%
Remove output bypass oᵐ2.29981.8%
Remove MShift2.27794.2%

Memory capacity: Default dk=2nd_k = 2n sits at the knee of the loss-vs-slots curve; even dk=16d_k = 16 (2D state) outperforms DHC (4D) and MUDDFormer (9D average).


Theoretical and Practical Implications

Theoretical Contributions

  1. Formal framework for circuit rewiring: Introduces the wiring matrix formalism and distinguishes node-gated (rank ≤ 1) from rewirable architectures, with a rigorous lower bound (Theorem 2.4) on the head cost of node gating.
  2. Dual-stream architecture principle: Shows that splitting control (vector stream) from content (matrix stream) enables per-token circuit selection at one head per reader, replacing Θ(w)\Theta(w) heads with dkd_k memory slots.
  3. Unified view of prior work: Table 1 positions LoomFormer among residual-stream mechanisms, showing its wiring rank of min⁡(dk,3n)=2n\min(d_k, 3n) = 2n vs. rank 1 (Transformer, DenseFormer, HC/mHC, AttnRes) and rank 3 (MUDDFormer).

Practical Implications

  • Compute efficiency: Matches Transformers trained with >3× compute at only 2–3.7% extra FLOPs—a substantial cost reduction for large-scale pretraining.
  • In-context learning: The widening 0-shot → 5-shot gain (+3.6 → +4.3) indicates stronger in-context learning, traced to per-token rewiring freeing deep heads from attention sinks.
  • Architecture design: The head-level granularity of rewiring uses memory far more effectively than layer-wise mechanisms, suggesting a new design axis for foundation models.

Conclusion

Main takeaways:

  • Standard Transformers and layer-wise residual-stream mechanisms are restricted to low-rank (node) gating, requiring many heads per reader to avoid circuit interference.
  • Loom attention interweaves vertical linear attention over a per-token matrix memory with horizontal attention, making circuits rewirable per token at one head per reader.
  • LoomFormer matches Transformers trained with >3× compute and recent baselines with ~1.7–2.8× compute, while keeping >75% of attention heads active and dynamically rewiring ports.

Future directions:

  • Combining loom attention with linear or sparse attention along the sequence axis (the matrix memory operates along depth at each token, making the two orthogonal).
  • The Jacquard loom analogy suggests potential connections to programmable computation and mixture-of-experts-style routing, where "experts" are wirings sharing all other weights.

Related papers