Skip to content

About

Tiny loop transformers trained from scratch: does running layers several times help reasoning or memory? Code and every result for Kobkrit.com EP.7

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

Tiny loop transformers: does thinking twice make a small model smarter?

A loop transformer stores a few layers and runs them several times. In this repo, k×L means k stored layers run L times:

  • 2×4 has the same parameters as 2×1, but the compute of an 8-layer model.
  • 8×1 has the same compute as 2×4, but 4 times the layer parameters.

This repo trains very small models from scratch (197K to 3.5M layer parameters) to test three things:

  • whether looping helps multi-step reasoning;
  • whether it helps memory;
  • whether a loop can be added to an existing model.

It is the code behind Kobkrit.com EP.7 (video and Thai write-up).

ภาษาไทย: โค้ดทดลอง Loop Transformer ตัวจิ๋ว ประกอบคลิป Kobkrit.com EP.7 ผลสรุปคือ วนช่วยเรื่องคิดหลายขั้น ไม่ช่วยเรื่องความจำ และผลแกว่งมากตามเลขสุ่ม รันรอบเดียวเชื่อไม่ได้

Results (H100, 24 Nov and 2 Dec 2026)

1. Follow the arrows (multi-step reasoning, 3 seeds; 10 seeds for the six main setups)

Each example is a map of 16 boxes, where every box points to another one. Questions ask where you end up after 1 to 8 steps from a start box. The map is new in every example, so the model has to chain the steps inside one forward pass. Random guessing scores about 6%.

Setup Layer params Compute Seeds (%) Mean (%)
1×1 197K 1 layer 31.2 / 31.2 / 31.3 31.2
1×8 197K 8 layers 51.9 / 46.8 / 100 66.2
2×1 394K 2 layers 37.0 / 31.9 / 37.7 35.5
2×2 394K 4 layers 32.3 / 47.1 / 50.8 43.4
2×4 394K 8 layers 32.5 / 70.4 / 97.4 66.8
2×8 394K 16 layers 34.1 / 45.8 / 43.3 41.0
4×1 788K 4 layers 44.8 / 45.1 / 46.9 45.6
4×2 788K 8 layers 55.1 / 57.4 / 54.1 55.5
8×1 1.58M 8 layers 54.3 / 73.9 / 74.5 67.6
16×1 3.15M 16 layers 45.7 / 98.4 / 100 81.4

10 seeds (round 2, jobs2.txt) for the six main setups. The video uses the first 3 seeds.

Setup Mean (%) Median (%) Min–max (%)
2×1 33.9 33.0 30.5–37.7
2×4 62.6 58.0 32.5–97.4
4×1 55.4 55.9 42.0–68.0
4×2 67.1 62.2 54.1–89.4
8×1 73.3 73.0 54.3–96.4
1×8 84.8 95.6 46.8–100

Paired by seed (10 seeds): 2×4 beats 2×1 in 9/10, 4×2 beats 4×1 in 9/10. Against 8×1 (same compute): 1×8 wins 7/10, 4×2 4/10, 2×4 3/10.

With 3 seeds:

  • Same parameters, more loops:
    • 1×1 → 1×8 and 4×1 → 4×2 improve in every seed.
    • 2×1 → 2×4 improves on average, but one seed stayed at 32.5%.
  • Same compute: 2×4 and 1×8 tie with 8×1 on average, with a very wide spread across seeds. 4×2 loses.
  • More loops is not always better: 2×8 scores below 2×4.
  • Extra loops at test time: running more loops than the model was trained with gains at most about 1 point, and sometimes loses (2×4 seed 2 drops from 97.4% to 81.5% at 16 loops).

2. Memory (60,000 invented people, 2 seeds)

Each person has 5 random attributes, with 64 possible values each.

Setup Params Correct (%) Bits stored per parameter
2×1 500K 63.5 / 63.2 2.30 / 2.29
2×4 500K 56.6 / 57.2 2.02 / 2.05
4×1 894K 99.9 / 99.9 1.98 / 1.98
8×1 1.68M 100 / 100 1.07 / 1.07 (everything stored)

Looping does not add memory; more stored layers do.

3. TinyStories language model (1 seed, validation loss, lower is better)

The models are trained on TinyStories, a dataset of short English children's stories.

Setup 2×1 2×4 4×1 4×2 8×1
Loss 2.132 2.072 1.977 1.972 1.914

The validation stories that also appear in the training shard (1,550 of them) are removed first.

4. Adding a loop to SmolLM2-135M (3 seeds)

In SmolLM2-135M, layers 8–23 run extra passes.

  • Each extra pass is added through a per-token gate that starts at zero. Before training, the looped model therefore gives exactly the original model's logits (maximum difference 0.0).
  • The model is then fine-tuned on multi-hop questions ("who is the boss of the boss of X?", 1 to 6 hops). Before training it scores 7–10%.
Setup Mean accuracy over hops 1–6 (%)
No loop, 1,200 steps 79.4 / 89.0 / 87.8
No loop, 1,840 steps (same compute as 2 passes) 79.0 / 86.9 / 92.7
2 passes 90.0 / 69.3 / 65.1
3 passes 67.9 / 78.6 / 93.0

Inconclusive: the spread between seeds is larger than any difference between setups.

Caveats

  • Seed variance: 2×4 scored 32.5%, 70.4% and 97.4% with three seeds. Do not trust a single run, including any of these.
  • Mac vs H100: the first runs on an Apple M3 Max (MPS, one seed, in results_mac_first_try/) did not match the first 3 server seeds: 4×1 scored 64% on the Mac and 45–47% on the server. With 10 server seeds, 4×1 ranges from 42% to 68%, which includes the Mac's 64%, so most of the gap is seed variance (the two machines draw different random numbers). A no-bf16 check (--fp32, seed 0) gave 4×1 68.1%, 4×2 61.2% and 2×8 42.5%, all inside the bf16 seed spread. 2×8 (87% on the Mac, 34–46% on the server) is still unexplained.
  • Small and short: the models are tiny and training is short. Several curves were still rising at the end.

Run it yourself

pip install -r requirements.txt
./download.sh                      # TinyStories shard + SmolLM2-135M into work/ (only lm.py and retrofit.py need them)
cd experiments && python prep_lm.py && cd ..

# one run
cd experiments && python run.py --task chase --k 2 --L 4 --steps 8000 --seed 0

# every job of the first round (56 jobs; resumes if stopped)
./run_queue.sh jobs.txt
cd experiments && python summarize.py

On one H100, each arrow-task run takes 40 s to 5 min. The SmolLM2 runs take about 2–4 min each. It falls back to Apple MPS or CPU (slow) when there is no CUDA GPU.

Each run writes experiments/results/<name>.json, which contains:

  • the settings;
  • parameter counts;
  • final accuracy (per hop for the arrow task);
  • accuracy at other loop counts;
  • the training curve;
  • wall time.

Files

File What it is
experiments/looplm.py The model: k stored blocks run L times (the loop is 2 lines in forward)
experiments/tasks.py Data for the arrow task (generated fresh every batch) and the memory task
experiments/run.py Training and evaluation for the arrow and memory tasks
experiments/prep_lm.py, lm.py TinyStories tokenizer (BPE, 4,096 tokens) and language-model runs
experiments/retrofit.py Adds a gated loop to SmolLM2-135M and fine-tunes it
experiments/jobs.txt, jobs2.txt The exact job lists of round 1 and round 2
experiments/summarize.py Means and per-seed tables from results/
experiments/results/ Every result JSON behind the numbers above

Only two things differ from the code that produced these results:

  • the CPU fallback;
  • the GPU memory cap, which is now off unless GPU_MEM_FRACTION is set (the runs used 0.19 on a shared card).

Related work

License

Apache-2.0

About

Tiny loop transformers trained from scratch: does running layers several times help reasoning or memory? Code and every result for Kobkrit.com EP.7

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages