Rewirable Circuits in Transformers with Loom Attention

Media reports Hugging Face Papers Source language: Chinese

Researchers propose loom attention and LoomFormer to dynamically rewire Transformer circuits per token. Adding 2%–3.7% FLOPs, it matches baselines with over 3× compute.

Full text

Rewirable Circuits in Transformers with Loom Attention

Da Xiao Affiliation: Beijing University of Posts and Telecommunications, Beijing, China Correspondence to: xiaoda99@bupt.edu.cn Qingye Meng Affiliation: ColorfulClouds Technology Co., Ltd., Beijing, China Shengping Li Affiliation: ColorfulClouds Technology Co., Ltd., Beijing, China Xingyuan Yuan Affiliation: ColorfulClouds Technology Co., Ltd., Beijing, China

Abstract

We propose loom attention, which allows a Transformer to dynamically rewire its circuits at every token by interweaving a head’s vertical attention over the outputs of earlier heads (to obtain its query, key and value) with its horizontal attention over tokens. We instantiate vertical attention efficiently with normalized and gated linear attention over a per-token matrix memory, which stores the compressed head outputs and serves as a second residual stream. This yields LoomFormer, a dual-stream architecture that splits control and content between the vector and matrix streams. Across model sizes and training token budgets in language modeling and downstream tasks, LoomFormer matches Transformers trained with over 3× compute and recent residual-stream baselines (HC/mHC, MUDDFormer, AttnRes) trained with ~1.7–2.8×. For example, trained on the same 300B tokens with only 2% extra FLOPs, LoomPythia-2.8B achieves ~1.6× the average downstream gain of mHC-Pythia-2.8B over Pythia-2.8B, and matches (0-shot) or surpasses (5-shot) Pythia-12B.

Keywords: Multi-Head Attention, Transformer

1Beijing University of Posts and Telecommunications 2ColorfulClouds Technology

xiaoda99@bupt.edu.cn Code: https://github.com/Caiyun-AI/LoomFormer

\icml@noticeprintedtrue

1 Introduction

Figure 1: Conceptual sketch of loom attention. At each token, each head chooses which slots of that token’s matrix memory MM to read and write (i.e., vertical attention, every head does both; only one is shown per layer) and which tokens to attend to (horizontal attention), so a different circuit is woven for each token.

Figure 2: Downstream accuracy averaged over 10 tasks.

Transformers solve tasks by composing attention heads across layers into circuits (Elhage et al., 2020; Olsson et al., 2022; Wang et al., 2023; Merullo et al., 2024): an earlier head writes a feature into a subspace of the residual stream, and a later head reads from that subspace into its query, key or value. Induction heads, for example, read the output of a previous-token head through their keys (K-composition) (Olsson et al., 2022).

The composition between two heads, however, is determined by the product of their output and input projections, a virtual weight that does not change after training (Elhage et al., 2020). Attention makes the horizontal flow of information across tokens input-dependent: a head chooses which tokens to attend to. In contrast, the vertical flow across layers is fixed: a head cannot choose which heads to read from. A model that needs different circuits for different inputs therefore keeps all of them wired at once, lets them compete for the residual stream, and can gate them only indirectly, e.g., by sending a head’s attention to a sink token (Xiao et al., 2024b). We ask whether the wiring itself can be chosen per token, i.e., whether circuits can be made rewirable.

Recent work has enriched the vertical flow. Dense connections let a layer aggregate the outputs of all earlier layers (Pagliardini et al., 2024); MUDDFormer (Xiao et al., 2025) and Attention Residuals (AttnRes) (Kimi Team, 2026a) make this aggregation input-dependent, turning it into a depth-wise (i.e., vertical) attention (separate for the query, key, value and residual inputs in MUDDFormer); and Hyper-Connections (HC) (Zhu et al., 2025) and its manifold-constrained variant mHC (Xie et al., 2025) maintain several residual streams with learnable or dynamic mixing. In all of them, however, the vertical channel meets horizontal attention only through the hidden state of a layer, in which the outputs of all heads have already been summed and mixed together: a head can neither single out a particular earlier head nor be singled out by a later one. The vertical and horizontal flows run side by side, never woven together.

To make circuits rewirable, we propose loom attention, in which a head chooses which earlier heads to read from just as it chooses which tokens to attend to. By attending vertically over the outputs of these heads at the same token, the head obtains its query, key and value to perform the standard horizontal attention over tokens, and its output is in turn attended to vertically by later heads. Vertical and horizontal attention are thus interwoven, each feeding the other at every head, like the warp and weft of a loom (Figure 2).

Naive vertical attention over the outputs of all earlier heads is, however, expensive. We therefore instantiate it with normalized and gated linear attention over a per-token matrix memory, which stores the compressed head outputs and serves as a second residual stream. We call the resulting dual-stream architecture LoomFormer, after a drawloom---a loom in which a separate stored pattern selects which warp threads to lift at each pick, rather than repeating one fixed pattern. 11 1 The automated form of the drawloom, the Jacquard loom, encoded this pattern on punched cards and inspired early programmable computers such as Babbage’s Analytical Engine. Here the ordinary vector stream plays the pattern, carrying control—which memory slots each head writes and reads (weaving a different circuit per token) as well as which tokens to attend to—while the matrix stream carries the content moved across tokens. The two streams are coupled through attention outputs and FFN writes.

Extensive experiments on language modeling and downstream tasks across model sizes and training token budgets show that LoomFormer adds only 2.0–3.7% FLOPs while matching Transformers trained with over 3× compute and recent residual-stream baselines (HC/mHC, MUDDFormer, AttnRes) trained with ~1.7–2.8×. For example, trained on the same 300B tokens, LoomPythia-2.8B achieves ~1.6× the average downstream gain of mHC-Pythia-2.8B over Pythia-2.8B, and matches (0-shot) or surpasses (5-shot) Pythia-12B (Figure 2). We also analyze the trained 2.8B models to understand how and why LoomFormer works—e.g., by rewiring head outputs to inputs per token, it keeps over three times as many deep-layer heads off attention sinks as Pythia and mHC.

2 Rewirable Circuits

Writers, readers and circuits.

