ExtractBench-results / tools /validate.py
boyang-runllama's picture
Super-squash branch 'main' using huggingface_hub
4f24d15
Raw History Blame Contribute Delete
4.98 kB
#!/usr/bin/env python3
"""Validate ``results/<pipeline>/*.jsonl`` before publishing or opening a PR.
python tools/validate.py --benchmark parsebench results/datalab_accurate
Checks: canonical JSON per line, ids sorted and unique, every id exists in the
pinned data revision (``data_manifest.json`` next to ``results/``), required
fields, pipeline name matches the folder, and no private hosts, paths, run ids
or credentials anywhere in the file. Exit 1 on any finding.
Python 3.9+, standard library only.
"""
import argparse
import json
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from hfresults_common import ( # noqa: E402
BENCHMARKS,
canonical_dumps,
check_record,
foreign_hosts,
gate_scan,
load_data_manifest,
read_jsonl,
record_stem,
)
def validate_pipeline(benchmark: str, results_dir: str, manifest, *, max_report: int = 20):
spec = BENCHMARKS[benchmark]
pipeline = os.path.basename(os.path.normpath(results_dir))
findings = []
counts = {}
files = [c for c in spec["categories"] if os.path.exists(os.path.join(results_dir, c + ".jsonl"))]
if not files:
return ["no <category>.jsonl in %s" % results_dir], counts
for name in sorted(os.listdir(results_dir)):
if name.endswith(".jsonl") and name[:-6] not in spec["categories"]:
findings.append("unknown category file %s" % name)
for category in files:
path = os.path.join(results_dir, category + ".jsonl")
previous = None
seen = set()
n = 0
for number, line, record in read_jsonl(path):
n += 1
where = "%s:%d" % (path, number)
if canonical_dumps(record) != line:
findings.append("%s: line is not canonical JSON (re-run pack.py)" % where)
problems = check_record(record, benchmark=benchmark, pipeline=pipeline, category=category)
findings.extend("%s: %s" % (where, p) for p in problems)
if problems:
continue
_group, stem = record_stem(record)
example_id = record["request"]["example_id"]
if example_id in seen:
findings.append("%s: duplicate id %s" % (where, example_id))
seen.add(example_id)
if previous is not None and example_id < previous:
findings.append("%s: ids not sorted (%s after %s)" % (where, example_id, previous))
previous = example_id
if manifest is not None and stem not in manifest["_stems"].get(category, set()):
findings.append("%s: %s is not a document of %s@%s" % (where, example_id, manifest["repo"], manifest["revision"][:8]))
for pattern, snippet in gate_scan(line):
findings.append("%s: private pattern %s: %r" % (where, pattern, snippet))
hosts = foreign_hosts(record)
if hosts:
findings.append("%s: unexpected hosts outside model output: %s" % (where, hosts))
if len(findings) > 5000:
break
counts[category] = n
return findings, counts
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--benchmark", required=True, choices=sorted(BENCHMARKS))
parser.add_argument("results_dirs", nargs="+", help="one or more results/<pipeline> folders")
parser.add_argument("--data-manifest", default=None, help="data_manifest.json (default: ../../data_manifest.json)")
parser.add_argument("--no-ids", action="store_true", help="skip the document id check")
parser.add_argument("--json", action="store_true", help="machine-readable summary")
args = parser.parse_args()
manifest = None
if not args.no_ids:
manifest_path = args.data_manifest or os.path.join(os.path.dirname(os.path.normpath(args.results_dirs[0])), "..", "data_manifest.json")
if os.path.exists(manifest_path):
manifest = load_data_manifest(manifest_path)
else:
print("WARNING: no data_manifest.json at %s; skipping id check" % manifest_path, file=sys.stderr)
exit_code = 0
summary = {}
for results_dir in args.results_dirs:
findings, counts = validate_pipeline(args.benchmark, results_dir, manifest)
summary[results_dir] = {"documents": counts, "findings": len(findings), "first_findings": findings[:20]}
if findings:
exit_code = 1
if not args.json:
status = "FAIL" if findings else "ok"
print("%s %s %s" % (status, results_dir, json.dumps(counts)))
for finding in findings[:20]:
print(" " + finding)
if len(findings) > 20:
print(" ... %d more" % (len(findings) - 20))
if args.json:
print(json.dumps(summary, indent=2))
return exit_code
if __name__ == "__main__":
sys.exit(main())