glenn-jocher commited on
Commit
e7bf382
1 Parent(s): af00134

Fix `device` count check (#6290)

Browse files

* Fix device count check()

* Update torch_utils.py

* Update torch_utils.py

* Update hubconf.py

Files changed (2) hide show
  1. hubconf.py +1 -1
  2. utils/torch_utils.py +3 -2
hubconf.py CHANGED
@@ -61,7 +61,7 @@ def _create(name, pretrained=True, channels=3, classes=80, autoshape=True, verbo
61
 
62
  except Exception as e:
63
  help_url = 'https://github.com/ultralytics/yolov5/issues/36'
64
- s = 'Cache may be out of date, try `force_reload=True`. See %s for help.' % help_url
65
  raise Exception(s) from e
66
 
67
 
 
61
 
62
  except Exception as e:
63
  help_url = 'https://github.com/ultralytics/yolov5/issues/36'
64
+ s = f'{e}. Cache may be out of date, try `force_reload=True` or see {help_url} for help.'
65
  raise Exception(s) from e
66
 
67
 
utils/torch_utils.py CHANGED
@@ -61,8 +61,9 @@ def select_device(device='', batch_size=0, newline=True):
61
  if cpu:
62
  os.environ['CUDA_VISIBLE_DEVICES'] = '-1' # force torch.cuda.is_available() = False
63
  elif device: # non-cpu device requested
64
- os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable
65
- assert torch.cuda.is_available(), f'CUDA unavailable, invalid device {device} requested' # check availability
 
66
 
67
  cuda = not cpu and torch.cuda.is_available()
68
  if cuda:
 
61
  if cpu:
62
  os.environ['CUDA_VISIBLE_DEVICES'] = '-1' # force torch.cuda.is_available() = False
63
  elif device: # non-cpu device requested
64
+ assert torch.cuda.is_available(), 'CUDA unavailable' # check CUDA is available
65
+ assert torch.cuda.device_count() > int(device), f'invalid CUDA device {device} requested' # check index
66
+ os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable (must be after asserts)
67
 
68
  cuda = not cpu and torch.cuda.is_available()
69
  if cuda: