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
- 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 |
-
|
65 |
-
|
66 |
-
assert
|
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()
|