Lock GPU pin with warm CUDA alloc before ultralytics select_device runs

Ultralytics select_device() rewrites CUDA_VISIBLE_DEVICES on first
predict() when the CUDA context is uninitialized: --gpu 1 silently
became a two-GPU cuda:0,1 context that OOMed on GPU0 (Strata's card).
Initializing torch and touching cuda once after setting CVD locks the
pin; both scripts now use device cuda:0 within the pin.
This commit is contained in:
2026-10-18 09:10:55 -04:00
parent e8fd716d82
commit 940c59d6c9
2 changed files with 23 additions and 4 deletions
+9 -1
View File
@@ -40,8 +40,16 @@ def main():
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
device = "cpu"
elif args.gpu.lower() != "auto":
# Lock the pin with a warm CUDA alloc BEFORE ultralytics runs
# select_device(), which would otherwise rewrite
# CUDA_VISIBLE_DEVICES and un-pin us onto GPU0.
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu
device = "cuda"
import torch
if torch.cuda.is_available():
torch.zeros(1, device="cuda")
device = "cuda:0"
else:
device = "cpu"
else:
device = None