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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Run all frozen Fable donor-bank structural smokes sequentially.
|
| 2 |
+
|
| 3 |
+
This is an interactive cloud-notebook convenience wrapper, not a scheduler and
|
| 4 |
+
not a training entry point. Each bank executes in a fresh subprocess so CUDA
|
| 5 |
+
state is released between candidates. Every child persists its own append-only
|
| 6 |
+
pass/fail evidence; the wrapper additionally uploads one campaign summary.
|
| 7 |
+
"""
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
|
| 10 |
+
import argparse
|
| 11 |
+
import json
|
| 12 |
+
import os
|
| 13 |
+
import subprocess
|
| 14 |
+
import sys
|
| 15 |
+
from datetime import datetime, timezone
|
| 16 |
+
from pathlib import Path
|
| 17 |
+
from typing import Any
|
| 18 |
+
|
| 19 |
+
from huggingface_hub import HfApi
|
| 20 |
+
|
| 21 |
+
from fable_router_common import read_json
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
DEFAULT_BANKS = (
|
| 25 |
+
"kat-coder-v25-dev-q4km",
|
| 26 |
+
"qwen36-35b-base-q4km",
|
| 27 |
+
"ornith10-35b-q4km",
|
| 28 |
+
"qwen35-35b-base-q4km",
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def now() -> str:
|
| 33 |
+
return datetime.now(timezone.utc).isoformat()
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def run_bank(command: list[str]) -> tuple[int, list[str]]:
|
| 37 |
+
lines: list[str] = []
|
| 38 |
+
process = subprocess.Popen(
|
| 39 |
+
command,
|
| 40 |
+
stdout=subprocess.PIPE,
|
| 41 |
+
stderr=subprocess.STDOUT,
|
| 42 |
+
text=True,
|
| 43 |
+
bufsize=1,
|
| 44 |
+
)
|
| 45 |
+
assert process.stdout is not None
|
| 46 |
+
for raw in process.stdout:
|
| 47 |
+
line = raw.rstrip("\n")
|
| 48 |
+
lines.append(line)
|
| 49 |
+
print(line, flush=True)
|
| 50 |
+
return process.wait(), lines
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def result_from_output(lines: list[str]) -> tuple[str | None, dict[str, Any] | None]:
|
| 54 |
+
for line in reversed(lines):
|
| 55 |
+
candidate = Path(line.strip())
|
| 56 |
+
if candidate.name != "result.json" or not candidate.is_file():
|
| 57 |
+
continue
|
| 58 |
+
try:
|
| 59 |
+
payload = json.loads(candidate.read_text(encoding="utf-8"))
|
| 60 |
+
except (OSError, json.JSONDecodeError):
|
| 61 |
+
return str(candidate), None
|
| 62 |
+
return str(candidate), payload if isinstance(payload, dict) else None
|
| 63 |
+
return None, None
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def main() -> int:
|
| 67 |
+
parser = argparse.ArgumentParser(description=__doc__)
|
| 68 |
+
parser.add_argument("--owner-execute", action="store_true")
|
| 69 |
+
parser.add_argument("--config", type=Path, required=True)
|
| 70 |
+
parser.add_argument("--platform", choices=("colab", "kaggle", "other"), default="other")
|
| 71 |
+
parser.add_argument("--work-dir", type=Path, required=True)
|
| 72 |
+
parser.add_argument("--runner", type=Path, required=True)
|
| 73 |
+
parser.add_argument("--source-revision", required=True)
|
| 74 |
+
parser.add_argument("--banks", nargs="+", default=list(DEFAULT_BANKS))
|
| 75 |
+
parser.add_argument("--continue-on-failure", action="store_true")
|
| 76 |
+
args = parser.parse_args()
|
| 77 |
+
if not args.owner_execute:
|
| 78 |
+
raise SystemExit("refusing GPU/model execution without --owner-execute")
|
| 79 |
+
|
| 80 |
+
config = read_json(args.config.resolve())
|
| 81 |
+
if config.get("trainingAuthorized") is not False or config.get("nonRouting") is not True:
|
| 82 |
+
raise SystemExit("campaign requires an explicitly non-routing, training-disabled config")
|
| 83 |
+
configured = set(config["banks"]["artifacts"])
|
| 84 |
+
if len(args.banks) != len(set(args.banks)) or not set(args.banks).issubset(configured):
|
| 85 |
+
raise SystemExit("campaign banks must be unique configured bank IDs")
|
| 86 |
+
token = os.environ.get("HF_TOKEN")
|
| 87 |
+
if not token:
|
| 88 |
+
raise SystemExit("HF_TOKEN is required for private evidence persistence")
|
| 89 |
+
|
| 90 |
+
stamp = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ")
|
| 91 |
+
output = args.work_dir.resolve() / f"fable-router-structural-campaign-{stamp}"
|
| 92 |
+
output.mkdir(parents=True, exist_ok=False)
|
| 93 |
+
summary: dict[str, Any] = {
|
| 94 |
+
"schema": "AutonomaFableRouterStructuralCampaign.v1",
|
| 95 |
+
"status": "running_nonrouting",
|
| 96 |
+
"nonRouting": True,
|
| 97 |
+
"trainingAuthorized": False,
|
| 98 |
+
"startedAt": now(),
|
| 99 |
+
"platform": args.platform,
|
| 100 |
+
"sourceRevision": args.source_revision,
|
| 101 |
+
"bankOrder": list(args.banks),
|
| 102 |
+
"continueOnFailure": bool(args.continue_on_failure),
|
| 103 |
+
"runs": [],
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
for index, bank in enumerate(args.banks, start=1):
|
| 107 |
+
print(f"\n=== [{index}/{len(args.banks)}] {bank} ===", flush=True)
|
| 108 |
+
started = now()
|
| 109 |
+
command = [
|
| 110 |
+
sys.executable,
|
| 111 |
+
str(args.runner.resolve()),
|
| 112 |
+
"--owner-execute",
|
| 113 |
+
"--config", str(args.config.resolve()),
|
| 114 |
+
"--bank", bank,
|
| 115 |
+
"--platform", args.platform,
|
| 116 |
+
"--work-dir", str(args.work_dir.resolve()),
|
| 117 |
+
]
|
| 118 |
+
returncode, lines = run_bank(command)
|
| 119 |
+
result_path, result = result_from_output(lines)
|
| 120 |
+
row = {
|
| 121 |
+
"bank": bank,
|
| 122 |
+
"startedAt": started,
|
| 123 |
+
"finishedAt": now(),
|
| 124 |
+
"returnCode": returncode,
|
| 125 |
+
"resultPath": result_path,
|
| 126 |
+
"status": result.get("status") if result else "result_unreadable",
|
| 127 |
+
"passed": bool(result and result.get("passed") is True and returncode == 0),
|
| 128 |
+
}
|
| 129 |
+
if result and isinstance(result.get("error"), dict):
|
| 130 |
+
row["error"] = {
|
| 131 |
+
"type": result["error"].get("type"),
|
| 132 |
+
"message": result["error"].get("message"),
|
| 133 |
+
}
|
| 134 |
+
summary["runs"].append(row)
|
| 135 |
+
(output / "result.json").write_text(json.dumps(summary, indent=2) + "\n", encoding="utf-8")
|
| 136 |
+
if returncode and not args.continue_on_failure:
|
| 137 |
+
break
|
| 138 |
+
|
| 139 |
+
all_passed = len(summary["runs"]) == len(args.banks) and all(row["passed"] for row in summary["runs"])
|
| 140 |
+
summary["passed"] = all_passed
|
| 141 |
+
summary["status"] = (
|
| 142 |
+
"campaign_passed_nonrouting" if all_passed else "campaign_completed_with_failures_nonrouting"
|
| 143 |
+
)
|
| 144 |
+
summary["finishedAt"] = now()
|
| 145 |
+
result_path = output / "result.json"
|
| 146 |
+
result_path.write_text(json.dumps(summary, indent=2) + "\n", encoding="utf-8")
|
| 147 |
+
repo = config["smoke"]["resultRepo"]
|
| 148 |
+
remote = f"campaigns/{output.name}/result.json"
|
| 149 |
+
HfApi(token=token).upload_file(
|
| 150 |
+
path_or_fileobj=str(result_path),
|
| 151 |
+
path_in_repo=remote,
|
| 152 |
+
repo_id=repo,
|
| 153 |
+
repo_type="dataset",
|
| 154 |
+
commit_message=f"Persist non-routing structural campaign {output.name}",
|
| 155 |
+
)
|
| 156 |
+
summary["evidenceUpload"] = {"repo": repo, "private": True, "path": remote}
|
| 157 |
+
result_path.write_text(json.dumps(summary, indent=2) + "\n", encoding="utf-8")
|
| 158 |
+
HfApi(token=token).upload_file(
|
| 159 |
+
path_or_fileobj=str(result_path),
|
| 160 |
+
path_in_repo=remote,
|
| 161 |
+
repo_id=repo,
|
| 162 |
+
repo_type="dataset",
|
| 163 |
+
commit_message=f"Finalize non-routing structural campaign {output.name}",
|
| 164 |
+
)
|
| 165 |
+
print(result_path, flush=True)
|
| 166 |
+
return 0 if all_passed else 1
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
if __name__ == "__main__":
|
| 170 |
+
raise SystemExit(main())
|