-
Notifications
You must be signed in to change notification settings - Fork 5
/
qa_utils.py
511 lines (429 loc) · 23.4 KB
/
qa_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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
import pickle
import os
import numpy as np
import torch
import torch.nn as nn
from transformers import AutoModel
import json
from tqdm import tqdm
from transformers import (OpenAIGPTTokenizer, BertTokenizer, XLNetTokenizer, RobertaTokenizer, AutoTokenizer)
try:
from transformers import AlbertTokenizer
except:
pass
from transformers import (OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP,
XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP)
import math
from torch.optim.optimizer import Optimizer
MODEL_CLASS_TO_NAME = {
'gpt': list(OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP.keys()),
'bert': list(BERT_PRETRAINED_CONFIG_ARCHIVE_MAP.keys()),
'xlnet': list(XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP.keys()),
'roberta': list(ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP.keys()),
'lstm': ['lstm'],
}
MODEL_NAME_TO_CLASS = {model_name: model_class for model_class, model_name_list in MODEL_CLASS_TO_NAME.items() for model_name in model_name_list}
def gelu(x):
""" Implementation of the gelu activation function currently in Google Bert repo (identical to OpenAI GPT).
Also see https://arxiv.org/abs/1606.08415
"""
return 0.5 * x * (1 + torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(x, 3))))
class GELU(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
return gelu(x)
class MatrixVectorScaledDotProductAttention(nn.Module):
def __init__(self, temperature, attn_dropout=0.1):
super().__init__()
self.temperature = temperature
self.dropout = nn.Dropout(attn_dropout)
self.softmax = nn.Softmax(dim=1)
def forward(self, q, k, v, mask=None):
"""
q: tensor of shape (n*b, d_k)
k: tensor of shape (n*b, l, d_k)
v: tensor of shape (n*b, l, d_v)
returns: tensor of shape (n*b, d_v), tensor of shape(n*b, l)
"""
attn = (q.unsqueeze(1) * k).sum(2) # (n*b, l)
attn = attn / self.temperature
if mask is not None:
# import pdb; pdb.set_trace()
attn = attn.masked_fill(mask, -np.inf)
attn = self.softmax(attn)
attn = self.dropout(attn)
output = (attn.unsqueeze(2) * v).sum(1)
return output, attn
class MultiheadAttPoolLayer(nn.Module):
def __init__(self, n_head, d_q_original, d_k_original, dropout=0.1):
super().__init__()
assert d_k_original % n_head == 0 # make sure the outpute dimension equals to d_k_origin
self.n_head = n_head
self.d_k = d_k_original // n_head
self.d_v = d_k_original // n_head
self.w_qs = nn.Linear(d_q_original, n_head * self.d_k)
self.w_ks = nn.Linear(d_k_original, n_head * self.d_k)
self.w_vs = nn.Linear(d_k_original, n_head * self.d_v)
nn.init.normal_(self.w_qs.weight, mean=0, std=np.sqrt(2.0 / (d_q_original + self.d_k)))
nn.init.normal_(self.w_ks.weight, mean=0, std=np.sqrt(2.0 / (d_k_original + self.d_k)))
nn.init.normal_(self.w_vs.weight, mean=0, std=np.sqrt(2.0 / (d_k_original + self.d_v)))
self.attention = MatrixVectorScaledDotProductAttention(temperature=np.power(self.d_k, 0.5))
self.dropout = nn.Dropout(dropout)
def forward(self, q, k, mask=None):
"""
q: tensor of shape (b, d_q_original)
k: tensor of shape (b, l, d_k_original)
mask: tensor of shape (b, l) (optional, default None)
returns: tensor of shape (b, n*d_v)
"""
n_head, d_k, d_v = self.n_head, self.d_k, self.d_v
bs, _ = q.size()
bs, len_k, _ = k.size()
qs = self.w_qs(q).view(bs, n_head, d_k) # (b, n, dk)
ks = self.w_ks(k).view(bs, len_k, n_head, d_k) # (b, l, n, dk)
vs = self.w_vs(k).view(bs, len_k, n_head, d_v) # (b, l, n, dv)
qs = qs.permute(1, 0, 2).contiguous().view(n_head * bs, d_k)
ks = ks.permute(2, 0, 1, 3).contiguous().view(n_head * bs, len_k, d_k)
vs = vs.permute(2, 0, 1, 3).contiguous().view(n_head * bs, len_k, d_v)
if mask is not None:
mask = mask.repeat(n_head, 1)
output, attn = self.attention(qs, ks, vs, mask=mask)
output = output.view(n_head, bs, d_v)
output = output.permute(1, 0, 2).contiguous().view(bs, n_head * d_v) # (b, n*dv)
output = self.dropout(output)
return output, attn
class MLP(nn.Module):
"""
Multi-layer perceptron
Parameters
----------
num_layers: number of hidden layers
"""
activation_classes = {'gelu': GELU, 'relu': nn.ReLU, 'tanh': nn.Tanh}
def __init__(self, input_size, hidden_size, output_size, num_layers, dropout, batch_norm=False,
init_last_layer_bias_to_zero=False, layer_norm=False, activation='gelu'):
super().__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.num_layers = num_layers
self.dropout = dropout
self.batch_norm = batch_norm
self.layer_norm = layer_norm
assert not (self.batch_norm and self.layer_norm)
self.layers = nn.Sequential()
for i in range(self.num_layers + 1):
n_in = self.input_size if i == 0 else self.hidden_size
n_out = self.hidden_size if i < self.num_layers else self.output_size
self.layers.add_module(f'{i}-Linear', nn.Linear(n_in, n_out))
if i < self.num_layers:
self.layers.add_module(f'{i}-Dropout', nn.Dropout(self.dropout))
if self.batch_norm:
self.layers.add_module(f'{i}-BatchNorm1d', nn.BatchNorm1d(self.hidden_size))
if self.layer_norm:
self.layers.add_module(f'{i}-LayerNorm', nn.LayerNorm(self.hidden_size))
self.layers.add_module(f'{i}-{activation}', self.activation_classes[activation.lower()]())
if init_last_layer_bias_to_zero:
self.layers[-1].bias.data.fill_(0)
def forward(self, input):
return self.layers(input)
class TextEncoder(nn.Module):
valid_model_types = set(MODEL_CLASS_TO_NAME.keys())
def __init__(self, model_name, output_token_states=False, from_checkpoint=None, **kwargs):
super().__init__()
self.model_type = MODEL_NAME_TO_CLASS[model_name]
self.output_token_states = output_token_states
assert not self.output_token_states or self.model_type in ('bert', 'roberta', 'albert')
# import pdb; pdb.set_trace()
if self.model_type in ('lstm',):
self.module = LSTMTextEncoder(**kwargs, output_hidden_states=True)
self.sent_dim = self.module.output_size
else:
model_class = AutoModel
self.module = model_class.from_pretrained('./roberta-large', output_hidden_states=True)
# self.module = model_class.from_pretrained(model_name, output_hidden_states=True)
if from_checkpoint is not None:
self.module = self.module.from_pretrained(from_checkpoint, output_hidden_states=True)
if self.model_type in ('gpt',):
self.module.resize_token_embeddings(get_gpt_token_num())
self.sent_dim = self.module.config.n_embd if self.model_type in ('gpt',) else self.module.config.hidden_size
def forward(self, *inputs, layer_id=-1):
'''
layer_id: only works for non-LSTM encoders
output_token_states: if True, return hidden states of specific layer and attention masks
'''
if self.model_type in ('lstm',): # lstm
input_ids, lengths = inputs
outputs = self.module(input_ids, lengths)
elif self.model_type in ('gpt',): # gpt
input_ids, cls_token_ids, lm_labels = inputs # lm_labels is not used
outputs = self.module(input_ids)
else: # bert / xlnet / roberta
input_ids, attention_mask, token_type_ids, output_mask = inputs
outputs = self.module(input_ids, token_type_ids=token_type_ids, attention_mask=attention_mask)
all_hidden_states = outputs[-1]
hidden_states = all_hidden_states[layer_id]
if self.model_type in ('lstm',):
sent_vecs = outputs[1]
elif self.model_type in ('gpt',):
cls_token_ids = cls_token_ids.view(-1).unsqueeze(-1).unsqueeze(-1).expand(-1, 1, hidden_states.size(-1))
sent_vecs = hidden_states.gather(1, cls_token_ids).squeeze(1)
elif self.model_type in ('xlnet',):
sent_vecs = hidden_states[:, -1]
elif self.model_type in ('albert',):
if self.output_token_states:
return hidden_states, output_mask
sent_vecs = hidden_states[:, 0]
else: # bert / roberta
if self.output_token_states:
return hidden_states, output_mask
sent_vecs = self.module.pooler(hidden_states)
return sent_vecs, all_hidden_states
def load_input_tensors(statement_jsonl_path, model_type, model_name, max_seq_length):
class InputExample(object):
def __init__(self, example_id, question, contexts, endings, label=None):
self.example_id = example_id
self.question = question
self.contexts = contexts
self.endings = endings
self.label = label
class InputFeatures(object):
def __init__(self, example_id, choices_features, label):
self.example_id = example_id
self.choices_features = [
{
'input_ids': input_ids,
'input_mask': input_mask,
'segment_ids': segment_ids,
'output_mask': output_mask,
}
for _, input_ids, input_mask, segment_ids, output_mask in choices_features
]
self.label = label
def read_examples(input_file):
with open(input_file, "r", encoding="utf-8") as f:
examples = []
for line in f.readlines():
json_dic = json.loads(line)
label = ord(json_dic["answerKey"]) - ord("A") if 'answerKey' in json_dic else 0
contexts = json_dic["question"]["stem"]
if "para" in json_dic:
contexts = json_dic["para"] + " " + contexts
if "fact1" in json_dic:
contexts = json_dic["fact1"] + " " + contexts
examples.append(
InputExample(
example_id=json_dic["id"],
contexts=[contexts] * len(json_dic["question"]["choices"]),
question="",
endings=[ending["text"] for ending in json_dic["question"]["choices"]],
label=label
))
return examples
def convert_examples_to_features(examples, label_list, max_seq_length,
tokenizer,
cls_token_at_end=False,
cls_token='[CLS]',
cls_token_segment_id=1,
sep_token='[SEP]',
sequence_a_segment_id=0,
sequence_b_segment_id=1,
sep_token_extra=False,
pad_token_segment_id=0,
pad_on_left=False,
pad_token=0,
mask_padding_with_zero=True):
""" Loads a data file into a list of `InputBatch`s
`cls_token_at_end` define the location of the CLS token:
- False (Default, BERT/XLM pattern): [CLS] + A + [SEP] + B + [SEP]
- True (XLNet/GPT pattern): A + [SEP] + B + [SEP] + [CLS]
`cls_token_segment_id` define the segment id associated to the CLS token (0 for BERT, 2 for XLNet)
"""
label_map = {label: i for i, label in enumerate(label_list)}
features = []
for ex_index, example in enumerate(tqdm(examples)):
choices_features = []
for ending_idx, (context, ending) in enumerate(zip(example.contexts, example.endings)):
tokens_a = tokenizer.tokenize(context)
tokens_b = tokenizer.tokenize(example.question + " " + ending)
# import pdb; pdb.set_trace() # tokens_b 没有G ; tokens_a 有G
special_tokens_count = 4 if sep_token_extra else 3
_truncate_seq_pair(tokens_a, tokens_b, max_seq_length - special_tokens_count)
# The convention in BERT is:
# (a) For sequence pairs:
# tokens: [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP]
# type_ids: 0 0 0 0 0 0 0 0 1 1 1 1 1 1
# (b) For single sequences:
# tokens: [CLS] the dog is hairy . [SEP]
# type_ids: 0 0 0 0 0 0 0
#
# Where "type_ids" are used to indicate whether this is the first
# sequence or the second sequence. The embedding vectors for `type=0` and
# `type=1` were learned during pre-training and are added to the wordpiece
# embedding vector (and position vector). This is not *strictly* necessary
# since the [SEP] token unambiguously separates the sequences, but it makes
# it easier for the model to learn the concept of sequences.
#
# For classification tasks, the first vector (corresponding to [CLS]) is
# used as as the "sentence vector". Note that this only makes sense because
# the entire model is fine-tuned.
tokens = tokens_a + [sep_token]
if sep_token_extra:
# roberta uses an extra separator b/w pairs of sentences
tokens += [sep_token]
segment_ids = [sequence_a_segment_id] * len(tokens)
if tokens_b:
tokens += tokens_b + [sep_token]
segment_ids += [sequence_b_segment_id] * (len(tokens_b) + 1)
if cls_token_at_end:
tokens = tokens + [cls_token]
segment_ids = segment_ids + [cls_token_segment_id]
else:
tokens = [cls_token] + tokens
segment_ids = [cls_token_segment_id] + segment_ids
input_ids = tokenizer.convert_tokens_to_ids(tokens)
# The mask has 1 for real tokens and 0 for padding tokens. Only real
# tokens are attended to.
input_mask = [1 if mask_padding_with_zero else 0] * len(input_ids)
special_token_id = tokenizer.convert_tokens_to_ids([cls_token, sep_token])
output_mask = [1 if id in special_token_id else 0 for id in input_ids] # 1 for mask
# Zero-pad up to the sequence length.
padding_length = max_seq_length - len(input_ids)
if pad_on_left:
input_ids = ([pad_token] * padding_length) + input_ids
input_mask = ([0 if mask_padding_with_zero else 1] * padding_length) + input_mask
output_mask = ([1] * padding_length) + output_mask
segment_ids = ([pad_token_segment_id] * padding_length) + segment_ids
else:
input_ids = input_ids + ([pad_token] * padding_length)
input_mask = input_mask + ([0 if mask_padding_with_zero else 1] * padding_length)
output_mask = output_mask + ([1] * padding_length)
segment_ids = segment_ids + ([pad_token_segment_id] * padding_length)
assert len(input_ids) == max_seq_length
assert len(output_mask) == max_seq_length
assert len(input_mask) == max_seq_length
assert len(segment_ids) == max_seq_length
choices_features.append((tokens, input_ids, input_mask, segment_ids, output_mask))
label = label_map[example.label]
features.append(InputFeatures(example_id=example.example_id, choices_features=choices_features, label=label))
return features
def _truncate_seq_pair(tokens_a, tokens_b, max_length):
"""Truncates a sequence pair in place to the maximum length."""
# This is a simple heuristic which will always truncate the longer sequence
# one token at a time. This makes more sense than truncating an equal percent
# of tokens from each, since if one sequence is very short then each token
# that's truncated likely contains more information than a longer sequence.
while True:
total_length = len(tokens_a) + len(tokens_b)
if total_length <= max_length:
break
if len(tokens_a) > len(tokens_b):
tokens_a.pop()
else:
tokens_b.pop()
def select_field(features, field):
return [[choice[field] for choice in feature.choices_features] for feature in features]
def convert_features_to_tensors(features):
all_input_ids = torch.tensor(select_field(features, 'input_ids'), dtype=torch.long)
all_input_mask = torch.tensor(select_field(features, 'input_mask'), dtype=torch.long)
all_segment_ids = torch.tensor(select_field(features, 'segment_ids'), dtype=torch.long)
all_output_mask = torch.tensor(select_field(features, 'output_mask'), dtype=torch.bool)
all_label = torch.tensor([f.label for f in features], dtype=torch.long)
return all_input_ids, all_input_mask, all_segment_ids, all_output_mask, all_label
# try:
# tokenizer_class = {'bert': BertTokenizer, 'xlnet': XLNetTokenizer, 'roberta': RobertaTokenizer, 'albert': AlbertTokenizer}.get(model_type)
# except:
# tokenizer_class = {'bert': BertTokenizer, 'xlnet': XLNetTokenizer, 'roberta': RobertaTokenizer}.get(model_type)
tokenizer_class = AutoTokenizer
# tokenizer = tokenizer_class.from_pretrained(model_name) #
# tokenizer = tokenizer_class.from_pretrained(f'./{model_name}')
tokenizer = tokenizer_class.from_pretrained('./roberta-large')
# import pdb; pdb.set_trace()
examples = read_examples(statement_jsonl_path)
features = convert_examples_to_features(examples, list(range(len(examples[0].endings))), max_seq_length, tokenizer,
cls_token_at_end=bool(model_type in ['xlnet']), # xlnet has a cls token at the end
cls_token=tokenizer.cls_token,
sep_token=tokenizer.sep_token,
sep_token_extra=bool(model_type in ['roberta', 'albert']),
cls_token_segment_id=2 if model_type in ['xlnet'] else 0,
pad_on_left=bool(model_type in ['xlnet']), # pad on the left for xlnet
pad_token_segment_id=4 if model_type in ['xlnet'] else 0,
sequence_b_segment_id=0 if model_type in ['roberta', 'albert'] else 1)
example_ids = [f.example_id for f in features]
*data_tensors, all_label = convert_features_to_tensors(features)
return (example_ids, all_label, *data_tensors)
class RAdam(Optimizer):
def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0, degenerated_to_sgd=True):
if not 0.0 <= lr:
raise ValueError("Invalid learning rate: {}".format(lr))
if not 0.0 <= eps:
raise ValueError("Invalid epsilon value: {}".format(eps))
if not 0.0 <= betas[0] < 1.0:
raise ValueError("Invalid beta parameter at index 0: {}".format(betas[0]))
if not 0.0 <= betas[1] < 1.0:
raise ValueError("Invalid beta parameter at index 1: {}".format(betas[1]))
self.degenerated_to_sgd = degenerated_to_sgd
if isinstance(params, (list, tuple)) and len(params) > 0 and isinstance(params[0], dict):
for param in params:
if 'betas' in param and (param['betas'][0] != betas[0] or param['betas'][1] != betas[1]):
param['buffer'] = [[None, None, None] for _ in range(10)]
defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay, buffer=[[None, None, None] for _ in range(10)])
super(RAdam, self).__init__(params, defaults)
def __setstate__(self, state):
super(RAdam, self).__setstate__(state)
def step(self, closure=None):
loss = None
if closure is not None:
loss = closure()
for group in self.param_groups:
for p in group['params']:
if p.grad is None:
continue
grad = p.grad.data.float()
if grad.is_sparse:
raise RuntimeError('RAdam does not support sparse gradients')
p_data_fp32 = p.data.float()
state = self.state[p]
if len(state) == 0:
state['step'] = 0
state['exp_avg'] = torch.zeros_like(p_data_fp32)
state['exp_avg_sq'] = torch.zeros_like(p_data_fp32)
else:
state['exp_avg'] = state['exp_avg'].type_as(p_data_fp32)
state['exp_avg_sq'] = state['exp_avg_sq'].type_as(p_data_fp32)
exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq']
beta1, beta2 = group['betas']
exp_avg_sq.mul_(beta2).addcmul_(1 - beta2, grad, grad)
exp_avg.mul_(beta1).add_(1 - beta1, grad)
state['step'] += 1
buffered = group['buffer'][int(state['step'] % 10)]
if state['step'] == buffered[0]:
N_sma, step_size = buffered[1], buffered[2]
else:
buffered[0] = state['step']
beta2_t = beta2 ** state['step']
N_sma_max = 2 / (1 - beta2) - 1
N_sma = N_sma_max - 2 * state['step'] * beta2_t / (1 - beta2_t)
buffered[1] = N_sma
# more conservative since it's an approximated value
if N_sma >= 5:
step_size = math.sqrt((1 - beta2_t) * (N_sma - 4) / (N_sma_max - 4) * (N_sma - 2) / N_sma * N_sma_max / (N_sma_max - 2)) / (1 - beta1 ** state['step'])
elif self.degenerated_to_sgd:
step_size = 1.0 / (1 - beta1 ** state['step'])
else:
step_size = -1
buffered[2] = step_size
# more conservative since it's an approximated value
if N_sma >= 5:
if group['weight_decay'] != 0:
p_data_fp32.add_(-group['weight_decay'] * group['lr'], p_data_fp32)
denom = exp_avg_sq.sqrt().add_(group['eps'])
p_data_fp32.addcdiv_(-step_size * group['lr'], exp_avg, denom)
p.data.copy_(p_data_fp32)
elif step_size > 0:
if group['weight_decay'] != 0:
p_data_fp32.add_(-group['weight_decay'] * group['lr'], p_data_fp32)
p_data_fp32.add_(-step_size * group['lr'], exp_avg)
p.data.copy_(p_data_fp32)
return loss