forked from facebookresearch/msn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
261 lines (208 loc) · 8.06 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
import math
import os
from logging import getLogger
import torch
import torch.distributed as dist
logger = getLogger()
def gpu_timer(closure, log_timings=True):
"""Helper to time gpu-time to execute closure()"""
elapsed_time = -1.0
if log_timings:
start = torch.cuda.Event(enable_timing=True)
end = torch.cuda.Event(enable_timing=True)
start.record()
result = closure()
if log_timings:
end.record()
torch.cuda.synchronize()
elapsed_time = start.elapsed_time(end)
return result, elapsed_time
def init_distributed(port=40111, rank_and_world_size=(None, None)):
if dist.is_available() and dist.is_initialized():
return dist.get_world_size(), dist.get_rank()
rank, world_size = rank_and_world_size
os.environ["MASTER_ADDR"] = "localhost"
if (rank is None) or (world_size is None):
try:
world_size = int(os.environ["SLURM_NTASKS"])
rank = int(os.environ["SLURM_PROCID"])
os.environ["MASTER_ADDR"] = os.environ["HOSTNAME"]
except Exception:
logger.info("SLURM vars not set (distributed training not available)")
world_size, rank = 1, 0
return world_size, rank
try:
os.environ["MASTER_PORT"] = str(port)
torch.distributed.init_process_group(backend="nccl", world_size=world_size, rank=rank)
except Exception:
world_size, rank = 1, 0
logger.info("distributed training not available")
return world_size, rank
class WarmupCosineSchedule(object):
def __init__(
self, optimizer, warmup_steps, start_lr, ref_lr, T_max, last_epoch=-1, final_lr=0.0
):
self.optimizer = optimizer
self.start_lr = start_lr
self.ref_lr = ref_lr
self.final_lr = final_lr
self.warmup_steps = warmup_steps
self.T_max = T_max - warmup_steps
self._step = 0.0
def step(self):
self._step += 1
if self._step < self.warmup_steps:
progress = float(self._step) / float(max(1, self.warmup_steps))
new_lr = self.start_lr + progress * (self.ref_lr - self.start_lr)
else:
# -- progress after warmup
progress = float(self._step - self.warmup_steps) / float(max(1, self.T_max))
new_lr = max(
self.final_lr,
self.final_lr
+ (self.ref_lr - self.final_lr) * 0.5 * (1.0 + math.cos(math.pi * progress)),
)
for group in self.optimizer.param_groups:
group["lr"] = new_lr
return new_lr
class CosineWDSchedule(object):
def __init__(self, optimizer, ref_wd, T_max, final_wd=0.0):
self.optimizer = optimizer
self.ref_wd = ref_wd
self.final_wd = final_wd
self.T_max = T_max
self._step = 0.0
def step(self):
self._step += 1
progress = self._step / self.T_max
new_wd = self.final_wd + (self.ref_wd - self.final_wd) * 0.5 * (
1.0 + math.cos(math.pi * progress)
)
if self.final_wd <= self.ref_wd:
new_wd = max(self.final_wd, new_wd)
else:
new_wd = min(self.final_wd, new_wd)
for group in self.optimizer.param_groups:
if ("WD_exclude" not in group) or not group["WD_exclude"]:
group["weight_decay"] = new_wd
return new_wd
class CSVLogger(object):
def __init__(self, fname, *argv):
self.fname = fname
self.types = []
# -- print headers
with open(self.fname, "+a") as f:
for i, v in enumerate(argv, 1):
self.types.append(v[0])
if i < len(argv):
print(v[1], end=",", file=f)
else:
print(v[1], end="\n", file=f)
def log(self, *argv):
with open(self.fname, "+a") as f:
for i, tv in enumerate(zip(self.types, argv), 1):
end = "," if i < len(argv) else "\n"
print(tv[0] % tv[1], end=end, file=f)
class AverageMeter(object):
"""computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.max = float("-inf")
self.min = float("inf")
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.max = max(val, self.max)
self.min = min(val, self.min)
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
class AllGather(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
if dist.is_available() and dist.is_initialized() and (dist.get_world_size() > 1):
outputs = [torch.zeros_like(x) for _ in range(dist.get_world_size())]
dist.all_gather(outputs, x)
return torch.cat(outputs, 0)
return x
@staticmethod
def backward(ctx, grads):
if dist.is_available() and dist.is_initialized() and (dist.get_world_size() > 1):
s = (grads.shape[0] // dist.get_world_size()) * dist.get_rank()
e = (grads.shape[0] // dist.get_world_size()) * (dist.get_rank() + 1)
grads = grads.contiguous()
dist.all_reduce(grads)
return grads[s:e]
return grads
class AllReduceSum(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
if dist.is_available() and dist.is_initialized() and (dist.get_world_size() > 1):
x = x.contiguous()
dist.all_reduce(x)
return x
@staticmethod
def backward(ctx, grads):
return grads
class AllReduce(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
if dist.is_available() and dist.is_initialized() and (dist.get_world_size() > 1):
x = x.contiguous() / dist.get_world_size()
dist.all_reduce(x)
return x
@staticmethod
def backward(ctx, grads):
return grads
def _no_grad_trunc_normal_(tensor, mean, std, a, b):
# Cut & paste from PyTorch official master until it's in a few official releases - RW
# Method based on https://people.sc.fsu.edu/~jburkardt/presentations/truncated_normal.pdf
def norm_cdf(x):
# Computes standard normal cumulative distribution function
return (1.0 + math.erf(x / math.sqrt(2.0))) / 2.0
with torch.no_grad():
# Values are generated by using a truncated uniform distribution and
# then using the inverse CDF for the normal distribution.
# Get upper and lower cdf values
l = norm_cdf((a - mean) / std)
u = norm_cdf((b - mean) / std)
# Uniformly fill tensor with values from [l, u], then translate to
# [2l-1, 2u-1].
tensor.uniform_(2 * l - 1, 2 * u - 1)
# Use inverse cdf transform for normal distribution to get truncated
# standard normal
tensor.erfinv_()
# Transform to proper mean, std
tensor.mul_(std * math.sqrt(2.0))
tensor.add_(mean)
# Clamp to ensure it's in the proper range
tensor.clamp_(min=a, max=b)
return tensor
def trunc_normal_(tensor, mean=0.0, std=1.0, a=-2.0, b=2.0):
# type: (Tensor, float, float, float, float) -> Tensor
return _no_grad_trunc_normal_(tensor, mean, std, a, b)
def grad_logger(named_params):
stats = AverageMeter()
stats.first_layer = None
stats.last_layer = None
for n, p in named_params:
if (p.grad is not None) and not (n.endswith(".bias") or len(p.shape) == 1):
grad_norm = float(torch.norm(p.grad.data))
stats.update(grad_norm)
if "qkv" in n:
stats.last_layer = grad_norm
if stats.first_layer is None:
stats.first_layer = grad_norm
if stats.first_layer is None or stats.last_layer is None:
stats.first_layer = stats.last_layer = 0.0
return stats