Method · Installation · Training · Inference · Evaluation · Citation · Acknowledgement
This is the official implementation of LoopRes. LoopRes turns pretrained LLMs into looped transformers through finetuning. Each looped layer attends directly to the outputs of earlier loops through loop residuals. Each of those outputs carries a trainable loop-aware key, and each layer only sees a window of the latest W loops. At 2× compute, LoopRes improves Qwen3-1.7B-Base by 29.1% over SFT, more than twice the gain of the best previous method. The gains hold across 6 pretrained LLMs from 3 model families, up to 30B, on 9 math and code benchmarks. From 1.7B to 8B, LoopRes also scales more steeply with test-time compute than SFT.
LoopRes applies a block of decoder layers several loop steps per token: a
prelude runs once, the core layers loop under block attention residuals whose
sources carry trained static keys, read through learned loop and content keys,
and a coda reads the last step out. Models train on SFT data and score on math
and code benchmarks; loopres/model.py's docstring gives the method and every
config key.
configs/ example run configs; eval.yaml, the benchmarks protocol
loopres/ the model, its Triton read kernel and the data pipeline
minisgl/ mini-sglang serving checkpoints
scripts/ data download and preparation, training, inference, serving and evaluation
tests/ CPU suite; tests/gpu/ runs by path on a GPU
data/ datasets; outputs/ run outputs (both gitignored)
One environment covers training, inference, serving and evaluation (transformers 5.2, torch 2.10):
conda create -n loopres python=3.11 -y
conda activate loopres
pip install -e .
pip install flash-attn # add --no-build-isolation if the build cannot import torchThe training data is
oahzxl/LoopRes-Qwen3-SFT:
{question, generation} jsonl, the same questions answered by Qwen3-8B
(Qwen3-8B/) and by Qwen3-235B-A22B (Qwen3-235B-A22B/), one train split
each.
Download the jsonl splits; Qwen3-8B is the other subset:
python -m scripts.download_data --subsets Qwen3-235B-A22B --output data/LoopRes-Qwen3-SFTRender the subset through the chat template into the tokenised dataset the configs read:
python -m scripts.prepare_data --source data/LoopRes-Qwen3-SFT/Qwen3-235B-A22B \
--output data/Qwen3-235B-A22B-16k --max-length 16384Train LoopRes:
torchrun --nproc_per_node 4 -m scripts.train --config configs/loopres.yamlconfigs/loopres.yaml trains LoopRes on the data/Qwen3-235B-A22B-16k dataset
built above; configs/sft.yaml is the baseline, the pretrained model at one
step. The configs match the paper's setup: 4 GPUs, a global batch of 16 packed
samples. To train on the Qwen3-8B replies, download and prepare that subset
the same way and point the config's data: paths at its dataset. --resume
continues from the latest complete checkpoint. A checkpoint directory is a
transformers checkpoint: config.json, bfloat16 weights and the tokenizer; the
run config is copied to the output directory as config.yaml.
Download the model:
huggingface-cli download oahzxl/LoopRes-Qwen3-1.7B --local-dir checkpoints/LoopRes-Qwen3-1.7BLoad a checkpoint:
import loopres # required to load a LoopRes checkpoint
from transformers import AutoModelForCausalLM, AutoTokenizer
checkpoint = "checkpoints/LoopRes-Qwen3-1.7B"
model = AutoModelForCausalLM.from_pretrained(checkpoint, dtype="auto").cuda().eval()
tokenizer = AutoTokenizer.from_pretrained(checkpoint)scripts.infer does that and prints one reply's thinking and its content,
sampling as configs/eval.yaml's sampling:
python -m scripts.infer --checkpoint checkpoints/LoopRes-Qwen3-1.7B \
--prompt "Give me a short introduction to large language model." \
--max-new-tokens 32768 --device cudaEvalPlus runs in an Apptainer sandbox. Apptainer is not on PyPI; install it with Conda:
conda install -c conda-forge apptainer=1.5.4 -yPrepare the benchmark files, EvalPlus's problems and its image (an Apptainer sandbox), kept once set up; math500 and olympiadbench come from the hub:
python -m scripts.prepare_benchmarks --output data/benchmarksServe a checkpoint, one server per GPU:
python -m scripts.serve --checkpoint checkpoints/LoopRes-Qwen3-1.7B --gpus 0 1 2 3Request and score. Replies are saved as they finish and a rerun on the same
--output and --seed reuses them:
python -m scripts.eval --servers http://127.0.0.1:30080 http://127.0.0.1:30082 \
http://127.0.0.1:30084 http://127.0.0.1:30086 \
--output outputs/loopres/eval32k-12000 [--benchmarks aime25 amc23 ...] [--samples <count>]If you find LoopRes useful, please cite:
@misc{zhao2026unlockingpowerloopspretrained,
title={Unlocking the Power of Loops in Pretrained LLMs with Loop Residuals},
author={Xuanlei Zhao and Hailun Xu and Kai Wang and Yang Luo and Yingxuan Ren and Zirui Zhu and Yuxuan Lou and Kun Xu and Yan Wang and Kanchan Sarkar and Yang You},
year={2026},
eprint={2611.31094},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2611.31094},
}We thank the following projects for their open-source code:
This project is released under the MIT License.
The minisgl/ directory is vendored from
sgl-project/mini-sglang
(commit 9a91cfa), also under the MIT License; see
minisgl/README.md for the modifications.