# Loop-surgery proof — RESULTS (2026-11-22) De-risks the crux of the "loop our security base" plan (`../inkling-like-training-plan.md`, Stage B): can we add Inkling's weight-tied middle-loop to a pretrained Qwen3 model **stably** (start == base) and **trainably** (gate opens, loop learns to help)? **Yes — proven on CPU, $0.** ## What's here - `loopify.py` — `loopify(model, start, end, n_passes, gate_init)` collapses decoder layers `[start:end)` into one weight-tied `LoopedMiddle` pseudo-layer that the stock `transformers` model.forward drives unchanged. Combines one-pass and K-pass results through a **zero-init interpolation gate**: `out = y1 + gate*(yK - y1)`, so `gate==0 ⇒ out == y1` exactly. - Eval fast-path short-circuits at `gate==0` (keeps the invariant exact and avoids KV-cache mutation under `generate`); training always runs the full gated path so the gate stays in the autograd graph and can open from exactly zero. - Needed a per-block `attention_type` (the model picks each layer's causal mask by it); asserts the block's layers share one type. - `prove_invariant.py` — the correctness checks. - `heal_demo.py` — the trainability / micro-heal check. ## Results (Qwen/Qwen3-0.6B, 28 layers, middle block = layers [9:18), 9 layers tied, 3 passes) **Surgery correctness (`prove_invariant.py`, eval):** | Check | Result | |---|---| | [1] INVARIANT — gate=0, 3 passes: `max|loop−base|` logits | **0.000e+00 (bit-identical) ✓** | | [2] WIRED — gate=0.5: logits move | 7.28 ✓ (loop flows) | | [3] PASSES — 3→5 passes @ gate=0.5 | 7.39 ✓ (real depth dial) | | [4] GEN — greedy 24 tok @ gate=0 | identical to base ✓ | **Trainability (`heal_demo.py`, train gate+middle-block, 30 steps, tiny corpus):** - Gate receives gradient immediately (`d(loss)/d(gate)` nonzero at step 1) and moves off zero. - Loss **5.69 → 0.77**; gate **0.0 → +0.125 (opened)**. - Bonus: by later steps `d(loss)/d(gate)` turns **negative** — once the tied block adapts slightly, the optimizer actively opens the loop because the extra passes start reducing loss. ## What this proves / does NOT prove - **Proven:** the zero-init-gate loop surgery is correct (start == base, bit-exact), the loop is a live test-time-depth dial, and the whole thing trains end-to-end with the gate opening from zero. The single biggest novelty-risk of Stage B is retired. - **NOT yet proven:** that a loop *measurably improves reasoning at matched visible tokens* — that needs a real heal on real tokens (continued-pretrain + on-policy distillation), i.e. GPU/Modal. The micro-heal above overfits a 6-sentence toy; it shows the mechanism trains, not that the loop wins on held data. ## Reproduce ```bash cd custom_models/loop_proof PYTHONPATH=. python3 prove_invariant.py PYTHONPATH=. python3 heal_demo.py ``` ## Next step (first GPU spend) Real small-model heal on Modal (loopify Qwen3-0.6B/1.7B → continue-train on tens of M tokens + on-policy distillation → eval looped vs non-looped at matched visible tokens on a held set). Est. a few GPU-hours on L40S/A100 ≈ **$10–30**. Only after that wins do we loopify the 35B security base (the ~$low-thousands Phase 2).