calib-corpora / tools /crosscheck.py
worthant's picture
Restructure into a pool + per-model builds; add Muse-Glimmer-30B build
c9faf30 verified
Raw History Blame Contribute Delete
3.76 kB
"""Pairwise 13-gram intersections between calibration and measurement files.
python tools/crosscheck.py builds/muse-glimmer-30b/calib_train.txt \
builds/muse-glimmer-30b/calib_longctx.txt eval/*/*.txt
Reports each pair twice: the raw count, and the count with the chat template's
own scaffolding removed. The template emits a fixed preamble, a worked example
and a `Reasoning strength:` line into every conversation that declares tools;
those 13-grams belong to the model, not to either corpus, and counting them
makes an agentic measurement corpus look contaminated by an agentic
calibration corpus when the only thing they share is the format.
"""
from __future__ import annotations
import argparse
import json
import os
import sys
import numpy as np
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
import glimmer_fmt as G
import poollib as P
NGRAM = 13
def template_grams() -> set[int]:
dummy = [{"type": "function", "function": {
"name": "ns.fn", "description": "d",
"parameters": {"type": "object", "properties": {}}}}]
out: set[int] = set()
rendered = []
for rs in G.REASONING_STRENGTHS:
for t in (dummy, None):
for msgs in ([{"role": "user", "content": ""},
{"role": "assistant", "content": ""}],
[{"role": "system", "content": ""},
{"role": "user", "content": ""}]):
s = G.render(msgs, tools=t, reasoning_strength=rs,
knowledge_cutoff="2026-01-04",
current_date="2026-08-10")
rendered.append(s)
out |= {int(x) for x in P.shingles(s, NGRAM)}
# Documents are joined with a blank line in the flat files, so the join
# itself produces 13-grams -- `<|eot|>` closing one conversation followed by
# the default system block opening the next. Still the template's text, not
# either corpus's.
for a in rendered:
for b in rendered:
out |= {int(x) for x in P.shingles(a + "\n\n" + b, NGRAM)}
return out
def main() -> int:
ap = argparse.ArgumentParser()
ap.add_argument("files", nargs="+")
ap.add_argument("--json-out")
args = ap.parse_args()
ig = template_grams()
print(f"{len(ig)} {NGRAM}-grams belong to the chat template itself\n")
sh = {}
for f in args.files:
if not os.path.exists(f):
continue
sh[os.path.relpath(f, P.REPO_ROOT)] = np.unique(
P.shingles(open(f, encoding="utf-8").read(), NGRAM))
names, matrix, bad = list(sh), {}, 0
for i, a in enumerate(names):
for b in names[i + 1:]:
inter = np.intersect1d(sh[a], sh[b], assume_unique=True)
adj = int(sum(1 for x in inter.tolist() if x not in ig))
matrix[f"{a} ^ {b}"] = {"shared": int(inter.size),
"excluding_chat_template": adj}
note = ""
if adj:
# a build's two files are drawn from one pool; that is not a leak
if not ("calib" in a and "calib" in b) and \
not (a.startswith(os.path.dirname(b)) or b.startswith(os.path.dirname(a))):
note = " <-- NON-ZERO"
bad += 1
else:
note = " (same side, expected)"
print(f" {a:44} ^ {b:44} {inter.size:7} raw {adj:7} adj{note}")
print(f"\n{bad} calibration/measurement pairs still intersect")
if args.json_out:
with open(args.json_out, "w") as f:
json.dump(matrix, f, indent=2)
f.write("\n")
return 0
if __name__ == "__main__":
sys.exit(main())