Download code/src/prepare_001b_splits.py from PratikBuilds/modelclinic-experiment-001: direct link, hf CLI and curl.
- Browser
- Download file 6.14 kB
-
https://huggingface.co/datasets/PratikBuilds/modelclinic-experiment-001/resolve/main/code/src/prepare_001b_splits.py
- Command line
-
hf download hf://datasets/PratikBuilds/modelclinic-experiment-001/code/src/prepare_001b_splits.py
-
curl -L -o prepare_001b_splits.py https://huggingface.co/datasets/PratikBuilds/modelclinic-experiment-001/resolve/main/code/src/prepare_001b_splits.py
6.14 kB
| """Freeze disjoint 001B discovery, calibration, and held-out MATH-500 rows. | |
| The published MATH-500 test set is reused only as a finite diagnostic source; | |
| its rows were already evaluated by the original authors. This script excludes | |
| all questions selected by earlier ModelClinic pilots and keeps the three new | |
| roles disjoint. It does not run model inference. | |
| """ | |
| from __future__ import annotations | |
| import hashlib | |
| import json | |
| import urllib.request | |
| from pathlib import Path | |
| ROOT = Path(__file__).resolve().parents[1] | |
| EXPERIMENTS = ROOT / "experiments" | |
| DATASET = "HuggingFaceH4/MATH-500" | |
| REVISION = "6e4ed1a2a79af7d8630a6b768ec859cb5af4d3be" | |
| SOURCE_URL = ( | |
| f"https://huggingface.co/datasets/{DATASET}/resolve/{REVISION}/test.jsonl" | |
| ) | |
| EXPECTED_SOURCE_SHA256 = ( | |
| "35dc41080a3680858b27fa7e0533d2d547825316fc5dafe5d316f4ccc5a06132" | |
| ) | |
| SEED = "modelclinic-001b-splits-v1" | |
| SPLIT_SIZES = {"discovery": 4, "calibration": 6, "heldout": 10} | |
| OUTPUTS = { | |
| "discovery": EXPERIMENTS / "001b_discovery_4.jsonl", | |
| "calibration": EXPERIMENTS / "001b_calibration_6.jsonl", | |
| "heldout": EXPERIMENTS / "001b_heldout_10.jsonl", | |
| } | |
| def sha256(data: bytes) -> str: | |
| return hashlib.sha256(data).hexdigest() | |
| def _read_previous_rows() -> set[int]: | |
| manifests = ( | |
| EXPERIMENTS / "math500_pilot_20_manifest.json", | |
| EXPERIMENTS / "long_diagnostic_5_manifest.json", | |
| ) | |
| rows: set[int] = set() | |
| for path in manifests: | |
| manifest = json.loads(path.read_text(encoding="utf-8")) | |
| values = manifest.get("row_indices", manifest.get("source_rows")) | |
| if not isinstance(values, list): | |
| raise ValueError(f"Missing source rows in prior manifest: {path}") | |
| rows.update(int(value) for value in values) | |
| if len(rows) != 25: | |
| raise ValueError(f"Expected exactly 25 distinct prior rows, found {len(rows)}") | |
| return rows | |
| def _load_source() -> tuple[bytes, list[dict[str, object]]]: | |
| request = urllib.request.Request( | |
| SOURCE_URL, headers={"User-Agent": "ModelClinic/001B split freezer"} | |
| ) | |
| with urllib.request.urlopen(request, timeout=30) as response: | |
| raw = response.read() | |
| actual_sha = sha256(raw) | |
| if actual_sha != EXPECTED_SOURCE_SHA256: | |
| raise ValueError( | |
| f"Pinned source hash mismatch: expected {EXPECTED_SOURCE_SHA256}, got {actual_sha}" | |
| ) | |
| rows = [json.loads(line) for line in raw.splitlines() if line.strip()] | |
| if len(rows) != 500: | |
| raise ValueError(f"Expected 500 source rows, found {len(rows)}") | |
| for index, row in enumerate(rows): | |
| for field in ("problem", "solution", "answer", "subject", "level", "unique_id"): | |
| if field not in row: | |
| raise ValueError(f"Source row {index} is missing {field!r}") | |
| return raw, rows | |
| def _selection_order(eligible: list[int]) -> list[int]: | |
| prefix = (SEED + "|").encode("utf-8") | |
| return sorted( | |
| eligible, | |
| key=lambda index: (hashlib.sha256(prefix + str(index).encode("ascii")).digest(), index), | |
| ) | |
| def prepare() -> dict[str, object]: | |
| _raw, source_rows = _load_source() | |
| excluded = _read_previous_rows() | |
| eligible = [index for index in range(len(source_rows)) if index not in excluded] | |
| order = _selection_order(eligible) | |
| needed = sum(SPLIT_SIZES.values()) | |
| selected = order[:needed] | |
| split_rows: dict[str, list[int]] = {} | |
| offset = 0 | |
| for split, size in SPLIT_SIZES.items(): | |
| split_rows[split] = sorted(selected[offset : offset + size]) | |
| offset += size | |
| all_rows = [row for values in split_rows.values() for row in values] | |
| if len(all_rows) != len(set(all_rows)) or set(all_rows) & excluded: | |
| raise AssertionError("Split overlap or previous-run leakage detected") | |
| file_hashes: dict[str, str] = {} | |
| for split, indices in split_rows.items(): | |
| content = b"".join( | |
| ( | |
| json.dumps( | |
| {"source_row": index, **source_rows[index]}, | |
| ensure_ascii=False, | |
| separators=(",", ":"), | |
| ) | |
| + "\n" | |
| ).encode("utf-8") | |
| for index in indices | |
| ) | |
| OUTPUTS[split].write_bytes(content) | |
| file_hashes[OUTPUTS[split].name] = sha256(content) | |
| manifest: dict[str, object] = { | |
| "dataset": DATASET, | |
| "revision": REVISION, | |
| "source_url": SOURCE_URL, | |
| "source_sha256": EXPECTED_SOURCE_SHA256, | |
| "source_row_count": len(source_rows), | |
| "excluded_previous_rows": sorted(excluded), | |
| "selection_algorithm": "sort eligible row indices by SHA-256(seed + '|' + decimal row index), tie-break by row index; assign first 4/6/10 to discovery/calibration/heldout; sort rows within each split", | |
| "seed": SEED, | |
| "selection_order": selected, | |
| "split_rows": split_rows, | |
| "split_file_sha256": file_hashes, | |
| "notes": [ | |
| "All 25 rows used in earlier ModelClinic pilot/long diagnostic were excluded.", | |
| "Discovery, calibration, and held-out splits are pairwise disjoint.", | |
| "MATH-500 was already evaluated by the original authors; this is not a novel benchmark or unseen-item claim.", | |
| "Freeze the intervention and analysis plan before inspecting held-out generations.", | |
| "Planned split exception: the current independent-direction artifact is derived from the prior five long-diagnostic traces, not discovery_4; its early-versus-late comparison is confounded and its 8-gram proxy is post-hoc, so it does not support a productive-versus-repetitive causal claim.", | |
| ], | |
| } | |
| manifest_path = EXPERIMENTS / "001b_splits_manifest.json" | |
| manifest_path.write_text(json.dumps(manifest, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") | |
| return manifest | |
| if __name__ == "__main__": | |
| prepared = prepare() | |
| print(json.dumps({"split_rows": prepared["split_rows"], "split_file_sha256": prepared["split_file_sha256"]}, indent=2)) | |