Empty index `[0,,1]` robust device selection (#16631)

pull/16638/head^2
Glenn Jocher 2 months ago committed by GitHub
parent 7cc40f6847
commit 5c76601d21
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      ultralytics/utils/torch_utils.py

@ -170,6 +170,8 @@ def select_device(device="", batch=0, newline=False, verbose=True):
elif device: # non-cpu device requested elif device: # non-cpu device requested
if device == "cuda": if device == "cuda":
device = "0" device = "0"
if "," in device:
device = ",".join([x for x in device.split(",") if x]) # remove sequential commas, i.e. "0,,1" -> "0,1"
visible = os.environ.get("CUDA_VISIBLE_DEVICES", None) visible = os.environ.get("CUDA_VISIBLE_DEVICES", None)
os.environ["CUDA_VISIBLE_DEVICES"] = device # set environment variable - must be before assert is_available() os.environ["CUDA_VISIBLE_DEVICES"] = device # set environment variable - must be before assert is_available()
if not (torch.cuda.is_available() and torch.cuda.device_count() >= len(device.split(","))): if not (torch.cuda.is_available() and torch.cuda.device_count() >= len(device.split(","))):
@ -191,7 +193,7 @@ def select_device(device="", batch=0, newline=False, verbose=True):
) )
if not cpu and not mps and torch.cuda.is_available(): # prefer GPU if available if not cpu and not mps and torch.cuda.is_available(): # prefer GPU if available
devices = device.split(",") if device else "0" # range(torch.cuda.device_count()) # i.e. 0,1,6,7 devices = device.split(",") if device else "0" # i.e. "0,1" -> ["0", "1"]
n = len(devices) # device count n = len(devices) # device count
if n > 1: # multi-GPU if n > 1: # multi-GPU
if batch < 1: if batch < 1:

Loading…
Cancel
Save