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_ssm_a.py

25 lines · 1158 bytes Raw Blame History
1#!/usr/bin/env python3
2"""Throwaway: max ULP distance between GGUF ssm_a and -exp(A_log) from the HF safetensors, per layer."""
3import glob, sys, re
4from safetensors import safe_open
5import numpy as np, torch, gguf
6HF, GG = sys.argv[1], sys.argv[2]
7alog = {}
8for 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)
14worst = 0
15for 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)
25print("WORST_ULP", worst)