Skip to content
ThrillcrazyerPublic

About

Official repository for the paper "LoopUS: Recasting Pretrained LLMs into Looped Latent Refinement Models"

Resources

Stars

15 stars

Watchers

0 watching

Forks

Repository files navigation

LoopUS:
Recasting Pretrained LLMs into Looped Latent Refinement Models

1 Pusan National University, 2 Changwon National University

* Corresponding author

Paper Project Page Huggingface

Overview

LoopUS is a post-training framework that converts a standard pretrained LLM into a looped latent refinement model, enabling test-time compute scaling without training a recurrent architecture from scratch or heavily disrupting pretrained capabilities. Instead of extending output traces, LoopUS restructures the model into an encoder, a looped reasoning block, and a decoder, then performs iterative latent refinement in the hidden space. This design targets reasoning-oriented gains while keeping the system compatible with practical pretrained checkpoints and efficient training workflows.

Method

  1. Block Decomposition: recasts a pretrained transformer into an encoder, a looped reasoning block, and a decoder based on staged representation dynamics, turning a feed-forward backbone into a reusable latent-refinement architecture.
  2. Input-Dependent Selective Gate: adaptively controls how refined hidden states are propagated across recursive steps, mitigating hidden-state drift and reducing the risk of representation collapse during looping.
  3. Random Deep Supervision: applies supervision to sampled reasoning steps rather than every recursive step, making long-horizon loop training more memory-efficient while preserving optimization stability.
  4. Confidence Head for Adaptive Early Exit: estimates whether additional refinement is still useful and allows the model to stop recursion early at inference time, improving compute-efficiency without changing the external generation format.

LoopUS framework

Installation

The codebase targets Python 3.10 or newer. It was developed primarily in NVIDIA GPU environments with CUDA-enabled PyTorch, so non-NVIDIA setups may require small adjustments.

uv sync

For optional external integrations, create a local .env file:

HF_TOKEN=...
WANDB_KEY=...

Quick Start

1. Pretraining

uv run LoopUS-train \
	--model-name Qwen/Qwen3-1.7B \
	--train-dataset HuggingFaceFW/fineweb-edu \
	--train-config CC-MAIN-2025-26 \
	--train-split train \
	--train-max-tokens 1500000000 \
	--batch-size 2 \
	--gradient-accumulation-steps 64 \
	--learning-rate 5e-5 \
	--max-length 1024 \
	--n-supervision 5 \
	--n-reasoning-steps 20 \
	--encoder-layers 0..1 \
	--decoder-layers 27..27 \
	--checkpoint-dir checkpoints/example_run

For a configurable template suitable for cluster jobs, see script/train.sh.

Download first, then train from local files

The shell template uses script/data_config.sh for model, dataset, sample/token budgets, sequence length, and local download paths. Its defaults are Qwen/Qwen3-4B and HuggingFaceFW/fineweb-edu / CC-MAIN-2025-26 / train.

# 1. Download model weights/tokenizer and materialize the training data.
bash script/download.sh

# 2. Validate the prepared files and launch training using local paths.
bash script/train.sh

Files are stored under downloads/ (excluded from Git). Existing completed downloads are reused. If dataset settings change, use a new LOCAL_DATASET_DIR or move the old directory before downloading again. Incomplete dataset downloads are not published as ready; rerun the download command after a failure.

The downloader saves the first TRAIN_MAX_TOKENS // MAX_LENGTH raw examples, matching the training code's approximate token budget. With the defaults this is 2,929,687 examples, including the later validation split; it is not an exact count of non-padding tokens. If TRAIN_MAX_TOKENS <= 0, TRAIN_MAX_SAMPLES controls the download; setting both budgets to zero or negative downloads the entire split. Data is written to disk incrementally, and preparation temporarily needs space for both its Arrow cache and final saved dataset. Tokenization happens during training. Local datasets use a seeded full shuffle; remote streams retain their buffer shuffle.

