Download docs/SIMPLIFIED_GUIDE_QWEN_LOOP.md from Vnswz/Qwen3.8-9B-Loop6-Adapter: direct link, hf CLI and curl.
- Browser
- Download file 9.54 kB
-
https://huggingface.co/Vnswz/Qwen3.8-9B-Loop6-Adapter/resolve/main/docs/SIMPLIFIED_GUIDE_QWEN_LOOP.md
- Command line
-
hf download hf://Vnswz/Qwen3.8-9B-Loop6-Adapter/docs/SIMPLIFIED_GUIDE_QWEN_LOOP.md
-
curl -L -o SIMPLIFIED_GUIDE_QWEN_LOOP.md https://huggingface.co/Vnswz/Qwen3.8-9B-Loop6-Adapter/resolve/main/docs/SIMPLIFIED_GUIDE_QWEN_LOOP.md
Simplified & Logical Guide — How We Turned Qwen3.8 into a Looped Model (1 to 6 Loops)
Who this guide is for: Anyone with a general technical background (programming, IT, systems) who knows the basics of LLMs and wants a clear, intuitive explanation of what was built, why each decision makes logical sense, and what changed in practice — without heavy math or ML jargon.
For full mathematical equations, C++/CUDA architecture, and training hyperparameters, see the Technical Documentation.
Versão em Português: GUIA_SIMPLIFICADO_QWEN_LOOP.md
1. What Problem Did We Solve? (What Is a "Looped" Model?)
A standard LLM works like a straight-line assembly line: text enters, passes through 32 computation layers once (K=1), and the model immediately picks the next token. If the task is hard — like spotting a security trap (prompt injection) or solving a logic puzzle — the model only gets one pass per token to "think".
The idea behind a looped model (like Dory) is simple: instead of buying a massive GPU to run a 150-layer model, we reuse the middle of the exact same model multiple times before producing each token.
- With 1 loop (
K=1): the model answers on the first pass (faster, shallower). - With 6 loops (
K=6): the model runs its internal reasoning core 6 times in a row, refining its answer before speaking — using almost the same GPU memory.
flowchart LR
A["Your Prompt"] --> B["Input: 4 layers (1x)"]
B --> C["Reasoning Core: 24 layers"]
C -->|"K=1: go straight to output"| D["Output: 4 layers (1x)"]
C -->|"K=2..6: refine again with loop adjustments"| C
D --> E["Final Answer"]
In practice, at 6 loops, Qwen3.8-9B (32 physical layers) executes the equivalent of 152 virtual layers (4 + 24×6 + 4) using the same ~5 GB of quantized weights on an 8 GB RTX 3070 Ti.
2. How We Adapted the Engine (llama.cpp-qwen-loop) for an 8 GB GPU
You can't just put a for loop around a model's layers — if a model wasn't trained to loop, its internal numbers drift and it outputs gibberish. So we added four clean building blocks to the C++ engine:
2.1 Smart Split: Input (4) + Core (24 × K) + Output (4)
The first 4 layers act as "reading" and the last 4 as "writing". Repeating reading or writing doesn't help; we loop only the 24 middle layers where reasoning happens.
2.2 Loop Bridges — A Safety "Volume Knob"
Between one loop and the next, we insert a tiny connector called a Loop Bridge. It does three logical things:
- Keeps a clean reference copy of the original input (so the model never "forgets" the prompt after several passes).
- Tells the core which pass it is on (Loop 1, Loop 2, ..., Loop 5).
- Acts like a volume knob starting near zero: at the start of training, extra loops don't scramble what Qwen3.8 already knows.
2.3 Lightweight Per-Loop "Revision Notes" (LoopLoRA, Only 45 MB)
Instead of modifying all 9 billion original parameters (impossible to train on 8 GB VRAM), we freeze the entire base model and add small adjustment matrices (LoRAs) dedicated to each loop (Loop 1 through Loop 5), leaving Loop 0 untouched.
- The final exported adapter file is only 45.27 MB.
- Because
Loop 0is untouched, running atK=1preserves exact base-model behavior.
2.4 Two Crucial 8 GB Stability Fixes
Real-world testing uncovered and fixed two classic systems issues:
- Each loop needs its own "scratchpad" (
per_loop): we first tested a memory-saving mode (last_loop) where all loops shared the same attention KV cache. Result: Loop 5 overwrote notes that Loop 0 needed on the next token, crashing long-context accuracy (10 → 0). Giving each virtual layer its own slot (per_loop) restored10/10while fitting in6.24 GBVRAM. - Preventing server RAM crashes: at 6 loops, each saved prompt-state checkpoint takes ~234 MB in RAM. Cloning multiple checkpoints under fragmented RAM caused an unhandled out-of-memory crash (
0xc0000409). We added safestd::bad_allocguards inllama-serverand capped the prompt cache (--cache-ram 1024 --ctx-checkpoints 2). Result: zero crashes across the entire benchmark suite.
3. How We Taught the Model to Actually Get Smarter With More Loops
Running 6 loops is only half the story. The other half was teaching the model to use those extra passes productively — all trained directly on an 8 GB RTX 3070 Ti (without downloading the 19 GB FP16 weights).
What Went Wrong First (And the Key Lesson)
Our early attempts used handwritten synthetic examples with short, artificial reasoning traces. The problem: Qwen3.8 already has its own natural reasoning style inside <think>...</think>. Forcing an artificial style caused it to memorize training templates and forget things it already did well (like step-by-step arithmetic and strict table formatting).
The 3 Logical Ideas That Solved It
- Native Self-Distillation ("learn from its own best drafts"):
Instead of forcing an artificial style, we asked Qwen3.8 itself (
K=1) to solve hundreds of varied problems (math, ordering logic, strict tables, JSON, graphs, security) and used automated verifier scripts to keep only 100% verified answers. The model learns in its own natural "language of thought". - Context Distillation ("train by removing the cheat sheet"):
On the security test (prompt injection), the 1-loop model fell for the trap and tried to delete the database. To teach it without artificial examples, we gave the model an explicit security reminder ("cheat sheet") while generating correct training examples — and then deleted the reminder when training loops
K=2..6. That way, deeper loops learned to spot malicious instructions on their own. - Preservation Anchor (Selective KL — "don't break what's already right"):
We added a mathematical guardrail: whenever the 1-loop model already predicts a token with high confidence, training prevents deeper loops from drifting away; whenever 1-loop hesitates or makes a mistake, loops
2..6are free to fix it. On top of that, a monotonicity rule pushes each loop to make fewer errors than the previous one.
Engineering note: to fit training inside 8 GB VRAM, the script loads the quantized
.ggufdirectly into 4-bit GPU buffers, keeps the word embedding table in CPU RAM, and slices the output head — peaking at 6.3 GB VRAM and taking ~14.5 minutes for the final polish stage.
4. What Improved in Practice (Before vs. After)
All numbers below were measured on the real llama-server.exe running on an RTX 3070 Ti:
| Metric | 1 Loop (K=1, Base) |
6 Loops (K=6, Trained) |
What It Means in Practice |
|---|---|---|---|
Quality Score (/90) |
70.0 / 90 |
90.0 / 90 (100%) |
Perfect score across all 6 technical quality benchmarks. |
Total Score (/100, includes speed) |
80.0 / 100 |
95.0 / 100 |
+15 net point gain even after accounting for slower speed. |
Security / Prompt Injection (B2) |
0 / 20 (fell for trap) |
20 / 20 (blocked & diagnosed) |
Refused malicious delete_database and called the right tool. |
| 4-Item Deductive Ordering | Failed (1>2>3>4) |
Passed (Delta>Beta>Gamma>Alpha) |
Correctly deduced chained logical ordering. |
Long Context (B6) & Strict Table (B5) |
10/10 and 10/10 |
10/10 and 10/10 |
Zero regression on existing base-model strengths. |
| Multi-Hop Probes (6 extra challenges) | 5 / 6 |
5 / 6 |
Fixed logic ordering; still misses 1 specific 6-step arithmetic probe (state_track_6hop_a). |
| Generation Speed | 56.6 tokens/s |
19.1 tokens/s |
Expected trade-off: thinking 6× deeper takes ~3× longer per token. |
| Peak VRAM Usage | 5.47 GB |
6.24 GB |
Fits comfortably on 8 GB GPUs with 0 crashes. |
Choosing How Many Loops to Use Day-to-Day
The exact same 45 MB adapter works for any setting from 1 to 6:
K=1orK=2: fast drafts, summaries, simple Q&A (43–57 t/s).K=4: balanced mode for everyday coding and logic (26.5 t/s).K=6: critical tasks, tool-calling agents, security, code review, and complex reasoning (19.1 t/s,90/90quality).
5. How to Run It (Copy & Paste)
Option 1 — Using Ready-Made Presets (models.example.ini)
Includes presets for 2, 4, and 6 loops (with mmproj-offload = false ready to uncomment if you use vision):
.\build\bin\Release\llama-server.exe --models-preset models.example.ini --port 8080
Option 2 — Direct llama-server Command (With Vision in System RAM)
Change int:6 to any value from int:1 to int:6. The --no-mmproj-offload flag keeps the vision encoder (mmproj) in CPU RAM so your GPU VRAM stays free for loops and context:
.\build\bin\Release\llama-server.exe `
-m .\models\Qwen3.8-9B-Distill-uncensored-heretic.Q4_K_S.gguf `
--mmproj .\models\Qwen3.8-9B-Distill-uncensored-heretic.mmproj-f16.gguf `
--no-mmproj-offload `
--lora .\qwen3.8-9b-heretic-loop6-lora.gguf `
--override-kv qwen35.num_loops=int:6,qwen35.recurrent_kv_cache_mode=str:per_loop `
-c 8192 -np 1 --cache-ram 1024 --ctx-checkpoints 2 `
-ngl 999 --fit off --cache-type-k q8_0 --cache-type-v q8_0 --repeat-penalty 1.15 --port 8080