Datasets:
File size: 4,183 Bytes
b1a18a7 | 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 77 78 79 80 81 82 83 84 85 86 | """Completion checks and bounded cleanup around the unmodified Prime launcher."""
import asyncio
import contextlib
import os
from pathlib import Path
import signal
async def stop_launcher(process, grace=30):
import psutil
from prime_rl.utils.process import cleanup_process
children = []
if process.returncode is None:
with contextlib.suppress(psutil.NoSuchProcess):
children = psutil.Process(process.pid).children(recursive=True)
cleanup_process(process.pid, signal.SIGTERM)
# Include descendants that remained in the launcher's group after it exited.
with contextlib.suppress(ProcessLookupError):
os.killpg(process.pid, signal.SIGTERM)
try:
await asyncio.wait_for(process.wait(), grace)
except asyncio.TimeoutError:
with contextlib.suppress(ProcessLookupError):
os.killpg(process.pid, signal.SIGKILL)
with contextlib.suppress(ProcessLookupError):
process.kill()
await asyncio.wait_for(process.wait(), 5)
_, alive = await asyncio.to_thread(psutil.wait_procs, children, timeout=grace)
for child in alive:
with contextlib.suppress(psutil.NoSuchProcess):
child.kill()
# A parent exiting early must not spare a TERM-resistant group member.
with contextlib.suppress(ProcessLookupError):
os.killpg(process.pid, signal.SIGKILL)
_, alive = await asyncio.to_thread(psutil.wait_procs, alive, timeout=5)
survivors = [p.pid for p in alive if p.is_running() and p.status() != psutil.STATUS_ZOMBIE]
if survivors:
raise RuntimeError(f"Launcher descendants still running: {survivors}")
def verify_checkpoint(step_dir, expected_step, orchestrator_expected_step=None):
"""Read native DCP metadata, check shard extents and load saved progress."""
import torch
import torch.distributed.checkpoint as dcp
step_dir = Path(step_dir)
trainer = step_dir / "trainer"
if not (trainer / ".metadata").is_file():
raise RuntimeError(f"Incomplete native DCP checkpoint: {trainer / '.metadata'} missing")
metadata = dcp.FileSystemReader(trainer).read_metadata()
keys = metadata.state_dict_metadata
for prefix in ("app.model.", "app.optimizers.", "app.scheduler.", "app.progress."):
if not any(key.startswith(prefix) for key in keys):
raise RuntimeError(f"Native checkpoint has no {prefix} state")
if not metadata.storage_data:
raise RuntimeError("Native checkpoint contains no storage records")
shards = set()
for item in metadata.storage_data.values():
shard = trainer / item.relative_path
if not shard.is_file() or shard.stat().st_size < item.offset + item.length:
raise RuntimeError(f"Missing or truncated native checkpoint shard: {shard}")
shards.add(shard.name)
state = {"app": {"progress": {"step": 0, "total_tokens": 0, "total_samples": 0}}}
dcp.load(state, checkpoint_id=trainer)
if state["app"]["progress"]["step"] != expected_step:
raise RuntimeError(f"Trainer checkpoint progress disagrees with step {expected_step}")
saved = torch.load(step_dir / "orchestrator" / "progress.pt", weights_only=False, map_location="cpu")
# Prime 9e15490: interval saves happen after incrementing progress; final
# teardown decrements before saving. Resume derives its step from the path.
if orchestrator_expected_step is None:
orchestrator_expected_step = expected_step
if saved["progress"].step != orchestrator_expected_step:
raise RuntimeError(f"Orchestrator progress {saved['progress'].step} != expected {orchestrator_expected_step}")
if "train_source" not in saved:
raise RuntimeError("Orchestrator checkpoint has no train_source state")
return {"step": expected_step, "metadata_readable": True, "shards": sorted(shards),
"progress_loaded": True, "orchestrator_progress_step": saved["progress"].step,
"full_resume_tested": False}
if __name__ == "__main__":
import json
import sys
print(json.dumps(verify_checkpoint(sys.argv[1], int(sys.argv[2]), int(sys.argv[3]) if len(sys.argv)>3 else None)))
|