-
Notifications
You must be signed in to change notification settings - Fork 0
/
lm.py
309 lines (241 loc) · 11.1 KB
/
lm.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
import os
import torch
import torch.nn.functional as F
from torch.nn.parallel import DistributedDataParallel as DDP
# to point to where we already have mistral downloaded
# TODO each cluster this needs to change for where huggingface
# put mistral
# for T5s, etc., we don't reall
# os.environ["HF_HOME"] = "/juice2/scr2/houjun/hf"
from transformers import AutoModelForCausalLM, AutoTokenizer, StoppingCriteria
DEVICE = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
# DEVICE = torch.device('cuda') if torch.cuda.is_available() else torch.device("mps") if torch.backends.mps.is_available() else torch.device('cpu')
class EosListStoppingCriteria(StoppingCriteria):
def __init__(self, eos_sequence):
self.eos_sequence = eos_sequence
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
last_ids = input_ids[:,-len(self.eos_sequence):].tolist()
return self.eos_sequence in last_ids
# initialize models
class LanguageModel(object):
"""Language Model Inference
Parameters
----------
model: optional, str
default = mistralai/Mistral-7B-v0.1
The language model to use.
Arguments
---------
prompt: str
The prompt to infer with the LM.
**kwargs: inference parameters.
"""
def __init__(self, model="openai-community/gpt2", dont_init=False, model_load_params={}):
# because huggingface accelerate / deepspeed may move the models
# we lazily initialize where we actually are by looking it up (see
# self.device) whenever we are asked. for now, we don't know
self.__device = None
if not dont_init:
self.model = AutoModelForCausalLM.from_pretrained(model, **model_load_params)
# attn_implementation="flash_attention_2",)
self.tokenizer = AutoTokenizer.from_pretrained(model)
def to(self, device):
self.model = self.model.to(device)
return self
def rollout(self, prompt, stop_sequence=None, temperature=0.7, top_p=0.7, do_sample=True, max_new_tokens=48, dont_stop=False, skip_special_tokens=False, **kwargs):
"""Rollout our policy until a stop sequence.
Parameters
----------
prompt : str
State to begin rollout at.
stop_sequence : List[int]
Stop sequence to stop rollout.
**kwargs
Rollout sampling parameters.
Returns
-------
str
Our rollout!
"""
self.tokenizer.pad_token = self.tokenizer.eos_token
crit = None
if stop_sequence:
crit = EosListStoppingCriteria(stop_sequence)
if isinstance(prompt, str):
model_inputs = self.tokenizer([prompt], return_tensors="pt",
max_length=950, truncation=True).to(self.device)
else:
model_inputs = self.tokenizer(prompt, return_tensors="pt",
max_length=950, truncation=True,
padding=True).to(self.device)
# if we are using DDP, the model sits in a wrapper object which we have
# to untangle before generate
underlying = self.model
# if isinstance(underlying, DDP):
# underlying = self.model.module
# print("RO in")
# we need to set
if stop_sequence:
generated_ids = underlying.generate(**model_inputs, **kwargs, stopping_criteria = [crit],
temperature=temperature, top_p=top_p,
pad_token_id=self.tokenizer.eos_token_id,
do_sample=do_sample, max_new_tokens=max_new_tokens)
elif dont_stop:
generated_ids = underlying.generate(**model_inputs, **kwargs,
temperature=temperature, top_p=top_p,
pad_token_id=self.tokenizer.eos_token_id,
do_sample=do_sample, max_new_tokens=max_new_tokens,
stopping_criteria = [])
else:
generated_ids = underlying.generate(**model_inputs, **kwargs,
temperature=temperature, top_p=top_p,
pad_token_id=self.tokenizer.eos_token_id,
do_sample=do_sample, max_new_tokens=max_new_tokens)
# print("RO out")
if isinstance(prompt, str):
return self.tokenizer.batch_decode(generated_ids, skip_special_tokens=skip_special_tokens)[0]
else:
return self.tokenizer.batch_decode(generated_ids, skip_special_tokens=skip_special_tokens)
@property
def device(self):
if not self.__device:
self.__device = next(self.model.parameters()).device
return self.__device
def perplexity(self, y, x="", device=None):
"""Obtain ppl(y|x) from the LM.
REMEMBER: LOWER IS BETTER!
In particular, we define P(y|x) as the sum of logprobs
of each individual element.
Parameters
----------
x : str
The prompt.
y : str
The entailments from the prompt to calculate the probability of.
device : Optional[torch.device]
The device to push the model inputs.
Returns
-------
torch.Tensor: 1
The perplexity of y given x
"""
# combine the input and output and forward pass
x_enc = self.tokenizer([x], max_length=959, truncation=True)["input_ids"][0]
y_enc = self.tokenizer([y], max_length=64, truncation=True)["input_ids"][0]
model_inputs = torch.tensor([x_enc+y_enc]).to(device if device else self.device)
underlying = self.model
if isinstance(underlying, DDP):
underlying = self.model.module
res = self.model(input_ids=model_inputs)
if isinstance(res, tuple):
res = res[0].squeeze(0)
else:
res = res["logits"].squeeze(0)
res = F.log_softmax(res, dim=1)
# isolate the output components' probabilities; remember that
# the last token omponent is one token beyond y (i.e. the final autoregression
# token, beyond our value y, so we discard that)
cond_log_probs = torch.gather(res[:len(x_enc)-1], 1, (torch.tensor(x_enc)[1:]).unsqueeze(1).to(device if device else self.device))
all_log_probs = torch.gather(res[:-1], 1, (torch.tensor(x_enc+y_enc)[1:]).unsqueeze(1).to(device if device else self.device))
log_probs = all_log_probs.sum(0) - cond_log_probs.sum(0)
# sum of logs probs is product of probs
return -log_probs[0]/len(y_enc)
def logprob_batched(self, ys, device=None):
"""Obtain P(y) from the LM.
In particular, we define P(y) as the sum of logprobs
of each individual element.
Parameters
----------
y : str
The entailments from the prompt to calculate the probability of.
device : Optional[torch.device]
The device to push the model inputs.
Returns
-------
torch.Tensor: 1
The probability of y.
"""
# force a pad token
self.tokenizer.pad_token = self.tokenizer.eos_token
self.tokenizer.truncation_side = "left"
# combine the input and output and forward pass
y_enc = self.tokenizer(ys, return_tensors="pt", padding=True,
truncation=True, max_length=1024).to(device if device else self.device)
underlying = self.model
if isinstance(underlying, DDP):
underlying = self.model.module
# we need to chop off the front because we don't
# have the log prob of our first token
loss_mask = (y_enc["attention_mask"][:,1:] != 0)
# brrr
res = self.model(**y_enc)["logits"]
res = F.log_softmax(res, dim=2)
# isolate the output components' probabilities; remember that
# the last token omponent is one token beyond y (i.e. the final autoregression
# token, beyond our value y, so we discard that)
cond_log_probs = torch.gather(res[:,:-1,:], 2,
(y_enc["input_ids"][:,1:]).unsqueeze(2).to(device if device else self.device)).squeeze(2)
# sum of logs probs is product of probs
# and we apply the mask to ignore logprob of padding
return (cond_log_probs*loss_mask).sum(-1)
def logprob(self, y, device=None):
"""Obtain P(y) from the LM.
In particular, we define P(y) as the sum of logprobs
of each individual element.
Parameters
----------
y : str
The entailments from the prompt to calculate the probability of.
device : Optional[torch.device]
The device to push the model inputs.
Returns
-------
torch.Tensor: 1
The probability of y.
"""
# combine the input and output and forward pass
y_enc = self.tokenizer([y])["input_ids"][0]
model_inputs = torch.tensor([y_enc]).to(device if device else self.device)
underlying = self.model
if isinstance(underlying, DDP):
underlying = self.model.module
res = self.model(input_ids=model_inputs)
if isinstance(res, tuple):
res = res[0].squeeze(0)
else:
res = res["logits"].squeeze(0)
res = F.log_softmax(res, dim=1)
# isolate the output components' probabilities; remember that
# the last token omponent is one token beyond y (i.e. the final autoregression
# token, beyond our value y, so we discard that)
cond_log_probs = torch.gather(res[:-1], 1, (torch.tensor(y_enc)[1:]).unsqueeze(1).to(device if device else self.device))
# sum of logs probs is product of probs
return cond_log_probs.sum()
# lm = LanguageModel()
# lm.logprob_batched(["Hello! How's the weather?", "jerk", "beef jerky samurai!", "shibat shebat shemibu", "Advertisement"])
# lm.logprob("bjork there.")
# breakpoint()
# prompt1 = """
# anon1: what do you still fucking want?
# anon2: money and, you know, also
# anon1: """
# prompt2 = """
# anon1: that fucking person is so...
# anon2: """
# prompt = prompt1
# # get the anon stop token
# anon = lm.tokenizer("anon")["input_ids"][0]
# # rollout! yipee
# res = lm.rollout(prompt.strip(), stop_sequence=[anon])
# # get only the new generation
# new_utterance = res.replace(prompt, "").strip().split("\n")[0].strip()
# # as perplexity is the inverse of probability, we have to
# # negate it to measure likelyhood
# likelihood = -lm.perplexity(x=prompt, y=new_utterance)
# compute the entailment probabilities
# new_utterance
# get log probs of our rollout
# rollout_probs = lm.condition([res])
# rollout_probs.shape
# print()
# anon2: Here's an example of the bullshit that Kevin Sutherland was spouting on his"""