#!/usr/bin/env python3 """Convergence chain: cos(imatrix@N, imatrix@M) for consecutive checkpoints. If cos(N, 2N) -> 1.000 the extra data changed nothing the quantiser can see, i.e. the corpus is saturated at N chunks and everything beyond is wasted time. """ import sys, numpy as np from gguf import GGUFReader def load(path): r = GGUFReader(path) v = {t.name[:-len(".in_sum2")]: np.array(t.data, dtype=np.float64).reshape(-1) for t in r.tensors if t.name.endswith(".in_sum2")} n = None for f in r.fields.values(): if f.name == "imatrix.chunk_count": n = f.contents() return n, v paths = sys.argv[1:] loaded = [load(p) + (p.split("/")[-1],) for p in paths] loaded.sort(key=lambda x: x[0] or 0) def cos_stats(a, b): common = sorted(set(a) & set(b)) sims = [] for k in common: x, y = a[k], b[k] if x.shape != y.shape: continue nx, ny = np.linalg.norm(x), np.linalg.norm(y) if nx and ny: sims.append(float(np.dot(x, y) / (nx * ny))) s = np.array(sims) return s print("%-12s %-12s %10s %10s %10s %8s" % ("from", "to", "mean cos", "median", "min", "<0.99")) print("-" * 68) for i in range(len(loaded) - 1): n1, v1, _ = loaded[i] n2, v2, _ = loaded[i + 1] s = cos_stats(v1, v2) print("%-12s %-12s %10.6f %10.6f %10.6f %8d" % ( "%d ch" % n1, "%d ch" % n2, s.mean(), np.median(s), s.min(), int((s < 0.99).sum()))) print() print("against the FINAL imatrix (%d chunks):" % loaded[-1][0]) print("%-12s %10s %10s %10s %8s" % ("checkpoint", "mean cos", "median", "min", "<0.99")) print("-" * 56) nf, vf, _ = loaded[-1] for n, v, _ in loaded[:-1]: s = cos_stats(v, vf) print("%-12s %10.6f %10.6f %10.6f %8d" % ( "%d ch" % n, s.mean(), np.median(s), s.min(), int((s < 0.99).sum())))