Attention heads communicate through the residual stream (Elhage et al., 2020). Consider a Transformer with LL layers of nn heads of dim dd, and model dim D=n​dD=nd. We call the output of a head a writer and each of its inputs (query, key and value) a reader, so a head has one writer and three readers. Writer 𝖶i\mathsf{W}_{i} adds its output oi∈ℝdo_{i}\in\mathbb{R}^{d} to the residual stream through its block WiO∈ℝd×DW^{O}_{i}\in\mathbb{R}^{d\times D} of the output projection, and reader 𝖱j\mathsf{R}_{j} of a later head reads the stream through its input projection Wjs∈ℝD×dW^{s}_{j}\in\mathbb{R}^{D\times d} (s∈{Q,K,V}s\in\{Q,K,V\}), so 𝖶i\mathsf{W}_{i} reaches 𝖱j\mathsf{R}_{j} through the virtual weight C¯i​j=WiO​Wjs∈ℝd×d\bar{C}_{ij}=W^{O}_{i}W^{s}_{j}\in\mathbb{R}^{d\times d}: the two communicate only if 𝖶i\mathsf{W}_{i} writes into a subspace that 𝖱j\mathsf{R}_{j} reads. Heads composed in this way form circuits whose nodes are the writers and readers.

A task may need different circuits at different tokens, i.e., different pairs of writers and readers to be connected (Figure 3). To describe how the wiring can change, we give it a dynamic part besides the static virtual weights: at a token, reader 𝖱j\mathsf{R}_{j} receives

uj=∑ioi​ci​j​C¯i​j,u_{j}=\textstyle\sum_{i}o_{i}\,c_{ij}\bar{C}_{ij}, (1)

where the per-token scalars ci​jc_{ij} form the wiring matrix C∈ℝw×rC\in\mathbb{R}^{w\times r} from the ww earlier writers to the rr readers of one layer, and ci​j​C¯i​jc_{ij}\bar{C}_{ij} is the effective wiring from 𝖶i\mathsf{W}_{i} to 𝖱j\mathsf{R}_{j}. The two are connected at the token if their effective wiring is nonzero.22 2 In trained models entries are rarely exactly zero; we count those below a threshold as zero (Section A.3). A wiring pattern P⊆[w]×[r]P\subseteq[w]\times[r] is a set of writer-reader pairs, realized when each reader in PP is connected to exactly its writers in PP.

Definition 2.1.

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

Figure 3: Effective wiring for three wiring patterns. Transformers (top two rows) can only gate whole heads, i.e., rows and columns, on and off, so they connect only rectangles within the fixed support of their virtual weights. LoomFormer (bottom row) connects exactly the required pairs.

Transformers are node-gated.

In a Transformer, pre-LN only rescales all writers of a token by a common factor (Lemma A.1 in Section A.1). Within a token, nodes can thus only be gated through whole heads: a head that attends to a sink token (Xiao et al., 2024b) outputs nearly nothing, turning off both its writer and, since what its readers receive has no effect, its readers.

Definition 2.2.

An architecture is node-gated if, at every token, its wiring matrix has the form

C=g​g′⁣⊤,C=g\,g^{\prime\top}, (2)

with gate vectors g∈ℝwg\in\mathbb{R}^{w} and g′∈ℝrg^{\prime}\in\mathbb{R}^{r} whose nonzero entries mark the writers and readers that are on.

We call the rank of CC the wiring rank; a node-gated architecture has wiring rank at most one, so existing connections can be gated on and off, but not rewired.

Proposition 2.3.

A Transformer with pre-LN is node-gated: at any token, it connects precisely the pairs with C¯i​j≠0\bar{C}_{ij}\neq 0 whose writer and reader are both on, a rectangle within the fixed support of the virtual weights.

Gating a writer or a reader acts on its whole row or column of CC, so a wiring pattern is realized only if it is such a rectangle, as in the first column of Figure 3; other patterns fail in one of two ways. With dense virtual weights, where heads share subspaces, every pair can be connected, but a non-rectangular pattern such as {𝖶1→𝖱1,𝖶3→𝖱3}\{\mathsf{W}_{1}\to\mathsf{R}_{1},\mathsf{W}_{3}\to\mathsf{R}_{3}\} (last column) also connects 𝖶1→𝖱3\mathsf{W}_{1}\to\mathsf{R}_{3} and 𝖶3→𝖱1\mathsf{W}_{3}\to\mathsf{R}_{1}: the two circuits interfere. With sparse virtual weights, where groups of heads use separate subspaces, circuits in different groups do not interfere, but pairs across groups such as 𝖶3→𝖱2\mathsf{W}_{3}\to\mathsf{R}_{2} (second column) are unreachable.

The price of rewiring by node gating.

Can a node-gated architecture overcome this by spending more heads? Suppose the role of each of r≥2r\geq 2 readers may be played by several heads, gated on and off per token, and at every token each reader must receive exactly one of ww writers, chosen independently of the other readers; the last pattern in Figure 3 is the smallest instance. Let N⁡(w,t)N(w,t) denote the minimum size of a ground set carrying a tt-cover-free family of ww sets, i.e., ww sets none of which is covered by the union of tt others (Kautz & Singleton, 1964).

Theorem 2.4.

To realize all such wiring patterns, a node-gated architecture needs at least N⁡(w,r−1)N(w,r-1) heads per reader, and this number suffices, where N⁡(w,1)≈log2⁡wN(w,1)\approx\log_{2}w, N⁡(w,t)=Ω⁡(t2​log⁡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}.

Intuitively, a head receives every writer that is on and has a nonzero virtual weight to it, so a reader must hold in reserve a head that sees its chosen writer and none of the up to r−1r-1 writers chosen by the other readers (Theorem A.2 in Section A.2; bounds in Section A.3). With the r=3​nr=3n readers of a layer choosing among its w=n​lw=nl earlier writers, r−1>wr-1>\sqrt{w} whenever l≤9​n−6l\leq 9n-6, so at practical sizes node gating needs Θ⁡(w)\Theta(w) heads per reader.

Naive loom attention makes Transformers rewirable.

To rewire with one head per reader, the wiring matrix must be chosen entry by entry at each token. The most direct way is to let every reader attend over the outputs of all earlier heads of the same token, with attention weights generated from the token itself:

uj=∑iαi​j​(x)​oi,α⋅j​(x)=softmax⁡(x​Wjα),u_{j}=\textstyle\sum_{i}\alpha_{ij}(x)\,o_{i},\qquad\alpha_{\cdot j}(x)=\operatorname{softmax}(xW^{\alpha}_{j}), (3)

