Download scripts/compare_logprobs.py from sambit9238/Inkling-2-Mini-NVFP4-M2: direct link, hf CLI and curl.
- Browser
- Download file 2.91 kB
-
https://huggingface.co/sambit9238/Inkling-2-Mini-NVFP4-M2/resolve/main/scripts/compare_logprobs.py
- Command line
-
hf download hf://sambit9238/Inkling-2-Mini-NVFP4-M2/scripts/compare_logprobs.py
-
curl -L -o compare_logprobs.py https://huggingface.co/sambit9238/Inkling-2-Mini-NVFP4-M2/resolve/main/scripts/compare_logprobs.py
2.91 kB
| #!/usr/bin/env python3 | |
| """Compare logprob dumps: KL, top-1 agreement, perplexity. | |
| Reads both the sensenova list schema and the inkling {"meta","seqs"} schema. | |
| Usage: compare_logprobs.py ref.json test.json [out.txt] | |
| """ | |
| import json | |
| import math | |
| import sys | |
| def load(path: str) -> tuple[list, dict]: | |
| with open(path, encoding="utf-8") as f: | |
| d = json.load(f) | |
| return (d["seqs"], d.get("meta", {})) if isinstance(d, dict) else (d, {}) | |
| def main() -> None: | |
| if len(sys.argv) < 3: | |
| print("Usage: compare_logprobs.py ref.json test.json [out.txt]", file=sys.stderr) | |
| sys.exit(1) | |
| a, ma = load(sys.argv[1]) | |
| b, mb = load(sys.argv[2]) | |
| for key in ("reasoning_effort", "set"): | |
| if ma.get(key) != mb.get(key): | |
| print(f"warning: meta {key} differs: {ma.get(key)} vs {mb.get(key)}", file=sys.stderr) | |
| assert len(a) == len(b), "different number of sequences" | |
| kls: list[float] = [] | |
| agree = 0 | |
| n = 0 | |
| nll_a = 0.0 | |
| nll_b = 0.0 | |
| for x, y in zip(a, b): | |
| assert x["ids"] == y["ids"], "tokenization differs - same tokenizer?" | |
| for t, (p, q) in enumerate(zip(x["lp"], y["lp"])): | |
| keys = set(p) | set(q) | |
| fp, fq = min(p.values()), min(q.values()) | |
| p_dist = {k: math.exp(p.get(k, fp)) for k in keys} | |
| q_dist = {k: math.exp(q.get(k, fq)) for k in keys} | |
| sp, sq = sum(p_dist.values()), sum(q_dist.values()) | |
| kls.append( | |
| sum( | |
| (p_dist[k] / sp) * math.log((p_dist[k] / sp) / (q_dist[k] / sq)) | |
| for k in keys | |
| ) | |
| ) | |
| agree += max(p, key=p.get) == max(q, key=q.get) | |
| tok = str(x["ids"][t + 1]) | |
| nll_a -= p.get(tok, fp) | |
| nll_b -= q.get(tok, fq) | |
| n += 1 | |
| if n == 0: | |
| print("no positions to compare", file=sys.stderr) | |
| sys.exit(1) | |
| kls.sort() | |
| mean_kl = sum(kls) / n | |
| p99_kl = kls[int(0.99 * n)] | |
| ppl_a = math.exp(nll_a / n) | |
| ppl_b = math.exp(nll_b / n) | |
| healthy = mean_kl < 0.05 and p99_kl < 0.5 and agree / n >= 0.95 | |
| gate = "PASS" if healthy else "INVESTIGATE" | |
| lines = [ | |
| f"positions : {n}", | |
| f"reasoning_effort : {ma.get('reasoning_effort')} / {mb.get('reasoning_effort')}", | |
| f"mean KL : {mean_kl:.4f}", | |
| f"p99 KL : {p99_kl:.4f}", | |
| f"top-1 agreement : {100 * agree / n:.2f}%", | |
| f"PPL ref : {ppl_a:.3f}", | |
| f"PPL test : {ppl_b:.3f} " | |
| f"(+{100 * (math.exp((nll_b - nll_a) / n) - 1):.2f}%)", | |
| f"gate (healthy KL/PPL/agree): {gate}", | |
| ] | |
| text = "\n".join(lines) + "\n" | |
| print(text, end="") | |
| out_path = sys.argv[3] if len(sys.argv) > 3 else "results/kl_compare.txt" | |
| with open(out_path, "w", encoding="utf-8") as f: | |
| f.write(text) | |
| if __name__ == "__main__": | |
| main() | |