interplane.Get Omega
Read in your language

gpu-head-to-head

aien-dev/gpu-head-to-head

Brought by drakestapleton

branch main Branches Tags History

gpu-head-to-head / q35-9b / tools / cmp_weights2.py

39 lines · 1601 bytes Raw Blame History
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
3values 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."""
5import hashlib, json, sys, glob
6from safetensors import safe_open
7import torch, gguf
8import numpy as np
9
10HF, GG, PREV = sys.argv[1], sys.argv[2], sys.argv[3]
11prev = json.load(open(PREV))
12todo = {n for n, _, _ in prev["not_identical"] if not n.startswith("blk.32.") and not n.startswith("nextn")}
13
14def key(a):
15 return hashlib.sha256(np.sort(a.astype(np.float32).ravel()).tobytes()).hexdigest()
16
17hf = {}
18for 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
27r = gguf.GGUFReader(GG)
28res = {}
29for 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
38for (n, ty, how), c in sorted(res.items()):
39 print(n, ty, how, "x%d" % c)