"""Shared reproducibility helpers for the Swift 1.5 build.""" import hashlib import json import os import time from pathlib import Path ROOT = Path(__file__).resolve().parents[1] RUN = Path(os.environ.get("SWIFT15_RUN_DIR", ROOT / "runs/swift15")).resolve() SOURCE = ROOT / "models/Swift-1.5-Qwen3.8-27B-AWQ-source" BASELINE = ROOT / "models/Swift-1.5-Qwen3.8-27B-W4A16-HyperQwen" FAST = Path(os.environ.get("SWIFT15_FAST_MODEL", ROOT / "models/Swift-1.5-Qwen3.8-27B-W4A16-HyperQwen-fast")).resolve() def read_json(path): return json.loads(Path(path).read_text()) def write_json(path, value): path = Path(path) path.parent.mkdir(parents=True, exist_ok=True) temporary = path.with_name(path.name + ".tmp") temporary.write_text(json.dumps(value, indent=2, ensure_ascii=False) + "\n") os.replace(temporary, path) def sha256(path): h = hashlib.sha256() with open(path, "rb") as f: for chunk in iter(lambda: f.read(8 << 20), b""): h.update(chunk) return h.hexdigest() def records(path): with open(path) as f: return [json.loads(line) for line in f if line.strip()] def write_records(path, rows): path = Path(path) path.parent.mkdir(parents=True, exist_ok=True) with path.open("w") as f: for row in rows: f.write(json.dumps(row, ensure_ascii=False) + "\n") def stamp(): return time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())