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
- 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 |
-
|
49 |
-
|
|
|
|
|
|
|
50 |
os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable
|
51 |
-
assert torch.cuda.is_available(), f'CUDA unavailable, invalid device {device} requested' # check
|
52 |
|
53 |
-
cuda =
|
54 |
if cuda:
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
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 |
-
|
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 |
-
|
|
|
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 |
|