where Wjα∈ℝD×n​lW^{\alpha}_{j}\in\mathbb{R}^{D\times nl} for a reader in layer ll, which has n​lnl earlier writers. We call this naive loom attention: its virtual weights are identities and all the wiring is left to C=[αi​j]C=[\alpha_{ij}], unlike a Transformer, which wires heads through fixed virtual weights and only gates them through a rank-one CC. With one head per reader, it is rewirable w.r.t. all wiring patterns (cf. Figure 3, bottom row), but expensive: over the 3​n3n readers of all LL layers, generating the attention weights takes about 32​n2​L2​D\frac{3}{2}n^{2}L^{2}D parameters and multiply-adds per token, growing quadratically in the total number of heads n​LnL and rivaling the whole model at practical sizes (Section A.4).

3 Method

Figure 4: LoomFormer architecture: the model (left), the LoomMHA and FFN sub-layers of a block (middle), and the MRead (MR) and MWrite (MW) modules (right). Vertical attention: reads from MM are in red and writes to MM in blue.
Figure 4: LoomFormer architecture: the model (left), the LoomMHA and FFN sub-layers of a block (middle), and the MRead (MR) and MWrite (MW) modules (right). Vertical attention: reads from MM are in red and writes to MM in blue.

Figure 4: LoomFormer architecture: the model (left), the LoomMHA and FFN sub-layers of a block (middle), and the MRead (MR) and MWrite (MW) modules (right). Vertical attention: reads from MM are in red and writes to MM in blue.

3.1 From Naive to Affordable Loom Attention

As linear attention compresses a growing key-value cache into a fixed-size state (Katharopoulos et al., 2020; Schlag et al., 2021), we compress the head outputs of each token along depth into a matrix memory M∈ℝdk×dM\in\mathbb{R}^{d_{k}\times d} of dkd_{k} slots of width dd. Writer 𝖶i\mathsf{W}_{i} writes its output to MM with a key kim∈ℝdkk^{m}_{i}\in\mathbb{R}^{d_{k}}, and reader 𝖱j\mathsf{R}_{j} reads with a query qjm∈ℝdkq^{m}_{j}\in\mathbb{R}^{d_{k}}, so a reader attends over dkd_{k} slots instead of all earlier writers, at a cost independent of depth. Since MM accumulates writes layer by layer, it is a second residual stream, the matrix stream, besides the original vector stream (Figure 4 left).

We call the interface through which a head reads from or writes to a stream a port. The two streams have dual ports:

vector stream: uj=xWsj,Δx=oiWOimatrix stream: uj=qmjM,ΔM=kmi⊗oi\begin{split}&\textrm{vector stream: }u_{j}=xW^{s}_{j},\quad\Delta x=o_{i}W^{O}_{i}\\ &\textrm{matrix stream: }u_{j}=q^{m}_{j}M,\quad\Delta M=k^{m}_{i}\otimes o_{i}\end{split} (4)

where kim⊗oi=(kim)⊤​oik^{m}_{i}\otimes o_{i}=(k^{m}_{i})^{\top}o_{i} is the outer product. Data and ports swap shapes: a port shrinks from a D×dD\times d matrix to a vector of length dkd_{k}, small enough to be generated per token from the vector stream, which we keep as the control path.

3.2 Architecture

LoomFormer keeps the backbone of the Transformer block and changes how sub-layers interface with the two streams: the vector stream carries control, i.e., where to attend and how to wire, and the matrix stream carries the content that is moved (Figure 4). We describe the architecture bottom up, from ports to sub-layers to the model (pseudocode in Appendix C), with xx denoting a sub-layer’s pre-LN input.

MRead and MWrite.

MRead and MWrite realize the matrix-stream ports of Eq. (4). MRead⁡(M,x,xr)\operatorname{MRead}(M,x,x^{r}) generates a query qm∈ℝdkq^{m}\in\mathbb{R}^{d_{k}} to read u∈ℝdu\in\mathbb{R}^{d} from MM, where xr=GELU⁡(x​W1R)∈ℝDrx^{r}=\operatorname{GELU}(xW^{R}_{1})\in\mathbb{R}^{D_{r}} with W1R∈ℝD×DrW^{R}_{1}\in\mathbb{R}^{D\times D_{r}} (Dr≪DD_{r}\ll D) is a low-rank feature shared by all MRead modules of a sub-layer to keep port generation cheap:

qm=Norm⁡(xr​W2R)⊙σ⁡(x​WgR)+q¯⊙σ⁡(x​Wg¯R)u=qm​M\begin{split}&q^{m}=\operatorname{Norm}(x^{r}W^{R}_{2})\odot\sigma(xW^{R}_{g})+\bar{q}\odot\sigma(xW^{R}_{\bar{g}})\\ &u=q^{m}M\end{split} (5)

Here xrx^{r} is projected by W2R∈ℝDr×dkW^{R}_{2}\in\mathbb{R}^{D_{r}\times d_{k}}, normalized with RMSNorm and gated to get a dynamic query, which is summed with a static query q¯\bar{q} (also gated) to obtain qm∈ℝdkq^{m}\in\mathbb{R}^{d_{k}}. The two scalar gates are computed from xx by projecting to scalars with WgR,Wg¯R∈ℝD×1W^{R}_{g},W^{R}_{\bar{g}}\in\mathbb{R}^{D\times 1} and then applying sigmoid. Similarly, with a shared xw=GELU⁡(x​W1W)∈ℝDrx^{w}=\operatorname{GELU}(xW^{W}_{1})\in\mathbb{R}^{D_{r}}, MWrite⁡(o,x,xw)\operatorname{MWrite}(o,x,x^{w}) generates a key km∈ℝdkk^{m}\in\mathbb{R}^{d_{k}} and writes a vector o∈ℝdo\in\mathbb{R}^{d} into MM as their outer product:

km=Norm⁡(xw​W2W)⊙σ⁡(x​WgW)Δ​M=km⊗o\begin{split}&k^{m}=\operatorname{Norm}(x^{w}W^{W}_{2})\odot\sigma(xW^{W}_{g})\\ &\Delta M=k^{m}\otimes o\end{split} (6)

