File size: 3,758 Bytes
c9faf30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""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())