From f12a2a513acda68146616368281fb0b63d15bb36 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Sun, 24 Nov 2019 18:29:29 -1000 Subject: [PATCH] updates --- test.py | 2 +- train.py | 2 +- utils/torch_utils.py | 8 +++++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/test.py b/test.py index c233adcf..c6656246 100644 --- a/test.py +++ b/test.py @@ -20,7 +20,7 @@ def test(cfg, model=None): # Initialize/load model and set device if model is None: - device = torch_utils.select_device(opt.device) + device = torch_utils.select_device(opt.device, batch_size=batch_size) verbose = True # Initialize model diff --git a/train.py b/train.py index 3a4bc34f..ecd8f4fd 100644 --- a/train.py +++ b/train.py @@ -423,7 +423,7 @@ if __name__ == '__main__': opt = parser.parse_args() opt.weights = last if opt.resume else opt.weights print(opt) - device = torch_utils.select_device(opt.device, apex=mixed_precision) + device = torch_utils.select_device(opt.device, apex=mixed_precision, batch_size=opt.batch_size) if device.type == 'cpu': mixed_precision = False diff --git a/utils/torch_utils.py b/utils/torch_utils.py index b631262e..466a22ae 100644 --- a/utils/torch_utils.py +++ b/utils/torch_utils.py @@ -14,7 +14,7 @@ def init_seeds(seed=0): torch.backends.cudnn.benchmark = False -def select_device(device='', apex=False): +def select_device(device='', apex=False, batch_size=None): # device = 'cpu' or '0' or '0,1,2,3' cpu_request = device.lower() == 'cpu' if device and not cpu_request: # if device requested other than 'cpu' @@ -25,11 +25,13 @@ def select_device(device='', apex=False): if cuda: c = 1024 ** 2 # bytes to MB ng = torch.cuda.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) x = [torch.cuda.get_device_properties(i) for i in range(ng)] - cuda_str = 'Using CUDA ' + ('Apex ' if apex else '') # apex for mixed precision https://github.com/NVIDIA/apex + s = 'Using CUDA ' + ('Apex ' if apex else '') # apex for mixed precision https://github.com/NVIDIA/apex for i in range(0, ng): if i == 1: - cuda_str = ' ' * len(cuda_str) + s = ' ' * len(s) print("%sdevice%g _CudaDeviceProperties(name='%s', total_memory=%dMB)" % (cuda_str, i, x[i].name, x[i].total_memory / c)) else: