coderofpears commited on
Commit
25848bb
·
verified ·
1 Parent(s): 865cee7

publish portable stage runner + frozen sources

Browse files
Files changed (1) hide show
  1. scripts/fetch_stage_head.py +117 -0
scripts/fetch_stage_head.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Fetch the campaign head checkpoint (and its lineage) from the HF public bucket.
2
+
3
+ Produces a directory usable directly as ``--head-root`` by
4
+ ``scripts/run_stage_portable.py``::
5
+
6
+ <out>/step_<step>_<uuid>/{checkpoint.pt,manifest.json,READY}
7
+ <out>/info/stage-lineage.json
8
+ <out>/info/stage-head.json
9
+
10
+ If the bucket has no ``info/stage-head.json`` pointer yet, the anchored
11
+ step-88587 checkpoint of the original stage is used.
12
+ """
13
+ from __future__ import annotations
14
+
15
+ import argparse
16
+ import json
17
+ import os
18
+ from pathlib import Path
19
+ import shutil
20
+
21
+ DEFAULT_REPO = "coderofpears/pear15-stage3-checkpoints"
22
+ ANCHOR_DIR = "step_000000088587_1d237b6711244c088ddc0f4bea41bce3"
23
+ ANCHOR_PRECISION = "bf16"
24
+ ANCHOR_CAMPAIGN = 3_190_636_544
25
+ ANCHOR_LINEAGE_SHA = "0ff259dfbf25a1ca945c6691fa4aa1497ce9066e1423bb1bf9dc9ef209b92bc7"
26
+
27
+
28
+ def _token(explicit=None):
29
+ if explicit:
30
+ return explicit
31
+ env = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
32
+ if env:
33
+ return env.strip()
34
+ path = Path.home() / ".cache" / "huggingface" / "token"
35
+ if path.exists():
36
+ return path.read_text(encoding="utf-8").strip()
37
+ return None
38
+
39
+
40
+ def fetch(repo=DEFAULT_REPO, out_dir=None, checkpoint_dir=None, token=None,
41
+ force=False, quiet=False):
42
+ from huggingface_hub import hf_hub_download
43
+
44
+ if out_dir is None:
45
+ raise ValueError("out_dir is required")
46
+ out = Path(out_dir)
47
+ out.mkdir(parents=True, exist_ok=True)
48
+ (out / "info").mkdir(exist_ok=True)
49
+ token = _token(token)
50
+
51
+ def log(message):
52
+ if not quiet:
53
+ print(message, flush=True)
54
+
55
+ head = None
56
+ try:
57
+ head_path = hf_hub_download(repo, "info/stage-head.json", repo_type="dataset",
58
+ token=token)
59
+ head = json.loads(Path(head_path).read_text(encoding="utf-8"))
60
+ shutil.copyfile(head_path, out / "info" / "stage-head.json")
61
+ except Exception as exc:
62
+ log(f"no stage-head pointer ({type(exc).__name__}); using anchored checkpoint")
63
+
64
+ if head is None:
65
+ ckpt = checkpoint_dir or ANCHOR_DIR
66
+ precision = ANCHOR_PRECISION
67
+ campaign = ANCHOR_CAMPAIGN
68
+ lineage_sha = ANCHOR_LINEAGE_SHA
69
+ step = 88587
70
+ else:
71
+ ckpt = checkpoint_dir or head["checkpoint_dir"]
72
+ precision = head.get("precision", "bf16")
73
+ campaign = head.get("campaign_token_count")
74
+ lineage_sha = head.get("lineage_sha256")
75
+ step = head.get("step")
76
+
77
+ lineage_src = hf_hub_download(repo, "info/stage-lineage.json", repo_type="dataset",
78
+ token=token)
79
+ shutil.copyfile(lineage_src, out / "info" / "stage-lineage.json")
80
+
81
+ target = out / ckpt
82
+ target.mkdir(parents=True, exist_ok=True)
83
+ for name in ("checkpoint.pt", "manifest.json", "READY"):
84
+ dest = target / name
85
+ if dest.exists() and not force:
86
+ log(f"have {ckpt}/{name} ({dest.stat().st_size} bytes)")
87
+ continue
88
+ src = hf_hub_download(repo, f"checkpoints/{ckpt}/{name}", repo_type="dataset",
89
+ token=token)
90
+ shutil.copyfile(src, dest)
91
+ log(f"fetched {ckpt}/{name} ({dest.stat().st_size} bytes)")
92
+
93
+ manifest = json.loads((target / "manifest.json").read_text(encoding="utf-8"))
94
+ result = {
95
+ "repo": repo, "checkpoint_dir": ckpt, "step": manifest.get("step", step),
96
+ "bytes": manifest.get("bytes"), "sha256": manifest.get("sha256"),
97
+ "precision": precision, "campaign_token_count": campaign,
98
+ "lineage_sha256": lineage_sha, "head_root": str(out.resolve()),
99
+ }
100
+ log(json.dumps(result, indent=2, sort_keys=True))
101
+ return result
102
+
103
+
104
+ def main():
105
+ parser = argparse.ArgumentParser(description=__doc__)
106
+ parser.add_argument("--repo", default=DEFAULT_REPO)
107
+ parser.add_argument("--out", type=Path, required=True)
108
+ parser.add_argument("--checkpoint-dir")
109
+ parser.add_argument("--token")
110
+ parser.add_argument("--force", action="store_true")
111
+ parser.add_argument("--quiet", action="store_true")
112
+ args = parser.parse_args()
113
+ fetch(args.repo, args.out, args.checkpoint_dir, args.token, args.force, args.quiet)
114
+
115
+
116
+ if __name__ == "__main__":
117
+ main()