Spaces:
Running
Running
Download scripts/remote_runner.py from Vir007/continual-ai: direct link, hf CLI and curl.
- Browser
- Download file 6.78 kB
-
https://huggingface.co/spaces/Vir007/continual-ai/resolve/main/scripts/remote_runner.py
- Command line
-
hf download hf://spaces/Vir007/continual-ai/scripts/remote_runner.py
-
curl -L -o remote_runner.py https://huggingface.co/spaces/Vir007/continual-ai/resolve/main/scripts/remote_runner.py
6.78 kB
| """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()) | |