Normalization and gating separate a port’s direction from its magnitude: the normalized vector selects which slots to read or write, and the gate alone sets how much.

LoomMHA.

To access the previous token’s memory without horizontal attention, MShift first mixes M[t]M_{[t]} with M[t−1]M_{[t-1]} via a per-slot gate z[t]=σ⁡(x[t]​WgS)∈ℝdkz_{[t]}=\sigma(x_{[t]}W^{S}_{g})\in\mathbb{R}^{d_{k}} (WgS∈ℝD×dkW^{S}_{g}\in\mathbb{R}^{D\times d_{k}}), and MNorm RMS-normalizes the matrix into M~[t]\widetilde{M}_{[t]}. Each head then reads four vectors from M~\widetilde{M} with separate MRead modules: query, key, value and output bypass omo^{m}:

M~[t]=MNorm⁡(z[t]⊙M[t−1]+(1−z[t])⊙M[t])q~,k~,v,om=MReadQ,K,V,O⁡(M~,x,xr)q,k=RoPE⁡(x​WQ,K)|(q~,k~)\begin{split}&\widetilde{M}_{[t]}=\operatorname{MNorm}\big(z_{[t]}\odot M_{[t-1]}+(1-z_{[t]})\odot M_{[t]}\big)\\ &\tilde{q},\tilde{k},v,o^{m}=\operatorname{MRead}^{Q,K,V,O}(\widetilde{M},x,x^{r})\\ &q,k=\operatorname{RoPE}(xW^{Q,K})\,\|\,(\tilde{q},\tilde{k})\end{split} (7)

where WQ,WK∈ℝD×d/4W^{Q},W^{K}\in\mathbb{R}^{D\times d/4} project xx to d/4d/4 dimensions, to which rotary position embedding (RoPE) (Su et al., 2024) is applied; the results are concatenated (∥\|) with q~\tilde{q} and k~\tilde{k} read from MM. The value is read from MM alone: the vector stream supplies only ports and RoPE queries/keys for slot and token routing, while all content moved by attention comes from MM. Unlike MHA, LoomMHA has two outputs: the projected head outputs for the vector stream, and the writes of all heads i∈{1,…,n}i\in\{1,\dots,n\} summed into Δ​M\Delta M:

oi=oim+Attention⁡(qi,ki,vi)Δ​M=∑i=1nMWritei⁡(Norm⁡(oi),x,xw)LoomMHA⁡(x,M)=(Concati=1n⁡[oi]​WO,Δ​M)\begin{split}&o_{i}=o^{m}_{i}+\operatorname{Attention}(q_{i},k_{i},v_{i})\\ &\Delta M=\textstyle\sum_{i=1}^{n}\operatorname{MWrite}_{i}(\operatorname{Norm}(o_{i}),x,x^{w})\\ &\operatorname{LoomMHA}(x,M)=\big(\operatorname{Concat}_{i=1}^{n}[o_{i}]W^{O},\,\Delta M\big)\end{split} (8)

where qi,ki,vi,oimq_{i},k_{i},v_{i},o^{m}_{i} are given by Eq. (7) for head ii, and Attention\operatorname{Attention} is scaled dot-product attention over tokens with a strictly causal mask that excludes the current token: a token’s own content reaches the head’s output through the bypass oimo^{m}_{i} instead. LoomMHA is where vertical and horizontal attention are interwoven: the vertical reads from MM feed the horizontal attention of head ii, and its output oio_{i} is written back to MM for later layers.

FFN with MWrite.

The feed-forward network (FFN) sub-layer reads only xx and, like LoomMHA, returns a pair (FFNx⁡(x),Δ​M)(\operatorname{FFN}_{x}(x),\Delta M), where FFNx\operatorname{FFN}_{x} is a standard FFN module. A second, smaller FFNM\operatorname{FFN}_{M} computes the values to write, which are split into nn head-sized vectors (spliti\operatorname{split}_{i}) and written to MM with the sub-layer’s MWrite modules. This write is the only channel through which content of the vector stream enters MM, since attention moves only content read from MM.

Δ​M=∑i=1nMWritei⁡(spliti⁡(FFNM⁡(x)),x,xw)\Delta M=\textstyle\sum_{i=1}^{n}\operatorname{MWrite}_{i}(\operatorname{split}_{i}(\operatorname{FFN}_{M}(x)),x,x^{w}) (9)

LoomFormer.

The embedding initializes both residual streams; M0M_{0} is written from the embedding x0x_{0} in the same way as the FFN writes:

x0=Embedding⁡(X)M0=∑i=1nMWritei⁡(spliti⁡(x0​WE),x0,x0w)\begin{split}&x_{0}=\operatorname{Embedding}(X)\\ &M_{0}=\textstyle\sum_{i=1}^{n}\operatorname{MWrite}_{i}(\operatorname{split}_{i}(x_{0}W^{E}),x_{0},x^{w}_{0})\end{split} (10)

where WE∈ℝD×DW^{E}\in\mathbb{R}^{D\times D} is a value projection and x0w=GELU⁡(x0​W1W)x^{w}_{0}=\operatorname{GELU}(x_{0}W^{W}_{1}). Each block ll then applies the two sub-layers with pre-LN, updating both streams residually (pairs are added element-wise):

(xl′,Ml′)=(xl,Ml)+LoomMHA⁡(LN1⁡(xl),Ml)(xl+1,Ml+1)=(xl′,Ml′)+FFN⁡(LN2⁡(xl′))\begin{split}&(x^{\prime}_{l},M^{\prime}_{l})=(x_{l},M_{l})+\operatorname{LoomMHA}(\operatorname{LN}_{1}(x_{l}),M_{l})\\ &(x_{l+1},M_{l+1})=(x^{\prime}_{l},M^{\prime}_{l})+\operatorname{FFN}(\operatorname{LN}_{2}(x^{\prime}_{l}))\end{split} (11)

for l=0,…,L−1l=0,\dots,L-1. Finally, xLx_{L} is normalized and projected to produce the final output. As MLM_{L} is not used, the last block does not write to MM. We typically set dk=2​nd_{k}=2n slots (so MM has size 2​D2D) and Dr=n​dk/2=n2D_{r}=nd_{k}/2=n^{2} (a 2×2\times compression bottleneck relative to n​dknd_{k}), matching the Transformer’s parameter budget by adjusting FFN widths at an approximately 2:12{:}1 ratio between FFNx\operatorname{FFN}_{x} and the FFNM\operatorname{FFN}_{M} write branch.

