mattbitzesty commited on
Commit
f0a181f
·
unverified ·
1 Parent(s): d97b291

hf_train_job: snapshot_download source+data, writable registry

Browse files
Files changed (1) hide show
  1. scripts/hf_train_job.py +109 -136
scripts/hf_train_job.py CHANGED
@@ -6,19 +6,20 @@ import time
6
  import traceback
7
  from pathlib import Path
8
 
9
- # HF Job entrypoint for the Milestone 4.16 5-epoch convergence run.
10
- # This script is executed inside the HF Jobs container. It assumes the
11
- # pino source tree has been mounted into /workspace/pino.
12
 
13
- REPO_ROOT = Path(__file__).resolve().parents[1]
14
- SRC_DIR = REPO_ROOT / "src"
15
- sys.path.insert(0, str(SRC_DIR))
16
 
17
- # Writable workspace outside the read-only source mount.
18
  WORKSPACE = Path("/workspace")
19
  OUTPUT_DIR = WORKSPACE / "models"
20
  LOG_DIR = WORKSPACE / "runs" / "hf_job_5epoch"
21
  DIAG_LOG = WORKSPACE / "hf_train.log"
 
 
22
 
23
 
24
  def _log(msg: str) -> None:
@@ -34,46 +35,74 @@ def _log(msg: str) -> None:
34
  print(f"[LOG-FAIL] {exc}", flush=True)
35
 
36
 
37
- # Force the fallback to the mounted dataset volume if the source-tree registry
38
- # is not available (model volumes only contain tracked files).
39
- if "HF_REGISTRY_PATH" not in os.environ:
40
- os.environ["HF_REGISTRY_PATH"] = "/data/registry.db"
41
-
 
 
 
 
 
 
 
 
42
 
43
- _log("[HF-JOB] hf_train_job.py top-level started")
44
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- def _copy_registry_to_writable(registry_path: Path) -> Path:
47
- """Copy a mounted read-only registry to a writable workspace location."""
48
- if not registry_path.exists():
49
- return registry_path
50
- writable_registry = WORKSPACE / "registry.db"
51
- if writable_registry.exists():
52
- return writable_registry
53
- import shutil
54
 
55
- _log(f"📦 Copying registry from {registry_path} to writable {writable_registry}")
56
- shutil.copy2(registry_path, writable_registry)
57
- return writable_registry
 
 
 
 
 
 
 
 
 
 
 
58
 
59
 
60
- def _prepare_registry() -> None:
61
- """Prepare a writable registry path before any pino imports happen."""
62
- registry_path = Path(
63
- os.environ.get("HF_REGISTRY_PATH", REPO_ROOT / "src" / "pino" / "registry.db")
64
- )
65
- _log(f"[REGISTRY] initial path: {registry_path} (exists={registry_path.exists()})")
66
- if not registry_path.exists():
67
- # Try the mounted dataset location as a fallback.
68
- registry_path = Path("/data/registry.db")
69
- _log(f"[REGISTRY] fallback path: {registry_path} (exists={registry_path.exists()})")
70
- if registry_path.exists():
71
- registry_path = _copy_registry_to_writable(registry_path)
72
- os.environ["PINO_REGISTRY_PATH"] = str(registry_path)
73
- _log(f"📍 PINO_REGISTRY_PATH set to {registry_path}")
74
 
 
 
 
 
 
 
 
 
75
 
76
- _prepare_registry()
 
 
 
 
 
 
 
 
 
 
 
77
 
78
  _log("[HF-JOB] About to import torch/pino.train")
79
 
@@ -83,110 +112,54 @@ try:
83
  except Exception as exc:
84
  _log(f"[HF-JOB] Import failed: {exc}")
85
  traceback.print_exc()
 
86
  sys.exit(1)
87
 
88
  _log("[HF-JOB] Imports completed")
89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
- def _upload_log(token: str | None) -> None:
92
- """Upload the diagnostic log to the dataset repo so it survives container exit."""
93
- if not token or not DIAG_LOG.exists():
94
- return
95
- try:
96
- from huggingface_hub import HfApi
97
-
98
- HfApi(token=token).upload_file(
99
- path_or_fileobj=str(DIAG_LOG),
100
- path_in_repo="hf_train.log",
101
- repo_id="mattbitzesty/pino-synthetic-dataset-v3",
102
- repo_type="dataset",
103
- )
104
- _log("[HF-JOB] Diagnostic log uploaded")
105
- except Exception as exc: # noqa: BLE001
106
- _log(f"[HF-JOB] Log upload failed: {exc}")
107
-
108
-
109
- def hf_train_5epoch() -> None:
110
- """Run a controlled 5-epoch training pass and push artifacts to the Hub."""
111
- token = os.environ.get("HF_TOKEN")
112
-
113
- # Allow mounted data path to override the repo default.
114
- data_path = Path(os.environ.get("HF_DATA_PATH", REPO_ROOT / "data" / "empirical_dataset_v3.jsonl"))
115
-
116
- if not data_path.exists() and token:
117
- # Download the dataset from the canonical dataset repository.
118
- from huggingface_hub import hf_hub_download
119
 
