loop-surgery / reports /RESULTS.md
rkreddyp's picture
Loop Surgery: recipe + invariant proof + 5-experiment investigation
a7b9459 verified
|
Raw History Blame Contribute Delete
3.22 kB
# 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).