Download src/linedet/plan_long.py from treoa/turkic-ocr-code: direct link, hf CLI and curl.
- Browser
- Download file 4.12 kB
-
https://huggingface.co/treoa/turkic-ocr-code/resolve/main/src/linedet/plan_long.py
- Command line
-
hf download hf://treoa/turkic-ocr-code/src/linedet/plan_long.py
-
curl -L -o plan_long.py https://huggingface.co/treoa/turkic-ocr-code/resolve/main/src/linedet/plan_long.py
4.12 kB
| """Choose one common P_long for all arms from the measured makespan of phase 1, so that every arm finishes its (fully annealed) phase 2 with | |
| the same number of samples before the training end time. If DATA/outputs/r5/linedet/p_long_override.json exists (key p_long_final) it is used instead. | |
| The five arms share one GPU as separate processes. During phase 1 they ran together and kept the GPU busy, so the sum over arms of the | |
| alone-equivalent GPU seconds per sample is (time from the first training start to the last short checkpoint) / (samples of phase 1). Phase 2 | |
| is work-conserving, so E extra samples per arm need E x that sum of GPU seconds: E = safety x (end - now) / sum (safety 0.75 covers predictions, | |
| evaluations and detector jobs that share the GPU in phase 2). P_long = samples at the short checkpoint + E, rounded down to a multiple of 128 and | |
| capped at --max (2,000,000, the P_long of amendment 3.2 C3). No assumption about how the GPU time was shared is needed. | |
| """ | |
| import argparse | |
| import datetime | |
| import json | |
| import os | |
| from .common import ARMS, LD_CKPT, LD_OUT, load_json, save_json, utcnow | |
| def parse_t(x): | |
| return datetime.datetime.strptime(x, "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=datetime.timezone.utc).timestamp() | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--end_utc", required=True) | |
| ap.add_argument("--safety", type=float, default=0.75) | |
| ap.add_argument("--max", type=int, default=2000000) | |
| ap.add_argument("--out", default=LD_OUT + "/p_long_plan.json") | |
| ap.add_argument("--override", default=LD_OUT + "/p_long_override.json") | |
| a = ap.parse_args() | |
| if os.path.exists(a.override): | |
| d = load_json(a.override) | |
| save_json(a.out, {"created": utcnow(), "p_long_final": int(d["p_long_final"]), "source": "override file " + a.override, "override": d}) | |
| print("override used:", d) | |
| return | |
| now = datetime.datetime.now(datetime.timezone.utc).timestamp() | |
| end = parse_t(a.end_utc) | |
| seen_short, t_start, t_short, used = {}, {}, {}, [] | |
| for arm in ARMS: | |
| if not os.path.exists("%s/%s/short.pt" % (LD_CKPT, arm)): | |
| continue | |
| for line in open("%s/train_logs/%s.jsonl" % (LD_OUT, arm)): | |
| try: | |
| d = json.loads(line) | |
| except Exception: | |
| continue | |
| if d.get("event") == "start" and arm not in t_start: | |
| t_start[arm] = parse_t(d["t"]) | |
| if d.get("event") == "saved" and d.get("ckpt") == "short": | |
| seen_short[arm] = d["samples_seen"] | |
| t_short[arm] = parse_t(d["t"]) | |
| if arm in seen_short and arm in t_start: | |
| used.append(arm) | |
| base = max(seen_short.values()) if seen_short else 150016 | |
| t0 = min(t_start[x] for x in used) if used else now | |
| t1 = max(t_short[x] for x in used) if used else now | |
| # makespan of phase 1: the five arms ran together and the GPU was busy, so the sum of the alone-equivalent GPU seconds per sample of all arms | |
| # is (t1 - t0) / samples of phase 1; no assumption about how the GPU time was shared is needed | |
| csum = (t1 - t0) / float(base) if used else 0.0 | |
| t_rem = max(end - now, 0.0) | |
| extra = a.safety * t_rem / csum if csum > 0 else 0.0 | |
| p_long = int(min(a.max, base + extra) // 128 * 128) | |
| p_long = max(p_long, base) | |
| plan = {"created": utcnow(), "end_utc": a.end_utc, "seconds_left": t_rem, "safety": a.safety, "arms_used": used, | |
| "phase1_start_first_arm": t0, "phase1_end_last_arm": t1, "phase1_makespan_s": t1 - t0, "samples_at_short": seen_short, | |
| "sum_cost_gpu_s_per_sample_all_arms": csum, "extra_samples_per_arm": extra, | |
| "p_long_final": p_long, "p_long_planned_by_amendment": a.max, "capped_by_planned": p_long >= a.max, | |
| "method": "extra samples per arm = safety x seconds left / (phase-1 makespan / samples of phase 1); phase 2 is work-conserving; the safety factor covers predictions, evaluations and detector jobs that share the GPU in phase 2"} | |
| save_json(a.out, plan) | |
| print(json.dumps(plan, indent=1)) | |
| if __name__ == "__main__": | |
| main() | |