You can override shared settings through environment variables. Use the same overrides for both commands, or edit script/data_config.sh once:

export DOWNLOAD_DIR=/mnt/training-assets
bash script/download.sh
bash script/train.sh

Direct Python/CLI training commands still accept Hub IDs. To use prepared data, pass the local model directory to --model-name and the saved dataset directory to --train-dataset; this also works with train_sft.py. W&B, Hub uploads, and optional lm-eval benchmarks may still use the network (benchmark datasets are not prepared by this download script). For multiple nodes, make the prepared directories available at the same path on every node.

2. Supervised Fine-Tuning

uv run LoopUS-train-sft \
	--model-name Qwen/Qwen3-1.7B \
	--train-dataset HuggingFaceH4/ultrachat_200k \
	--train-split train_sft \
	--checkpoint-dir checkpoints_sft/example_run

3. Evaluation

uv run LoopUS-eval \
	--model-name Qwen/Qwen3-1.7B \
	--decomposed-model Thrillcrazyer/Qwen1.7_LoopUS \
	--tasks mmlu,hellaswag,arc_easy,arc_challenge,piqa,winogrande \
	--n-recursion 8 \
	--batch-size 8 \
	--max-length 1024 \
	--output-json results/eval.json

For a reusable evaluation template, see script/eval_model.sh.

4. Qualitative Generation

uv run LoopUS-generate \
	--model-name Qwen/Qwen3-1.7B \
	--decomposed-model Thrillcrazyer/Qwen3-1.7B_ver0.2_5000 \
	--prompt "The meaning of life is" \
	--n-recursion 8

The legacy test_model.py entrypoint is kept as a thin compatibility wrapper around generate.py.

5. Chatting Mode

ur run chat.py \
	--model-name Thrillcrazyer/SFT_LDS_QWEN1.7B

Thrillcrazyer/SFT_LDS_QWEN1.7B is a model obtained by supervised fine-tuning Thrillcrazyer/Qwen1.7_LoopUS on HuggingFaceH4/ultrachat_200k for 1 epoch. It does not work perfectly yet.

Checkpoints and Model Loading

The public inference path supports three checkpoint sources:

  1. a local save_pretrained directory
  2. a Hugging Face Hub repository containing saved LoopUS weights
  3. a legacy training checkpoint directory containing combined_model.pt

Both evaluate.py and generate.py use the same loading path through utils/inference.py, so evaluation and qualitative sampling follow identical checkpoint semantics.

Reproducibility

  • TrainConfig validates the main experimental settings before launch.
  • set_seed fixes Python, NumPy, and PyTorch seeds.
  • training_runtime.py centralizes checkpoint save/resume logic, schedulers, and external service setup.
  • Evaluation is driven by lm-eval through utils/lm_eval_harness.py.
  • The public shell templates in script/ are intended to be editable run manifests for released experiments.

Repository Structure

.
├── train.py
├── train_sft.py
├── evaluate.py
├── generate.py
├── training_cli.py
├── training_runtime.py
├── models/
├── utils/
├── ds_configs/
└── script/

License

This project is released under the Apache 2.0 License. See LICENSE for details.

GPU Sponsorship

We are always looking for GPU sponsorship. If you are interested, please contact. pthpark1@pusan.ac.kr

Citation

If you find LoopUS useful in your research, please cite the following paper:

@misc{park2026loopus,
  title        = {LoopUS: Recasting Pretrained LLMs into Looped Latent Refinement Models},
  author       = {Taekhyun Park and Yongjae Lee and Dohee Kim and Hyerim Bae},
  year         = {2026},
  eprint       = {2605.11011},
  archivePrefix= {arXiv},
  primaryClass = {cs.LG},
  doi          = {10.48550/arXiv.2605.11011},
  url          = {https://arxiv.org/abs/2605.11011}
}

About

Official repository for the paper "LoopUS: Recasting Pretrained LLMs into Looped Latent Refinement Models"

Resources

Stars

15 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages