gpu-head-to-head
aien-dev/gpu-head-to-head
Brought by drakestapleton
gpu-head-to-head / q35-9b / tools / cmp_weights2.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Throwaway check, part 2: for GGUF tensors that are not byte-identical to an HF tensor, test whether the |
| 3 | values are the same up to (a) a reordering (sorted-value multiset equal after exact bf16->f32 widening), |
| 4 | (b) +1 (zero-centred RMSNorm stored as 1+w), or (c) -exp(A_log). Prints one line per class.""" |
| 5 | import hashlib, json, sys, glob |
| 6 | from safetensors import safe_open |
| 7 | import torch, gguf |
| 8 | import numpy as np |
| 9 | |
| 10 | HF, GG, PREV = sys.argv[1], sys.argv[2], sys.argv[3] |
| 11 | prev = json.load(open(PREV)) |
| 12 | todo = {n for n, _, _ in prev["not_identical"] if not n.startswith("blk.32.") and not n.startswith("nextn")} |
| 13 | |
| 14 | def key(a): |
| 15 | return hashlib.sha256(np.sort(a.astype(np.float32).ravel()).tobytes()).hexdigest() |
| 16 | |
| 17 | hf = {} |
| 18 | for f in sorted(glob.glob(HF + "/model.safetensors-*.safetensors")): |
| 19 | with safe_open(f, "pt") as s: |
| 20 | for k in s.keys(): |
| 21 | if "visual" in k or k.startswith("mtp."): |
| 22 | continue |
| 23 | t = s.get_tensor(k).float().numpy() |
| 24 | for tag, v in (("same", t), ("plus1", t + np.float32(1)), ("negexp", -np.exp(t))): |
| 25 | hf.setdefault(key(v), []).append((k, tag)) |
| 26 | |
| 27 | r = gguf.GGUFReader(GG) |
| 28 | res = {} |
| 29 | for t in r.tensors: |
| 30 | if t.name not in todo: |
| 31 | continue |
| 32 | a = np.array(t.data) |
| 33 | if t.tensor_type.name == "BF16": |
| 34 | a = (a.view(np.uint16).astype(np.uint32) << 16).view(np.float32) |
| 35 | m = hf.get(key(a)) |
| 36 | cls = (t.name.split(".", 2)[-1], t.tensor_type.name, m[0][1] if m else "NO_MATCH") |
| 37 | res[cls] = res.get(cls, 0) + 1 |
| 38 | for (n, ty, how), c in sorted(res.items()): |
| 39 | print(n, ty, how, "x%d" % c) |