# Rewirable Circuits in Transformers with Loom Attention

> LoomFormer's loom attention interweaves vertical and horizontal attention over a per-token matrix memory, making transformer circuits rewirable per token and matching models trained with over 3x more compute.

- **Source:** [arXiv](https://arxiv.org/abs/2611.30634)
- **Published:** 2026-11-28
- **Permalink:** https://picx.dev/p/o8Eo0V
- **Whiteboard:** https://picx.dev/p/o8Eo0V/image

## Summary

# 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* $W_i$; its query, key, and value are *readers* $R_j$. Writer $W_i$ reaches reader $R_j$ through the virtual weight $\bar{C}_{ij} = W_i^O W_j^s \in \mathbb{R}^{d \times d}$.

**Dynamic wiring matrix:** At a token, reader $R_j$ receives:

$$u_j = \sum_i o_i c_{ij} \bar{C}_{ij}$$

where per-token scalars $c_{ij}$ form the wiring matrix $C \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 $C$ that it admits at a token.

> **Definition 2.2.** An architecture is *node-gated* if its wiring matrix has the form $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)$ heads per reader, where $N(w, 1) \approx \log_2 w$, $N(w, t) = \Omega(t^2 \log w / \log t)$ for $2 \leq t \leq \sqrt{w}$, and $N(w, t) = \Theta(w)$ for $t > \sqrt{w}$. At practical sizes, node gating needs $\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:

$$u_j = \sum_i \alpha_{ij}(x) o_i, \quad \alpha_{\cdot j}(x) = \text{softmax}(x W_j^{\alpha})$$

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

---

## Methodology

### From Naive to Affordable Loom Attention

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

**Dual ports (Eq. 4):**

$$\text{vector stream: } u_j = x W_j^s, \quad \Delta x = o_i W_i^O$$
$$\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 $q^m \in \mathbb{R}^{d_k}$ to read from $M$:

$$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 = q^m M$$

**MWrite module (Eq. 6):** Generates key $k^m$ and writes output $o$ as outer product:

$$k^m = \text{Norm}(x_w W_2^W) \odot \sigma(x W_g^W)$$
$$\Delta M = k^m \otimes o$$

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

$$\tilde{q}, \tilde{k}, v, o^m = \text{MRead}^{Q,K,V,O}(\tilde{M}, x, x_r)$$
$$q, k = \text{RoPE}(x W^{Q,K}) \| (\tilde{q}, \tilde{k})$$
$$o_i = o_i^m + \text{Attention}(q_i, k_i, v_i)$$
$$\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 $M$:

$$\Delta M = \sum_{i=1}^{n} \text{MWrite}_i(\text{split}_i(\text{FFN}_M(x)), x, x_w)$$

**Residual updates (Eq. 11):**

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

**Default settings:** $d_k = 2n$ slots (memory size $2D$), $D_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 $d_k$ writers, replacing the $N(w, r-1)$ heads per reader needed by node gating.

The wiring matrix is $C = KQ^{\top}$ with rank at most $d_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):**

| Model | Pile ppl ↓ | FLAN ppl ↓ | 0-shot Avg | 5-shot Avg | Δ acc (mean) |
|---|---|---|---|---|---|
| Pythia-2.8B | 6.63 | 8.16 | 56.3 | 57.3 | — |
| Pythia-6.9B | 6.29 | 7.85 | 58.1 | 59.3 | — |
| mHC-Pythia-2.8B | 6.24 | 7.35 | 58.8 | 59.9 | +2.55 |
| Pythia-12B | 6.01 | 7.26 | 59.9 | 60.8 | — |
| **LoomPythia-2.8B** | **6.02** | **6.94** | **59.9** | **61.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) |
|---|---|---|---|---|
| Medium | 3.70 | 90.5 | 87.4/90.4 | 91.7/89.2/92.3 |
| Large | 3.13 | 89.2 | 85.3/86.2 | 85.4/87.8/84.9 |
| XL | 2.77 | 92.3 | 83.8/85.3 | 86.9/84.6/86.6 |
| 2.8B | 1.95 | 92.4 | 87.5/90.5 | 86.4/87.6/88.9 |

### Ablation Studies (Section 5.5)

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

| Ablation | Loss | Retained Gain |
|---|---|---|
| Full LoomFormer | 2.266 | 100% |
| Static read & write (qᵐ, kᵐ) | 2.344 | 56.0% |
| Static read queries only | 2.310 | 75.4% |
| Static write keys only | 2.283 | 90.3% |
| Remove FFN_M write | 2.300 | 80.7% |
| Remove output bypass oᵐ | 2.299 | 81.8% |
| Remove MShift | 2.277 | 94.2% |

**Memory capacity:** Default $d_k = 2n$ sits at the knee of the loss-vs-slots curve; even $d_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 $\Theta(w)$ heads with $d_k$ memory slots.
3. **Unified view of prior work:** Table 1 positions LoomFormer among residual-stream mechanisms, showing its wiring rank of $\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.

---

_Markdown view of https://picx.dev/p/o8Eo0V, served by PicX — AI-generated visual whiteboard summaries of research papers._