3.3 Rewiring Capacity

On the matrix stream without MShift, reader 𝖱j\mathsf{R}_{j} receives uj=qjm​M=∑i⟨qjm,kim⟩​oiu_{j}=q^{m}_{j}M=\sum_{i}\langle q^{m}_{j},k^{m}_{i}\rangle\,o_{i} by Eq. (4). The matrix stream thus keeps the division of roles of naive loom attention: its virtual weights are identities, and its wiring matrix is C=K​Q⊤C=KQ^{\top}, of rank at most dkd_{k}, where the rows of KK and QQ are the keys kimk^{m}_{i} of the writers and the queries qjmq^{m}_{j} of the readers.

Proposition 3.1.

(i) With ports fixed to (K,Q)(K,Q), LoomFormer is a standard Transformer TfmK,Q\operatorname{Tfm}_{K,Q} with residual stream (x,vec⁡(M))∈ℝD+dk​d(x,\operatorname{vec}(M))\in\mathbb{R}^{D+d_{k}d}, whose virtual weights absorb K​Q⊤KQ^{\top}, hence node-gated. (ii) With ports generated per token, LoomFormer is rewirable with one head per reader w.r.t. the wiring patterns with at most dkd_{k} writers, whatever the total number of writers ww.

In (ii), only the writers in the pattern are on, each taking a slot through a one-hot key while each reader’s query selects the slots of its writers (Section A.6). The N⁡(w,r−1)N(w,r-1) heads per reader that node gating needs (Theorem 2.4) are thus replaced by a per-token assignment of dkd_{k} slots; when more than dkd_{k} writers are on, slots are shared and writers interfere, as in linear attention. By (i), this rewiring capacity comes from generating ports per token, not from the matrix memory itself. Together, (i) and (ii) give a model-level view of LoomFormer’s expressiveness: within each token, LoomFormer wires heads as TfmK⁡(x),Q⁡(x)\operatorname{Tfm}_{K(x),Q(x)}, selecting per token one Transformer from the continuum {TfmK,Q}\{\operatorname{Tfm}_{K,Q}\} much as a mixture of experts selects experts (Fedus et al., 2022), except that the experts are wirings, share all other weights, and cost only dkd_{k} numbers per port to select.

Table 1: LoomFormer among residual-stream mechanisms. Ports kmk^{m}, qmq^{m} are indexed by heads 𝒊{\color[rgb]{0.8789,0,0}\boldsymbol{i}}, 𝒋{\color[rgb]{0.8789,0,0}\boldsymbol{j}} or, when shared across a layer, by layers li{\color[rgb]{0,0,0.8789}l_{i}}, lj{\color[rgb]{0,0,0.8789}l_{j}} (and Q/K/V input s{\color[rgb]{0.4961,0,1}s}), and marked (x)(x) or (M)(M) when computed from the vector stream or from the memory itself; softmaxli\operatorname{softmax}_{{\color[rgb]{0,0,0.8789}l_{i}}} normalizes over writer layers. Typical sizes assume dk=4d_{k}=4 in HC and dk=2​nd_{k}=2n in LoomFormer (+D{+}D for its vector stream).

Effective wiring Wiring rank Granularity Memory: slots ×\times width == typical size Residual stream

Transformer C¯i​j\bar{C}_{ij} 11 – 1×D1\times D={}={} DD vector

DenseFormer ⟨qljm,klim⟩​C¯i​j\langle q^{m}_{{\color[rgb]{0,0,0.8789}l_{j}}},k^{m}_{{\color[rgb]{0,0,0.8789}l_{i}}}\rangle\,\bar{C}_{ij} 11 layer l×Dl\times D={}={} l​DlD vector

HC / mHC ⟨qljm​(M),klim​(M)⟩​C¯i​j\langle q^{m}_{{\color[rgb]{0,0,0.8789}l_{j}}}(M),k^{m}_{{\color[rgb]{0,0,0.8789}l_{i}}}(M)\rangle\,\bar{C}_{ij} 11 layer dk×Dd_{k}\times D={}={} 4​D4D matrix

AttnRes (full) softmaxli⁡⟨qljm,klim​(M)⟩​C¯i​j\operatorname{softmax}_{{\color[rgb]{0,0,0.8789}l_{i}}}\langle q^{m}_{{\color[rgb]{0,0,0.8789}l_{j}}},k^{m}_{{\color[rgb]{0,0,0.8789}l_{i}}}(M)\rangle\,\bar{C}_{ij} 11 layer 2​l×D2l\times D={}={} 2​l​D2lD none

MUDDFormer ⟨qljm,s​(x),klim⟩​C¯i​j\langle q^{m,{\color[rgb]{0.4961,0,1}s}}_{{\color[rgb]{0,0,0.8789}l_{j}}}(x),k^{m}_{{\color[rgb]{0,0,0.8789}l_{i}}}\rangle\,\bar{C}_{ij} 33 layer ×\times QKV l×Dl\times D={}={} l​DlD vector

LoomFormer ⟨q𝒋m,s​(x),k𝒊m​(x)⟩​I\langle q^{m,{\color[rgb]{0.4961,0,1}s}}_{{\color[rgb]{0.8789,0,0}\boldsymbol{j}}}(x),k^{m}_{{\color[rgb]{0.8789,0,0}\boldsymbol{i}}}(x)\rangle\,I min⁡(dk,3​n)=2​n\min(d_{k},3n)=2n head ×\times QKV dk×dd_{k}\times d={}={} 2​D​(+D)2D\,({+}D) matrix + vector

4 Connections to Prior Work

Table 1 compares LoomFormer with residual-stream mechanisms (derivations in Section A.7; extended related work in Appendix B). In DenseFormer (Pagliardini et al., 2024), HC (Zhu et al., 2025), mHC (Xie et al., 2025), AttnRes (Kimi Team, 2026a) and MUDDFormer (Xiao et al., 2025), the ports are shared by all heads of a layer: apart from node gating and the fixed projections, their wiring depends on a writer only through its layer and on a reader only through its layer (and Q/K/V input in MUDDFormer). Their wiring rank is therefore 11, or 33 in MUDDFormer, one per Q/K/V input, whereas LoomFormer generates its ports per head and per token and reaches min⁡(dk,3​n)\min(d_{k},3n), i.e., 2​n2n by default, with a smaller state than HC (2​D+D2D+D against 4​D4D).

