glenn-jocher commited on
Commit
9f5a18b
1 Parent(s): 0b6266f

Torch CUDA synchronize update (#1826)

Browse files

* torch.cuda.synchronize() update

* torch.cuda.synchronize() update

* torch.cuda.synchronize() update

* newline

Files changed (1) hide show
  1. utils/torch_utils.py +20 -21
utils/torch_utils.py CHANGED
@@ -36,42 +36,41 @@ def init_torch_seeds(seed=0):
36
  # Speed-reproducibility tradeoff https://pytorch.org/docs/stable/notes/randomness.html
37
  torch.manual_seed(seed)
38
  if seed == 0: # slower, more reproducible
39
- cudnn.deterministic = True
40
- cudnn.benchmark = False
41
  else: # faster, less reproducible
42
- cudnn.deterministic = False
43
- cudnn.benchmark = True
44
 
45
 
46
  def select_device(device='', batch_size=None):
47
  # device = 'cpu' or '0' or '0,1,2,3'
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
 
68
- logger.info('') # skip a line
69
  return torch.device('cuda:0' if cuda else 'cpu')
70
 
71
 
72
  def time_synchronized():
73
  # pytorch-accurate time
74
- torch.cuda.synchronize() if torch.cuda.is_available() else None
 
75
  return time.time()
76
 
77
 
 
36
  # Speed-reproducibility tradeoff https://pytorch.org/docs/stable/notes/randomness.html
37
  torch.manual_seed(seed)
38
  if seed == 0: # slower, more reproducible
39
+ cudnn.benchmark, cudnn.deterministic = False, True
 
40
  else: # faster, less reproducible
41
+ cudnn.benchmark, cudnn.deterministic = True, False
 
42
 
43
 
44
  def select_device(device='', batch_size=None):
45
  # device = 'cpu' or '0' or '0,1,2,3'
46
+ s = f'Using torch {torch.__version__} ' # string
47
+ cpu = device.lower() == 'cpu'
48
+ if cpu:
49
+ os.environ['CUDA_VISIBLE_DEVICES'] = '-1' # force torch.cuda.is_available() = False
50
+ elif device: # non-cpu device requested
51
  os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable
52
+ assert torch.cuda.is_available(), f'CUDA unavailable, invalid device {device} requested' # check availability
53
 
54
+ cuda = torch.cuda.is_available() and not cpu
55
  if cuda:
56
+ n = torch.cuda.device_count()
57
+ if n > 1 and batch_size: # check that batch_size is compatible with device_count
58
+ assert batch_size % n == 0, f'batch-size {batch_size} not multiple of GPU count {n}'
59
+ space = ' ' * len(s)
60
+ for i, d in enumerate(device.split(',') if device else range(n)):
61
+ p = torch.cuda.get_device_properties(i)
62
+ s += f"{'' if i == 0 else space}CUDA:{d} ({p.name}, {p.total_memory / 1024 ** 2}MB)\n" # bytes to MB
 
 
 
63
  else:
64
+ s += 'CPU'
65
 
66
+ logger.info(f'{s}\n') # skip a line
67
  return torch.device('cuda:0' if cuda else 'cpu')
68
 
69
 
70
  def time_synchronized():
71
  # pytorch-accurate time
72
+ if torch.cuda.is_available():
73
+ torch.cuda.synchronize()
74
  return time.time()
75
 
76