turkic-ocr-code / src /r5eval /metautil.py
treoa's picture
Release 1
24aaa76
Raw History Blame Contribute Delete
3.7 kB
"""Reads speed, memory, training figures and the confidence kind from a checkpoint's meta.json, whatever the writer called its keys.
VLMA (eval_vlm.py) writes peak_mem_reserved_batch1_GiB, peak_mem_batch1_GiB, images_per_second_batch16_V1, samples_seen, learning_rate.
LINEDET (predict.py) writes peak_gpu_memory_gib_eval_batch_reserved (the evaluation batch, not batch 1), learning_rate_peak, samples_seen and
per-set lines_per_second_eval. The reader says which basis a figure has; gate G needs batch 1, and an evaluation-batch figure is an upper bound.
"""
import re
LOGPROB_RE = re.compile(r"natural[- ]log|log[- ]?prob|mean log|log-likelihood", re.I)
def find_num(meta, test):
"""First numeric value in meta (nested dicts searched) whose lower-cased key satisfies test."""
if isinstance(meta, dict):
for k, v in meta.items():
if isinstance(v, (int, float)) and not isinstance(v, bool) and test(str(k).lower()):
return v
for v in meta.values():
if isinstance(v, dict):
r = find_num(v, test)
if r is not None:
return r
return None
def memory(meta):
"""{'reserved_gib', 'allocated_gib', 'basis'}: batch-1 keys first, then any peak figure of the evaluation (marked as an upper bound)."""
res = find_num(meta, lambda k: "reserved" in k and "batch1" in k and "pipeline" not in k)
alloc = find_num(meta, lambda k: "batch1" in k and "reserved" not in k and "pipeline" not in k and ("peak" in k or "mem" in k))
basis = "batch 1" if res is not None else None
if res is None:
res = find_num(meta, lambda k: "reserved" in k and ("peak" in k or "mem" in k) and "pipeline" not in k)
if res is not None:
basis = "evaluation batch, not batch 1 (an upper bound for batch 1)"
if alloc is None:
alloc = find_num(meta, lambda k: "peak" in k and "mem" in k and "reserved" not in k and "pipeline" not in k)
return {"reserved_gib": res, "allocated_gib": alloc, "basis": basis}
def pipeline_memory(meta):
return find_num(meta, lambda k: "pipeline" in k and ("reserved" in k or "peak" in k))
def speed(meta):
"""{'images_per_second', 'basis'}: the batch-16 figure of VLMA, else the evaluation-batch lines per second of LINEDET."""
v = meta.get("images_per_second_batch16_V1") if isinstance(meta, dict) else None
if isinstance(v, (int, float)) and not isinstance(v, bool):
return {"images_per_second": v, "basis": "batch 16 on V1, image loading, processor and generate (VLMA)"}
v = find_num(meta, lambda k: k.startswith("images_per_second") and "note" not in k)
if v is not None:
return {"images_per_second": v, "basis": "images_per_second in meta.json"}
v = find_num(meta, lambda k: k.startswith("lines_per_second_eval"))
if v is not None:
return {"images_per_second": v, "basis": "lines per second in the evaluation batch including data loading (LINEDET); not the same as the VLM figure"}
return {"images_per_second": None, "basis": None}
def learning_rate(meta):
for k in ("learning_rate", "learning_rate_peak", "lr"):
v = meta.get(k) if isinstance(meta, dict) else None
if isinstance(v, (int, float)) and not isinstance(v, bool):
return v
return None
def conf_kind(meta):
"""('logprob' | 'probability', definition text). logprob only when the writer's own text says natural-log or log-probability."""
s = ""
if isinstance(meta, dict):
s = " ".join(str(meta.get(k, "")) for k in ("conf_definition", "confidence", "tsv_columns"))
return ("logprob" if LOGPROB_RE.search(s) else "probability"), s.lower()