Linear attention.

Linear attention (Katharopoulos et al., 2020; Schlag et al., 2021) and its gated and delta-rule variants (Dao & Gu, 2024; Yang et al., 2024a; Yang et al., 2024b; Yang et al., 2025; Peng et al., 2024) also read and write a fixed-size matrix state per head with keys and queries from the vector stream, but differ in axis and sharing. Their state accumulates across tokens and its read is the head output, replacing horizontal attention, whereas MM accumulates along depth and its reads feed the query, key and value of horizontal attention. Their state is also private to one head across tokens, so heads still compose through the static ports of the vector stream, whereas MM is shared by all heads of all layers at a token, as a channel through which heads read each other. The two are thus orthogonal and can be combined.

5 Experiments

We train all models on the Pile dataset (Gao et al., 2020) in JAX, initializing dynamic port gates near zero so training starts smoothly from static ports (Section D.1). We evaluate scaling laws (Section 5.1), large-scale pretraining and downstream performance (Section 5.2), mechanisms of the trained models (Section 5.3), computational efficiency (Section 5.4) and ablations (Section 5.5).

5.1 Scaling Laws

Settings. We train three model scales—Medium (432​M432\textrm{M}), Large (778​M778\textrm{M}) and XL (1.4​B1.4\textrm{B})—with sequence length T=4096T=4096, batch size 0.5​M0.5\textrm{M} tokens (128128 sequences), and token budgets proportional to non-embedding parameters following Hoffmann et al. (2022) (architectural dimensions and optimizer hyperparameters in Section D.1).

Baselines. We compare LoomFormer against recent residual-stream baselines on the same Transformer++ backbone (Touvron et al., 2023) (RoPE (Su et al., 2024), SwiGLU FFN (Shazeer, 2020), RMSNorm, no linear bias): DHC (dynamic HC with expansion rate 44; Zhu et al., 2025), mHC (Xie et al., 2025), AttnRes (full) (Kimi Team, 2026a), and MUDDFormer (Xiao et al., 2025) (details in Section D.2).

Figure 5: Scaling curves on Pile validation loss against training compute (total parameters ×\times training tokens).

Results. Across the Medium, Large and XL scales (Figure 5), mHC, AttnRes and DHC improve over Transformer++, and MUDDFormer improves further, while LoomFormer achieves the lowest loss at every scale. Interpolating log-linearly along each baseline curve across Medium and Large, LoomFormer matches Transformer++ trained with 3.19–3.25× compute (3.10× net at Large after the 3.1% FLOP overhead in Table 3) and all four residual-stream baselines at 1.73–2.76×. At XL, where LoomFormer lies below the largest model of every baseline, it lowers validation loss from 2.1012.101 to 1.9791.979 (Δ​L=0.122\Delta L=0.122, 1.8× MUDDFormer’s 0.0680.068) at 2.8% extra FLOPs (Table 3).

Table 2: Zero/five-shot downstream evaluation results (Δ\Deltaacc is over Pythia-2.8B; best in bold within each block).

Model Pile ppl↓\downarrow FLAN ppl↓\downarrow LAM BADA PIQA Wino Grande ARC -E ARC -C SciQ BoolQ Hella Swag RACE -M RACE -H Avg acc↑\uparrow /Δ\Deltaacc

Pythia-2.8B 6.63 8.16 0-shot 64.7 73.9 59.4 64.4 29.5 88.2 64.5 45.4 38.1 34.9 56.3

Pythia-6.9B 6.29 7.85 67.3 75.2 60.9 67.3 31.3 89.7 63.7 48.0 40.6 37.0 58.1

mHC-Pythia-2.8B 6.24 7.35 68.5 74.4 62.8 67.1 30.7 90.5 66.9 47.5 42.3 37.0 58.8/+2.5

Pythia-12B 6.01 7.26 70.5 76.0 63.9 70.2 31.8 90.2 67.4 50.3 40.6 38.3 59.9

LoomPythia-2.8B 6.02 6.94 71.8 74.7 63.7 68.5 31.7 92.4 65.8 49.1 42.3 39.0 59.9/+3.6

Pythia-2.8B - - 5-shot 60.5 73.6 60.6 67.3 32.3 94.3 65.6 45.1 38.4 35.6 57.3

Pythia-6.9B - - 63.8 75.5 63.7 70.2 35.6 95.1 65.7 48.1 39.0 36.5 59.3

mHC-Pythia-2.8B - - 62.4 74.9 64.1 70.8 35.5 95.6 69.4 47.6 40.3 38.2 59.9/+2.6

Pythia-12B - - 67.3 76.0 64.2 71.0 36.5 95.3 68.0 50.3 40.1 38.8 60.8

LoomPythia-2.8B - - 69.0 75.7 65.7 70.8 35.1 96.0 71.2 49.5 43.1 40.1 61.6/+4.3

Figure 6: Mechanistic comparison of 2.8B models across layers l=1,…,32l=1,\dots,32 on Pile validation sequences (definitions in Appendix H). (a) Energy effective rank of the token-centered dynamic Q/K/V wiring matrix (band: 10th–90th percentiles) and per single Q, K or V input, vs. the bounds of Table 1. (b) Cross-token cosine distance (1−cos1-\cos) of read ports (q~,k~,v,om\tilde{q},\tilde{k},v,o^{m}) and write ports (kMHAm,kFFNmk^{m}_{\mathrm{MHA}},k^{m}_{\mathrm{FFN}}) in ℝdk\mathbb{R}^{d_{k}} (static/gated =0=0). (c) Percentage of heads that are on (non-sink; legend: 32-layer mean). (d) Within-head writer routing similarity across port pairs in layers 3–32.

5.2 Large-Scale Pretraining and Evaluation

