| |
| import datetime as dt |
| import glob |
| import json |
| import os |
| import re |
| import sys |
| import time |
|
|
|
|
| def hms_to_seconds(value): |
| parts = [int(part) for part in value.split(":")] |
| if len(parts) == 2: |
| return parts[0] * 60 + parts[1] |
| if len(parts) == 3: |
| return parts[0] * 3600 + parts[1] * 60 + parts[2] |
| return None |
|
|
|
|
| def fmt_duration(seconds): |
| seconds = max(0, int(seconds)) |
| days, rem = divmod(seconds, 86400) |
| hours, rem = divmod(rem, 3600) |
| minutes, _ = divmod(rem, 60) |
| if days: |
| return f"{days}d {hours}h {minutes}m" |
| if hours: |
| return f"{hours}h {minutes}m" |
| return f"{minutes}m" |
|
|
|
|
| def parse_train_log(train_log): |
| if not os.path.exists(train_log): |
| return None |
| with open(train_log, "rb") as f: |
| f.seek(0, os.SEEK_END) |
| f.seek(max(0, f.tell() - 1024 * 1024)) |
| text = f.read().decode("utf-8", errors="replace").replace("\r", "\n") |
| matches = re.findall( |
| r"Train:\s+[^|]*\|\s*(\d+)/(\d+)\s*\[([0-9:]+)<([0-9:]+),\s*([0-9.]+)s/it\]", |
| text, |
| ) |
| if not matches: |
| return None |
| step, total, elapsed, remaining, speed = matches[-1] |
| return { |
| "source": "train.log", |
| "step": int(step), |
| "total": int(total), |
| "elapsed_s": hms_to_seconds(elapsed), |
| "remaining_s": hms_to_seconds(remaining), |
| "speed_s_it": float(speed), |
| } |
|
|
|
|
| def parse_logging_jsonl(run_dir): |
| files = glob.glob(os.path.join(run_dir, "v*-*", "logging.jsonl")) |
| if not files: |
| return None |
| latest = max(files, key=os.path.getmtime) |
| last = None |
| with open(latest, encoding="utf-8") as f: |
| for line in f: |
| line = line.strip() |
| if line: |
| last = line |
| if not last: |
| return None |
| obj = json.loads(last) |
| match = re.match(r"(\d+)/(\d+)", str(obj.get("global_step/max_steps", ""))) |
| if not match: |
| return None |
| step, total = map(int, match.groups()) |
| speed = obj.get("train_speed(s/it)") |
| remaining = (total - step) * float(speed) if speed else None |
| return { |
| "source": "logging.jsonl", |
| "step": step, |
| "total": total, |
| "elapsed_s": None, |
| "remaining_s": remaining, |
| "speed_s_it": float(speed) if speed else None, |
| } |
|
|
|
|
| def estimate(run_dir): |
| train_log = os.path.join(run_dir, "train.log") |
| progress = parse_train_log(train_log) or parse_logging_jsonl(run_dir) |
| now = dt.datetime.now() |
| if progress is None: |
| return f"[{now:%F %T}] no_progress run_dir={run_dir}" |
|
|
| step = progress["step"] |
| total = progress["total"] |
| speed = progress["speed_s_it"] |
| remaining = progress["remaining_s"] |
| if remaining is None and speed is not None: |
| remaining = (total - step) * speed |
| finish = now + dt.timedelta(seconds=remaining or 0) |
| pct = 100.0 * step / total if total else 0.0 |
| speed_text = f"{speed:.2f}s/it" if speed is not None else "n/a" |
| status = "done" if total and step >= total else "running" |
| return ( |
| f"[{now:%F %T}] status={status} step={step}/{total} ({pct:.2f}%) " |
| f"speed={speed_text} remaining={fmt_duration(remaining or 0)} " |
| f"finish={finish:%F %T} source={progress['source']}" |
| ) |
|
|
|
|
| def main(): |
| run_dir, log_path, latest_path = sys.argv[1:4] |
| interval = int(os.environ.get("ETA_MONITOR_INTERVAL", "300")) |
| with open(log_path, "a", encoding="utf-8") as log: |
| log.write(f"\n--- ETA monitor started {dt.datetime.now():%F %T}; interval={interval}s; run_dir={run_dir} ---\n") |
| while True: |
| line = estimate(run_dir) |
| with open(log_path, "a", encoding="utf-8") as log: |
| log.write(line + "\n") |
| with open(latest_path, "w", encoding="utf-8") as latest: |
| latest.write(line + "\n") |
| if "status=done" in line: |
| break |
| time.sleep(interval) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|