-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
283 lines (232 loc) · 8.49 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import logging
import os
import sys
import time
# from torch._six import inf
from math import inf
import torch
from termcolor import colored
def ampscaler_get_grad_norm(parameters, norm_type: float = 2.0) -> torch.Tensor:
if isinstance(parameters, torch.Tensor):
parameters = [parameters]
parameters = [p for p in parameters if p.grad is not None]
norm_type = float(norm_type)
if len(parameters) == 0:
return torch.tensor(0.0)
device = parameters[0].grad.device
if norm_type == inf:
total_norm = max(p.grad.detach().abs().max().to(device) for p in parameters)
else:
total_norm = torch.norm(
torch.stack(
[torch.norm(p.grad.detach(), norm_type).to(device) for p in parameters]
),
norm_type,
)
return total_norm
class NativeScalerWithGradNormCount:
state_dict_key = "amp_scaler"
def __init__(self):
self._scaler = torch.cuda.amp.GradScaler()
def __call__(
self,
loss,
optimizer,
clip_grad=None,
parameters=None,
create_graph=False,
update_grad=True,
retain_graph=False,
):
self._scaler.scale(loss).backward(
create_graph=create_graph, retain_graph=retain_graph
)
if update_grad:
if clip_grad is not None:
assert parameters is not None
self._scaler.unscale_(
optimizer
) # unscale the gradients of optimizer's assigned params in-place
norm = torch.nn.utils.clip_grad_norm_(parameters, clip_grad)
else:
self._scaler.unscale_(optimizer)
norm = ampscaler_get_grad_norm(parameters)
self._scaler.step(optimizer)
self._scaler.update()
else:
norm = None
return norm
def state_dict(self):
return self._scaler.state_dict()
def load_state_dict(self, state_dict):
self._scaler.load_state_dict(state_dict)
def create_logger(output_dir, dist_rank=0, name=""):
# create logger
logger = logging.getLogger(name)
logger.setLevel(logging.INFO)
logger.propagate = False
# create formatter
fmt = "[%(asctime)s %(name)s] (%(filename)s %(lineno)d): %(levelname)s %(message)s"
color_fmt = (
colored("[%(asctime)s %(name)s]", "green")
+ colored("(%(filename)s %(lineno)d)", "yellow")
+ ": %(levelname)s %(message)s"
)
# create console handlers for master process
if dist_rank == 0:
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.DEBUG)
console_handler.setFormatter(
logging.Formatter(fmt=color_fmt, datefmt="%Y-%m-%d %H:%M:%S")
)
logger.addHandler(console_handler)
# create file handlers
file_handler = logging.FileHandler(
os.path.join(output_dir, f"log_rank{dist_rank}_{int(time.time())}.txt"),
mode="a",
)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(logging.Formatter(fmt=fmt, datefmt="%Y-%m-%d %H:%M:%S"))
logger.addHandler(file_handler)
return logger
oc_maxmin_dict = {}
def layer_omax_hook(m, i, o):
name = m.name
if not isinstance(o, torch.Tensor):
return
if o.ndim == 3:
xmax = torch.amax(o, [0, 1]) # shape d
xmin = torch.amin(o, [0, 1]) # shape d
elif o.ndim == 2:
xmax = torch.amax(o, [0]) # shape d
xmin = torch.amin(o, [0]) # shape d
# if "q_proj" in name or "k_proj" in name:
# o_shape = o.shape
# reshape_o = o.reshape(o_shape[0], o_shape[1], m.num_heads, m.head_dim).transpose(1, 2)
# xmax = torch.amax(reshape_o, [0, 1, 2])
# xmin = torch.amin(reshape_o, [0, 1, 2])
if name not in oc_maxmin_dict:
oc_maxmin_dict[name] = (xmax.detach_(), xmin.detach_())
else:
oc_maxmin_dict[name] = (
torch.max(oc_maxmin_dict[name][0], xmax).detach_(),
torch.min(oc_maxmin_dict[name][1], xmin).detach_(),
)
# oc_maxmin_dict[name] = oc_maxmin_dict[name][0]*0.99+xmax*0.01,oc_maxmin_dict[name][1]*0.99+xmin*0.01
oc_mean_std_dict = {}
def layer_omean_std_hook(m, i, o):
name = m.name
if not isinstance(o, torch.Tensor):
return
std, mean = torch.std_mean(o)
if name not in oc_mean_std_dict:
oc_mean_std_dict[name] = (mean.detach_(), std.detach_())
else:
oc_mean_std_dict[name] = (
(oc_mean_std_dict[name][0] * 0.99 + mean).detach_(),
(oc_mean_std_dict[name][1] * 0.99 + std).detach_(),
)
ic_maxmin_dict = {}
def layer_i0max_hook(m, i, o):
name = m.name
if len(i) == 0 or not isinstance(i[0], torch.Tensor):
return
if i[0].ndim == 3:
xmax = torch.amax(i[0], [0, 1]) # shape d
xmin = torch.amin(i[0], [0, 1]) # shape d
elif i[0].ndim == 2:
xmax = torch.amax(i[0], [0]) # shape d
xmin = torch.amin(i[0], [0]) # shape d
elif i[0].ndim == 4:
xmax = torch.amax(i[0], [0, 1, 2])
xmin = torch.amin(i[0], [0, 1, 2])
if name not in ic_maxmin_dict:
ic_maxmin_dict[name] = xmax.detach_(), xmin.detach_()
else:
ic_maxmin_dict[name] = (
torch.max(ic_maxmin_dict[name][0], xmax).detach_(),
torch.min(ic_maxmin_dict[name][1], xmin).detach_(),
)
# ic_maxmin_dict[name] = ic_maxmin_dict[name][0]*0.99+xmax*0.01,ic_maxmin_dict[name][1]*0.99+xmin*0.01
def layer_i01max_hook(m, i, o):
name = m.name
if len(i) == 0 or not isinstance(i[0], torch.Tensor):
return
if i[0].ndim == 3:
i0_xmax = torch.amax(i[0], [0, 1]) # shape d
i0_xmin = torch.amin(i[0], [0, 1]) # shape d
i1_xmax = torch.amax(i[1], [0, 1]) # shape d
i1_xmin = torch.amin(i[1], [0, 1]) # shape d
elif i[0].ndim == 2:
i0_xmax = torch.amax(i[0], [0]) # shape d
i0_xmin = torch.amin(i[0], [0]) # shape d
i1_xmax = torch.amax(i[1], [0]) # shape d
i1_xmin = torch.amin(i[1], [0]) # shape d
# for i0
i0_name = f"{name}_i0"
if i0_name not in ic_maxmin_dict:
ic_maxmin_dict[i0_name] = i0_xmax.detach_(), i0_xmin.detach_()
else:
ic_maxmin_dict[i0_name] = (
torch.max(ic_maxmin_dict[i0_name][0], i0_xmax).detach_(),
torch.min(ic_maxmin_dict[i0_name][1], i0_xmin).detach_(),
)
# ic_maxmin_dict[name] = ic_maxmin_dict[name][0]*0.99+xmax*0.01,ic_maxmin_dict[name][1]*0.99+xmin*0.01
i1_name = f"{name}_i1"
if i1_name not in ic_maxmin_dict:
ic_maxmin_dict[i1_name] = i1_xmax.detach_(), i1_xmin.detach_()
else:
ic_maxmin_dict[i1_name] = (
torch.max(ic_maxmin_dict[i1_name][0], i1_xmax).detach_(),
torch.min(ic_maxmin_dict[i1_name][1], i1_xmin).detach_(),
)
oc_mean_feat_dict = {}
oc_mean_feat_num_dict = {}
def layer_omean_feature_hook(m, i, o):
name = m.name
if not isinstance(o, torch.Tensor):
return
o = o.float()
if name not in oc_mean_feat_dict:
oc_mean_feat_dict[name] = o.detach_()
oc_mean_feat_num_dict[name] = 1.0
else:
oc_mean_feat_dict[name] = (
oc_mean_feat_dict[name] * oc_mean_feat_num_dict[name] + o.detach_()
) / (oc_mean_feat_num_dict[name] + 1)
oc_mean_feat_num_dict[name] += 1.0
oc_feat_dict = {}
def layer_o_feature_hook(m, i, o):
name = m.name
bs = m.bs
if not isinstance(o, torch.Tensor):
return
if name not in oc_feat_dict:
oc_feat_dict[name] = o.detach_()
else:
if oc_feat_dict[name].shape[0] < bs:
oc_feat_dict[name] = torch.cat([oc_feat_dict[name], o], dim=0)
# oc_mean_feat_dict[name] = (
# oc_mean_feat_dict[name] * oc_mean_feat_num_dict[name] + o.detach_()
# ) / (oc_mean_feat_num_dict[name] + 1)
# oc_mean_feat_num_dict[name] += 1.0
oc_norm_dict = {}
oc_norm_num_dict = {}
def layer_onorm_hook(m, i, o):
name = m.name
if not isinstance(o, torch.Tensor):
return
if len(o.shape) == 2:
o = o.unsqueeze(0)
B, N, C = o.shape
o = o.reshape(B * N, C)
o = o.t()
o = o.float()
if name not in oc_norm_dict:
oc_norm_dict[name] = torch.norm(o, p=2, dim=1) ** 2
oc_norm_num_dict[name] = 1.0
else:
oc_norm_dict[name] = (
oc_norm_dict[name] * oc_norm_num_dict[name] + torch.norm(o, p=2, dim=1) ** 2
) / (oc_norm_num_dict[name] + 1)
oc_norm_num_dict[name] += 1.0