turkic-ocr-code / scripts /r5_vlma /make_vlmb_testpass.py
treoa's picture
Release 1
24aaa76
Raw History Blame Contribute Delete
6.14 kB
"""R5 VLMA: build vlmb_eval_vlm_testpass.py, the gated test-pass copy of VLMB's vlmb_eval_vlm.py, by exact text
replacements (each must match exactly once; the source sha256 is checked). VLMB's own file is not changed.
Changes (and nothing else): docstring; --test-pass / --gate-file / --not-before-utc arguments and the gate check before
the adapter or any manifest is loaded (same rule as score.py: locked sets need --test-pass AND the gate file); the
registry filter accepts ONLY locked sets; manifest rows must carry the registry entry's split; no gate-G memory
measurement (clarification H6 uses validation inputs); the bookkeeping goes to <ckpt>/meta_testpass.json and
<ckpt>_retry/meta_testpass.json (the validation meta.json, which the scorer reads, stays as it is); no CER/WER is
computed or printed (the scorer does that); clarification J4: an unfinished cap-retry set is written only as
<set>.partial.tsv (the greedy folder already writes unfinished sets that way).
usage: make_vlmb_testpass.py SOURCE OUT
"""
import hashlib
import sys
src, out = sys.argv[1:3]
SRC_SHA = "fb4df8ee5e703ef6be28f79c1766f39844ddf4a59eb1a2b1483db9531147bb30"
s = open(src, encoding="utf-8").read()
assert hashlib.sha256(s.encode("utf-8")).hexdigest() == SRC_SHA, "vlmb_eval_vlm.py changed since it was read"
R = [
('"""R5 VLMB: validation predictions of one checkpoint on every VALIDATION set of the registry (never a test set).\n',
'"""R5 TEST-PASS copy of VLMB\'s vlmb_eval_vlm.py (sha256 ' + SRC_SHA + '), made by VLMA\'s make_vlmb_testpass.py\n'
'(test-pass plan, Part B): predictions of one maker-code checkpoint on LOCKED sets only, with the gate of\n'
'score.py (--test-pass AND the gate file DATA/outputs/r5/RULE/test_pass_started.txt), decided before the adapter or any\n'
'manifest is loaded. Decoding, caps, batches, outputs and the cap-retry folder are those of the validation script;\n'
'bookkeeping goes to meta_testpass.json; no CER is computed or printed. Original docstring follows.\n\n'
'R5 VLMB: validation predictions of one checkpoint on every VALIDATION set of the registry (never a test set).\n'),
('args = ap.parse_args()\n',
'ap.add_argument("--test-pass", action="store_true")\n'
'ap.add_argument("--gate-file", default="/path/to/TURKIC_OCR_DATA/outputs/r5/RULE/test_pass_started.txt")\n'
'ap.add_argument("--not-before-utc", default="2026-11-24T12:00:00Z")\n'
'args = ap.parse_args()\n'
'# ---- TEST-PASS GATE (VLMA): before the adapter, the registry or any manifest is opened ----\n'
'_gate_open = bool(args.test_pass) and os.path.exists(args.gate_file)\n'
'if not _gate_open:\n'
' print("REFUSED: locked sets need --test-pass AND the gate file %s (test_pass=%s, exists=%s); nothing opened" %\n'
' (args.gate_file, bool(args.test_pass), os.path.exists(args.gate_file)), flush=True)\n'
' sys.exit(3)\n'
'if os.path.realpath(args.evalsets or "") == os.path.realpath("/path/to/TURKIC_OCR_DATA/data/processed/r5/evalsets.json") and \\\n'
' time.time() < datetime.strptime(args.not_before_utc, "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=timezone.utc).timestamp():\n'
' print("REFUSED: the real registry is not read before %s" % args.not_before_utc, flush=True)\n'
' sys.exit(3)\n'
'if not [s for s in args.sets.split(",") if s]:\n'
' print("REFUSED: --sets must name the locked sets", flush=True)\n'
' sys.exit(4)\n'),
(' if e.get("locked") is True or str(e.get("split", "")).lower() not in VAL:\n'
' refused.append({"name": name, "why": "locked or registry split is not val"})\n',
' if not (e.get("locked") is True or str(e.get("split", "")).lower() == "test"):\n'
' refused.append({"name": name, "why": "not a locked set (validation goes through vlmb_eval_vlm.py)"})\n'),
('meta_path = os.path.join(out_dir, "meta.json")\n',
'meta_path = os.path.join(out_dir, "meta_testpass.json") # TEST-PASS: the validation meta.json is left as it is\n'),
('if todo and use_cuda and "peak_mem_reserved_batch1_GiB" not in meta:\n',
'if False: # TEST-PASS: no gate-G memory measurement on test inputs (H6 uses validation inputs)\n'),
(' sc = pp.score([r["text"] for r in rows], [final[r["id"]]["raw"] for r in rows])\n',
' sc = {"plain": {"cer": None, "wer": None}, "pp": {"cer": None, "wer": None}} # TEST-PASS: the scorer computes figures\n'),
(' mp = os.path.join(rdir, "meta.json")\n',
' mp = os.path.join(rdir, "meta_testpass.json") # TEST-PASS\n'),
(' bad = [r for r in rows if str(r.get("split", "")).lower() not in VAL]\n',
' bad = [r for r in rows if str(r.get("split", "")).lower() != str(e.get("split", "")).lower()] # TEST-PASS\n'),
(' refused.append({"name": name, "why": "%d manifest rows with split != val" % len(bad)})\n',
' refused.append({"name": name, "why": "%d manifest rows with split != the registry split" % len(bad)})\n'),
(' sc = pp.score(refs, raws)\n',
' sc = {"plain": {"cer": None, "wer": None}, "pp": {"cer": None, "wer": None}} # TEST-PASS: the scorer computes figures\n'),
# clarification J4 (23 Nov): a test set counts only if all of its rows were written; an unfinished cap-retry set is
# written only as <set>.partial.tsv in the retry folder (the greedy folder already does this for unfinished sets)
(' write_pred_tsv(os.path.join(rdir, name + ".tsv"), rows, final, pp.plain_ref, pp.plain_output)\n',
' _dst, _stale = ((name + ".tsv", name + ".partial.tsv") if complete_r else (name + ".partial.tsv", name + ".tsv")) # TEST-PASS J4\n'
' write_pred_tsv(os.path.join(rdir, _dst), rows, final, pp.plain_ref, pp.plain_output)\n'
' if os.path.exists(os.path.join(rdir, _stale)):\n'
' os.remove(os.path.join(rdir, _stale))\n'),
]
for a, b in R:
n = s.count(a)
assert n == 1, ("expected exactly one match", n, a[:80])
s = s.replace(a, b)
open(out, "w", encoding="utf-8").write(s)
print("written", out, hashlib.sha256(s.encode("utf-8")).hexdigest())