-
Notifications
You must be signed in to change notification settings - Fork 40
/
simctgopt.py
292 lines (270 loc) · 11.4 KB
/
simctgopt.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
import sys
import os
import operator
from operator import itemgetter
import torch
from torch import nn
import random
import argparse
import numpy as np
import torch.nn.functional as F
from torch.nn import CrossEntropyLoss
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
val_fct = CrossEntropyLoss(reduction='none')
class SimCTGOPT(nn.Module):
def __init__(self, model_name, special_token_list=[]):
super(SimCTGOPT, self).__init__()
from transformers import GPT2Tokenizer, OPTForCausalLM
self.tokenizer = GPT2Tokenizer.from_pretrained(model_name)
self.model = OPTForCausalLM.from_pretrained(model_name)
if len(special_token_list) > 0:
print ('Original vocabulary size is {}'.format(len(self.tokenizer)))
print ('Adding special tokens...')
self.tokenizer.add_tokens(special_token_list)
print ('Special token added.')
print ('Resizing language model embeddings...')
self.model.resize_token_embeddings(len(self.tokenizer))
print ('Language model embeddings resized.')
self.vocab_size = self.model.config.vocab_size
print ('The vocabulary size of the language model is {}'.format(len(self.tokenizer)))
self.embed_dim = self.model.config.hidden_size
def compute_logits_and_hidden_states(self, input_ids):
# used for advanced decoding
# input_ids: 1 x seqlen
outputs = self.model(input_ids=input_ids, output_hidden_states=True)
last_hidden_states = outputs.hidden_states[-1]
logits = outputs.logits
return last_hidden_states, logits
def forward(self, input_ids, labels):
bsz, seqlen = input_ids.size()
outputs = self.model(input_ids=input_ids, output_hidden_states=True)
logits = outputs.logits
assert logits.size() == torch.Size([bsz, seqlen, self.vocab_size])
last_hidden_states = outputs.hidden_states[-1]
assert last_hidden_states.size() == torch.Size([bsz, seqlen, self.embed_dim])
return last_hidden_states, logits
def eval_loss(self, input_ids, labels):
bsz, seqlen = input_ids.size()
outputs = self.model(input_ids=input_ids, output_hidden_states=True)
logits = outputs.logits
assert logits.size() == torch.Size([bsz, seqlen, self.vocab_size])
mle_loss = val_fct(logits.view(-1, self.vocab_size), labels.view(-1))
assert mle_loss.size() == torch.Size([bsz * seqlen])
mask_tmp = labels.masked_fill(~labels.eq(-100), 1.0)
mask = mask_tmp.masked_fill(mask_tmp.eq(-100), 0.0)
# sum
mle_loss_sum = torch.sum(mle_loss)
token_num_sum = torch.sum(mask)
return mle_loss_sum, token_num_sum
def save_model(self, ckpt_save_path):
import os
if os.path.exists(ckpt_save_path):
pass
else: # recursively construct directory
os.makedirs(ckpt_save_path, exist_ok=True)
# save model
self.model.save_pretrained(ckpt_save_path)
# save tokenizer
self.tokenizer.save_pretrained(ckpt_save_path)
# decoding functions
# ------------------------------------------------------- #
@torch.no_grad()
def fast_contrastive_search(self,
input_ids,
beam_width,
alpha,
decoding_len,
end_of_sequence_token_id = None,
early_stop = False,
block_context_degeneration_penalty=False):
'''
input_ids: prefix input; 1 x prefix_len
decoding_len: how many tokens to generate
beam_width: size of candidate pool during decoding
alpha: regulates importance of model confidence and degeneration penalty
end_of_sequence_token_id: the token id that denotes the end of generation
early_stop: whether to use the end_of_sequence_token_id to truncate the output
'''
if early_stop:
try:
assert end_of_sequence_token_id != None
except AssertionError:
raise Exception('When early_stop is True, end_of_sequence_token_id cannot be None!!!')
self.model.eval()
from .utlisopt import ContrastiveDecodingOneStepFast
# sanity check
assert alpha >= 0. and alpha <= 1.0
# fast mode
batch_size, seqlen = input_ids.size()
prefix_len = seqlen
if block_context_degeneration_penalty:
block_context_span = prefix_len
else:
block_context_span = 0
#generated = [[] for _ in range(batch_size)]
generated = [item for item in input_ids.tolist()]
past_key_values = None
last_hidden_states = None
logits = None
stop_flag = False
for step in range(decoding_len):
if stop_flag:
break
input_ids, past_key_values, last_hidden_states, logits = ContrastiveDecodingOneStepFast(
self.model,
input_ids,
beam_width,
alpha,
past_key_values,
last_hidden_states,
self.tokenizer,
logits,
first_step=step == 0,
block_context_degeneration_penalty=block_context_degeneration_penalty,
block_context_span=block_context_span
)
tokens = input_ids.squeeze(dim=-1).tolist()
for idx, t in enumerate(tokens):
if early_stop:
if t == end_of_sequence_token_id:
stop_flag = True
break
else:
generated[idx].append(t)
else:
generated[idx].append(t)
output = generated[0]
return output
def diverse_contrastive_search(self, input_ids, sample_step, nucleus_p, beam_width, alpha, decoding_len,
end_of_sequence_token_id = None, early_stop = False, block_context_degeneration_penalty=False):
'''
sample_step:
number of steps to decode with nucleus sampling,
for the remaining steps we use contrastive search
decoding_len:
the total number of generated tokens
beam_width:
size of candidate pool during decoding
alpha:
regulates importance of model confidence and degeneration penalty
'''
if early_stop:
try:
assert end_of_sequence_token_id != None
except AssertionError:
raise Exception('When early_stop is True, end_of_sequence_token_id cannot be None!!!')
contrastive_step = decoding_len - sample_step
_, prefix_len = input_ids.size()
# first do sample
input_ids = self.model.generate(
input_ids,
do_sample=True,
max_length=prefix_len+sample_step,
top_p=nucleus_p,
top_k=0)
# then do contrastive search
output = self.fast_contrastive_search(input_ids, beam_width, alpha, contrastive_step,
end_of_sequence_token_id = end_of_sequence_token_id, early_stop = early_stop,
block_context_degeneration_penalty=block_context_degeneration_penalty)
return output
def greedy_search(self, input_ids, decoding_len, end_of_sequence_token_id = None, early_stop = False):
if early_stop:
try:
assert end_of_sequence_token_id != None
except AssertionError:
raise Exception('When early_stop is True, end_of_sequence_token_id cannot be None!!!')
_, prefix_len = input_ids.size()
output = self.model.generate(
input_ids,
max_length=prefix_len+decoding_len)
output = output[0]
if early_stop:
tmp = []
for idx in range(len(output)):
if len(tmp) < prefix_len:
tmp.append(output[idx])
else:
if output[idx] != end_of_sequence_token_id:
tmp.append(output[idx])
else:
break
output = tmp
return output
def beam_search(self, input_ids, beam_width, decoding_len, end_of_sequence_token_id = None, early_stop = False):
if early_stop:
try:
assert end_of_sequence_token_id != None
except AssertionError:
raise Exception('When early_stop is True, end_of_sequence_token_id cannot be None!!!')
_, prefix_len = input_ids.size()
output = self.model.generate(
input_ids,
max_length=prefix_len+decoding_len,
num_beams=beam_width)
output = output[0]
if early_stop:
tmp = []
for idx in range(len(output)):
if len(tmp) < prefix_len:
tmp.append(output[idx])
else:
if output[idx] != end_of_sequence_token_id:
tmp.append(output[idx])
else:
break
output = tmp
return output
def nucleus_sampling(self, input_ids, nucleus_p, decoding_len, end_of_sequence_token_id = None, early_stop = False):
if early_stop:
try:
assert end_of_sequence_token_id != None
except AssertionError:
raise Exception('When early_stop is True, end_of_sequence_token_id cannot be None!!!')
_, prefix_len = input_ids.size()
output = self.model.generate(
input_ids,
do_sample=True,
max_length=prefix_len+decoding_len,
top_p=nucleus_p,
top_k=0)
output = output[0]
if early_stop:
tmp = []
for idx in range(len(output)):
if len(tmp) < prefix_len:
tmp.append(output[idx])
else:
if output[idx] != end_of_sequence_token_id:
tmp.append(output[idx])
else:
break
output = tmp
return output
def topk_sampling(self, input_ids, topk, decoding_len, end_of_sequence_token_id = None, early_stop = False):
if early_stop:
try:
assert end_of_sequence_token_id != None
except AssertionError:
raise Exception('When early_stop is True, end_of_sequence_token_id cannot be None!!!')
_, prefix_len = input_ids.size()
output = self.model.generate(
input_ids,
do_sample=True,
max_length=prefix_len+decoding_len,
top_p=1.0,
top_k=topk)
output = output[0]
if early_stop:
tmp = []
for idx in range(len(output)):
if len(tmp) < prefix_len:
tmp.append(output[idx])
else:
if output[idx] != end_of_sequence_token_id:
tmp.append(output[idx])
else:
break
output = tmp
return output