mudassiruddin21 commited on
Commit
fcfc3d2
·
verified ·
1 Parent(s): 0ec358a

Survive a machine change

Browse files
Files changed (3) hide show
  1. scripts/launch.py +8 -2
  2. scripts/pipeline.sh +25 -1
  3. scripts/supervise.py +25 -6
scripts/launch.py CHANGED
@@ -58,11 +58,17 @@ def get_uv(root):
58
 
59
 
60
  def alive(pidfile):
 
 
61
  try:
62
- os.kill(int(pidfile.read_text()), 0)
63
- return True
64
  except (OSError, ValueError):
65
  return False
 
 
 
 
66
 
67
 
68
  def status(root):
 
58
 
59
 
60
  def alive(pidfile):
61
+ """True only if the recorded process exists and is our supervisor (a pid file left over
62
+ from an earlier machine can name an unrelated process)."""
63
  try:
64
+ pid = int(pidfile.read_text())
65
+ os.kill(pid, 0)
66
  except (OSError, ValueError):
67
  return False
68
+ try:
69
+ return b"supervise.py" in Path(f"/proc/{pid}/cmdline").read_bytes()
70
+ except OSError:
71
+ return False
72
 
73
 
74
  def status(root):
scripts/pipeline.sh CHANGED
@@ -72,7 +72,7 @@ s_env() {
72
  for d in "$UV_PYTHON_INSTALL_DIR"/cpython-3.12*; do
73
  if [ -d "$d" ] && [ ! -x "$d/bin/python3.12" ]; then echo "removing incomplete $d"; rm -rf "$UV_PYTHON_INSTALL_DIR"; break; fi
74
  done
75
- if ! "$PY" -c "import sys" >/dev/null 2>&1; then rm -rf "$VENV"; "$UV" venv --python 3.12 "$VENV"; fi
76
  "$UV" pip install --python "$PY" "vllm==0.31.0" accelerate datasets
77
  "$UV" cache clean >/dev/null 2>&1 || true
78
  "$PY" - <<'EOF'
@@ -194,6 +194,30 @@ s_bench_target() { SUB="" bench --tag target_only --target-only --what batch,lon
194
  log "pipeline start: run $(cat "$STATE/run_id"), code $(cat "$REPO/.commit" 2>/dev/null | cut -c1-12), training run $RUN_NAME"
195
  # a vLLM server left behind by an interrupted run would still hold the GPU
196
  if pkill -f -- "--served-model-name target" 2>/dev/null; then log "stopped a leftover vLLM server"; sleep 20; fi
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
  stage env 1 s_env
198
  stage download 1 s_download
199
  stage baseline 1 s_baseline
 
72
  for d in "$UV_PYTHON_INSTALL_DIR"/cpython-3.12*; do
73
  if [ -d "$d" ] && [ ! -x "$d/bin/python3.12" ]; then echo "removing incomplete $d"; rm -rf "$UV_PYTHON_INSTALL_DIR"; break; fi
74
  done
75
+ if ! "$PY" -c "import sys" >/dev/null 2>&1; then rm -rf "$VENV" "$UV_CACHE_DIR"; "$UV" venv --python 3.12 "$VENV"; fi
76
  "$UV" pip install --python "$PY" "vllm==0.31.0" accelerate datasets
77
  "$UV" cache clean >/dev/null 2>&1 || true
78
  "$PY" - <<'EOF'
 
194
  log "pipeline start: run $(cat "$STATE/run_id"), code $(cat "$REPO/.commit" 2>/dev/null | cut -c1-12), training run $RUN_NAME"
195
  # a vLLM server left behind by an interrupted run would still hold the GPU
196
  if pkill -f -- "--served-model-name target" 2>/dev/null; then log "stopped a leftover vLLM server"; sleep 20; fi
197
+ # a machine change can keep the "done" markers but lose what the stages produced: check each
198
+ # finished stage's output and run the stage again when it is gone
199
+ check_done() { # check_done <stage> <command ...>
200
+ local name=$1; shift
201
+ if is_done "$name" && ! "$@" >/dev/null 2>&1; then rm -f "$STATE/$name.done"; log "redo $name (its output is missing)"; fi
202
+ }
203
+ has_files() { compgen -G "$1" >/dev/null; }
204
+ shards_ok() { # every shard listed by its metadata file is present and non-empty
205
+ local d=$1 j
206
+ has_files "$d/*.json" || return 1
207
+ for j in "$d"/*.json; do [ -s "${j%.json}.tok.npy" ] && [ -s "${j%.json}.hid.npy" ] || return 1; done
208
+ }
209
+ check_done env "$PY" -c "import vllm, torch, transformers"
210
+ check_done download "$PY" -c "import json, os, sys; d = '$TARGET'; f = set(json.load(open(d + '/model.safetensors.index.json'))['weight_map'].values()); sys.exit(0 if os.path.isfile('$KIT/contract.json') and all(os.path.getsize(os.path.join(d, x)) > 0 for x in f) else 1)"
211
+ check_done baseline test -s "$BASELINE/model.safetensors"
212
+ check_done probe test -s "$PROBE/model.safetensors"
213
+ check_done prompts has_files "$DATA/prompts/*.jsonl"
214
+ check_done smoke_fp8 test -s "$RESULTS/smoke_fp8.json"
215
+ check_done generate has_files "$DATA/gen/*.jsonl"
216
+ check_done extract shards_ok "$DATA/train"
217
+ check_done extract shards_ok "$DATA/dev"
218
+ check_done train test -s "$OUTRUN/sub_bf16/model.safetensors"
219
+ check_done eval_main test -s "$RESULTS/$(new_tag).json"
220
+ check_done eval_baseline test -s "$RESULTS/eval_baseline.json"
221
  stage env 1 s_env
222
  stage download 1 s_download
223
  stage baseline 1 s_baseline
scripts/supervise.py CHANGED
@@ -48,8 +48,16 @@ def remote_sha():
48
  return m.group(1) if m else None
49
 
50
 
 
 
 
 
 
 
51
  def install(sha):
52
  dest = CODE / sha[:12]
 
 
53
  if not dest.exists():
54
  data = urllib.request.urlopen(f"https://codeload.github.com/{SOURCE}/tar.gz/{sha}", timeout=300).read()
55
  tmp = CODE / f".{sha[:12]}.tmp"
@@ -118,9 +126,12 @@ def stop(proc):
118
 
119
  def sync():
120
  if VENV_PY.exists() and (ROOT / "repo" / "scripts" / "sync.py").exists():
121
- with open(LOGS / "sync.log", "a") as f:
122
- subprocess.run([str(VENV_PY), str(ROOT / "repo" / "scripts" / "sync.py")], stdout=f,
123
- stderr=subprocess.STDOUT, env=dict(tool_env(), EMC_ROOT=str(ROOT)), timeout=1200)
 
 
 
124
 
125
 
126
  def start_pipeline(extra):
@@ -141,7 +152,7 @@ def main():
141
  if os.environ.get("RUN_ID") and not (STATE / "run_id").exists():
142
  (STATE / "run_id").write_text(os.environ["RUN_ID"] + "\n")
143
  head = ROOT / "repo" / ".commit"
144
- current = head.read_text().strip() if head.exists() else ""
145
  ran = (STATE / "pipeline_commit").read_text().strip() if (STATE / "pipeline_commit").exists() else ""
146
  # force: (re)start the pipeline once on every supervisor start - finished stages are skipped,
147
  # and a machine restart in the middle of a stage resumes there.
@@ -183,8 +194,16 @@ def main():
183
  log(f"pipeline exited with code {proc.returncode}")
184
  reported = True
185
  sync()
186
- if current and (current != ran or force) and (ROOT / "repo").exists():
187
- proc = start_pipeline(env)
 
 
 
 
 
 
 
 
188
  ran, force, reported, last_sync = current, False, False, time.time()
189
  (STATE / "pipeline_commit").write_text(current + "\n")
190
  log(f"pipeline started (pid {proc.pid}) at {current[:12]}")
 
48
  return m.group(1) if m else None
49
 
50
 
51
+ def complete(code_dir):
52
+ """A code snapshot is usable only with its commit stamp and the pipeline script (folders
53
+ can survive a machine change without their files)."""
54
+ return (code_dir / ".commit").is_file() and (code_dir / "scripts" / "pipeline.sh").is_file()
55
+
56
+
57
  def install(sha):
58
  dest = CODE / sha[:12]
59
+ if dest.exists() and not complete(dest):
60
+ shutil.rmtree(dest, ignore_errors=True)
61
  if not dest.exists():
62
  data = urllib.request.urlopen(f"https://codeload.github.com/{SOURCE}/tar.gz/{sha}", timeout=300).read()
63
  tmp = CODE / f".{sha[:12]}.tmp"
 
126
 
127
  def sync():
128
  if VENV_PY.exists() and (ROOT / "repo" / "scripts" / "sync.py").exists():
129
+ try:
130
+ with open(LOGS / "sync.log", "a") as f:
131
+ subprocess.run([str(VENV_PY), str(ROOT / "repo" / "scripts" / "sync.py")], stdout=f,
132
+ stderr=subprocess.STDOUT, env=dict(tool_env(), EMC_ROOT=str(ROOT)), timeout=1200)
133
+ except Exception as e: # noqa: BLE001 - an upload problem never stops the supervisor
134
+ log(f"log push failed: {type(e).__name__}: {e}")
135
 
136
 
137
  def start_pipeline(extra):
 
152
  if os.environ.get("RUN_ID") and not (STATE / "run_id").exists():
153
  (STATE / "run_id").write_text(os.environ["RUN_ID"] + "\n")
154
  head = ROOT / "repo" / ".commit"
155
+ current = head.read_text().strip() if complete(ROOT / "repo") else ""
156
  ran = (STATE / "pipeline_commit").read_text().strip() if (STATE / "pipeline_commit").exists() else ""
157
  # force: (re)start the pipeline once on every supervisor start - finished stages are skipped,
158
  # and a machine restart in the middle of a stage resumes there.
 
194
  log(f"pipeline exited with code {proc.returncode}")
195
  reported = True
196
  sync()
197
+ if current and not complete(ROOT / "repo"):
198
+ current = "" # snapshot lost its files: fetch it again next minute
199
+ log("code snapshot incomplete; reinstalling")
200
+ elif current and (current != ran or force):
201
+ try:
202
+ proc = start_pipeline(env)
203
+ except Exception as e: # noqa: BLE001 - retried next minute
204
+ log(f"pipeline start failed: {type(e).__name__}: {e}")
205
+ time.sleep(60)
206
+ continue
207
  ran, force, reported, last_sync = current, False, False, time.time()
208
  (STATE / "pipeline_commit").write_text(current + "\n")
209
  log(f"pipeline started (pid {proc.pid}) at {current[:12]}")