-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
384 lines (353 loc) · 15 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
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
import torch
import torch.nn.functional as F
import numpy as np
import torch.nn as nn
import ujson as json
from tqdm import tqdm
from transformers import AutoTokenizer
docred_rel2id = json.load(open("dataset/meta/rel2id.json", "r"))
def read_docred(file_in, tokenizer, max_seq_length=1024, limit=False, ast=True):
i_line = 0
pos_samples = 0
neg_samples = 0
features = []
if file_in == "":
return None
with open(file_in, "r") as fh:
data = json.load(fh)
if limit:
data = data[:10]
for sample in tqdm(data, desc="Example"):
sents = []
sent_map = []
entities = sample["vertexSet"]
entity_start, entity_end = [], []
for entity in entities:
for mention in entity:
sent_id = mention["sent_id"]
pos = mention["pos"]
entity_start.append(
(
sent_id,
pos[0],
)
)
entity_end.append(
(
sent_id,
pos[1] - 1,
)
)
for i_s, sent in enumerate(sample["sents"]):
new_map = {}
for i_t, token in enumerate(sent):
tokens_wordpiece = tokenizer.tokenize(token)
if (i_s, i_t) in entity_start:
tokens_wordpiece = ["*"] + tokens_wordpiece # add * around entity
if (i_s, i_t) in entity_end:
tokens_wordpiece = tokens_wordpiece + ["*"]
new_map[i_t] = len(sents)
sents.extend(tokens_wordpiece)
new_map[i_t + 1] = len(sents)
sent_map.append(new_map)
train_triple = {}
if "labels" in sample:
for label in sample["labels"]:
evidence = label["evidence"] if "evidence" in label else []
r = int(docred_rel2id[label["r"]])
if (label["h"], label["t"]) not in train_triple:
train_triple[(label["h"], label["t"])] = [{"relation": r, "evidence": evidence}]
else:
train_triple[(label["h"], label["t"])].append({"relation": r, "evidence": evidence})
entity_pos = []
for e in entities:
entity_pos.append([])
for m in e:
start = sent_map[m["sent_id"]][m["pos"][0]]
end = sent_map[m["sent_id"]][m["pos"][1]]
entity_pos[-1].append(
(
start,
end,
)
)
relations, hts = [], []
for h, t in train_triple.keys():
relation = [0] + [0] * len(docred_rel2id)
for mention in train_triple[h, t]:
relation[mention["relation"]] = 1
evidence = mention["evidence"]
relations.append(relation)
hts.append([h, t])
pos_samples += 1
if ast:
for h in range(len(entities)):
for t in range(len(entities)):
if h != t and [h, t] not in hts:
# relation = [1] + [0] * (len(docred_rel2id) - 1)
relation = [1] + [0] * len(docred_rel2id)
relations.append(relation)
hts.append([h, t])
neg_samples += 1
if ast:
assert len(relations) == len(entities) * (len(entities) - 1)
sents = sents[: max_seq_length - 2]
input_ids = tokenizer.convert_tokens_to_ids(sents)
input_ids = tokenizer.build_inputs_with_special_tokens(input_ids)
i_line += 1
feature = {
"input_ids": input_ids,
"entity_pos": entity_pos,
"labels": relations,
"hts": hts,
"title": sample["title"],
}
features.append(feature)
print("# of documents {}.".format(i_line))
print("# of positive examples {}.".format(pos_samples))
print("# of negative examples {}.".format(neg_samples))
return features
def get_wo_overlap_dataset(file_in):
model_type = "bert-base-cased"
tokenizer = AutoTokenizer.from_pretrained(model_type)
file_in = "dataset/docred/dev_keys_new.json"
dev_wo_overlap = []
with open(file_in, "r") as fh:
data = json.load(fh)
for si, sample in enumerate(tqdm(data, desc="Example")):
sent_map = []
sents, entity_start, entity_end = [], [], []
entities = sample["vertexSet"]
for entity in entities:
for mention in entity:
sent_id = mention["sent_id"]
pos = mention["pos"]
entity_start.append(
(
sent_id,
pos[0],
)
)
entity_end.append(
(
sent_id,
pos[1] - 1,
)
)
for i_s, sent in enumerate(sample["sents"]):
new_map = {}
for i_t, token in enumerate(sent):
tokens_wordpiece = tokenizer.tokenize(token)
if (i_s, i_t) in entity_start:
tokens_wordpiece = ["*"] + tokens_wordpiece # add * around entity
if (i_s, i_t) in entity_end:
tokens_wordpiece = tokens_wordpiece + ["*"]
new_map[i_t] = len(sents)
sents.extend(tokens_wordpiece)
new_map[i_t + 1] = len(sents)
sent_map.append(new_map)
sents_flag = [0] * len(sents)
fil = False
for e in entities:
for m in e:
start = sent_map[m["sent_id"]][m["pos"][0]]
end = sent_map[m["sent_id"]][m["pos"][1]]
for pos in range(start, end):
if sents_flag[pos] == 1:
fil = True
break
else:
sents_flag[pos] = 1
if fil:
break
if fil:
break
if not fil:
dev_wo_overlap.append(sample)
print(len(dev_wo_overlap))
json.dump(dev_wo_overlap, open("dataset/docred/dev_wo_overlap.json", "w"))
def collate_fn(batch):
max_len = max([len(f["input_ids"]) for f in batch])
input_ids = [f["input_ids"] + [0] * (max_len - len(f["input_ids"])) for f in batch]
input_mask = [[1.0] * len(f["input_ids"]) + [0.0] * (max_len - len(f["input_ids"])) for f in batch]
labels = [f["labels"] for f in batch]
entity_pos = [f["entity_pos"] for f in batch]
hts = [f["hts"] for f in batch]
input_ids = torch.tensor(input_ids, dtype=torch.long)
input_mask = torch.tensor(input_mask, dtype=torch.float)
output = (input_ids, input_mask, labels, entity_pos, hts)
return output
def process_long_input(model, input_ids, attention_mask, start_tokens, end_tokens):
n, c = input_ids.size()
start_tokens = torch.tensor(start_tokens).to(input_ids)
end_tokens = torch.tensor(end_tokens).to(input_ids)
len_start = start_tokens.size(0)
len_end = end_tokens.size(0)
if c <= 512:
output = model(
input_ids=input_ids,
attention_mask=attention_mask,
output_attentions=True,
)
sequence_output = output[0]
attention = output[-1][-1]
else:
new_input_ids, new_attention_mask, num_seg = [], [], []
seq_len = attention_mask.sum(1).cpu().numpy().astype(np.int32).tolist()
for i, l_i in enumerate(seq_len):
if l_i <= 512:
new_input_ids.append(input_ids[i, :512])
new_attention_mask.append(attention_mask[i, :512])
num_seg.append(1)
else:
input_ids1 = torch.cat([input_ids[i, : 512 - len_end], end_tokens], dim=-1)
input_ids2 = torch.cat([start_tokens, input_ids[i, (l_i - 512 + len_start) : l_i]], dim=-1)
attention_mask1 = attention_mask[i, :512]
attention_mask2 = attention_mask[i, (l_i - 512) : l_i]
new_input_ids.extend([input_ids1, input_ids2])
new_attention_mask.extend([attention_mask1, attention_mask2])
num_seg.append(2)
input_ids = torch.stack(new_input_ids, dim=0)
attention_mask = torch.stack(new_attention_mask, dim=0)
output = model(
input_ids=input_ids,
attention_mask=attention_mask,
output_attentions=True,
)
sequence_output = output[0]
attention = output[-1][-1]
i = 0
new_output, new_attention = [], []
for (n_s, l_i) in zip(num_seg, seq_len):
if n_s == 1:
output = F.pad(sequence_output[i], (0, 0, 0, c - 512))
att = F.pad(attention[i], (0, c - 512, 0, c - 512))
new_output.append(output)
new_attention.append(att)
elif n_s == 2:
output1 = sequence_output[i][: 512 - len_end]
mask1 = attention_mask[i][: 512 - len_end]
att1 = attention[i][:, : 512 - len_end, : 512 - len_end]
output1 = F.pad(output1, (0, 0, 0, c - 512 + len_end))
mask1 = F.pad(mask1, (0, c - 512 + len_end))
att1 = F.pad(att1, (0, c - 512 + len_end, 0, c - 512 + len_end))
output2 = sequence_output[i + 1][len_start:]
mask2 = attention_mask[i + 1][len_start:]
att2 = attention[i + 1][:, len_start:, len_start:]
output2 = F.pad(output2, (0, 0, l_i - 512 + len_start, c - l_i))
mask2 = F.pad(mask2, (l_i - 512 + len_start, c - l_i))
att2 = F.pad(att2, [l_i - 512 + len_start, c - l_i, l_i - 512 + len_start, c - l_i])
mask = mask1 + mask2 + 1e-10
output = (output1 + output2) / mask.unsqueeze(-1)
att = att1 + att2
att = att / (att.sum(-1, keepdim=True) + 1e-10)
new_output.append(output)
new_attention.append(att)
i += n_s
sequence_output = torch.stack(new_output, dim=0)
attention = torch.stack(new_attention, dim=0)
return sequence_output, attention
def process_long_input_emb(model, input_embs, attention_mask, start_tokens, end_tokens):
n, c, _ = input_embs.size()
start_tokens = torch.tensor(start_tokens).to(input_embs).long()
end_tokens = torch.tensor(end_tokens).to(input_embs).long()
embedding = model.get_input_embeddings()
start_tokens_embs = embedding(start_tokens)
end_tokens_embs = embedding(end_tokens)
len_start = start_tokens.size(0)
len_end = end_tokens.size(0)
if c <= 512:
output = model(
inputs_embeds=input_embs,
attention_mask=attention_mask,
output_attentions=True,
)
sequence_output = output[0]
attention = output[-1][-1]
else:
new_input_embs, new_attention_mask, num_seg = [], [], []
seq_len = attention_mask.sum(1).cpu().numpy().astype(np.int32).tolist()
for i, l_i in enumerate(seq_len):
if l_i <= 512:
new_input_embs.append(input_embs[i, :512])
new_attention_mask.append(attention_mask[i, :512])
num_seg.append(1)
else:
input_ids1 = torch.cat([input_embs[i, : 512 - len_end], end_tokens_embs], dim=-2)
input_ids2 = torch.cat([start_tokens_embs, input_embs[i, (l_i - 512 + len_start) : l_i]], dim=-2)
attention_mask1 = attention_mask[i, :512]
attention_mask2 = attention_mask[i, (l_i - 512) : l_i]
new_input_embs.extend([input_ids1, input_ids2])
new_attention_mask.extend([attention_mask1, attention_mask2])
num_seg.append(2)
input_embs = torch.stack(new_input_embs, dim=0)
attention_mask = torch.stack(new_attention_mask, dim=0)
output = model(
inputs_embeds=input_embs,
attention_mask=attention_mask,
output_attentions=True,
)
sequence_output = output[0]
attention = output[-1][-1]
i = 0
new_output, new_attention = [], []
for (n_s, l_i) in zip(num_seg, seq_len):
if n_s == 1:
output = F.pad(sequence_output[i], (0, 0, 0, c - 512))
att = F.pad(attention[i], (0, c - 512, 0, c - 512))
new_output.append(output)
new_attention.append(att)
elif n_s == 2:
output1 = sequence_output[i][: 512 - len_end]
mask1 = attention_mask[i][: 512 - len_end]
att1 = attention[i][:, : 512 - len_end, : 512 - len_end]
output1 = F.pad(output1, (0, 0, 0, c - 512 + len_end))
mask1 = F.pad(mask1, (0, c - 512 + len_end))
att1 = F.pad(att1, (0, c - 512 + len_end, 0, c - 512 + len_end))
output2 = sequence_output[i + 1][len_start:]
mask2 = attention_mask[i + 1][len_start:]
att2 = attention[i + 1][:, len_start:, len_start:]
output2 = F.pad(output2, (0, 0, l_i - 512 + len_start, c - l_i))
mask2 = F.pad(mask2, (l_i - 512 + len_start, c - l_i))
att2 = F.pad(att2, [l_i - 512 + len_start, c - l_i, l_i - 512 + len_start, c - l_i])
mask = mask1 + mask2 + 1e-10
output = (output1 + output2) / mask.unsqueeze(-1)
att = att1 + att2
att = att / (att.sum(-1, keepdim=True) + 1e-10)
new_output.append(output)
new_attention.append(att)
i += n_s
sequence_output = torch.stack(new_output, dim=0)
attention = torch.stack(new_attention, dim=0)
return sequence_output, attention
class ATLoss(nn.Module):
def __init__(self):
super().__init__()
def forward(self, logits, labels):
# TH label
th_label = torch.zeros_like(labels, dtype=torch.float).to(labels)
th_label[:, 0] = 1.0
labels[:, 0] = 0.0
p_mask = labels + th_label
n_mask = 1 - labels
# Rank positive classes to TH
logit1 = logits - (1 - p_mask) * 1e30
loss1 = -(F.log_softmax(logit1, dim=-1) * labels).sum(1)
# Rank TH to negative classes
logit2 = logits - (1 - n_mask) * 1e30
loss2 = -(F.log_softmax(logit2, dim=-1) * th_label).sum(1)
# Sum two parts
loss = loss1 + loss2
loss = loss.mean()
return loss
def get_label(self, logits, num_labels=-1):
th_logit = logits[:, 0].unsqueeze(1)
output = torch.zeros_like(logits).to(logits)
mask = logits > th_logit
if num_labels > 0:
top_v, _ = torch.topk(logits, num_labels, dim=1)
top_v = top_v[:, -1]
mask = (logits >= top_v.unsqueeze(1)) & mask
output[mask] = 1.0
output[:, 0] = (output.sum(1) == 0.0).to(logits)
return output