File size: 3,060 Bytes
e3bed1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Background watcher that monitors checkpoints_persistent/ for completed checkpoints
and atomically uploads each new step to Hugging Face Hub without blocking training.
"""
import argparse
import json
import os
import sys
import time

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from upload_to_hf_atomic import upload_checkpoint_atomic, verify_ckpt_dir


def find_latest_complete_ckpt(ckpt_root: str):
    if not os.path.isdir(ckpt_root):
        return None, -1
    best_dir = None
    best_step = -1
    for name in os.listdir(ckpt_root):
        d = os.path.join(ckpt_root, name)
        if not os.path.isdir(d):
            continue
        sp = os.path.join(d, "train_state.json")
        if not os.path.exists(sp):
            continue
        try:
            state = verify_ckpt_dir(d)
            step = int(state["step"])
            if step > best_step:
                best_step = step
                best_dir = d
        except Exception:
            continue
    return best_dir, best_step


def main():
    p = argparse.ArgumentParser()
    p.add_argument("--ckpt-root", type=str, default="checkpoints_persistent")
    p.add_argument("--repo-id", type=str, default="Cortex-Intelligence/Cortex-OSS-Flash-1.0")
    p.add_argument("--min-step", type=int, default=29855)
    p.add_argument("--poll-seconds", type=int, default=20)
    p.add_argument("--stop-file", type=str, default="checkpoints_persistent/.watcher_stop")
    args = p.parse_args()

    last_uploaded_step = args.min_step
    print(f"[WATCHER] Watching {args.ckpt_root} for steps > {last_uploaded_step} -> {args.repo_id}", flush=True)

    while True:
        best_dir, best_step = find_latest_complete_ckpt(args.ckpt_root)
        if best_dir and best_step > last_uploaded_step:
            print(f"[WATCHER] Detected complete checkpoint {best_dir} (step={best_step})", flush=True)
            try:
                res = upload_checkpoint_atomic(repo_id=args.repo_id, ckpt_dir=best_dir)
                last_uploaded_step = res["step"]
                with open(os.path.join(args.ckpt_root, "last_uploaded.json"), "w", encoding="utf-8") as f:
                    json.dump(res, f, indent=2)
            except Exception as e:
                print(f"[WATCHER] Upload failed for step {best_step}: {e} (will retry)", flush=True)
        if os.path.exists(args.stop_file):
            # One final check before exiting
            best_dir, best_step = find_latest_complete_ckpt(args.ckpt_root)
            if best_dir and best_step > last_uploaded_step:
                try:
                    res = upload_checkpoint_atomic(repo_id=args.repo_id, ckpt_dir=best_dir)
                    last_uploaded_step = res["step"]
                except Exception as e:
                    print(f"[WATCHER] Final upload failed for step {best_step}: {e}", flush=True)
            print(f"[WATCHER] Stop file seen; exiting (last_uploaded_step={last_uploaded_step}).", flush=True)
            break
        time.sleep(args.poll_seconds)


if __name__ == "__main__":
    main()