Display correct CUDA devices (#1776)
Browse files* Display correct CUDA devices
* cleanup
- utils/torch_utils.py +4 -4
utils/torch_utils.py
CHANGED
@@ -48,20 +48,20 @@ def select_device(device='', batch_size=None):
|
|
48 |
cpu_request = device.lower() == 'cpu'
|
49 |
if device and not cpu_request: # if device requested other than 'cpu'
|
50 |
os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable
|
51 |
-
assert torch.cuda.is_available(), 'CUDA unavailable, invalid device
|
52 |
|
53 |
cuda = False if cpu_request else torch.cuda.is_available()
|
54 |
if cuda:
|
55 |
c = 1024 ** 2 # bytes to MB
|
56 |
ng = torch.cuda.device_count()
|
57 |
if ng > 1 and batch_size: # check that batch_size is compatible with device_count
|
58 |
-
assert batch_size % ng == 0, 'batch-size
|
59 |
x = [torch.cuda.get_device_properties(i) for i in range(ng)]
|
60 |
s = f'Using torch {torch.__version__} '
|
61 |
-
for i in
|
62 |
if i == 1:
|
63 |
s = ' ' * len(s)
|
64 |
-
logger.info("
|
65 |
else:
|
66 |
logger.info(f'Using torch {torch.__version__} CPU')
|
67 |
|
|
|
48 |
cpu_request = device.lower() == 'cpu'
|
49 |
if device and not cpu_request: # if device requested other than 'cpu'
|
50 |
os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable
|
51 |
+
assert torch.cuda.is_available(), f'CUDA unavailable, invalid device {device} requested' # check availablity
|
52 |
|
53 |
cuda = False if cpu_request else torch.cuda.is_available()
|
54 |
if cuda:
|
55 |
c = 1024 ** 2 # bytes to MB
|
56 |
ng = torch.cuda.device_count()
|
57 |
if ng > 1 and batch_size: # check that batch_size is compatible with device_count
|
58 |
+
assert batch_size % ng == 0, f'batch-size {batch_size} not multiple of GPU count {ng}'
|
59 |
x = [torch.cuda.get_device_properties(i) for i in range(ng)]
|
60 |
s = f'Using torch {torch.__version__} '
|
61 |
+
for i, d in enumerate((device or '0').split(',')):
|
62 |
if i == 1:
|
63 |
s = ' ' * len(s)
|
64 |
+
logger.info(f"{s}CUDA:{d} ({x[i].name}, {x[i].total_memory / c}MB)")
|
65 |
else:
|
66 |
logger.info(f'Using torch {torch.__version__} CPU')
|
67 |
|