Unverified 提交 c0ffcdf9 authored 作者: Glenn Jocher's avatar Glenn Jocher 提交者: GitHub

Display correct CUDA devices (#1776)

* Display correct CUDA devices * cleanup
上级 8a81839c
...@@ -48,20 +48,20 @@ def select_device(device='', batch_size=None): ...@@ -48,20 +48,20 @@ def select_device(device='', batch_size=None):
cpu_request = device.lower() == 'cpu' cpu_request = device.lower() == 'cpu'
if device and not cpu_request: # if device requested other than 'cpu' if device and not cpu_request: # if device requested other than 'cpu'
os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable
assert torch.cuda.is_available(), 'CUDA unavailable, invalid device %s requested' % device # check availablity assert torch.cuda.is_available(), f'CUDA unavailable, invalid device {device} requested' # check availablity
cuda = False if cpu_request else torch.cuda.is_available() cuda = False if cpu_request else torch.cuda.is_available()
if cuda: if cuda:
c = 1024 ** 2 # bytes to MB c = 1024 ** 2 # bytes to MB
ng = torch.cuda.device_count() ng = torch.cuda.device_count()
if ng > 1 and batch_size: # check that batch_size is compatible with device_count if ng > 1 and batch_size: # check that batch_size is compatible with device_count
assert batch_size % ng == 0, 'batch-size %g not multiple of GPU count %g' % (batch_size, ng) assert batch_size % ng == 0, f'batch-size {batch_size} not multiple of GPU count {ng}'
x = [torch.cuda.get_device_properties(i) for i in range(ng)] x = [torch.cuda.get_device_properties(i) for i in range(ng)]
s = f'Using torch {torch.__version__} ' s = f'Using torch {torch.__version__} '
for i in range(0, ng): for i, d in enumerate((device or '0').split(',')):
if i == 1: if i == 1:
s = ' ' * len(s) s = ' ' * len(s)
logger.info("%sCUDA:%g (%s, %dMB)" % (s, i, x[i].name, x[i].total_memory / c)) logger.info(f"{s}CUDA:{d} ({x[i].name}, {x[i].total_memory / c}MB)")
else: else:
logger.info(f'Using torch {torch.__version__} CPU') logger.info(f'Using torch {torch.__version__} CPU')
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论