#!/usr/bin/env python3 """Verify a looped GGUF: metadata arrays extended correctly + sandwich-norm scaling exact. usage: check_loop_gguf.py BASE.gguf LOOP.gguf A B [REPS] [ALPHA] """ import os, struct, sys import numpy as np SZ = {0: 1, 1: 1, 2: 2, 3: 2, 4: 4, 5: 4, 6: 4, 7: 1, 10: 8, 11: 8, 12: 8} def read(p): f = open(p, 'rb') assert f.read(4) == b'GGUF' f.read(4) nt, nkv = struct.unpack(' 5 else 2 ALPHA = float(sys.argv[6]) if len(sys.argv) > 6 else 1.0 / REPS fA, sA, TA, kvA = read(base_p) fB, sB, TB, kvB = read(loop_p) W = B - A + 1 NEW = (REPS - 1) * W nb = kvA['lfm3.block_count'] nl = kvB['lfm3.block_count'] print(f"block_count {nb} -> {nl} (expected {nb + NEW}) {'OK' if nl == nb + NEW else 'MISMATCH'}") for key in ('lfm3.attention.head_count_kv', 'lfm3.attention.sliding_window_pattern'): want = list(kvA[key][:B + 1]) + [kvA[key][i] for _ in range(REPS - 1) for i in range(A, B + 1)] + list(kvA[key][B + 1:]) got = list(kvB[key]) print(f"{key}: {'OK' if got == want else 'MISMATCH'} (len {len(got)})") bad = 0 for i in range(A, B + 1): for nm in ('post_attention_norm.weight', 'post_ffw_norm.weight'): a = raw(fA, sA, TA, f'blk.{i}.{nm}') for rep in range(REPS): dst = i + rep * W d = np.abs(raw(fB, sB, TB, f'blk.{dst}.{nm}') - a * np.float32(ALPHA)).max() bad += int(d != 0) print(f"blk.{i}.{nm} -> blk.{dst}: max|got - {ALPHA}*base| = {d:.2e}") # untouched tensor must be byte-identical in the copy a = raw(fA, sA, TA, f'blk.{A}.attn_norm.weight') b = raw(fB, sB, TB, f'blk.{A + W}.attn_norm.weight') print(f"copy of blk.{A}.attn_norm identical: {np.array_equal(a, b)}") print("DAMPING EXACT" if bad == 0 else f"{bad} SCALE MISMATCHES")