#!/usr/bin/env python3 """Training-free loop, GGUF edition: repeat a mid-stack window `REPS` times with each application damped by `ALPHA` (default 1/REPS), physically duplicating the window's tensors. The damping lives in the sandwich norms (`post_attention_norm`, `post_ffw_norm`): both HF and llama.cpp apply them as RMSNorm on the branch output before the residual add, so scaling those f32 vectors scales that application's residual step by exactly ALPHA. usage: loop_gguf_k.py SRC.gguf DST.gguf A B REPS [ALPHA] """ import os, struct, sys from collections import OrderedDict import numpy as np SRC = sys.argv[1] DST = sys.argv[2] A, B = int(sys.argv[3]), int(sys.argv[4]) REPS = int(sys.argv[5]) if len(sys.argv) > 5 else 2 ALPHA = float(sys.argv[6]) if len(sys.argv) > 6 else 1.0 / REPS T = {0: ' {n_layer + NEW} window [{A}-{B}] x{REPS} alpha={ALPHA:.4f}") print(f"tensors {n_tensors} -> {len(src_idx)} data {data_len/1e9:.3f} -> {cur/1e9:.3f} GB") with open(DST, 'wb') as o: o.write(hdr); o.write(b'\x00' * pad) base = o.tell() assert (base + new_off[0]) % alignment == 0 for j, si in enumerate(src_idx): want = base + new_off[j] assert o.tell() <= want o.write(b'\x00' * (want - o.tell())) f.seek(data_start + tensors[si][3]) if scale[j] != 1.0: tt = tensors[si][2] assert tt == 0, f"damped tensor {out_names[j]} is type {tt}, expected f32" buf = np.frombuffer(f.read(nbytes[si]), dtype=np.float32) * np.float32(scale[j]) o.write(buf.astype(np.float32).tobytes()) else: left = nbytes[si] while left: chunk = f.read(min(left, 32 << 20)) if not chunk: raise EOFError o.write(chunk); left -= len(chunk) print("wrote", DST, os.path.getsize(DST), "bytes")