import json, os, sys, time, gc, collections, subprocess from pathlib import Path CFG=json.loads(Path(sys.argv[1]).read_text("utf-8")); ROOT=Path(CFG["OUT_ROOT"]) MODEL_ID="deepseek-ai/DeepSeek-R1-Distill-Llama-8B"; THINK=""; RESERVE=4.0 os.environ["TOKENIZERS_PARALLELISM"]="false"; os.environ["HF_HUB_ENABLE_HF_TRANSFER"]="1" q=subprocess.run(["nvidia-smi","--query-gpu=index,memory.total,memory.free,utilization.gpu", "--format=csv,noheader,nounits"],capture_output=True,text=True,check=True).stdout gpus=[{"index":int(a),"total":int(b)/1024,"free":int(c)/1024,"util":int(d)} for a,b,c,d in (x.split(",") for x in q.strip().splitlines())] print("devices: "+" ".join(f"[{g['index']}] {g['free']:.0f}/{g['total']:.0f}G {g['util']}%" for g in gpus)) NEED=CFG["MIN_ACT_GIB"] def usable_of(g): return g["free"]-RESERVE cands=sorted([g for g in gpus if usable_of(g)>=NEED],key=usable_of,reverse=True) if CFG["DEVICE_ID"] is not None: d=next(g for g in gpus if g["index"]==CFG["DEVICE_ID"]) if usable_of(d)