turkic-ocr-code / src /pagesynth /scripts /run_chunks.py
treoa's picture
Release 1
24aaa76
Raw History Blame Contribute Delete
2.71 kB
"""R5: work through a list of production chunks inside one Slurm job (dynamic work sharing between the array tasks of a job).
A chunk = one call of produce_r5.py (lang, split, population, first page, number of pages, pages saved whole).
Claiming: mkdir OUT_ROOT/_claims/<chunk tag> (atomic); a chunk whose part file exists is skipped. The loop stops claiming when the time
budget is nearly used up. A claim without a part file after the job ended is a lost chunk (the claim folder is removed when the call fails).
usage: python run_chunks.py --chunks chunks.tsv --out-root DIR --text-root DIR --workers 8 --budget-seconds 3000 [--expect-fonts N]
chunks.tsv columns: lang, split, pop, start, n, pages_save
"""
import argparse
import csv
import os
import subprocess
import sys
import time
ap = argparse.ArgumentParser()
ap.add_argument("--chunks", required=True)
ap.add_argument("--out-root", required=True)
ap.add_argument("--text-root", required=True)
ap.add_argument("--workers", type=int, default=8)
ap.add_argument("--budget-seconds", type=float, default=3000)
ap.add_argument("--expect-fonts", type=int, default=0)
ap.add_argument("--est-seconds-per-chunk", type=float, default=240)
a = ap.parse_args()
here = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
t0 = time.time()
rows = list(csv.reader(open(a.chunks, encoding="utf-8"), delimiter="\t"))
os.makedirs(os.path.join(a.out_root, "_claims"), exist_ok=True)
done = skipped = failed = 0
for lang, split, pop, start, n, psave in rows:
tag = "%s_%s_%s_%s_%s" % (split, lang, pop, start, n)
if os.path.exists(os.path.join(a.out_root, "_parts", tag + ".tsv")):
skipped += 1
continue
if time.time() - t0 + a.est_seconds_per_chunk > a.budget_seconds:
print("budget used up, stop", flush=True)
break
claim = os.path.join(a.out_root, "_claims", tag)
try:
os.mkdir(claim)
except FileExistsError:
continue
cmd = [sys.executable, os.path.join(here, "produce_r5.py"), "--lang", lang, "--split", split, "--pop", pop, "--start", start, "--n", n, "--text-root", a.text_root,
"--out-root", a.out_root, "--workers", str(a.workers), "--pages-save", psave]
if a.expect_fonts:
cmd += ["--expect-fonts", str(a.expect_fonts)]
t1 = time.time()
r = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
if r.returncode != 0:
failed += 1
os.rmdir(claim)
print("FAILED", tag, r.stdout[-600:], flush=True)
else:
done += 1
print("done %s in %.0f s" % (tag, time.time() - t1), flush=True)
print("chunks done %d skipped %d failed %d, %.0f s" % (done, skipped, failed, time.time() - t0))