Settings. To test whether LoomFormer’s gains persist under extended training, we compare against the Pythia suite (Biderman et al., 2023) on 300B Pile tokens (over 5× the Chinchilla budget of Section 5.1; Hoffmann et al., 2022). Specifically, we train LoomPythia-2.8B and mHC-Pythia-2.8B—the residual-stream baseline that also keeps a depth-independent state size (Table 1)---and compare them with Pythia 2.8B--12B. Except for using serial instead of parallel sub-layers33 3 mHC requires serial sub-layers by design, and parallel vs. serial FFN placement is quality-neutral for standard Transformers, as shown by Chowdhery et al. (2023) and our own experiments., both LoomPythia and mHC-Pythia use the same architecture choices and training hyperparameters as Pythia (Biderman et al., 2023, Appendix E). We evaluate zero- and five-shot accuracy across 10 standard benchmarks.

Results. As shown in Table 2 and Figure 2, LoomPythia-2.8B improves average accuracy over Pythia-2.8B by +3.6+3.6 in 0-shot and +4.3+4.3 in 5-shot (+3.95+3.95 on their mean), achieving 1.6× the gain of mHC-Pythia-2.8B (+2.5+2.5 / +2.6+2.6, mean +2.55+2.55). Its 0-shot average (59.959.9) matches Pythia-12B (59.959.9), its 5-shot average (61.661.6) exceeds Pythia-12B (60.860.8), and its 0/5-shot mean (60.7560.75) matches a log-linear extrapolation of Pythia at ~4.9× compute. On pretraining metrics, LoomPythia-2.8B reaches Pile ppl 6.026.02, on par with Pythia-12B (6.016.01) and well ahead of mHC-Pythia-2.8B (6.246.24).

As an out-of-domain evaluation, Table 2 also reports target-span perplexities on a randomly sampled subset of the FLAN Collection (Longpre et al., 2023), which features instruction following, chain-of-thought and in-context few-shot data. The advantage of LoomPythia on FLAN is even larger than on Pile: FLAN ppl 6.946.94 is best in the table, beating both Pythia-12B (7.267.26) and mHC-Pythia-2.8B (7.357.35). Whereas mHC’s gain barely changes from 0-shot to 5-shot (+2.5+2.5 vs. +2.6+2.6), LoomPythia’s widens from +3.6+3.6 to +4.3+4.3, indicating stronger in-context learning that Section 5.3 traces to per-token rewiring freeing deep heads from attention sinks.

5.3 Mechanistic Analysis

We analyze Pythia-2.8B, mHC-Pythia-2.8B and LoomPythia-2.8B (Section 5.2) on held-out Pile validation sequences; metric definitions and additional visualizations are in Appendix H.

Per-token circuit rewiring. After subtracting each pair’s token-mean static component, the dynamic wiring matrix on the 3​n=963n=96 Q/K/V readers of layer ll has rank 00 under fixed ports (Proposition 3.1(i)), at most 11 in Transformer and mHC, at most 33 in MUDDFormer, and up to min⁡(dk,3​n)=64\min(d_{k},3n)=64 in LoomFormer (Table 1). In Figure 6(a), LoomPythia-2.8B’s dynamic effective rank rises with depth to 41.041.0 (26.426.4 mean), placing 71.0%71.0\% of dynamic wiring energy (90.7%90.7\% at layer 32) outside rank 11, and averages 14.314.3 (20.920.9 at layer 32) within a single Q, K or V input where MUDDFormer also has rank ≤1\leq 1. Figure 6(b) directly tests Proposition 3.1(i) vs. (ii) in ℝdk\mathbb{R}^{d_{k}}: whereas static or scalar-gated ports have zero cross-token cosine distance, LoomPythia’s read ports q~,k~,v,om\tilde{q},\tilde{k},v,o^{m} average 0.330.33–0.460.46 (up to 0.680.68) and its attention and FFNM\operatorname{FFN}_{M} write ports average 0.360.36 and 0.560.56 (up to 0.750.75; Appendix H traces an in-context circuit rewiring across adjacent tokens).

Head utilization and within-head specialization. In a node-gated model (Section 2, Theorem 2.4), heads whose fixed inputs are not needed at a token turn themselves off by attending to sink tokens (Vig & Belinkov, 2019; Xiao et al., 2024b). Counting non-sink heads as on (Xiao et al., 2025), Figure 6(c) shows that in layers 17–32, only 22.9%22.9\% of heads are on in Pythia-2.8B and 17.7%17.7\% in mHC-Pythia-2.8B—whose four streams still merge into one shared input per layer—whereas LoomPythia-2.8B keeps 74.4%74.4\% on (3.3×3.3\times and 4.2×4.2\times), more than doubling content-token attention mass and accounting for its 5-shot and FLAN gains (Section 5.2). Finally, Figure 6(d) verifies head×QKV\text{head}\times\text{QKV} specialization (Table 1): although routing looks correlated at the layer level (0.920.92–0.960.96), per-writer correlation within the same head drops to 0.710.71–0.840.84 and top-32 writer overlap is 0.380.38–0.490.49, so over half of the top writers read by a head’s q~,k~,v,om\tilde{q},\tilde{k},v,o^{m} are distinct.

Table 3: Analytical computational overhead (+FLOPs+\textrm{FLOPs}) and measured relative throughput of LoomFormer vs. matched-size Transformer at sequence length TT (absolute speeds in Appendix F).

Model +FLOPs+\textrm{FLOPs} (%) Train (%) v5p Prefill (%) bs 11 / 88 Decode (%) bs 11 / 1616 / 6464

Medium 3.703.70 90.590.5 87.487.4 / 90.490.4 91.791.7 / 89.289.2 / 92.392.3

Large 3.133.13 89.289.2 85.385.3 / 86.286.2 85.485.4 / 87.887.8 / 84.984.9

XL 2.772.77 92.392.3 83.883.8 / 85.385.3 86.986.9 / 84.684.6 / 86.686.6

2.8B 1.951.95 92.492.4 87.587.5 / 90.590.5 86.486.4 / 87.687.6 / 88.988.9

5.4 Computational Complexity and Efficiency

Analytical computational overhead. Under a matched parameter budget, LoomFormer adds ~1d⁡(1+ρ/12)+18​(1+12/ρ)\frac{1}{d(1+\rho/12)}+\frac{1}{8(1+12/\rho)} relative forward FLOPs from reading/writing MM and wider Q/K (dk=2​nd_{k}=2n, ρ=T/D\rho=T/D; derived in Appendix E), totaling 1.95%1.95\%–3.70%3.70\% across our four scales (Table 3).

