-
Notifications
You must be signed in to change notification settings - Fork 0
/
sniffer_model.py
555 lines (478 loc) · 23.9 KB
/
sniffer_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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
import torch
import transformers
import numpy as np
from sniffer_ppl_calculation import BBPETokenizerPPLCalc, SPLlamaTokenizerPPLCalc, CharLevelTokenizerPPLCalc, SPChatGLMTokenizerPPLCalc
from sniffer_ppl_calculation import split_sentence
# mosec
from mosec import Worker
from mosec.mixin import MsgpackMixin
# llama
from transformers import LlamaForCausalLM, LlamaTokenizer
from transformers import AutoModelForCausalLM, AutoTokenizer, StoppingCriteria, StoppingCriteriaList
from transformers.models.gpt2.tokenization_gpt2 import bytes_to_unicode
class SnifferBaseModel(MsgpackMixin, Worker):
def __init__(self):
super().__init__()
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.do_generate = None
self.text = None
self.base_tokenizer = None
self.base_model = None
self.generate_len = 1024
def forward_calc_ppl(self):
pass
def forward_gen(self):
self.base_tokenizer.padding_side = 'left'
# 1. single generate
if isinstance(self.text, str):
tokenized = self.base_tokenizer(self.text, return_tensors="pt").to(
self.device)
tokenized = tokenized.input_ids
gen_tokens = self.base_model.generate(tokenized,
do_sample=True,
max_length=self.generate_len)
gen_tokens = gen_tokens.squeeze()
result = self.base_tokenizer.decode(gen_tokens.tolist())
return result
# 2. batch generate
# msgpack.unpackb(self.text, use_list=False) == tuple
elif isinstance(self.text, tuple):
inputs = self.base_tokenizer(self.text,
padding=True,
return_tensors="pt").to(self.device)
gen_tokens = self.base_model.generate(**inputs,
do_sample=True,
max_length=self.generate_len)
gen_texts = self.base_tokenizer.batch_decode(
gen_tokens, skip_special_tokens=True)
return gen_texts
def forward(self, data):
"""
:param data: ['text': str, "do_generate": bool]
:return:
"""
self.text = data["text"]
self.do_generate = data["do_generate"]
if self.do_generate:
return self.forward_gen()
else:
return self.forward_calc_ppl()
class SnifferGPT2Model(SnifferBaseModel):
def __init__(self):
super().__init__()
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.do_generate = None
self.text = None
self.base_tokenizer = transformers.AutoTokenizer.from_pretrained(
'gpt2-xl')
self.base_model = transformers.AutoModelForCausalLM.from_pretrained(
'gpt2-xl')
self.base_tokenizer.pad_token_id = self.base_tokenizer.eos_token_id
self.base_model.to(self.device)
byte_encoder = bytes_to_unicode()
self.ppl_calculator = BBPETokenizerPPLCalc(byte_encoder,
self.base_model,
self.base_tokenizer,
self.device)
def forward_calc_ppl(self):
self.base_tokenizer.padding_side = 'right'
return self.ppl_calculator.forward_calc_ppl(self.text)
class SnifferGPTNeoModel(SnifferBaseModel):
def __init__(self):
super().__init__()
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.do_generate = None
self.text = None
self.base_tokenizer = transformers.AutoTokenizer.from_pretrained(
'EleutherAI/gpt-neo-2.7B')
self.base_model = transformers.AutoModelForCausalLM.from_pretrained(
'EleutherAI/gpt-neo-2.7B', device_map="auto", load_in_8bit=True)
self.base_tokenizer.pad_token_id = self.base_tokenizer.eos_token_id
byte_encoder = bytes_to_unicode()
self.ppl_calculator = BBPETokenizerPPLCalc(byte_encoder,
self.base_model,
self.base_tokenizer,
self.device)
def forward_calc_ppl(self):
self.base_tokenizer.padding_side = 'right'
return self.ppl_calculator.forward_calc_ppl(self.text)
class SnifferGPTJModel(SnifferBaseModel):
def __init__(self):
super().__init__()
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.do_generate = None
self.text = None
self.base_tokenizer = transformers.AutoTokenizer.from_pretrained(
'EleutherAI/gpt-j-6B')
self.base_tokenizer.pad_token_id = self.base_tokenizer.eos_token_id
self.base_model = transformers.AutoModelForCausalLM.from_pretrained(
'EleutherAI/gpt-j-6B', device_map="auto", load_in_8bit=True)
byte_encoder = bytes_to_unicode()
self.ppl_calculator = BBPETokenizerPPLCalc(byte_encoder,
self.base_model,
self.base_tokenizer,
self.device)
def forward_calc_ppl(self):
self.base_tokenizer.padding_side = 'right'
return self.ppl_calculator.forward_calc_ppl(self.text)
class SnifferLlamaModel(SnifferBaseModel):
"""
More details can be seen:
https://huggingface.co/docs/transformers/main/model_doc/llama#transformers.LlamaModel
"""
def __init__(self):
super().__init__()
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.do_generate = None
self.text = None
model_path = 'model_name_or_path'
self.base_tokenizer = LlamaTokenizer.from_pretrained(model_path)
self.base_tokenizer.pad_token_id = self.base_tokenizer.eos_token_id
self.base_tokenizer.unk_token_id = self.base_tokenizer.unk_token_id
self.base_model = LlamaForCausalLM.from_pretrained(model_path,
device_map="auto",
load_in_8bit=True)
self.ppl_calculator = SPLlamaTokenizerPPLCalc(self.base_model,
self.base_tokenizer,
self.device)
def forward_calc_ppl(self):
self.base_tokenizer.padding_side = 'right'
return self.ppl_calculator.forward_calc_ppl(self.text)
class SnifferWenZhongModel(SnifferBaseModel):
def __init__(self):
super().__init__()
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.do_generate = None
self.text = None
# bpe tokenizer
self.base_tokenizer = transformers.AutoTokenizer.from_pretrained(
'IDEA-CCNL/Wenzhong2.0-GPT2-3.5B-chinese')
self.base_tokenizer.pad_token_id = self.base_tokenizer.eos_token_id
self.base_model = transformers.AutoModelForCausalLM.from_pretrained(
'IDEA-CCNL/Wenzhong2.0-GPT2-3.5B-chinese',
device_map="auto",
load_in_8bit=True)
byte_encoder = bytes_to_unicode()
self.ppl_calculator = BBPETokenizerPPLCalc(byte_encoder,
self.base_model,
self.base_tokenizer,
self.device)
def forward_calc_ppl(self):
self.base_tokenizer.padding_side = 'right'
return self.ppl_calculator.forward_calc_ppl(self.text)
class SnifferSkyWorkModel(SnifferBaseModel):
def __init__(self):
super().__init__()
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.do_generate = None
self.text = None
self.base_tokenizer = transformers.AutoTokenizer.from_pretrained(
'SkyWork/SkyTextTiny', trust_remote_code=True)
self.base_tokenizer.pad_token_id = self.base_tokenizer.eos_token_id
self.base_model = transformers.AutoModelForCausalLM.from_pretrained(
'SkyWork/SkyTextTiny', device_map="auto", load_in_8bit=True)
all_special_tokens = self.base_tokenizer.all_special_tokens
self.ppl_calculator = CharLevelTokenizerPPLCalc(
all_special_tokens, self.base_model, self.base_tokenizer,
self.device)
def forward_calc_ppl(self):
self.base_tokenizer.padding_side = 'right'
return self.ppl_calculator.forward_calc_ppl(self.text)
class SnifferDaMoModel(SnifferBaseModel):
def __init__(self):
from modelscope.models.nlp import DistributedGPT3
from modelscope.preprocessors import TextGenerationJiebaPreprocessor
super().__init__()
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.do_generate = None
self.text = None
model_dir = 'model_dir'
self.base_tokenizer = TextGenerationJiebaPreprocessor(model_dir)
self.base_model = DistributedGPT3(model_dir=model_dir, rank=0)
self.base_model.to(self.device)
self.all_special_tokens = ['']
def calc_sent_ppl(self, outputs, labels):
lm_logits = outputs.logits.squeeze() # seq-len, V
shift_logits = lm_logits[..., :-1, :].contiguous()
shift_labels = labels[..., 1:].contiguous()
loss_func = torch.nn.CrossEntropyLoss(reduction='none')
ll = loss_func(shift_logits, shift_labels.view(-1)) # [seq-len] ?
loss = ll.mean().item()
ll = ll.tolist()
return loss, ll
def calc_token_ppl(self, input_ids, ll):
input_ids = input_ids[0].cpu().tolist()
# char-level
words = split_sentence(self.text)
chars_to_words = []
for idx, word in enumerate(words):
char_list = list(word)
chars_to_words.extend([idx for i in range(len(char_list))])
# get char_level ll
tokenized_tokens = []
for input_id in input_ids:
tokenized_tokens.append(self.base_tokenizer.decode([input_id]))
chars_ll = []
# because tokenizer don't include <s> before the sub_tokens,
# so the first sub_token's ll cannot be obtained.
token = tokenized_tokens[0]
if token in self.all_special_tokens:
char_list = [token]
else:
char_list = list(token)
chars_ll.extend([0 for i in range(len(char_list))])
# next we process the following sequence
for idx, token in enumerate(tokenized_tokens[1:]):
if token in self.all_special_tokens:
char_list = [token]
else:
char_list = list(token)
chars_ll.extend(ll[idx] for i in range(len(char_list)))
# get token_level ll
start = 0
ll_tokens = []
while start < len(chars_to_words) and start < len(chars_ll):
end = start + 1
while end < len(chars_to_words
) and chars_to_words[end] == chars_to_words[start]:
end += 1
if end > len(chars_ll):
break
ll_token = chars_ll[start:end]
ll_tokens.append(np.mean(ll_token))
start = end
# get begin_word_idx
begin_token = self.base_tokenizer.decode([input_ids[0]])
if begin_token in self.all_special_tokens:
char_list = [begin_token]
else:
char_list = list(begin_token)
begin_word_idx = chars_to_words[len(char_list) - 1] + 1
return ll_tokens, begin_word_idx
def forward_calc_ppl(self):
# bugfix: clear the self.inference_params, so we can both generate and calc ppl
self.base_model.train()
self.base_model.eval()
input_ids = self.base_tokenizer(self.text)['input_ids'].to(self.device)
labels = input_ids
input_ids = input_ids[:, :1024, ]
labels = labels[:, :1024, ]
outputs = self.base_model(tokens=input_ids,
labels=input_ids,
prompts_len=torch.tensor([input_ids.size(1)
]))
loss, ll = self.calc_sent_ppl(outputs, labels)
ll_tokens, begin_word_idx = self.calc_token_ppl(input_ids, ll)
return [loss, begin_word_idx, ll_tokens]
def forward_gen(self):
# 1. single generate
if isinstance(self.text, str):
input_ids = self.base_tokenizer(self.text)['input_ids'].to(
self.device)
gen_tokens = self.base_model.generate(input_ids,
do_sample=True,
max_length=self.generate_len)
gen_tokens = gen_tokens.sequences
gen_tokens = gen_tokens[0].cpu().numpy().tolist()
result = self.base_tokenizer.decode(gen_tokens)
return result
# 2. batch generate
# damo model didn't implement batch_encode and batch_decode, so we use a for loop here
elif isinstance(self.text, tuple):
batch_res = []
for text in self.text:
input_ids = self.base_tokenizer(text)['input_ids'].to(
self.device)
gen_tokens = self.base_model.generate(
input_ids, do_sample=True, max_length=self.generate_len)
gen_tokens = gen_tokens.sequences
gen_tokens = gen_tokens[0].cpu().numpy().tolist()
result = self.base_tokenizer.decode(gen_tokens)
batch_res.append(result)
return batch_res
class SnifferChatGLMModel(SnifferBaseModel):
def __init__(self):
super().__init__()
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.do_generate = None
self.text = None
self.base_tokenizer = transformers.AutoTokenizer.from_pretrained(
"THUDM/chatglm-6b", trust_remote_code=True)
self.base_model = transformers.AutoModel.from_pretrained(
"THUDM/chatglm-6b",
trust_remote_code=True,
device_map="auto",
load_in_8bit=True)
self.base_tokenizer.pad_token_id = self.base_tokenizer.eos_token_id
self.ppl_calculator = SPChatGLMTokenizerPPLCalc(
self.base_model, self.base_tokenizer, self.device)
def forward_calc_ppl(self):
# self.base_tokenizer.padding_side = 'right'
return self.ppl_calculator.forward_calc_ppl(self.text)
def forward_gen(self):
self.base_tokenizer.padding_side = 'left'
# 1. single generate
if isinstance(self.text, str):
inputs = self.base_tokenizer(self.text,
padding=True,
return_tensors="pt").to(self.device)
gen_tokens = self.base_model.generate(**inputs,
do_sample=True,
max_new_tokens=self.generate_len)
gen_texts = self.base_tokenizer.batch_decode(
gen_tokens.tolist(), skip_special_tokens=True)
result = gen_texts[0]
return result
# 2. batch generate
# msgpack.unpackb(self.text, use_list=False) == tuple
elif isinstance(self.text, tuple):
self.text = list(self.text)
inputs = self.base_tokenizer(self.text,
padding=True,
return_tensors="pt").to(self.device)
gen_tokens = self.base_model.generate(
**inputs, do_sample=True, max_new_tokens=self.generate_len)
gen_texts = self.base_tokenizer.batch_decode(
gen_tokens.tolist(), skip_special_tokens=True)
return gen_texts
class SnifferAlpacaModel(SnifferBaseModel):
def __init__(self):
super().__init__()
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.do_generate = None
self.text = None
self.base_tokenizer = LlamaTokenizer.from_pretrained(
"chavinlo/alpaca-native", trust_remote_code=True)
self.base_model = LlamaForCausalLM.from_pretrained(
"chavinlo/alpaca-native",
trust_remote_code=True,
device_map="auto",
load_in_8bit=True)
# self.base_tokenizer.pad_token_id = self.base_tokenizer.eos_token_id
def forward_gen(self):
self.base_tokenizer.padding_side = 'left'
PROMPT_FORMAT = """Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction:
{instruction}
### Response:
"""
# instruction = "Rewrite the following paragraph in a different style using your own words."
# treat single generate as batch generate
if isinstance(self.text, str):
self.text = [self.text]
processed_text = [PROMPT_FORMAT.format(instruction=text) for text in self.text]
inputs = self.base_tokenizer(processed_text,
padding=True,
return_tensors="pt").to(self.device)
gen_tokens = self.base_model.generate(
**inputs, do_sample=True, max_new_tokens=self.generate_len)
gen_texts = self.base_tokenizer.batch_decode(
gen_tokens, skip_special_tokens=True)
# TODO change gen_text.find() to gen_text.split()
# TODO output.gen_text("### Response:")[1].strip()
gen_texts = [gen_text[gen_text.find('### Response:\n') + 14 : ].strip() for gen_text in gen_texts]
if len(gen_texts) == 1:
return gen_texts[0]
else:
return gen_texts
class SnifferDollyModel(SnifferBaseModel):
def __init__(self):
super().__init__()
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.do_generate = None
self.text = None
self.base_model = AutoModelForCausalLM.from_pretrained('databricks/dolly-v1-6b', trust_remote_code=True,
device_map="auto", load_in_8bit=True)
self.base_tokenizer = AutoTokenizer.from_pretrained('databricks/dolly-v1-6b', padding_side="left")
def forward_gen(self):
# NOTE only use for rephrase
PROMPT_FORMAT = """Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction:
{instruction}
### Response:
"""
# instruction = "Rewrite the following paragraph in a different style using your own words."
response_key_token_id = self.base_tokenizer.encode("### Response:")[0]
end_key_token_id = self.base_tokenizer.encode("### End")[0]
# treat single generate as batch generate
if isinstance(self.text, str):
self.text = [self.text]
processed_text = [PROMPT_FORMAT.format(instruction=text) for text in self.text]
# inputs = self.base_tokenizer(processed_text, padding=True, max_length=512, truncation=True,return_tensors="pt").to(self.device)
inputs = self.base_tokenizer(processed_text, return_tensors="pt").to(self.device)
gen_tokens = self.base_model.generate(**inputs, pad_token_id=self.base_tokenizer.pad_token_id,
eos_token_id=end_key_token_id, do_sample=True, max_new_tokens=1024, top_p=0.92, top_k=0)
gen_texts = []
discard_num = 0
for tokens in gen_tokens:
tokens = tokens.cpu()
response_positions = np.where(tokens == response_key_token_id)[0]
# becatuse we truncate the sequences to max_length=512, simply discard these samples
if len(response_positions) > 0:
response_pos = response_positions[0]
end_pos = None
end_positions = np.where(tokens == end_key_token_id)[0]
if len(end_positions) > 0:
end_pos = end_positions[0]
print("eos_pos: {}".format(end_pos))
gen_texts.append(self.base_tokenizer.decode(tokens[response_pos + 1 : end_pos]).strip())
else:
discard_num += 1
print("discard_num: {}/{}".format(discard_num, len(gen_tokens)))
if len(gen_texts) == 1:
return gen_texts[0]
else:
return gen_texts
class SnifferStableLMRawModel(SnifferBaseModel):
def __init__(self):
super().__init__()
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.do_generate = None
self.text = None
self.base_model = AutoModelForCausalLM.from_pretrained("StabilityAI/stablelm-base-alpha-7b", trust_remote_code=True,
device_map="auto", load_in_8bit=True)
self.base_tokenizer = AutoTokenizer.from_pretrained("StabilityAI/stablelm-base-alpha-7b", padding_side="left")
self.base_tokenizer.pad_token_id = self.base_tokenizer.eos_token_id
class StopOnTokens(StoppingCriteria):
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
# <|USER|><|ASSISTANT|><|SYSTEM|><|padding|><|endoftext|>
stop_ids = [50278, 50279, 50277, 1, 0]
for stop_id in stop_ids:
if input_ids[0][-1] == stop_id:
return True
return False
class SnifferStableLMTunedModel(SnifferBaseModel):
def __init__(self):
super().__init__()
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.do_generate = None
self.text = None
self.base_model = AutoModelForCausalLM.from_pretrained("StabilityAI/stablelm-tuned-alpha-7b", trust_remote_code=True,
device_map="auto", load_in_8bit=True)
self.base_tokenizer = AutoTokenizer.from_pretrained("StabilityAI/stablelm-tuned-alpha-7b", padding_side="left")
def forward_gen(self):
self.base_tokenizer.padding_side = 'left'
self.base_tokenizer.pad_token_id = self.base_tokenizer.eos_token_id
system_prompt = """<|SYSTEM|># StableLM Tuned (Alpha version)
- StableLM is a helpful and harmless open-source AI language model developed by StabilityAI.
- StableLM is excited to be able to help the user, but will refuse to do anything that could be considered harmful to the user.
- StableLM is more than just an information source, StableLM is also able to write poetry, short stories, and make jokes.
- StableLM will refuse to participate in anything that could harm a human.
"""
# treat single generate as batch generate
if isinstance(self.text, str):
self.text = [self.text]
processed_text = [f"{system_prompt}<|USER|>{user_prompt}<|ASSISTANT|>" for user_prompt in self.text]
inputs = self.base_tokenizer(processed_text, padding=True, return_tensors="pt").to("cuda")
gen_tokens = self.base_model.generate(**inputs, max_new_tokens=self.generate_len, temperature=0.7,
do_sample=True, stopping_criteria=StoppingCriteriaList([StopOnTokens()]))
gen_texts = []
for tokens in gen_tokens:
tokens = tokens.cpu()
assistant_key_token_id = 50279
response_pos = np.where(tokens == assistant_key_token_id)[0][-1]
gen_texts.append(self.base_tokenizer.decode(tokens[response_pos + 1 :], skip_special_tokens=True))
if len(gen_texts) == 1:
return gen_texts[0]
else:
return gen_texts