Download scripts/finalize_endless_gate1_candidate.py from laion/endless-terminals-rootless-apptainer: direct link, hf CLI and curl.
- Browser
- Download file 8.93 kB
-
https://huggingface.co/datasets/laion/endless-terminals-rootless-apptainer/resolve/main/scripts/finalize_endless_gate1_candidate.py
- Command line
-
hf download hf://datasets/laion/endless-terminals-rootless-apptainer/scripts/finalize_endless_gate1_candidate.py
-
curl -L -o finalize_endless_gate1_candidate.py https://huggingface.co/datasets/laion/endless-terminals-rootless-apptainer/resolve/main/scripts/finalize_endless_gate1_candidate.py
8.93 kB
| #!/usr/bin/env python3 | |
| """Freeze an exact Endless Gate-1 candidate after discovery replacements.""" | |
| from __future__ import annotations | |
| import argparse | |
| import hashlib | |
| import json | |
| import shutil | |
| from pathlib import Path | |
| from typing import Any | |
| def parse_args() -> argparse.Namespace: | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--initial-selection", type=Path, required=True) | |
| parser.add_argument("--replacement-selection", type=Path, required=True) | |
| parser.add_argument("--exclusions", type=Path, required=True) | |
| parser.add_argument("--replacements", type=Path, required=True) | |
| parser.add_argument("--replacement-oracle-dir", type=Path, required=True) | |
| parser.add_argument("--replacement-nop-dir", type=Path, required=True) | |
| parser.add_argument("--source-root", type=Path, required=True) | |
| parser.add_argument("--output-root", type=Path, required=True) | |
| parser.add_argument("--release-label", default="Endless-Terminals-1K") | |
| return parser.parse_args() | |
| def sha256_file(path: Path) -> str: | |
| digest = hashlib.sha256() | |
| with path.open("rb") as stream: | |
| for block in iter(lambda: stream.read(1024 * 1024), b""): | |
| digest.update(block) | |
| return digest.hexdigest() | |
| def load_jsonl(path: Path) -> list[dict[str, Any]]: | |
| records: list[dict[str, Any]] = [] | |
| with path.open() as stream: | |
| for line_number, line in enumerate(stream, start=1): | |
| if not line.strip(): | |
| continue | |
| record = json.loads(line) | |
| if not isinstance(record, dict) or not record.get("task"): | |
| raise SystemExit(f"Invalid record at {path}:{line_number}") | |
| records.append(record) | |
| return records | |
| def load_tasks(path: Path) -> list[str]: | |
| tasks = [ | |
| line.strip() | |
| for line in path.read_text().splitlines() | |
| if line.strip() and not line.lstrip().startswith("#") | |
| ] | |
| if len(tasks) != len(set(tasks)): | |
| raise SystemExit(f"Duplicate task in manifest: {path}") | |
| return tasks | |
| def trial_results(path: Path) -> dict[str, dict[str, Any]]: | |
| results: dict[str, dict[str, Any]] = {} | |
| for result_path in sorted(path.glob("task_*/result.json")): | |
| result = json.loads(result_path.read_text()) | |
| task = str(result.get("task_name", "")) | |
| if not task or task in results: | |
| raise SystemExit(f"Missing or duplicate task result under {path}: {task!r}") | |
| results[task] = result | |
| return results | |
| def write_jsonl(path: Path, records: list[dict[str, Any]]) -> None: | |
| path.write_text( | |
| "".join(json.dumps(record, sort_keys=True) + "\n" for record in records) | |
| ) | |
| def main() -> int: | |
| args = parse_args() | |
| initial_path = args.initial_selection.expanduser().resolve() | |
| replacement_path = args.replacement_selection.expanduser().resolve() | |
| exclusions_path = args.exclusions.expanduser().resolve() | |
| replacements_path = args.replacements.expanduser().resolve() | |
| source_root = args.source_root.expanduser().resolve() | |
| output_root = args.output_root.expanduser().resolve() | |
| if output_root.exists(): | |
| raise SystemExit(f"Refusing to replace output directory: {output_root}") | |
| initial = load_jsonl(initial_path) | |
| replacement_pool = load_jsonl(replacement_path) | |
| exclusions = load_jsonl(exclusions_path) | |
| replacements = load_tasks(replacements_path) | |
| initial_by_task = {str(record["task"]): record for record in initial} | |
| replacement_by_task = {str(record["task"]): record for record in replacement_pool} | |
| excluded_by_task = {str(record["task"]): record for record in exclusions} | |
| if len(initial_by_task) != len(initial): | |
| raise SystemExit("Initial selection contains duplicate tasks") | |
| if len(excluded_by_task) != len(exclusions): | |
| raise SystemExit("Exclusion ledger contains duplicate tasks") | |
| if set(excluded_by_task) - set(initial_by_task): | |
| raise SystemExit("Exclusion ledger contains tasks outside the initial cohort") | |
| if set(replacements) & set(initial_by_task): | |
| raise SystemExit("Replacement tasks overlap the initial cohort") | |
| if set(replacements) - set(replacement_by_task): | |
| raise SystemExit("Replacement selection metadata is incomplete") | |
| if len(exclusions) != len(replacements): | |
| raise SystemExit("Exclusion and replacement counts differ") | |
| oracle_results = trial_results(args.replacement_oracle_dir.expanduser().resolve()) | |
| nop_results = trial_results(args.replacement_nop_dir.expanduser().resolve()) | |
| if set(oracle_results) != set(replacements) or set(nop_results) != set( | |
| replacements | |
| ): | |
| raise SystemExit("Replacement result coverage does not match the manifest") | |
| for task in replacements: | |
| oracle = oracle_results[task] | |
| nop = nop_results[task] | |
| if ( | |
| oracle.get("exception_info") is not None | |
| or nop.get("exception_info") is not None | |
| ): | |
| raise SystemExit(f"Replacement has an exception: {task}") | |
| if oracle.get("verifier_result", {}).get("rewards", {}).get("reward") != 1: | |
| raise SystemExit(f"Replacement oracle did not pass: {task}") | |
| if nop.get("verifier_result", {}).get("rewards", {}).get("reward") != 0: | |
| raise SystemExit(f"Replacement nop was not rejected: {task}") | |
| accepted: list[dict[str, Any]] = [] | |
| for task, original in initial_by_task.items(): | |
| if task in excluded_by_task: | |
| continue | |
| record = dict(original) | |
| record["release_status"] = "accepted_from_initial_discovery" | |
| accepted.append(record) | |
| for task in replacements: | |
| record = dict(replacement_by_task[task]) | |
| record["release_status"] = "accepted_replacement" | |
| record["replacement_gate"] = { | |
| "oracle_reward": 1, | |
| "nop_reward": 0, | |
| "oracle_trial_uri": oracle_results[task].get("trial_uri"), | |
| "nop_trial_uri": nop_results[task].get("trial_uri"), | |
| } | |
| accepted.append(record) | |
| accepted.sort(key=lambda record: str(record["task"])) | |
| if len(accepted) != len(initial): | |
| raise SystemExit( | |
| f"Expected {len(initial)} final tasks, materialized {len(accepted)}" | |
| ) | |
| try: | |
| selection_dir = output_root / "selection" | |
| tasks_dir = output_root / "tasks" | |
| selection_dir.mkdir(parents=True) | |
| tasks_dir.mkdir() | |
| for record in accepted: | |
| task = str(record["task"]) | |
| source = source_root / task | |
| if not source.is_dir() or not (source / "task.toml").is_file(): | |
| raise SystemExit(f"Task source is missing or invalid: {source}") | |
| (tasks_dir / task).symlink_to(source, target_is_directory=True) | |
| compatible_path = selection_dir / "compatible.jsonl" | |
| copied_exclusions_path = selection_dir / "discovery-exclusions.jsonl" | |
| tasks_path = selection_dir / "tasks.txt" | |
| write_jsonl(compatible_path, accepted) | |
| write_jsonl(copied_exclusions_path, exclusions) | |
| tasks_path.write_text("".join(f"{record['task']}\n" for record in accepted)) | |
| summary = { | |
| "kind": "endless_terminals_gate1_candidate", | |
| "schema_version": 1, | |
| "release_label": args.release_label, | |
| "policy": "initial-minus-classified-failures-plus-next-ranked-paired-clean-v1", | |
| "candidate_not_final_gate1_release": True, | |
| "initial_task_count": len(initial), | |
| "excluded_task_count": len(exclusions), | |
| "replacement_task_count": len(replacements), | |
| "candidate_task_count": len(accepted), | |
| "initial_selection": str(initial_path), | |
| "initial_selection_sha256": sha256_file(initial_path), | |
| "replacement_selection": str(replacement_path), | |
| "replacement_selection_sha256": sha256_file(replacement_path), | |
| "replacement_manifest": str(replacements_path), | |
| "replacement_manifest_sha256": sha256_file(replacements_path), | |
| "discovery_exclusions": str(copied_exclusions_path), | |
| "discovery_exclusions_sha256": sha256_file(copied_exclusions_path), | |
| "compatible_manifest": str(compatible_path), | |
| "compatible_manifest_sha256": sha256_file(compatible_path), | |
| "tasks_manifest": str(tasks_path), | |
| "tasks_manifest_sha256": sha256_file(tasks_path), | |
| "tasks_dir": str(tasks_dir), | |
| "source_root": str(source_root), | |
| "source_tasks_unchanged": True, | |
| } | |
| summary_path = selection_dir / "summary.json" | |
| summary_path.write_text(json.dumps(summary, indent=2, sort_keys=True) + "\n") | |
| except Exception: | |
| shutil.rmtree(output_root, ignore_errors=True) | |
| raise | |
| print(json.dumps(summary, indent=2, sort_keys=True)) | |
| return 0 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |