-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
252 lines (213 loc) · 8 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
import random
from collections import defaultdict
from typing import Union, List, Dict, NoReturn
import timeit
from argparse import ArgumentTypeError
import numpy as np
import torch
import torch.nn as nn
import torch.nn.init as init
from transformers import BartTokenizerFast
from metrics import RougeScorer
def compute_rouge_l(candidates: np.ndarray, references: np.ndarray, remove_ids: np.ndarray) -> Dict:
assert len(candidates) == len(references)
prec_scores = []
rec_scores = []
f1_scores = []
for candidate, reference in zip(candidates, references):
# remove special token ids
candidate = np.setdiff1d(candidate, remove_ids)
reference = np.setdiff1d(reference, remove_ids)
# compute lcs
m = len(candidate)
n = len(reference)
dp = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(m + 1):
for j in range(n + 1):
if i == 0 or j == 0:
dp[i][j] = 0
elif candidate[i - 1] == reference[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
# append scores
match = dp[m][n]
precision = match / n if n > 0 else 0
recall = match / m if m > 0 else 0
f1 = (2 * precision * recall) / (precision + recall) if (precision + recall) > 0 else 0
prec_scores.append(precision)
rec_scores.append(recall)
f1_scores.append(f1)
return {"precision": np.array(prec_scores, dtype=np.float32),
"recall": np.array(rec_scores, dtype=np.float32),
"f1": np.array(f1_scores, dtype=np.float32)} # (B,)
def set_all_seeds(seed, verbose=False):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(seed)
random.seed(seed)
if verbose:
print("All random seeds set to", seed)
def collate_fn(
batched_samples: List[Dict[str, List[int]]],
pad_token_idx: int,
pad_keys: List[str] = ["input_ids", "labels"],
sort_by_length: bool = True
) -> Dict[str, torch.Tensor]:
if sort_by_length:
batched_samples = sorted(batched_samples, key=lambda x: len(x["input_ids"]), reverse=True)
keys = batched_samples[0].keys()
outputs = defaultdict(list)
for key in keys:
for sample in batched_samples:
if sample[key] is not None:
if not isinstance(sample[key], torch.Tensor):
sample[key] = torch.tensor(sample[key])
outputs[key].append(sample[key])
else:
outputs[key] = None
PAD = pad_token_idx if key in pad_keys else 0
PAD = -1 if key in "answers" else PAD
if outputs[key] is not None:
outputs[key] = torch.nn.utils.rnn.pad_sequence(outputs[key], padding_value=PAD, batch_first=True)
return dict(outputs)
def combine_sentences(paragraphs) -> List[str]:
result = []
for paragraph in paragraphs:
if len(paragraph) < 1:
# no sentence in paragraph
continue
result.extend([sentence["sentence"] for sentence in paragraph])
return result
def freeze(
model: nn.Module,
name: Union[str, List[str]],
exact: bool = False,
) -> List[str]:
"""Freeze layers whose names correspond to the `name` parameter given.
Args:
model (nn.Module)
name (str or List[str])
exact (bool): (default: False)
Returns:
List[str] - list of frozen layers including previously frozen ones.
"""
def _freeze_exact(model, name):
for n, p in model.named_parameters():
if n == name:
p.requires_grad = False
def _freeze(model, name):
for n, p in model.named_parameters():
if n.count(name):
p.requires_grad = False
if not isinstance(name, list):
name = [name]
for n in name:
if exact:
_freeze_exact(model, n)
else:
_freeze(model, n)
return [n for n, p in model.named_parameters() if not p.requires_grad]
def unfreeze_all(model: nn.Module) -> NoReturn:
for p in model.parameters():
p.requires_grad = True
def compute_metrics(pred_sentences, ref_sentences, apply_none=False):
rouge = RougeScorer()
if apply_none:
rouge.rouge_evaluator.apply_avg = False
rouge.rouge_evaluator.apply_best = False
scores = rouge.compute_rouge(ref_sentences, pred_sentences)
return scores
def np_sigmoid(x: np.ndarray):
x = np.clip(x, -10, 10)
return 1/(1+np.exp(-x))
def init_weight(m):
'''
source: https://gist.github.com/jeasinema/ed9236ce743c8efaf30fa2ff732749f5
Usage:
model = Model()
model.apply(weight_init)
'''
if isinstance(m, nn.Conv1d):
init.normal_(m.weight.data)
if m.bias is not None:
init.normal_(m.bias.data)
elif isinstance(m, nn.Conv2d):
init.xavier_normal_(m.weight.data)
if m.bias is not None:
init.normal_(m.bias.data)
elif isinstance(m, nn.Conv3d):
init.xavier_normal_(m.weight.data)
if m.bias is not None:
init.normal_(m.bias.data)
elif isinstance(m, nn.ConvTranspose1d):
init.normal_(m.weight.data)
if m.bias is not None:
init.normal_(m.bias.data)
elif isinstance(m, nn.ConvTranspose2d):
init.xavier_normal_(m.weight.data)
if m.bias is not None:
init.normal_(m.bias.data)
elif isinstance(m, nn.ConvTranspose3d):
init.xavier_normal_(m.weight.data)
if m.bias is not None:
init.normal_(m.bias.data)
elif isinstance(m, nn.BatchNorm1d):
init.normal_(m.weight.data, mean=1, std=0.02)
init.constant_(m.bias.data, 0)
elif isinstance(m, nn.BatchNorm2d):
init.normal_(m.weight.data, mean=1, std=0.02)
init.constant_(m.bias.data, 0)
elif isinstance(m, nn.BatchNorm3d):
init.normal_(m.weight.data, mean=1, std=0.02)
init.constant_(m.bias.data, 0)
elif isinstance(m, nn.Linear):
init.xavier_normal_(m.weight.data)
init.normal_(m.bias.data)
elif isinstance(m, nn.LSTM):
for param in m.parameters():
if len(param.shape) >= 2:
init.orthogonal_(param.data)
else:
init.normal_(param.data)
elif isinstance(m, nn.LSTMCell):
for param in m.parameters():
if len(param.shape) >= 2:
init.orthogonal_(param.data)
else:
init.normal_(param.data)
elif isinstance(m, nn.GRU):
for param in m.parameters():
if len(param.shape) >= 2:
init.orthogonal_(param.data)
else:
init.normal_(param.data)
elif isinstance(m, nn.GRUCell):
for param in m.parameters():
if len(param.shape) >= 2:
init.orthogonal_(param.data)
else:
init.normal_(param.data)
class PrintInfo:
def __init__(self):
self.time_step = timeit.default_timer()
self.accumulation = 0
def SECTION(self, section: str, simple: bool = False):
if not simple: print("\n" + "*" * 10)
print("{} // before_step: {}ms // total: {}s".format(section, round(self._reset_time()), round(self.accumulation, 2)))
if not simple: print("*" * 10 + "\n")
def _reset_time(self):
temp = self.time_step
self.time_step = timeit.default_timer()
diff = self.time_step - temp
self.accumulation += diff
return diff * 1000
def get_eos_positions(x: torch.Tensor, tokenizer: BartTokenizerFast):
eos_positions = []
for i in range(x.size(0)):
ids = torch.eq(x[i], tokenizer.eos_token_id).nonzero().squeeze(1)
eos_positions.append(ids)
return torch.nn.utils.rnn.pad_sequence(eos_positions, batch_first=True, padding_value=-1)