-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcollator.py
244 lines (197 loc) · 9.23 KB
/
collator.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
import numpy as np
import torch
import copy
import argparse
import transformers
import math
from transformers import DataCollatorForSeq2Seq
import torch.nn.utils.rnn as rnn_utils
class Collator(object):
def __init__(self, args, tokenizer):
self.args = args
self.tokenizer = tokenizer
self.tokenizer.model_max_length = args.model_max_length
self.llm_collator = DataCollatorForSeq2Seq(
tokenizer,
label_pad_token_id=-100,
pad_to_multiple_of=8,
padding="longest",
)
def _make_batch_assignees(self, batch_seqs):
seq_sizes = torch.tensor([len(seq) for seq in batch_seqs])
batch_index = torch.repeat_interleave(torch.arange(len(batch_seqs)), seq_sizes)
return batch_index
def __call__(self, batch):
batch = np.array(batch)
comm_rank_idx = []
rec_idx = []
for i, data in enumerate(batch):
if "com_photo_text" in data:
comm_rank_idx.append(i)
else:
rec_idx.append(i)
if len(comm_rank_idx) == 0:
comm_rank_photo_inputs = None
comm_rank_photo_id = None
else:
comm_rank_data = batch[comm_rank_idx].tolist()
batch = batch[rec_idx].tolist() + comm_rank_data
comm_rank_photo_inputs = self.tokenizer(
text=[d["com_photo_text"] for d in comm_rank_data],
return_tensors="pt",
padding="longest",
max_length=self.args.max_target_length,
truncation=True,
return_attention_mask=True,
)
comm_rank_photo_id = torch.LongTensor([d["com_photo_id"] for d in comm_rank_data])
batch_target_texts = [d["target_text"] for d in batch]
target_text_inputs = self.tokenizer(
text=batch_target_texts,
return_tensors="pt",
padding="longest",
max_length=self.args.max_target_length,
truncation=True,
return_attention_mask=True,
)
inputs = [{"input_ids": d["input_ids"], "labels": d["labels"]} for d in batch]
inputs = self.llm_collator(inputs)
batch_phis_titles = [d["phis_titles"] for d in batch]
batch_phis_comments = [d["phis_comments"] for d in batch]
batch_chis_titles = [d["chis_titles"] for d in batch]
batch_photo_his_id = [torch.LongTensor(d["photo_his_id"]) for d in batch]
batch_comment_his_id = [torch.LongTensor(d["comment_his_id"]) for d in batch]
photo_his_batch_idx = self._make_batch_assignees(batch_phis_titles)
comment_his_batch_idx = self._make_batch_assignees(batch_chis_titles)
batch_phis_titles = np.concatenate(batch_phis_titles).tolist()
batch_phis_comments = np.concatenate(batch_phis_comments).tolist()
batch_chis_titles = np.concatenate(batch_chis_titles).tolist()
tot_inter = 0
comment_pooling_index = []
batch_chis_comments = []
for d in batch:
for c_list in d["chis_comments"]:
comment_pooling_index.extend([tot_inter] * len(c_list))
batch_chis_comments.extend(c_list)
tot_inter += 1
comment_pooling_index = torch.LongTensor(comment_pooling_index)
phis_title_inputs = self.tokenizer(
text=batch_phis_titles,
return_tensors="pt",
padding="longest",
max_length=self.args.max_target_length,
truncation=True,
return_attention_mask=True,
)
phis_comment_inputs = self.tokenizer(
text=batch_phis_comments,
return_tensors="pt",
padding="longest",
max_length=self.args.max_target_length,
truncation=True,
return_attention_mask=True,
)
chis_title_inputs = self.tokenizer(
text=batch_chis_titles,
return_tensors="pt",
padding="longest",
max_length=self.args.max_target_length,
truncation=True,
return_attention_mask=True,
)
chis_comment_inputs = self.tokenizer(
text=batch_chis_comments,
return_tensors="pt",
padding="longest",
max_length=self.args.max_target_length,
truncation=True,
return_attention_mask=True,
)
batch_photo_his_id = rnn_utils.pad_sequence(batch_photo_his_id, batch_first=True)
batch_comment_his_id = rnn_utils.pad_sequence(batch_comment_his_id, batch_first=True)
return dict(**inputs,
target_text_inputs=target_text_inputs,
phis_title_inputs=phis_title_inputs,
phis_comment_inputs=phis_comment_inputs,
chis_title_inputs=chis_title_inputs,
chis_comment_inputs=chis_comment_inputs,
comment_pooling_index=comment_pooling_index,
photo_his_batch_idx=photo_his_batch_idx,
comment_his_batch_idx=comment_his_batch_idx,
photo_his_id=batch_photo_his_id,
comment_his_id=batch_comment_his_id,
comm_rank_photo_inputs=comm_rank_photo_inputs,
comm_rank_photo_id=comm_rank_photo_id
)
class FinetuneCollator(object):
def __init__(self, args, photo_embs, comment_embs):
self.args = args
self.max_phis_len = args.max_phis_len
self.max_chis_len = args.max_chis_len
self.photo_embs = photo_embs
self.comment_embs = comment_embs
def _make_batch_assignees(self, batch_seqs):
seq_sizes = torch.tensor([len(seq) for seq in batch_seqs])
batch_index = torch.repeat_interleave(torch.arange(len(batch_seqs)), seq_sizes)
return batch_index
def __call__(self, batch):
com_photo_id = None
com_photo_text_emb = None
comm_rank = False
if "com_photo_id" in batch[0]:
comm_rank = True
com_photo_id = torch.LongTensor([d["com_photo_id"] for d in batch])
com_photo_text_emb = torch.from_numpy(self.photo_embs[com_photo_id]).float()
batch_phis_pid = [torch.LongTensor(d["photo_his_pid"]) for d in batch]
batch_phis_cid = [torch.LongTensor(d["photo_his_cid"]) for d in batch]
batch_chis_pid = [torch.LongTensor(d["comment_his_pid"]) for d in batch]
comment_his_batch_idx = self._make_batch_assignees(batch_chis_pid)
tot_inter = 0
batch_chis_cid = []
comment_pooling_index = []
for d in batch:
for c_list in d["comment_his_cid"]:
comment_pooling_index.extend([tot_inter] * len(c_list))
batch_chis_cid.extend(c_list)
tot_inter += 1
comment_pooling_index = torch.LongTensor(comment_pooling_index)
batch_phis_pid = rnn_utils.pad_sequence(batch_phis_pid, batch_first=True)
batch_phis_cid = rnn_utils.pad_sequence(batch_phis_cid, batch_first=True)
batch_chis_pid = rnn_utils.pad_sequence(batch_chis_pid, batch_first=True)
batch_chis_cid = torch.LongTensor(batch_chis_cid)
photo_his_title_emb = torch.from_numpy(self.photo_embs[batch_phis_pid]).float()
comment_his_title_emb = torch.from_numpy(self.photo_embs[batch_chis_pid]).float()
photo_his_comment_emb = torch.from_numpy(self.comment_embs[batch_phis_cid]).float()
comment_his_comment_emb = torch.from_numpy(self.comment_embs[batch_chis_cid]).float()
if not isinstance(batch[0]["labels"], np.ndarray):
batch_labels = [d["labels"] for d in batch]
batch_labels = torch.LongTensor(batch_labels)
else:
batch_labels = [torch.LongTensor(d["labels"]) for d in batch]
batch_labels = rnn_utils.pad_sequence(batch_labels, batch_first=True)
batch_candidates = None
candidate_batch_idx = None
cand_text_emb = None
if batch[0]["candidates"] is not None:
batch_candidates = [d["candidates"].tolist() for d in batch]
candidate_batch_idx = self._make_batch_assignees(batch_candidates)
batch_candidates = torch.LongTensor(np.concatenate(batch_candidates))
if comm_rank:
cand_text_emb = torch.from_numpy(self.comment_embs[batch_candidates]).float()
else:
cand_text_emb = torch.from_numpy(self.photo_embs[batch_candidates]).float()
return dict(photo_his_pid=batch_phis_pid,
photo_his_title_emb=photo_his_title_emb,
photo_his_comment_emb=photo_his_comment_emb,
comment_his_pid=batch_chis_pid,
comment_his_title_emb=comment_his_title_emb,
comment_his_comment_emb=comment_his_comment_emb,
comment_his_batch_idx=comment_his_batch_idx,
comment_pooling_index=comment_pooling_index,
com_photo_id=com_photo_id,
com_photo_text_emb=com_photo_text_emb,
candidates=batch_candidates,
cand_text_emb=cand_text_emb,
labels=batch_labels,
candidate_batch_idx=candidate_batch_idx,
)