File size: 5,957 Bytes
24aaa76 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | """Task list of the follow-on job r5_ref_finish_L (clarification H12): resume the reference runs of stage REF that were cut short or never started in the
line block (job 51128), from REF's saved state, up to the paper's stopping rule or the 3 h limit of that job.
A run is FINISHED when its train_summary.json (written by REF's training scripts) says paper_rule_reached = true and provisional = false. Every
other run of R1 (HTR-Flor) and R2 (PaddleOCR PP-OCRv3 recogniser), (i), (ii), (iii), is resumed with REF's own command (which always carries
--resume) from DATA/outputs/r5/ref/gpu_tasks.json, with two changes only: the time budget and the budget cap.
- R1 (ref_flor_train.py) counts the budget as cumulative wall time of the run (state.json wall_used_s): --budget_s = wall seconds used so far
(from train_summary.json wall_seconds_total) + the seconds this job can give; --budget_cap_s is lifted (REF's cap of 16,800 s belonged to the line block).
- R2 (ref_ppocr_run.py) counts the budget per session: --budget_s = the seconds this job can give; the cap is lifted.
The prediction and metric tasks of every resumed run are queued after its training (REF's own commands), because the first session's predictions are
stale. If every run is finished the task list is empty and n_unfinished.txt holds 0, so the job exits at once.
Usage: python -m linedet.ref_finish_make --out <tasks json> --count_file <txt> [--job_seconds 10200 --reserve_s 2400]
"""
import argparse
import json
import os
import re
import time
from .common import DATA, LD_OUT, save_json
from .driver import normalize_task
RUNS = [("R1", "i"), ("R1", "ii"), ("R1", "iii"), ("R2", "i"), ("R2", "ii"), ("R2", "iii")]
def arg_after(cmd, flag):
return cmd[cmd.index(flag) + 1] if flag in cmd else None
def set_arg(cmd, flag, value):
cmd = list(cmd)
if flag in cmd:
cmd[cmd.index(flag) + 1] = str(value)
else:
cmd += [flag, str(value)]
return cmd
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--ref_tasks", default=DATA + "/outputs/r5/ref/gpu_tasks.json")
ap.add_argument("--out", default=LD_OUT + "/ref_finish/tasks_ref_finish.json")
ap.add_argument("--count_file", default=LD_OUT + "/ref_finish/n_unfinished.txt")
ap.add_argument("--job_seconds", type=float, default=10200, help="driver stop (the job limit is 3 h = 10,800 s)")
ap.add_argument("--reserve_s", type=float, default=2400, help="seconds before the stop at which a resumed training ends so that predictions fit")
a = ap.parse_args()
os.makedirs(os.path.dirname(a.out), exist_ok=True)
spec = json.load(open(a.ref_tasks))
by = {t["name"]: t for t in spec["tasks"]}
give = max(600.0, a.job_seconds - a.reserve_s - 90) # seconds a resumed training can use in this job
report, selected = {}, []
for arm, run in RUNS:
tn = "ref_%s_%s_train" % (arm, run)
if tn not in by:
report["%s_%s" % (arm, run)] = {"note": "no train task in REF's file"}
continue
out = arg_after(by[tn]["cmd"], "--out")
sp = os.path.join(out, "train_summary.json")
summ = json.load(open(sp)) if os.path.exists(sp) else None
finished = bool(summ and summ.get("paper_rule_reached") and not summ.get("provisional"))
report["%s_%s" % (arm, run)] = {"out": out, "summary_exists": summ is not None, "finished": finished,
"stopped_because": summ.get("stopped_because") if summ else None,
"samples_seen": summ.get("samples_seen") if summ else None,
"wall_seconds_total": (summ or {}).get("wall_seconds_total")}
if not finished:
selected.append((arm, run, summ))
tasks = []
names = set()
need_warm = False
for arm, run, summ in selected:
pre = "ref_%s_%s_" % (arm, run)
for t in spec["tasks"]:
if not t["name"].startswith(pre):
continue
nt = normalize_task(t)
if nt["name"].endswith("_train"):
cmd = nt["cmd"]
if arm == "R1":
used = float((summ or {}).get("wall_seconds_total") or 0.0)
cmd = set_arg(cmd, "--budget_s", int(used + give))
else:
cmd = set_arg(cmd, "--budget_s", int(give))
cmd = set_arg(cmd, "--budget_cap_s", 10 ** 7)
nt["cmd"] = cmd
nt["reserve_s"] = a.reserve_s
nt["max_runtime_s"] = a.job_seconds
if "ref_warm" in nt.get("after", []):
need_warm = True
nt["_orig_after"] = list(nt.get("after", []))
tasks.append(nt)
names.add(nt["name"])
if need_warm and "ref_warm" in by:
w = normalize_task(by["ref_warm"])
tasks.insert(0, w)
names.add("ref_warm")
for nt in tasks:
nt["after"] = [x for x in nt.get("after", []) if x in names]
nt.pop("_orig_after", None)
out = {"stop_utc": "2099-01-01T00:00:00Z", "gpu_budget_gb": 70, "ram_budget_gb": 140, "cpu_budget": 40, "grace_s": 300,
"priority_order": ["ref_warm", "ref_R1_iii_", "ref_R2_iii_", "ref_R1_i_", "ref_R2_i_", "ref_R1_ii_", "ref_R2_ii_"], "tasks": tasks}
json.dump(out, open(a.out, "w"), indent=1)
open(a.count_file, "w").write("%d\n" % len(selected))
save_json(os.path.dirname(a.out) + "/ref_finish_selection.json", {"created_unix": time.time(), "runs": report, "unfinished": ["%s_%s" % (x[0], x[1]) for x in selected],
"seconds_given_to_each_resumed_training": give, "tasks": [t["name"] for t in tasks]})
print(json.dumps({"unfinished": ["%s_%s" % (x[0], x[1]) for x in selected], "tasks": len(tasks), "seconds_given": give}, indent=1))
if __name__ == "__main__":
main()
|