-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmultitask_bart.py
339 lines (294 loc) · 11.7 KB
/
multitask_bart.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
import warnings
import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers.configuration_bart import BartConfig
from transformers.modeling_bart import (
PretrainedBartModel,
_make_linear_from_emb,
_reorder_buffer,
BartClassificationHead,
BartModel
)
class BartForMultitaskLearning(PretrainedBartModel):
def __init__(self, config: BartConfig, **kwargs):
super().__init__(config, **kwargs)
self.model = BartModel(config)
self.register_buffer(
"final_logits_bias",
torch.zeros((1, self.model.shared.num_embeddings))
)
self.num_cfemotions = 12
self.num_emotions = 6
self.num_sentiments = 2
self.cfemotion_head = BartClassificationHead(
config.d_model,
config.d_model,
self.num_cfemotions,
config.classif_dropout
)
self.model._init_weights(self.cfemotion_head.dense)
self.model._init_weights(self.cfemotion_head.out_proj)
self.emotion_head = BartClassificationHead(
config.d_model,
config.d_model,
self.num_emotions,
config.classif_dropout
)
self.model._init_weights(self.emotion_head.dense)
self.model._init_weights(self.emotion_head.out_proj)
self.sentiment_head = BartClassificationHead(
config.d_model,
config.d_model,
self.num_sentiments,
config.classif_dropout
)
self.model._init_weights(self.sentiment_head.dense)
self.model._init_weights(self.sentiment_head.out_proj)
def resize_token_embeddings(self, new_num_tokens):
old_num_tokens = self.model.shared.num_embeddings
new_embeddings = super().resize_token_embeddings(new_num_tokens)
self.model.shared = new_embeddings
self._resize_final_logits_bias(new_num_tokens, old_num_tokens)
return new_embeddings
def _resize_final_logits_bias(self, new_num_tokens, old_num_tokens):
if new_num_tokens <= old_num_tokens:
new_bias = self.final_logits_bias[:, :new_num_tokens]
else:
extra_bias = torch.zeros(
(1, new_num_tokens - old_num_tokens),
device=self.final_logits_bias.device
)
new_bias = torch.cat([self.final_logits_bias, extra_bias], dim=1)
self.register_buffer("final_logits_bias", new_bias)
def forward(
self,
input_ids,
attention_mask=None,
encoder_outputs=None,
decoder_input_ids=None,
decoder_attention_mask=None,
decoder_cached_states=None,
labels=None,
use_cache=None,
output_attentions=None,
output_hidden_states=None,
task=None,
**unused
):
if "lm_labels" in unused:
warnings.warn(
"The `lm_labels` argument is deprecated and will be removed "
"in a future version, use `labels` instead.",
DeprecationWarning
)
labels = unused.pop("lm_labels")
if labels is not None:
use_cache = False
outputs = self.model(
input_ids,
attention_mask=attention_mask,
decoder_input_ids=decoder_input_ids,
encoder_outputs=encoder_outputs,
decoder_attention_mask=decoder_attention_mask,
decoder_cached_states=decoder_cached_states,
use_cache=use_cache,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states
)
if task == "response":
lm_logits = F.linear(
outputs[0],
self.model.shared.weight,
bias=self.final_logits_bias
)
outputs = (lm_logits,) + outputs[1:] # Add cache, hidden states and attention if they are here
if labels is not None:
loss_fct = nn.CrossEntropyLoss()
# TODO(SS): do we need to ignore pad tokens in labels?
masked_lm_loss = loss_fct(
lm_logits.view(-1, self.config.vocab_size),
labels.view(-1)
)
outputs = (masked_lm_loss,) + outputs
elif task in ["cfemotion", "emotion", "sentiment"]:
x = outputs[0] # last hidden state
eos_mask = input_ids.eq(self.config.eos_token_id)
if len(torch.unique(eos_mask.sum(1))) > 1:
raise ValueError(
"All examples must have the same number of <eos> tokens."
)
if task == "cfemotion":
classification_head = self.cfemotion_head
num_labels = self.num_cfemotions
elif task == "emotion":
classification_head = self.emotion_head
num_labels = self.num_emotions
else:
classification_head = self.sentiment_head
num_labels = self.num_sentiments
sentence_representation = x[eos_mask, :].view(
x.size(0),
-1,
x.size(-1)
)[:, -1, :]
logits = classification_head(sentence_representation)
# Prepend logits
outputs = (logits,) + outputs[1:] # Add hidden states and attention if they are here
if labels is not None: # prepend loss to output,
loss = F.cross_entropy(
logits.view(-1, num_labels),
labels.view(-1)
)
outputs = (loss,) + outputs
else:
raise ValueError("The dataset contains an invalid task.")
return outputs
def prepare_inputs_for_generation(
self,
decoder_input_ids,
past,
attention_mask,
use_cache,
task,
**kwargs
):
assert past is not None, "past has to be defined for encoder_outputs"
encoder_outputs, decoder_cached_states = past
return {
"input_ids": None, # encoder_outputs is defined. input_ids not needed
"encoder_outputs": encoder_outputs,
"decoder_cached_states": decoder_cached_states,
"decoder_input_ids": decoder_input_ids,
"attention_mask": attention_mask,
"use_cache": use_cache, # change this to avoid caching (presumably for debugging)
"task": task
}
def adjust_logits_during_generation(self, logits, cur_len, max_length):
if cur_len == 1:
self._force_token_ids_generation(logits, self.config.bos_token_id)
if cur_len == max_length - 1 and self.config.eos_token_id is not None:
self._force_token_ids_generation(logits, self.config.eos_token_id)
return logits
def _force_token_ids_generation(self, scores, token_ids) -> None:
if isinstance(token_ids, int):
token_ids = [token_ids]
all_but_token_ids_mask = torch.tensor(
[x for x in range(self.config.vocab_size) if x not in token_ids],
dtype=torch.long,
device=next(self.parameters()).device
)
assert len(scores.shape) == 2, \
"scores should be of rank 2 with shape: [batch_size, vocab_size]"
scores[:, all_but_token_ids_mask] = -float("inf")
@staticmethod
def _reorder_cache(past, beam_idx):
((enc_out, enc_mask), decoder_cached_states) = past
reordered_past = []
for layer_past in decoder_cached_states:
# get the correct batch idx from decoder layer's batch dim for cross and self-attn
layer_past_new = {
attn_key: _reorder_buffer(attn_cache, beam_idx)
for attn_key, attn_cache in layer_past.items()
}
reordered_past.append(layer_past_new)
new_enc_out = (
enc_out if enc_out is None
else enc_out.index_select(0, beam_idx)
)
new_enc_mask = (
enc_mask if enc_mask is None
else enc_mask.index_select(0, beam_idx)
)
past = ((new_enc_out, new_enc_mask), reordered_past)
return past
def get_encoder(self):
return self.model.encoder
def get_output_embeddings(self):
return _make_linear_from_emb(self.model.shared) # make it on the fly
class BartForAdversarialMultitaskLearning(BartForMultitaskLearning):
def __init__(self, config: BartConfig, **kwargs):
super().__init__(config, **kwargs)
def forward(
self,
input_ids,
attention_mask=None,
encoder_outputs=None,
decoder_input_ids=None,
decoder_attention_mask=None,
decoder_cached_states=None,
labels=None,
use_cache=None,
output_attentions=None,
output_hidden_states=None,
task=None,
**unused
):
if "lm_labels" in unused:
warnings.warn(
"The `lm_labels` argument is deprecated and will be removed "
"in a future version, use `labels` instead.",
DeprecationWarning
)
labels = unused.pop("lm_labels")
if labels is not None:
use_cache = False
outputs = self.model(
input_ids,
attention_mask=attention_mask,
decoder_input_ids=decoder_input_ids,
encoder_outputs=encoder_outputs,
decoder_attention_mask=decoder_attention_mask,
decoder_cached_states=decoder_cached_states,
use_cache=use_cache,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states
)
if task == "response":
lm_logits = F.linear(
outputs[0],
self.model.shared.weight,
bias=self.final_logits_bias
)
outputs = (lm_logits,) + outputs#[1:] # Add cache, hidden states and attention if they are here
if labels is not None:
loss_fct = nn.CrossEntropyLoss()
# TODO(SS): do we need to ignore pad tokens in labels?
masked_lm_loss = loss_fct(
lm_logits.view(-1, self.config.vocab_size),
labels.view(-1)
)
outputs = (masked_lm_loss,) + outputs
elif task in ["cfemotion", "emotion", "sentiment"]:
x = outputs[0] # last hidden state
eos_mask = input_ids.eq(self.config.eos_token_id)
if len(torch.unique(eos_mask.sum(1))) > 1:
raise ValueError(
"All examples must have the same number of <eos> tokens."
)
if task == "cfemotion":
classification_head = self.cfemotion_head
num_labels = self.num_cfemotions
elif task == "emotion":
classification_head = self.emotion_head
num_labels = self.num_emotions
else:
classification_head = self.sentiment_head
num_labels = self.num_sentiments
sentence_representation = x[eos_mask, :].view(
x.size(0),
-1,
x.size(-1)
)[:, -1, :]
logits = classification_head(sentence_representation)
# Prepend logits
outputs = (logits,) + outputs#[1:] # Add hidden states and attention if they are here
if labels is not None: # prepend loss to output,
loss = F.cross_entropy(
logits.view(-1, num_labels),
labels.view(-1)
)
outputs = (loss,) + outputs
else:
raise ValueError("The dataset contains an invalid task.")
return outputs