Unlocking the Power of Loops in Pretrained LLMs with Loop Residuals

Xuanlei Zhao1,  Hailun Xu2,  Kai Wang1,  Yang Luo1,  Yingxuan Ren1,  Zirui Zhu1,  Yuxuan Lou1,  Kun Xu2,  Yan Wang2,  Kanchan Sarkar2,  Yang You1
1National University of Singapore   2Independent Researcher
+29.1%over SFT on Qwen3-1.7B-Base at 2× compute
2.1×gain over SOTA at 2× compute
6pretrained LLMs from 3 model families and 9 benchmarks
+4.8%training step time, with +0.7% peak activation memory
Bar and line charts showing LoopRes above SFT and eight looped baselines across compute budgets and backbones.
Figure 1. Average scores across compute budgets and backbones. Scores average seven math and code benchmarks in (a,b) and five in (c). SFT is the same model finetuned without looping.

Abstract

Looped Transformers iterate a shared block of layers to increase computation without adding parameters, offering stronger performance and test-time scaling. However, this power has so far required training from scratch and yielded only modest gains for LLMs. Finetuning existing models into loops is far cheaper, yet also brings only marginal gains over SFT. We propose LoopRes, an effective, scalable, and simple approach that turns loops into significant and consistent gains for pretrained LLMs. Building on Attention Residuals, LoopRes lets each looped layer attend directly to the outputs of earlier loops. Since these outputs are iterates of the same layers and become harder to tell apart, we give each a trainable loop-aware key that encodes its structural identity. Attending to all earlier loops also lets layers skip intermediate ones, so we restrict each layer to a residual window of the latest W loops, forcing every loop to build incrementally on recent ones. Across 6 pretrained LLMs from 3 model families and 9 benchmarks, LoopRes delivers significant and consistent gains: at 2× compute, it improves Qwen3-1.7B-Base by 29.1% over SFT, more than doubling the gain of the state-of-the-art baseline. The gains hold on larger models up to 30B and across all three families. More importantly, from 1.7B to 8B, LoopRes scales more steeply than SFT as compute grows, reaching a higher ceiling while needing less compute on long outputs. These results establish LoopRes as an effective looping approach, delivering both better performance and better test-time scaling.

Challenges of Looping with Attention Residuals

AttnRes assumes sources that differ in content and are fixed in number. In loops, the sources are iterates of the same layers, so they become similar in content and grow in number.

Similar iterates

Iterates of the same block are far more similar to each other than to other blocks, so they receive similar scores. This worsens as loops deepen.

Early convergence

The query norm reaches most of its final value within the first third of finetuning, which may commit the model to its early connection pattern.

Growing sources

Layers can skip intermediate loops. The weight on the path through every loop falls from 33.7% with three loops to 0.05% with seven.

Visualizations of AttnRes in a looped model: content similarity, attention scores, query norm, and path weights.
Figure 3. Visualization of AttnRes in a looped model. Huginn + AttnRes on Qwen3-1.7B-Base at L=3 and L=7, where LtBj denotes the output of block j in loop t.

LoopRes

LoopRes keeps the input-dependent mechanism of AttnRes and introduces two improvements:

Loop-aware keys

Each source gets a trainable key per block per loop alongside its content key. It distinguishes iterates of the same layers by their coordinates and is optimized directly.

Residual window

Each layer attends only to the previous W loops. This prevents skipping to distant loops and bounds each input to (W+1)B+1 sources.

\[\alpha_s=\frac{\exp\big(\mathbf{q}^{\text{loop}\top}\mathbf{k}^{\text{loop}}_s+\mathbf{q}^{\top}\mathbf{k}_s\big)}{\sum_{s'\in\mathcal{S}}\exp\big(\mathbf{q}^{\text{loop}\top}\mathbf{k}^{\text{loop}}_{s'}+\mathbf{q}^{\top}\mathbf{k}_{s'}\big)},\tag{3}\]

where \(\mathbf{q}^{\text{loop}}\) is the loop-aware query and \(\mathbf{q}\) the content query, both initialized to zero.

Diagram comparing layer loops, stack loops, AttnRes, and LoopRes with a residual window.
Figure 2. How a looped layer takes the outputs of earlier layers as input. AttnRes attends to every earlier loop output, while LoopRes attends only to a window of the latest W loops (shaded, W=1 here) and adds a loop-aware key alongside the content key.

Residual window

Select a block to view its sources, with B=2 and L=7.

Method
W
reader source outside window not yet run
5
PyTorch-style pseudo code
def loop_res(values: list[Tensor], loop_keys: list[Tensor], loop_query: Tensor, content_query: Tensor,
             norm: RMSNorm) -> Tensor:
    """
    AttnRes input with loop-aware keys.
    values:
        S sources of shape [N, T, D]
    loop_keys:
        S loop-aware keys of shape [D], one trainable vector per source
    """
    values = torch.stack(values)                                                        # [S, N, T, D]
    loop_keys = torch.stack(loop_keys)                                                  # [S, D]
    content_keys = norm(values)                                                         # [S, N, T, D], as in AttnRes
    logits = torch.einsum('d, s n t d -> s n t', content_query, content_keys)
    logits = logits + torch.einsum('d, s d -> s', loop_query, loop_keys)[:, None, None] # loop-aware keys
    weights = logits.softmax(0)
    return torch.einsum('s n t, s n t d -> n t d', weights, values)

def forward(self, prelude_out: Tensor) -> Tensor:
    """
    prelude_out:
        [N, T, D]: output of the prelude; the returned core output goes to the coda
    """
    # residual window: the blocks of the previous W loops and their loop-aware keys
    window_values, window_keys = [], []
    for t in range(self.num_loops):
        values = [prelude_out] + window_values
        loop_keys = [self.prelude_key] + window_keys

        # block AttnRes over the core; every sub-layer forms its input by loop_res, with its own queries in loop t
        blocks = []
        for i, block in enumerate(self.core_blocks):
            blocks.append(block(values + blocks, loop_keys + self.block_keys[t][:i], loop=t))

        # end of loop t: once the window is full, drop all blocks of its oldest loop, then add loop t
        if len(window_values) == self.window_size * self.num_blocks:
            del window_values[:self.num_blocks], window_keys[:self.num_blocks]
        window_values += blocks
        window_keys += self.block_keys[t]

    # output: the window and the blocks of the last loop
    return loop_res(values + blocks, loop_keys + self.block_keys[t],
                    self.output_loop_query, self.output_content_query, self.output_norm)

Figure 9. PyTorch-style pseudo code for LoopRes. Lines in blue mark its two changes to Block AttnRes: (i) loop_res adds a loop-aware key to each source. (ii) Each loop takes a residual window of the previous W loops as input, from which older loops are dropped.

Main Results

LoopRes, SFT, and 8 baselines on 7 benchmarks at 2× and 4× compute, with every baseline reimplemented on the same backbone, data, and hyperparameters.

methodmathcodeavg.ΔlossΔ
AIME25AIME26AMC23OlympiadMATH500HE+MBPP+
SFT14.611.748.640.773.457.356.243.2–0.312–
Huginn19.614.454.146.580.464.264.049.013.5%↑0.3032.8%↓
Ouro12.59.247.840.474.659.156.642.90.8%↓0.3151.1%↑
Hyperloop18.316.754.447.177.666.960.448.812.9%↑0.3032.9%↓
TaH2-fixed15.812.550.041.375.862.357.045.04.0%↑0.3061.7%↓
WMRE16.210.651.240.774.060.154.143.81.5%↑0.3110.3%↓
CloseLoop16.513.350.044.178.861.456.745.86.0%↑0.3091.0%↓
InfiLoop14.410.849.442.473.058.456.343.50.7%↑0.3120.0%↑
BRT21.715.453.645.079.066.663.249.213.9%↑0.3023.0%↓
LoopRes27.919.464.753.884.071.868.955.829.1%↑0.2926.3%↓

Table 1. Comparison with baselines at 2× and 4× compute on Qwen3-1.7B-Base. Δ is the change relative to SFT. HE+ denotes HumanEval+.

LoopRes achieves the best score on every benchmark at both compute budgets, more than doubling the gain of the strongest baseline at 2× compute. The relative gains are largest on the hardest benchmarks, such as AIME.

Scalability

Scaling to deeper loops

The score improves as loops deepen and gradually saturates beyond L = 5. The best trade-off between score and compute is at L = 3.

Line chart of average score against decode compute for SFT and LoopRes at loop depths from 2 to 11.
Figure 6. Test-time scaling across loop depths. Average score as the thinking budget grows from 1K to 30K tokens, on Qwen3-1.7B-Base.

Scaling to larger backbones

LoopRes improves every benchmark on Qwen3-4B, 8B, and 30B-A3B, and its score rises with a clearly steeper slope than SFT as decode FLOPs grow. LoopRes on the 4B backbone even surpasses SFT on the 8B one in average score.

methodA25A26OlyIMOLCBavg.Δ
Qwen3-4B-Base
SFT35.438.361.620.527.436.6–
LoopRes47.151.068.329.632.945.8+9.2
Qwen3-8B-Base
SFT39.646.065.530.932.642.9–
LoopRes51.562.972.335.535.151.5+8.6
Qwen3-30B-A3B-Base
SFT59.666.372.938.638.355.1–
LoopRes65.472.576.444.944.060.6+5.5

Table 3. Results on larger backbones at 2× compute. A25/A26: AIME25/26, Oly: OlympiadBench, IMO: IMO-AnswerBench, LCB: LiveCodeBench v6.

Generalizing across model families

LoopRes also improves every benchmark on Llama-3.2-1B and Qwen3.5-2B-Base, whose architectures differ from Qwen3.

backboneSFT avg.LoopRes avg.Δ
Llama-3.2-1B14.319.6+5.4
Qwen3.5-2B-Base52.164.1+12.0

Table 4. Results on Llama-3.2-1B and Qwen3.5-2B-Base at 2× compute. Δ is in points.

Ablation Study

We ablate the components of LoopRes on Qwen3-1.7B-Base at both compute budgets.

variant2× compute4× compute
avg.Δavg.Δ
LoopRes55.8–57.8–
Huginn + AttnRes53.9−1.955.0−2.8
Residual window (LoopRes: W = 1)
W = 255.2−0.657.5−0.3
W = 3––57.7−0.1
Key (LoopRes: loop-aware + content)
loop-aware only54.9−0.957.5−0.3
content only54.9−0.955.8−2.0

Table 5. Ablation study of LoopRes on Qwen3-1.7B-Base. Each group varies one component.

Naive AttnRes

Applying AttnRes to Huginn without loop-aware keys or the residual window performs clearly worse, and the gap widens as loops deepen.

Residual window

A larger window reduces the average score, so LoopRes uses W = 1, which achieves the best score at the lowest cost.

Loop-aware keys

Either key alone performs worse than the two combined. As loops deepen, dropping the loop-aware key hurts more.

Analysis

What LoopRes learns

We visualize how LoopRes weighs its sources, revealing three patterns.

Iterative loop updates

The first block mainly updates its own output from the previous loop, while the second on average relies most on the first block of the current loop.

Dominant loop-aware keys

The loop-aware key accounts for most of the selection at both compute budgets.

Adaptive content keys

The content key lets each token choose its own depth: simple tokens favor shallower outputs, while digits and math symbols favor deeper ones.

Charts of source weights, key contributions, and tokens that favor shallower or deeper outputs.
Figure 7. What LoopRes learns. LoopRes on Qwen3-1.7B-Base, averaged over 128 samples.

How LoopRes differs from AttnRes

We further compare LoopRes with AttnRes, with and without the residual window, revealing three differences.

Distinguishable loop iterates

LoopRes separates adjacent loops clearly from early in finetuning, while AttnRes struggles even with the window.

Recency-focused inputs

The window removes shortcuts to the first loop, and LoopRes further concentrates the weight on the preceding block.

Less is more for content keys

The content score of AttnRes is almost a fixed preference. In LoopRes, it varies far more with the token, even as loops deepen.

Charts comparing loop separation, source weights, and token-dependent content scores for AttnRes and LoopRes.
Figure 8. How LoopRes differs from AttnRes. AttnRes, AttnRes with W=1, and LoopRes on Qwen3-1.7B-Base over 128 samples at L=3, with (d) also showing L=7.

BibTeX

@misc{zhao2026loopres,
  title={Unlocking the Power of Loops in Pretrained LLMs with Loop Residuals},
  author={Zhao, Xuanlei and Xu, Hailun and Wang, Kai and Luo, Yang and Ren, Yingxuan and Zhu, Zirui and Lou, Yuxuan and Xu, Kun and Wang, Yan and Sarkar, Kanchan and You, Yang},
  year={2026},
  note={Preprint}
}