gpu-head-to-head
aien-dev/gpu-head-to-head
Brought by drakestapleton
gpu-head-to-head / q35-9b / tools / cmp_ssm_a.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Throwaway: max ULP distance between GGUF ssm_a and -exp(A_log) from the HF safetensors, per layer.""" |
| 3 | import glob, sys, re |
| 4 | from safetensors import safe_open |
| 5 | import numpy as np, torch, gguf |
| 6 | HF, GG = sys.argv[1], sys.argv[2] |
| 7 | alog = {} |
| 8 | for f in sorted(glob.glob(HF + "/model.safetensors-*.safetensors")): |
| 9 | with safe_open(f, "pt") as s: |
| 10 | for k in s.keys(): |
| 11 | m = re.search(r"layers\.(\d+)\.linear_attn\.A_log$", k) |
| 12 | if m and "mtp" not in k: |
| 13 | alog[int(m.group(1))] = s.get_tensor(k) |
| 14 | worst = 0 |
| 15 | for t in gguf.GGUFReader(GG).tensors: |
| 16 | m = re.match(r"blk\.(\d+)\.ssm_a$", t.name) |
| 17 | if not m: |
| 18 | continue |
| 19 | g = np.sort(np.array(t.data, dtype=np.float32).ravel()) # converter may reorder value heads |
| 20 | ref = np.sort((-torch.exp(alog[int(m.group(1))].float())).numpy().ravel()) |
| 21 | ulp = np.abs(g.view(np.int32).astype(np.int64) - ref.view(np.int32).astype(np.int64)).max() |
| 22 | rel = np.abs(g - ref).max() / np.abs(ref).max() |
| 23 | worst = max(worst, ulp) |
| 24 | print(t.name, "max_ulp", ulp, "max_rel", "%.3g" % rel, "A_log dtype", alog[int(m.group(1))].dtype) |
| 25 | print("WORST_ULP", worst) |