forked from HKUDS/EasyRec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
325 lines (289 loc) · 12.6 KB
/
model.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
import math
import torch
import numpy as np
import torch.nn as nn
from tqdm import tqdm
import scipy.sparse as sp
import torch.nn.functional as F
import torch.distributed as dist
import transformers
from transformers import RobertaTokenizer
from transformers.models.roberta.modeling_roberta import RobertaPreTrainedModel, RobertaModel, RobertaLMHead
from transformers.models.bert.modeling_bert import BertPreTrainedModel, BertModel, BertLMPredictionHead
from transformers.activations import gelu
from transformers.file_utils import (
add_code_sample_docstrings,
add_start_docstrings,
add_start_docstrings_to_model_forward,
replace_return_docstrings,
)
from transformers.modeling_outputs import SequenceClassifierOutput, BaseModelOutputWithPoolingAndCrossAttentions
from utility.loss_utils import *
init = nn.init.xavier_uniform_
uniformInit = nn.init.uniform
"""
EasyRec
"""
def dot_product_scores(q_vectors, ctx_vectors):
r = torch.matmul(q_vectors, torch.transpose(ctx_vectors, 0, 1))
return r
class MLPLayer(nn.Module):
"""
Head for getting sentence representations over RoBERTa/BERT's CLS representation.
"""
def __init__(self, config):
super().__init__()
self.dense = nn.Linear(config.hidden_size, config.hidden_size)
self.activation = nn.Tanh()
def forward(self, features, **kwargs):
x = self.dense(features)
x = self.activation(x)
return x
class Pooler(nn.Module):
"""
Parameter-free poolers to get the sentence embedding
'cls': [CLS] representation with BERT/RoBERTa's MLP pooler.
'cls_before_pooler': [CLS] representation without the original MLP pooler.
'avg': average of the last layers' hidden states at each token.
'avg_top2': average of the last two layers.
'avg_first_last': average of the first and the last layers.
"""
def __init__(self, pooler_type):
super().__init__()
self.pooler_type = pooler_type
assert self.pooler_type in ["cls", "cls_before_pooler", "avg", "avg_top2", "avg_first_last"], "unrecognized pooling type %s" % self.pooler_type
def forward(self, attention_mask, outputs):
last_hidden = outputs.last_hidden_state
pooler_output = outputs.pooler_output
hidden_states = outputs.hidden_states
if self.pooler_type in ['cls_before_pooler', 'cls']:
return last_hidden[:, 0]
elif self.pooler_type == "avg":
return ((last_hidden * attention_mask.unsqueeze(-1)).sum(1) / attention_mask.sum(-1).unsqueeze(-1))
elif self.pooler_type == "avg_first_last":
first_hidden = hidden_states[1]
last_hidden = hidden_states[-1]
pooled_result = ((first_hidden + last_hidden) / 2.0 * attention_mask.unsqueeze(-1)).sum(1) / attention_mask.sum(-1).unsqueeze(-1)
return pooled_result
elif self.pooler_type == "avg_top2":
second_last_hidden = hidden_states[-2]
last_hidden = hidden_states[-1]
pooled_result = ((last_hidden + second_last_hidden) / 2.0 * attention_mask.unsqueeze(-1)).sum(1) / attention_mask.sum(-1).unsqueeze(-1)
return pooled_result
else:
raise NotImplementedError
class Similarity(nn.Module):
"""
Dot product or cosine similarity
"""
def __init__(self, temp):
super().__init__()
self.temp = temp
self.cos = nn.CosineSimilarity(dim=-1)
def forward(self, x, y):
return self.cos(x, y) / self.temp
class Easyrec(RobertaPreTrainedModel):
_keys_to_ignore_on_load_missing = [r"position_ids"]
def __init__(self, config, *model_args, **model_kargs):
super().__init__(config)
try:
self.model_args = model_kargs["model_args"]
self.roberta = RobertaModel(config, add_pooling_layer=False)
if self.model_args.pooler_type == "cls":
self.mlp = MLPLayer(config)
if self.model_args.do_mlm:
self.lm_head = RobertaLMHead(config)
"""
Contrastive learning class init function.
"""
self.pooler_type = self.model_args.pooler_type
self.pooler = Pooler(self.pooler_type)
self.sim = Similarity(temp=self.model_args.temp)
self.init_weights()
except:
self.roberta = RobertaModel(config, add_pooling_layer=False)
self.mlp = MLPLayer(config)
self.lm_head = RobertaLMHead(config)
self.pooler_type = 'cls'
self.pooler = Pooler(self.pooler_type)
self.init_weights()
def forward(self,
user_input_ids=None,
user_attention_mask=None,
pos_item_input_ids=None,
pos_item_attention_mask=None,
neg_item_input_ids=None,
neg_item_attention_mask=None,
token_type_ids=None,
position_ids=None,
head_mask=None,
inputs_embeds=None,
labels=None,
output_attentions=None,
output_hidden_states=None,
return_dict=None,
mlm_input_ids=None,
mlm_attention_mask=None,
mlm_labels=None,
):
"""
Contrastive learning forward function.
"""
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
batch_size = user_input_ids.size(0)
# Get user embeddings
user_outputs = self.roberta(
input_ids=user_input_ids,
attention_mask=user_attention_mask,
token_type_ids=None,
position_ids=None,
head_mask=None,
inputs_embeds=None,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
# Get positive item embeddings
pos_item_outputs = self.roberta(
input_ids=pos_item_input_ids,
attention_mask=pos_item_attention_mask,
token_type_ids=None,
position_ids=None,
head_mask=None,
inputs_embeds=None,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
# Get negative item embeddings
neg_item_outputs = self.roberta(
input_ids=neg_item_input_ids,
attention_mask=neg_item_attention_mask,
token_type_ids=None,
position_ids=None,
head_mask=None,
inputs_embeds=None,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
# MLM auxiliary objective
if mlm_input_ids is not None:
mlm_outputs = self.roberta(
input_ids=mlm_input_ids,
attention_mask=mlm_attention_mask,
token_type_ids=None,
position_ids=None,
head_mask=None,
inputs_embeds=None,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
# Pooling
user_pooler_output = self.pooler(user_attention_mask, user_outputs)
pos_item_pooler_output = self.pooler(pos_item_attention_mask, pos_item_outputs)
neg_item_pooler_output = self.pooler(neg_item_attention_mask, neg_item_outputs)
# If using "cls", we add an extra MLP layer
# (same as BERT's original implementation) over the representation.
if self.pooler_type == "cls":
user_pooler_output = self.mlp(user_pooler_output)
pos_item_pooler_output = self.mlp(pos_item_pooler_output)
neg_item_pooler_output = self.mlp(neg_item_pooler_output)
# Gather all item embeddings if using distributed training
if dist.is_initialized() and self.training:
# Dummy vectors for allgather
user_list = [torch.zeros_like(user_pooler_output) for _ in range(dist.get_world_size())]
pos_item_list = [torch.zeros_like(pos_item_pooler_output) for _ in range(dist.get_world_size())]
neg_item_list = [torch.zeros_like(neg_item_pooler_output) for _ in range(dist.get_world_size())]
# Allgather
dist.all_gather(tensor_list=user_list, tensor=user_pooler_output.contiguous())
dist.all_gather(tensor_list=pos_item_list, tensor=pos_item_pooler_output.contiguous())
dist.all_gather(tensor_list=neg_item_list, tensor=neg_item_pooler_output.contiguous())
# Since allgather results do not have gradients, we replace the
# current process's corresponding embeddings with original tensors
user_list[dist.get_rank()] = user_pooler_output
pos_item_list[dist.get_rank()] = pos_item_pooler_output
neg_item_list[dist.get_rank()] = neg_item_pooler_output
# Get full batch embeddings
user_pooler_output = torch.cat(user_list, dim=0)
pos_item_pooler_output = torch.cat(pos_item_list, dim=0)
neg_item_pooler_output = torch.cat(neg_item_list, dim=0)
cos_sim = self.sim(user_pooler_output.unsqueeze(1), pos_item_pooler_output.unsqueeze(0))
neg_sim = self.sim(user_pooler_output.unsqueeze(1), neg_item_pooler_output.unsqueeze(0))
cos_sim = torch.cat([cos_sim, neg_sim], 1)
labels = torch.arange(cos_sim.size(0)).long().to(self.device)
loss_fct = nn.CrossEntropyLoss()
loss = loss_fct(cos_sim, labels)
# Calculate loss for MLM
if mlm_outputs is not None and mlm_labels is not None and self.model_args.do_mlm:
mlm_labels = mlm_labels.view(-1, mlm_labels.size(-1))
prediction_scores = self.lm_head(mlm_outputs.last_hidden_state)
masked_lm_loss = loss_fct(prediction_scores.view(-1, self.config.vocab_size), mlm_labels.view(-1))
loss = loss + self.model_args.mlm_weight * masked_lm_loss
if not return_dict:
raise NotImplementedError
return SequenceClassifierOutput(
loss=loss,
logits=cos_sim,
)
def encode(self,
input_ids=None,
attention_mask=None,
token_type_ids=None,
position_ids=None,
head_mask=None,
inputs_embeds=None,
labels=None,
output_attentions=None,
output_hidden_states=None,
return_dict=None,
):
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
outputs = self.roberta(
input_ids=input_ids,
attention_mask=attention_mask,
token_type_ids=None,
position_ids=None,
head_mask=None,
inputs_embeds=None,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
pooler_output = self.pooler(attention_mask, outputs)
if self.pooler_type == "cls":
pooler_output = self.mlp(pooler_output)
if not return_dict:
return (outputs[0], pooler_output) + outputs[2:]
return BaseModelOutputWithPoolingAndCrossAttentions(
pooler_output=pooler_output,
last_hidden_state=outputs.last_hidden_state,
hidden_states=outputs.hidden_states,
)
def inference(self,
user_profile_list,
item_profile_list,
dataset_name,
tokenizer,
infer_batch_size=128
):
n_user = len(user_profile_list)
profiles = user_profile_list + item_profile_list
n_batch = math.ceil(len(profiles) / infer_batch_size)
text_embeds = []
for i in tqdm(range(n_batch), desc=f'Encoding Text {dataset_name}'):
batch_profiles = profiles[i * infer_batch_size: (i + 1) * infer_batch_size]
inputs = tokenizer(batch_profiles, padding=True, truncation=True, max_length=512, return_tensors="pt")
for k in inputs:
inputs[k] = inputs[k].to(self.device)
with torch.inference_mode():
embeds = self.encode(
input_ids=inputs.input_ids,
attention_mask=inputs.attention_mask
)
text_embeds.append(embeds.pooler_output.detach().cpu())
text_embeds = torch.concat(text_embeds, dim=0).cuda()
user_embeds = F.normalize(text_embeds[: n_user], dim=-1)
item_embeds = F.normalize(text_embeds[n_user: ], dim=-1)
return user_embeds, item_embeds