| |
| """Compare imatrix files directly. |
| |
| Two things it answers: |
| 1) DEAD EXPERTS - using the authoritative `.counts` array (how many times each |
| expert was actually routed to). counts[i]==0 => expert i never used. |
| 2) AGREEMENT - cosine similarity between the SAME tensor in two imatrix files. |
| cos ~ 1.000 => the two runs produce an interchangeable importance vector, |
| i.e. the extra data changed nothing the quantiser can see. |
| """ |
| import sys, numpy as np |
| from gguf import GGUFReader |
|
|
| def load(path): |
| r = GGUFReader(path) |
| vals, cnts = {}, {} |
| for t in r.tensors: |
| arr = np.array(t.data, dtype=np.float64).reshape(-1) |
| if t.name.endswith(".in_sum2"): |
| vals[t.name[:-len(".in_sum2")]] = arr |
| elif t.name.endswith(".counts"): |
| cnts[t.name[:-len(".counts")]] = arr |
| meta = {f.name: f for f in r.fields.values()} |
| nchunk = None |
| if "imatrix.chunk_count" in meta: |
| nchunk = meta["imatrix.chunk_count"].contents() |
| return vals, cnts, nchunk |
|
|
| paths = sys.argv[1:] |
| data = {} |
| for p in paths: |
| lbl = p.split("/")[-1].replace(".gguf", "") |
| data[lbl] = load(p) |
| print("%-34s chunks=%s tensors=%d" % (lbl, data[lbl][2], len(data[lbl][0]))) |
|
|
| print() |
| print("=" * 92) |
| print("1) DEAD EXPERTS (counts[i] == 0 => expert never routed to)") |
| print("=" * 92) |
| print("%-34s %14s %16s %14s" % ("imatrix", "expert tensors", "total experts", "DEAD")) |
| for lbl, (vals, cnts, _) in data.items(): |
| ntens = tot = dead = 0 |
| for name, c in cnts.items(): |
| if "exps" not in name: |
| continue |
| ntens += 1 |
| tot += c.size |
| dead += int((c == 0).sum()) |
| print("%-34s %14d %16d %14d" % (lbl, ntens, tot, dead)) |
|
|
| print() |
| print("=" * 92) |
| print("2) AGREEMENT between imatrices (cosine similarity, per tensor, then summarised)") |
| print("=" * 92) |
| labels = list(data) |
| base = labels[0] |
| for other in labels[1:]: |
| va, vb = data[base][0], data[other][0] |
| common = sorted(set(va) & set(vb)) |
| sims, worst = [], [] |
| for k in common: |
| a, b = va[k], vb[k] |
| if a.shape != b.shape: |
| continue |
| na, nb = np.linalg.norm(a), np.linalg.norm(b) |
| if na == 0 or nb == 0: |
| continue |
| s = float(np.dot(a, b) / (na * nb)) |
| sims.append(s) |
| worst.append((s, k)) |
| sims = np.array(sims) |
| worst.sort() |
| print() |
| print("%s vs %s (%d tensors compared)" % (base, other, len(sims))) |
| print(" mean cos = %.6f median = %.6f min = %.6f" % ( |
| sims.mean(), np.median(sims), sims.min())) |
| print(" tensors with cos < 0.99: %d" % int((sims < 0.99).sum())) |
| print(" 5 worst:") |
| for s, k in worst[:5]: |
| print(" %.6f %s" % (s, k)) |
|
|