| |
|
|
| |
| |
| |
| |
| |
| |
|
|
| import multiprocessing as mp |
| import subprocess |
| import os |
| from pathlib import Path |
| import socket |
| import argparse |
|
|
|
|
| HOSTNAME = socket.gethostname() |
|
|
| |
| |
| VRAM_PER_TRAINING_MB = 22500 |
|
|
|
|
| def get_gpu_memory(): |
|
|
| result = subprocess.check_output( |
| [ |
| "nvidia-smi", |
| "--query-gpu=memory.total,memory.used", |
| "--format=csv,noheader,nounits", |
| ] |
| ) |
|
|
| gpu_info = [] |
|
|
| for line in result.decode().strip().split("\n"): |
|
|
| total, used = line.split(",") |
|
|
| gpu_info.append( |
| { |
| "total": int(total), |
| "used": int(used), |
| "free": int(total) - int(used), |
| } |
| ) |
|
|
| return gpu_info |
|
|
|
|
| def create_gpu_slots(): |
|
|
| gpu_info = get_gpu_memory() |
|
|
| print("\nGPU memory status:") |
|
|
| for gpu_id, gpu in enumerate(gpu_info): |
|
|
| print( |
| f"GPU {gpu_id}: " |
| f"{gpu['free']} MB free / " |
| f"{gpu['total']} MB total" |
| ) |
|
|
| slots = [] |
| used = set() |
|
|
| |
| |
| |
| for gpu_id, gpu in enumerate(gpu_info): |
|
|
| if gpu["free"] >= VRAM_PER_TRAINING_MB: |
|
|
| slots.append(gpu_id) |
| used.add(gpu_id) |
|
|
| |
| |
| |
| remaining = [ |
| (gpu_id, gpu_info[gpu_id]) |
| for gpu_id in range(len(gpu_info)) |
| if gpu_id not in used |
| ] |
|
|
| current_group = [] |
| current_memory = 0 |
|
|
| for gpu_id, gpu in remaining: |
|
|
| current_group.append(gpu_id) |
| current_memory += gpu["free"] |
|
|
| if current_memory >= VRAM_PER_TRAINING_MB: |
|
|
| slots.append(current_group) |
|
|
| current_group = [] |
| current_memory = 0 |
|
|
| if current_group: |
|
|
| print( |
| "\nWarning: Remaining GPUs " |
| f"{current_group} do not have enough combined free memory " |
| "for another training job." |
| ) |
|
|
| if not slots: |
| raise RuntimeError( |
| "No GPU (or GPU group) has enough free memory to start training." |
| ) |
|
|
| return slots |
|
|
|
|
| def train_worker(run_id, gpu, seed, dataset_path): |
|
|
| from ultralytics import YOLO |
|
|
| model = YOLO("yolo11m.pt") |
|
|
| dataset_name = Path(dataset_path).parents[1].name |
|
|
| project_folder = os.path.join( |
| "training_stats", |
| dataset_name, |
| HOSTNAME, |
| ) |
|
|
| print( |
| f"Starting run {run_id} " |
| f"on GPU(s) {gpu} " |
| f"with seed {seed}" |
| ) |
|
|
| model.train( |
| data=dataset_path, |
| epochs=500, |
| patience=30, |
| batch=16, |
| imgsz=1024, |
| lr0=0.01, |
| device=gpu, |
| project=project_folder, |
| name=f"run_{run_id}", |
| seed=seed, |
| ) |
|
|
|
|
| def run_job(args): |
|
|
| run_id, gpu, seed, dataset_path = args |
|
|
| train_worker( |
| run_id, |
| gpu, |
| seed, |
| dataset_path, |
| ) |
|
|
|
|
| if __name__ == "__main__": |
|
|
| parser = argparse.ArgumentParser() |
|
|
| parser.add_argument( |
| "--dataset", |
| type=str, |
| required=True, |
| help="Path to YOLO dataset.yaml", |
| ) |
|
|
| parser.add_argument( |
| "--run-start", |
| type=int, |
| default=1, |
| help="First run number.", |
| ) |
|
|
| parser.add_argument( |
| "--run-end", |
| type=int, |
| default=6, |
| help="Last run number (inclusive).", |
| ) |
|
|
| args = parser.parse_args() |
|
|
| if args.run_start > args.run_end: |
| raise ValueError("--run-start must be <= --run-end") |
|
|
| mp.set_start_method( |
| "spawn", |
| force=True, |
| ) |
|
|
| slots = create_gpu_slots() |
|
|
| print("\nAvailable training slots:") |
|
|
| for i, slot in enumerate(slots): |
|
|
| if isinstance(slot, list): |
| print(f"Slot {i}: GPUs {slot}") |
| else: |
| print(f"Slot {i}: GPU {slot}") |
|
|
| jobs = [] |
|
|
| for run_id in range( |
| args.run_start, |
| args.run_end + 1, |
| ): |
|
|
| gpu = slots[ |
| (run_id - args.run_start) % len(slots) |
| ] |
|
|
| seed = run_id - 1 |
|
|
| jobs.append( |
| ( |
| run_id, |
| gpu, |
| seed, |
| args.dataset, |
| ) |
| ) |
|
|
| processes = [] |
|
|
| for job in jobs: |
|
|
| p = mp.Process( |
| target=run_job, |
| args=(job,), |
| ) |
|
|
| p.start() |
|
|
| processes.append(p) |
|
|
| |
| |
| |
| if len(processes) >= len(slots): |
|
|
| for p in processes: |
| p.join() |
|
|
| processes = [] |
|
|
| for p in processes: |
| p.join() |
|
|
| print("\nAll training runs completed.") |
|
|