glenn-jocher commited on
Commit
436ffc4
1 Parent(s): a1a9c68

`select_device()` cleanup (#6302)

Browse files

* `select_device()` cleanup

* Update torch_utils.py

* Update torch_utils.py

* Update torch_utils.py

* Update torch_utils.py

* Update torch_utils.py

Files changed (1) hide show
  1. utils/torch_utils.py +3 -3
utils/torch_utils.py CHANGED
@@ -61,9 +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
- assert torch.cuda.is_available(), 'CUDA unavailable' # check CUDA is available
65
- device_list = [int(val) for val in device.replace(',', '')]
66
- assert all([torch.cuda.device_count() > element for element in device_list]), f'invalid CUDA device {device} requested' # check index
67
  os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable (must be after asserts)
68
 
69
  cuda = not cpu and torch.cuda.is_available()
 
61
  if cpu:
62
  os.environ['CUDA_VISIBLE_DEVICES'] = '-1' # force torch.cuda.is_available() = False
63
  elif device: # non-cpu device requested
64
+ nd = torch.cuda.device_count() # number of CUDA devices
65
+ assert torch.cuda.is_available(), 'CUDA is not available, use `--device cpu` or do not pass a --device'
66
+ assert nd > int(max(device.split(','))), f'Invalid `--device {device}` request, valid devices are 0 - {nd - 1}'
67
  os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable (must be after asserts)
68
 
69
  cuda = not cpu and torch.cuda.is_available()