Spaces:
Running
Running
File size: 6,778 Bytes
dcf4a90 | 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | """Remote Runner & Synchronizer for Lightning AI L4 GPU Execution.
Handles syncing codebase to the remote instance, executing GPU training,
and fetching metrics and artifacts back to the local repository.
"""
from __future__ import annotations
import argparse
import os
import subprocess
import sys
from pathlib import Path
REMOTE_HOST = "s_01m2wg556paa8dekwgs35svwpc@ssh.lightning.ai"
REMOTE_DIR = "/home/zeus/continual_learning"
REMOTE_PYTHON = "/home/zeus/miniconda3/envs/cloudspace/bin/python"
ROOT = Path(__file__).resolve().parents[1]
def run_ssh_command(cmd: str) -> int:
full_cmd = ["ssh", "-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=no", REMOTE_HOST, cmd]
print(f">> Running on remote: {cmd}")
res = subprocess.run(full_cmd)
return res.returncode
def sync_to_remote() -> int:
print(f">> Syncing codebase to {REMOTE_HOST}:{REMOTE_DIR}...")
run_ssh_command(f"mkdir -p {REMOTE_DIR}/continual_ai {REMOTE_DIR}/scripts {REMOTE_DIR}/artifacts {REMOTE_DIR}/checkpoints")
# Sync continual_ai package
files_to_sync = list((ROOT / "continual_ai").glob("*.py"))
scripts_to_sync = list((ROOT / "scripts").glob("*.py"))
scp_cmd = ["scp", "-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=no"]
# Sync python files
for f in files_to_sync:
subprocess.run(scp_cmd + [str(f), f"{REMOTE_HOST}:{REMOTE_DIR}/continual_ai/{f.name}"])
for s in scripts_to_sync:
subprocess.run(scp_cmd + [str(s), f"{REMOTE_HOST}:{REMOTE_DIR}/scripts/{s.name}"])
print(">> Sync completed successfully.")
return 0
def run_stage1() -> int:
cmd = f"cd {REMOTE_DIR} && PYTHONPATH={REMOTE_DIR} {REMOTE_PYTHON} scripts/train_gpu_stage1.py"
return run_ssh_command(cmd)
def run_stage2() -> int:
cmd = f"cd {REMOTE_DIR} && PYTHONPATH={REMOTE_DIR} {REMOTE_PYTHON} scripts/train_gpu_stage2.py"
return run_ssh_command(cmd)
def run_stage3() -> int:
cmd = f"cd {REMOTE_DIR} && PYTHONPATH={REMOTE_DIR} {REMOTE_PYTHON} scripts/train_gpu_stage3.py"
return run_ssh_command(cmd)
def run_stage4() -> int:
cmd = f"cd {REMOTE_DIR} && PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True PYTHONPATH={REMOTE_DIR} {REMOTE_PYTHON} scripts/train_gpu_stage4.py"
return run_ssh_command(cmd)
def run_stage4_2b() -> int:
cmd = f"cd {REMOTE_DIR} && PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True PYTHONPATH={REMOTE_DIR} {REMOTE_PYTHON} scripts/train_gpu_stage4_2b.py"
return run_ssh_command(cmd)
def run_stage4_2b_moe() -> int:
cmd = f"cd {REMOTE_DIR} && PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True PYTHONPATH={REMOTE_DIR} {REMOTE_PYTHON} scripts/train_gpu_stage4_2b_moe.py"
return run_ssh_command(cmd)
def run_eval_2b() -> int:
cmd = f"cd {REMOTE_DIR} && PYTHONPATH={REMOTE_DIR} {REMOTE_PYTHON} scripts/eval_2b_inference.py"
return run_ssh_command(cmd)
def fetch_artifacts() -> int:
print(">> Fetching artifacts from remote...")
local_artifacts = ROOT / "artifacts"
local_artifacts.mkdir(parents=True, exist_ok=True)
scp_cmd = ["scp", "-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=no"]
files = [
"gpu_stage1_metrics.json",
"gpu_stage2_metrics.json",
"gpu_stage3_metrics.json",
"gpu_stage4_metrics.json",
"gpu_stage4_2b_metrics.json",
"gpu_stage4_2b_moe_metrics.json",
"eval_2b_inference.json",
]
for f in files:
res = subprocess.run(scp_cmd + [f"{REMOTE_HOST}:{REMOTE_DIR}/artifacts/{f}", str(local_artifacts / f)])
if res.returncode == 0:
print(f">> Successfully fetched {local_artifacts / f}")
return 0
def download_model(model_name: str = "stage4_2b") -> int:
print(f">> Downloading {model_name} checkpoints from remote...")
local_checkpoints = ROOT / "checkpoints"
local_checkpoints.mkdir(parents=True, exist_ok=True)
scp_cmd = ["scp", "-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=no"]
if model_name == "stage4_2b":
files = ["stage4_2b.pt", "stage4_2b_lora.pt"]
elif model_name == "stage4_1b":
files = ["stage4_1b.pt", "stage4_lora.pt"]
elif model_name == "all":
files = [
"stage1_30m.pt", "stage2_135m.pt", "stage2_lora.pt",
"stage3_360m.pt", "stage3_lora_tech.pt", "stage3_lora_reasoning.pt",
"stage4_1b.pt", "stage4_lora.pt",
"stage4_2b.pt", "stage4_2b_lora.pt",
]
else:
files = [f"{model_name}.pt"]
for f in files:
dest = local_checkpoints / f
print(f">> Downloading {f} -> {dest}...")
res = subprocess.run(scp_cmd + [f"{REMOTE_HOST}:{REMOTE_DIR}/checkpoints/{f}", str(dest)])
if res.returncode == 0:
print(f">> Successfully downloaded {dest} ({dest.stat().st_size / (1024*1024):.1f} MB)")
else:
print(f">> Failed to download {f}")
return res.returncode
return 0
def main() -> int:
parser = argparse.ArgumentParser(description="Lightning AI GPU Remote Runner")
parser.add_argument("--action", choices=["sync", "train_stage1", "train_stage2", "train_stage3", "train_stage4", "train_stage4_2b", "train_stage4_2b_moe", "eval_2b", "fetch", "download_model", "all"], default="sync")
parser.add_argument("--model", default="stage4_2b", help="Model name to download (stage4_2b, stage4_1b, all)")
args = parser.parse_args()
if args.action in ("sync", "all"):
rc = sync_to_remote()
if rc != 0:
return rc
if args.action == "train_stage1":
rc = run_stage1()
if rc != 0:
return rc
if args.action == "train_stage2":
rc = run_stage2()
if rc != 0:
return rc
if args.action == "train_stage3":
rc = run_stage3()
if rc != 0:
return rc
if args.action == "train_stage4":
rc = run_stage4()
if rc != 0:
return rc
if args.action == "train_stage4_2b":
rc = run_stage4_2b()
if rc != 0:
return rc
if args.action == "train_stage4_2b_moe":
rc = sync_to_remote()
if rc != 0:
return rc
rc = run_stage4_2b_moe()
if rc != 0:
return rc
return fetch_artifacts()
if args.action == "eval_2b":
rc = sync_to_remote()
if rc != 0:
return rc
rc = run_eval_2b()
if rc != 0:
return rc
return fetch_artifacts()
if args.action in ("fetch", "all"):
rc = fetch_artifacts()
if rc != 0:
return rc
if args.action == "download_model":
rc = download_model(args.model)
if rc != 0:
return rc
return 0
if __name__ == "__main__":
sys.exit(main())
|