GLM 5.4's Loop Transformer: How Do 28 Layers of Weights Run Twice?

GLM 5.4's Loop Transformer makes the middle 28 layers share weights and run twice, with gated write-back and KV cache reuse.
GLM 5.4's Loop Transformer uses a "first 26 layers + middle 28 layers run twice + last 24 layers" architecture, totaling 106 layer traversals without adding weights. After each run of the middle layer group, a gating mechanism decides the write-back ratio: the difference between the candidate state and the pre-entry state is the modification, and the gate assigns each token a coefficient from 0 to 1—the first pass usually writes back lightly, the second opens wider. The KV cache can't simply be shared; the second pass inherits the distant KV selected in the first pass while retaining its own local window (128 tokens), and allocates attention weights uniformly within the same Softmax. The same position can retain two different representations because the two passes have different hidden states. This design balances parameter efficiency and context utilization.
GLM 5.4's Loop Transformer design is quite ingenious—it doesn't rerun the entire model, but instead makes the middle 28 layers share the same set of weights and run continuously twice. According to a Bilibili creator's breakdown in a video, what's added is computational depth, not another copy of layer weights. So how exactly does it loop? Can the KV cache be directly reused? Let's break it down step by step based on the manuscript.
Overall Architecture: First 26 Layers + Middle 28 Layers (Run Twice) + Last 24 Layers
GLM 5.4's forward pass isn't a simple stack of 106 layers. The top 26 layers run as usual to produce the initial hidden state X0. Then comes the middle shared layer group, running from layer 27 all the way to layer 54 to produce the first-pass candidate state. This candidate state isn't simply written back in full; it goes through a gating mechanism: the candidate state minus the state before entering this pass gives the amount to modify in this round, and the gate assigns each token a coefficient determining how much of it to add back. The closer the coefficient is to 0, the less is written; the closer to 1, the more fully the modification is written back.

According to the creator's observation, many positions write relatively lightly on the first pass, and the gate opens wider on the second pass. After the first write-back we get X1, which is sent back to layer 27 to let the same layer group run again; the second write-back produces X2, which then continues into the network from layer 55 onward. So in a single forward pass, the first 26 layers run once, the middle 28 layers run twice, and the last 24 layers run once, totaling 106 layer traversals.

Hidden States and Gated Write-Back
In a Transformer, each layer outputs a hidden state with the same shape as its input, which is essentially a vector representation of each token. Information is passed between layers through residual connections, so later layers can continue processing based on earlier representations. The key operation of the Loop Transformer is to have the same group of layers invoked multiple times, but after each invocation the original state isn't directly replaced—instead, gating decides "how much to write back." Specifically, the difference between the candidate state and the state before entering that pass represents the modification the layer group wants to apply in this round; the gate generates a coefficient between 0 and 1 for each token and adds the modification back to the original state proportionally. A coefficient near 0 means the token is barely updated this round, while near 1 means the modification is fully adopted. This mechanism lets the model adaptively control computational depth across different tokens, avoiding representation oscillation caused by repeated computation.
Can the KV Cache Be Shared? Breaking Down the Second Pass's Attention Mechanism
Layer weights can be reused twice—can the KV cache also use the same copy directly? The creator expands layer 27 to analyze the decode case processing the 4th token (T4). There's currently only one word, so the input and Q each have only one entry. The diagram draws K and V in their logical shapes within attention; the actual cache uses MLA-compressed vectors.
First pass: take the current word's vector from X0 and compute Q, K, V. Suppose the index selects T1 and T4 among the four already-visible positions, so the main attention reads only the KV of these two positions—hence two columns of K and two V's in the diagram. The attention weight bar is 1×2; after computing the weighted output, it goes through the FFN and continues through subsequent layers.

Second pass: the input has become the just-written-back X1. Although the projection weights are still the same set, the input has changed, so the current word's Q naturally changes too. Which KV should be read this time? Look at the purple part—the positions selected from the entire context in the first round are carried over, and those two selected KV are directly inherited, so the distant context already computed in the first pass can still be used in the second. Now look at the orange part—the second pass also retains its own local window, recomputed using the second pass's state. For simplicity the diagram only draws the two nearest KV, T3 and T4; the actual window size is 128 tokens.

There's a very noteworthy detail here: T4 appears once in purple and once in orange; although they correspond to the same position, they come from two different passes of hidden states, so the same position can retain two different representations. Finally, the second pass scores the two inherited keys together with the two keys in its own window using the new Q, normalizes all four scores in the same Softmax, and then weights and aggregates the four V's. The two paths aren't computed separately and then added; instead, they share weights within the same Softmax.
MLA Compression and KV Cache
MLA (Multi-head Latent Attention) is an attention variant that reduces KV cache usage through low-rank compression. Standard multi-head attention requires caching key (K) and value (V) vectors separately for each head, whereas MLA jointly projects K and V into a low-dimensional latent space, caches only the compressed latent vectors, and dynamically reconstructs each head's K, V when computing attention. This greatly reduces memory overhead in long-context scenarios, but it also means the cached representation isn't intuitively "one K/V per head" but rather a compressed latent vector. In the Loop Transformer's second pass, "inheriting distant KV" actually means inheriting these compressed latent vectors, so reuse is feasible, but they must participate in attention scoring together with the second pass's newly computed local-window KV after reconstruction.
Softmax Normalization and Multi-Path Attention Fusion
The Softmax function converts a set of real-valued scores into a probability distribution whose outputs sum to 1. In standard attention, the current query scores all visible keys, normalizes via Softmax to get weights, and then performs a weighted sum over values. When the second pass uses both inherited distant KV and self-computed local-window KV, the key design is: rather than performing attention on the two paths separately and then adding, all candidate keys' scores are normalized in the same Softmax. This means the inherited distant keys and the newly computed local keys compete for the same attention budget, and the model can dynamically decide whether to rely more on historical context or the recent window in this round. This unified normalization avoids weight inflation from simply stacking multiple attention paths and makes information fusion between the two passes smoother.
Two-Level View: Macro Sharing + Micro Reuse
GLM 5.4's loop can be viewed at two levels. Overall, the shared layer group runs twice through gated write-back, extending computational depth without adding parameters. At each layer specifically, the second pass reuses the distant KV selected in the first round and adds its own computed local window—both reusing context and allowing the second pass to reinterpret it based on the new hidden state. This design achieves a good balance between parameter efficiency and context utilization, and shows us the ingenuity of the Loop Transformer in engineering implementation.
One-line Summary
GLM 5.4's Loop Transformer makes the middle 28 layers share weights and run twice, with gated write-back and KV cache reuse.
Related articles

Zhipu GLM-5.4 Released: Loop Transformer Runs 28 Layers Twice
Zhipu releases GLM-5.4, the first Loop Transformer flagship: 28 layers run twice for 2-3T-equivalent intelligence, plus video and image input. API and open weights not yet released.

GLM-5.4 Coding Test 4.6x Slower? Unpacking the Loop Transformer Architecture
GLM-5.4 coding test took 431 minutes vs. competitor's 109, 4.6x more steps. We analyze its Loop Transformer architecture, KingBench4 score of 55.3, Max tier's 1.04M token cost, and tier selection advice.

Zhipu GLM-5.4 Quietly Released: A Debut for Loop Transformer Architecture
Zhipu GLM-5.4 quietly lands in coding plans: first Loop Transformer flagship with 750B params, 1M token context, AIM math up from 19.2 to 30.4, and native vision for coding from screenshots.