olmo-igsm-arith / verify_dataset.py
Mihara-bot's picture
Initial OLMo iGSM-Easy Arithmetic release
ff400bd verified
Raw
History Blame Contribute Delete
4.31 kB
#!/usr/bin/env python3
"""Verify the frozen OLMo iGSM-Easy Arithmetic release artifact."""
from __future__ import annotations
import hashlib
import importlib.util
import json
from collections import Counter, defaultdict
from pathlib import Path
ROOT = Path(__file__).resolve().parent
MANIFEST_PATH = ROOT / "manifest.json"
REQUIRED_FIELDS = {
"id",
"mod",
"ops_mode",
"question",
"preamble",
"equations",
"query",
"answer",
"cot",
"operator_table",
"target_depth",
"achieved_depth",
"num_vars",
"num_necessary",
"num_distractors",
"var_layer",
}
def sha256(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as stream:
for chunk in iter(lambda: stream.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def load_generator(path: Path):
spec = importlib.util.spec_from_file_location("igsm_release_generator", path)
if spec is None or spec.loader is None:
raise RuntimeError(f"Could not import generator from {path}")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def main() -> None:
manifest = json.loads(MANIFEST_PATH.read_text(encoding="utf-8"))
for relative_path, expected_digest in manifest["sha256"].items():
path = ROOT / relative_path
actual_digest = sha256(path)
if actual_digest != expected_digest:
raise ValueError(
f"Checksum mismatch for {relative_path}: expected "
f"{expected_digest}, got {actual_digest}"
)
data_path = ROOT / manifest["data_file"]
generator = load_generator(ROOT / manifest["generator_file"])
counts = Counter()
histograms: dict[int, Counter[int]] = defaultdict(Counter)
seen_ids = set()
with data_path.open(encoding="utf-8") as stream:
for line_number, line in enumerate(stream, start=1):
if not line.strip():
continue
record = json.loads(line)
missing = REQUIRED_FIELDS - record.keys()
if missing:
raise ValueError(f"Line {line_number} is missing fields: {sorted(missing)}")
example_id = record["id"]
if example_id in seen_ids:
raise ValueError(f"Duplicate example id: {example_id}")
seen_ids.add(example_id)
depth = int(record["target_depth"])
answer = int(record["answer"])
if depth not in manifest["generation"]["depths"]:
raise ValueError(f"Unexpected target depth {depth} in {example_id}")
if int(record["achieved_depth"]) != depth:
raise ValueError(f"Depth mismatch in {example_id}")
if answer not in range(7):
raise ValueError(f"Answer outside [0, 6] in {example_id}: {answer}")
if record["mod"] != 7 or record["ops_mode"] != "arith":
raise ValueError(f"Unexpected arithmetic mode in {example_id}")
if int(record["num_distractors"]) != 0:
raise ValueError(f"Unexpected distractors in {example_id}")
generator.verify_record(record)
counts[depth] += 1
histograms[depth][answer] += 1
if len(seen_ids) != manifest["total_examples"]:
raise ValueError(
f"Expected {manifest['total_examples']} examples, found {len(seen_ids)}"
)
for depth_text, expected in manifest["depths"].items():
depth = int(depth_text)
if counts[depth] != expected["count"]:
raise ValueError(
f"Depth {depth}: expected {expected['count']} examples, "
f"found {counts[depth]}"
)
expected_histogram = {
int(answer): count
for answer, count in expected["answer_histogram"].items()
}
if dict(histograms[depth]) != expected_histogram:
raise ValueError(
f"Depth {depth}: expected histogram {expected_histogram}, "
f"found {dict(histograms[depth])}"
)
print(
f"Verified {len(seen_ids)} examples across depths "
f"{sorted(counts)}; all checks passed."
)
if __name__ == "__main__":
main()