120
- _log("📥 Downloading empirical v3 dataset from Hugging Face Hub...")
121
- data_path.parent.mkdir(parents=True, exist_ok=True)
122
- downloaded = hf_hub_download(
123
- repo_id="mattbitzesty/pino-synthetic-dataset-v3",
124
- filename="empirical_dataset_v3.jsonl",
125
- repo_type="dataset",
126
- token=token,
127
- local_dir=str(data_path.parent),
128
- )
129
- data_path = Path(downloaded)
130
- _log(f" Dataset downloaded to {data_path}")
131
- elif not data_path.exists():
132
- # Allow the dataset to be mounted elsewhere in the container.
133
- data_path = Path("/data/empirical_dataset_v3.jsonl")
134
-
135
- _log(f"[HF-JOB] Final data path: {data_path} (exists={data_path.exists()})")
136
-
137
- OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
138
- LOG_DIR.mkdir(parents=True, exist_ok=True)
139
-
140
- sys.argv = [
141
- "pino.train",
142
- "--data", str(data_path),
143
- "--epochs", "5",
144
- "--batch-size", "8",
145
- "--train-ratio", "0.8",
146
- "--seed", "42",
147
- "--workers", "0",
148
- "--output-dir", str(OUTPUT_DIR),
149
- "--log-dir", str(LOG_DIR),
150
- ]
151
-
152
- _log("🚀 HF Job: starting PINO 5-epoch convergence pass")
153
- _log(f" Device: {'cuda' if torch.cuda.is_available() else 'cpu'}")
154
- _log(f" Args: {' '.join(sys.argv[1:])}")
155
 
 
 
156
  try:
157
- checkpoint_path = train_main()
158
- except Exception as exc:
159
- _log(f"❌ Training failed: {exc}")
160
- traceback.print_exc()
161
- _upload_log(token)
162
- sys.exit(1)
163
-
164
- _log(f"✅ Training complete. Checkpoint: {checkpoint_path}")
165
-
166
- # Push checkpoint to the model hub if token is present.
167
- if token and checkpoint_path and Path(checkpoint_path).exists():
168
- from huggingface_hub import HfApi, create_repo
169
-
170
- repo_id = "mattbitzesty/pino-pimt-checkpoint"
171
- try:
172
- create_repo(repo_id, private=True, token=token, exist_ok=True)
173
- except Exception as exc: # noqa: BLE001
174
- _log(f"Repo create/exists check: {exc}")
175
-
176
- api = HfApi(token=token)
177
- api.upload_file(
178
- path_or_fileobj=str(checkpoint_path),
179
- path_in_repo="pimt_v4.pt",
180
- repo_id=repo_id,
181
- repo_type="model",
182
- )
183
- _log(f"🤗 Checkpoint uploaded to https://huggingface.co/{repo_id}")
184
-
185
- _upload_log(token)
186
-
187
- # Sleep briefly so logs flush.
188
- time.sleep(5)
189
 
 
190
 
191
- if __name__ == "__main__":
192
- hf_train_5epoch()
 
6
  import traceback
7
  from pathlib import Path
8
 
9
+ # HF Job entrypoint for the v4 training run.
10
+ # This script is executed inside the HF Jobs container. It pulls fresh source
11
+ # and a fresh dataset into a writable workspace, then trains and uploads.
12
 
13
+ import shutil
14
+ from huggingface_hub import snapshot_download, HfApi, create_repo
 
15
 
16
+ TOKEN = os.environ.get("HF_TOKEN")
17
  WORKSPACE = Path("/workspace")
18
  OUTPUT_DIR = WORKSPACE / "models"
19
  LOG_DIR = WORKSPACE / "runs" / "hf_job_5epoch"
20
  DIAG_LOG = WORKSPACE / "hf_train.log"
21
+ PINOSRC = WORKSPACE / "pino"
22
+ DATA_DIR = WORKSPACE / "data"
23
 
24
 
25
  def _log(msg: str) -> None:
 
35
  print(f"[LOG-FAIL] {exc}", flush=True)
36
 
37
 
38
+ def _upload_log() -> None:
39
+ if not TOKEN or not DIAG_LOG.exists():
40
+ return
41
+ try:
42
+ HfApi(token=TOKEN).upload_file(
43
+ path_or_fileobj=str(DIAG_LOG),
44
+ path_in_repo="hf_train.log",
45
+ repo_id="mattbitzesty/pino-synthetic-dataset-v3",
46
+ repo_type="dataset",
47
+ )
48
+ _log("[HF-JOB] Diagnostic log uploaded")
49
+ except Exception as exc: # noqa: BLE001
50
+ _log(f"[HF-JOB] Log upload failed: {exc}")
51
 
 
52
 