Wall-clock training and inference throughput. Table 3 reports relative throughput for JAX training on TPU v5p pods (with fused Pallas kernels) and BF16 PyTorch inference on an NVIDIA A800 GPU (with a fused Triton kernel and torch.compile; Appendix F). LoomFormer sustains 89%89\%–92%92\% of Transformer training speed and 84%84\%–92%92\% of inference speed across prefill and decode batch sizes; the gap beyond the 2.0%2.0\%–3.7%3.7\% FLOPs reflects HBM I/O on MM, lower arithmetic intensity of per-token MM contractions, and smaller split projections and norms, and is far outweighed by the over 3× compute savings (Sections 5.1 and 5.2).

5.5 Ablation Studies

We ablate LoomFormer at the Medium scale (432M, 7B tokens; Section 5.1), reallocating saved parameters to FFNx\operatorname{FFN}_{x} to match the parameter budget, and report Pile validation loss and the retained gain Gain=(LTfm++−L)/(LTfm++−LLoom)\textrm{Gain}=(L_{\textrm{Tfm++}}-L)/(L_{\textrm{Tfm++}}-L_{\textrm{Loom}}) (Table 4; full suite in Appendix G).

Table 4: Ablations of LoomFormer at the Medium scale.

Variant Loss↓\downarrow Δ\DeltaLoss Gain

LoomFormer (full) 2.266 — 100.0%

Transformer++ (baseline) 2.443 ++0.177 0.0%

Dynamic rewiring (Proposition 3.1)

Static write keys (kmk^{m}) 2.283 ++0.017 90.3%

Static read queries (qmq^{m}) 2.310 ++0.043 75.4%

Static read & write (qm,kmq^{m},k^{m}) 2.344 ++0.078 56.0%

Port normalization & gating (Eqs. 5–6)

−- dynamic port gates 2.277 ++0.011 93.7%

−- dynamic port Norm\operatorname{Norm} 2.289 ++0.023 87.1%

−- dynamic port gates & Norm\operatorname{Norm} 2.283 ++0.017 90.3%

Sub-layer components (Section 3.2)

−- FFNM\operatorname{FFN}_{M} write 2.300 ++0.034 80.7%

−- output bypass omo^{m} 2.299 ++0.032 81.8%

−- vector-stream RoPE WQ,KW^{Q,K} 2.286 ++0.020 88.7%

−- MShift\operatorname{MShift} 2.277 ++0.010 94.2%

Dynamic rewiring. Replacing dynamic kmk^{m}, dynamic qmq^{m}, or both with learned static vectors (dropping their gates) tests Proposition 3.1. Fixing both reduces LoomFormer to TfmK,Q\operatorname{Tfm}_{K,Q} and cuts the gain to 56.0%—still ahead of layer-wise dynamic baselines (Figure 5)—so the static head-level dual-stream backbone and per-token rewiring contribute roughly equal halves. Fixing qmq^{m} hurts far more than fixing kmk^{m} (75.4% vs. 90.3% retained): writers can use stable slots if readers select among them per token.

Port normalization and gating. Removing either the dynamic-branch gates or port RMSNorm hurts, yet removing both beats removing RMSNorm alone (90.3% vs. 87.1%): an unnormalized projection already varies in norm, making a separate gate redundant. Pairing RMSNorm with gating works best by separating slot direction from read/write magnitude.

Figure 7: Validation loss vs. per-token memory size and matrix slots dkd_{k} at the Medium scale (L=18L=18).

Sub-layer components. Removing FFNM\operatorname{FFN}_{M} (Eq. 9) hurts most, as the sole post-embedding path from the vector stream into MM, followed closely by removing the bypass omo^{m} and restoring self-attention to the current token (Eq. 8). Applying RoPE to d/4d/4 of the MM-read coordinates instead of x​WQ,KxW^{Q,K} (Eq. 7) also hurts, supporting separate streams for positions and content, whereas removing MShift\operatorname{MShift} still retains 94.2% of the gain.

Memory capacity. As we vary the slot count dkd_{k} (Figure 7), validation loss drops sharply (−0.026-0.026) from dk=16d_{k}=16 to 3232 with diminishing returns beyond, placing our default dk=2​nd_{k}=2n at the knee given the linear growth o

Images from the source

Figure 8: Matrix-stream routing in LoomPythia-2.8B across Pile validation sequences. (a) Share of positive attribution from Embedding, LoomMHA and FFNM\operatorname{FFN}_{M} writes, and share of |ai​j||a_{ij}| from previous-token MShift (M[t−1]M_{[t-1]}). (b) Column-max normalized positive attribution from each writer source (vertical, bottom to top) to each reader layer ll (horizontal; gray upper triangle: causally unreachable).
Figure 8: Matrix-stream routing in LoomPythia-2.8B across Pile validation sequences. (a) Share of positive attribution from Embedding, LoomMHA and FFNM\operatorname{FFN}_{M} writes, and share of |ai​j||a_{ij}| from previous-token MShift (M[t−1]M_{[t-1]}). (b) Column-max normalized positive attribution from each writer source (vertical, bottom to top) to each reader layer ll (horizontal; gray upper triangle: causally unreachable).
Figure 9: Attribution submatrix [ai​j][a_{ij}] (rows: top-18 earlier head writers Lll.Hhh; columns: top-6 layer-19 MRead outputs per arm q~,k~,v,om\tilde{q},\tilde{k},v,o^{m}) of LoomPythia-2.8B at two consecutive tokens of an in-context prompt (next-token top-1: " Berlin", p=0.65p=0.65), showing non-rectangular within-token wiring at "Germany" (a) and per-token rewiring onto causal q~\tilde{q} and omo^{m} pathways at ":" (b; hatched k~,v\tilde{k},v columns affect only future tokens under s<ts<t
Figure 9: Attribution submatrix [ai​j][a_{ij}] (rows: top-18 earlier head writers Lll.Hhh; columns: top-6 layer-19 MRead outputs per arm q~,k~,v,om\tilde{q},\tilde{k},v,o^{m}) of LoomPythia-2.8B at two consecutive tokens of an in-context prompt (next-token top-1: " Berlin", p=0.65p=0.65), showing non-rectangular within-token wiring at "Germany" (a) and per-token rewiring onto causal q~\tilde{q} and omo^{m} pathways at ":" (b; hatched k~,v\tilde{k},v columns affect only future tokens under s<ts<t

Sources

← Back to Daily AI Report