53
+ def _pull_source() -> None:
54
+ if PINOSRC.exists():
55
+ shutil.rmtree(PINOSRC)
56
+ PINOSRC.parent.mkdir(parents=True, exist_ok=True)
57
+ _log("[HF-JOB] Downloading fresh source code...")
58
+ snapshot_download(
59
+ repo_id="mattbitzesty/pino-source-code",
60
+ local_dir=str(PINOSRC),
61
+ repo_type="model",
62
+ token=TOKEN,
63
+ )
64
+ _log(f"[HF-JOB] Source downloaded to {PINOSRC}")
65
 
 
 
 
 
 
 
 
 
66
 
67
+ def _pull_data() -> Path:
68
+ DATA_DIR.mkdir(parents=True, exist_ok=True)
69
+ data_path = DATA_DIR / "empirical_dataset_v3.jsonl"
70
+ if not data_path.exists():
71
+ _log("[HF-JOB] Downloading v3 dataset...")
72
+ snapshot_download(
73
+ repo_id="mattbitzesty/pino-synthetic-dataset-v3",
74
+ local_dir=str(DATA_DIR),
75
+ repo_type="dataset",
76
+ token=TOKEN,
77
+ allow_patterns=["empirical_dataset_v3.jsonl", "registry.db"],
78
+ )
79
+ _log(f"[HF-JOB] Dataset downloaded to {data_path}")
80
+ return data_path
81
 
82
 
83
+ _log("[HF-JOB] hf_train_job.py started")
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
+ try:
86
+ _pull_source()
87
+ data_path = _pull_data()
88
+ except Exception as exc:
89
+ _log(f"[HF-JOB] Download failed: {exc}")
90
+ traceback.print_exc()
91
+ _upload_log()
92
+ sys.exit(1)
93
 
94
+ # Set registry path to writable copy before any pino imports.
95
+ registry_src = DATA_DIR / "registry.db"
96
+ registry_dst = WORKSPACE / "registry.db"
97
+ if registry_src.exists() and not registry_dst.exists():
98
+ shutil.copy2(registry_src, registry_dst)
99
+ if not registry_dst.exists():
100
+ _log(f"[HF-JOB] WARNING: no writable registry at {registry_dst}")
101
+ os.environ["PINO_REGISTRY_PATH"] = str(registry_dst)
102
+ _log(f"[HF-JOB] PINO_REGISTRY_PATH={registry_dst}")
103
+
104
+ SRC_DIR = PINOSRC / "src"
105
+ sys.path.insert(0, str(SRC_DIR))
106
 
107
  _log("[HF-JOB] About to import torch/pino.train")
108
 
 
112
  except Exception as exc:
113
  _log(f"[HF-JOB] Import failed: {exc}")
114
  traceback.print_exc()
115
+ _upload_log()
116
  sys.exit(1)
117
 
118
  _log("[HF-JOB] Imports completed")
119
 
120
+ OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
121
+ LOG_DIR.mkdir(parents=True, exist_ok=True)
122
+
123
+ sys.argv = [
124
+ "pino.train",
125
+ "--data", str(data_path),
126
+ "--epochs", "5",
127
+ "--batch-size", "8",
128
+ "--train-ratio", "0.8",
129
+ "--seed", "42",
130
+ "--workers", "0",
131
+ "--output-dir", str(OUTPUT_DIR),
132
+ "--log-dir", str(LOG_DIR),
133
+ ]
134
+
135
+ _log("🚀 HF Job: starting PINO 5-epoch v4 convergence pass")
136
+ _log(f" Device: {'cuda' if torch.cuda.is_available() else 'cpu'}")
137
+ _log(f" Args: {' '.join(sys.argv[1:])}")
138
 
139
+ try:
140
+ checkpoint_path = train_main()
141
+ except Exception as exc:
142
+ _log(f"❌ Training failed: {exc}")
143
+ traceback.print_exc()
144
+ _upload_log()
145
+ sys.exit(1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
 
147
+ _log(f"✅ Training complete. Checkpoint: {checkpoint_path}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
 
149
+ if TOKEN and checkpoint_path and Path(checkpoint_path).exists():
150
+ repo_id = "mattbitzesty/pino-pimt-checkpoint"
151
  try:
152
+ create_repo(repo_id, private=True, token=TOKEN, exist_ok=True)
153
+ except Exception as exc: # noqa: BLE001
154
+ _log(f"Repo create/exists check: {exc}")
155
+ HfApi(token=TOKEN).upload_file(
156
+ path_or_fileobj=str(checkpoint_path),
157
+ path_in_repo="pimt_v4.pt",
158
+ repo_id=repo_id,
159
+ repo_type="model",
160
+ )
161
+ _log(f"🤗 Checkpoint uploaded to https://huggingface.co/{repo_id}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
 
163
+ _upload_log()
164
 
165
+ time.sleep(5)