-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmodel_utils.py
1232 lines (1082 loc) · 50.9 KB
/
model_utils.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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import time
import json
from typing import Optional, List, Dict, Callable, Any
import functools
import torch
from transformers import PreTrainedTokenizer, set_seed
from tqdm import tqdm
from tqdm.contrib.concurrent import thread_map
import logging
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
datefmt='%m/%d/%Y %H:%M:%S')
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def format_chat(
message: str,
system_message: Optional[str]=None,
) -> List[Dict[str, str]]:
"""
Format the message into a list of dictionaries with role and content keys.
This is useful for the chat-based models without tokenizer that does this.
"""
if system_message is not None:
chat = [
{"role": "system", "content": system_message},
{"role": "user", "content": message},
]
else:
chat = [{"role": "user", "content": message}]
return chat
def call_api(func:Callable, limit: int=5, pause: int=10):
"""
Call the API function with retries and rate limit handling.
TODO: more error handling?
"""
count = 0
while True:
try:
output = func()
break
except Exception as e:
logger.info(f"Exception while using api: {e}")
msg = str(e).lower()
if "rate limit" in msg or "rate_limit" in msg or "quota" in msg or "429" in msg:
logger.info(f"Rate limit exceeded, waiting {pause} secs and retrying...")
time.sleep(pause)
elif count < limit:
logger.info(f"Encountered error {e}, retrying...")
count += 1
else:
logger.info("Skipping generation due to unknown error")
output = None
break
return output
class LLM:
"""
Base class for generative models.
"""
def __init__(
self,
model_name: str,
temperature: float=0.9,
top_p: float=0.9,
max_length: int=32768,
generation_max_length: int=2048,
generation_min_length: int=0,
do_sample: bool=True,
stop_newline: bool=False,
use_chat_template: bool=False,
system_message: Optional[str]="You are a helpful assistant.",
):
self.model_name = model_name
self.temperature = temperature
self.top_p = top_p
self.max_length = max_length
self.generation_max_length = generation_max_length
self.generation_min_length = generation_min_length
self.do_sample = do_sample
self.use_chat_template = use_chat_template
self.system_message = system_message
self.stops = None
if stop_newline:
self.stops = ["\n", "\n\n"]
"""
Prepare the data for input to the llm
test_item: dict[str, any]
the test item to be used for the generation, this dictionary is from the data preprocessing step and are used for further formatting to specific models, such as tokenization and/or chat formatting
data: dict[str, any]
the data dictionary that contains the template for the user message and system
Returns the prepared input (type is model-specific)
"""
def prepare_inputs(self, test_item: Dict[str, Any], data: Dict[str, Any]) -> Any:
raise NotImplementedError("prepare_inputs not implemented for LLM")
"""
Generate the output from the model
The inputs have been prepared, the prompt is only the user message as a string that needs to be pre-processed.
kwargs contains any additional parameters.
This function should be implemented by the children class.
The output should be a dictionary with the following:
- "output" (str): the generated output
- "input_len" (int): the length of the input tokens
- "output_len" (int): the length of the output tokens
- "input_text" (str or List[Dict[str, str]]): the input text or the chat format
There may be additional keys depending on the model.
This function may also return None in case of errors (e.g., denied by the API provider).
"""
def generate(self, inputs: Optional[Any]=None, prompt: Optional[str]=None, **kwargs) -> Optional[Dict[str, Any]]:
raise NotImplementedError("generate not implemented for LLM")
"""
Generate the output from the model for a list of inputs or prompts.
This is similar to to the generate function but everything is in a list.
The children classes may override this function for optimization.
"""
def generate_batch(self, inputs: Optional[List[Any]]=None, prompt: Optional[List[str]]=None, **kwargs) -> List[Optional[Dict[str, Any]]]:
outputs = []
if inputs is None:
for p in tqdm(prompt):
outputs.append(self.generate(prompt=p, **kwargs))
else:
for i in tqdm(inputs):
outputs.append(self.generate(inputs=i, **kwargs))
return outputs
class OpenAIModel(LLM):
def __init__(
self,
model_name,
temperature=0.9,
top_p=0.9,
max_length=32768,
generation_max_length=2048,
generation_min_length=0,
do_sample=True,
stop_newline=False,
use_chat_template=True,
system_message=None,
seed=42,
**kwargs,
):
super().__init__(
model_name,
temperature=temperature,
top_p=top_p,
max_length=max_length,
generation_max_length=generation_max_length,
generation_min_length=generation_min_length,
do_sample=do_sample,
stop_newline=stop_newline,
use_chat_template=use_chat_template,
system_message=system_message,
)
import openai
import tiktoken
if "azure" in model_name:
# env var: AZURE_OPENAI_API_KEY, AZURE_OPENAI_ENDPOINT, and OPENAI_API_VERSION
self.model = openai.AzureOpenAI()
model_name = model_name[model_name.index("/")+1:]
else:
# make sure to set the OPENAI_API_KEY environment variable
self.model = openai.OpenAI()
self.model_name = model_name
self.tokenizer = tiktoken.encoding_for_model(model_name)
self.seed = seed
self.API_MAX_LENGTH = 128000 # this is defined by the OPENAI API
def prepare_inputs(self, test_item, data):
buffer = 100
# we don't include system message to stay consistent with other models, which defaults to None
prompt = format_chat(data["user_template"].format(**test_item), system_message=self.system_message)
inputs = "\n".join([f"Role: {x['role']}\nContent: {x['content']}" for x in prompt])
tokens = self.tokenizer.encode(inputs)
input_len = len(tokens)
if self.max_length > self.API_MAX_LENGTH:
logger.warning(f"max_length {self.max_length} is greater than {self.API_MAX_LENGTH}, setting to {self.API_MAX_LENGTH}")
self.max_length = self.API_MAX_LENGTH
if input_len > self.max_length - self.generation_max_length - buffer:
truncate_length = input_len - (self.max_length - self.generation_max_length - buffer)
new_context = self.tokenizer.decode(self.tokenizer.encode(test_item["context"])[:-truncate_length])
test_item["context"] = new_context
prompt = format_chat(data["user_template"].format(**test_item), system_message=self.system_message)
return prompt
def generate(self, inputs=None, prompt=None, **kwargs):
if inputs is None:
# for system_message, set the self.system_message attribute
inputs = format_chat(prompt, system_message=self.system_message)
# kwargs can be used to pass additional parameters to the model: max_tokens, stop, etc.
func = functools.partial(
self.model.chat.completions.create,
model=self.model_name,
messages=inputs,
max_tokens=self.generation_max_length,
temperature=self.temperature if self.do_sample else 0.0,
top_p=self.top_p,
stop=self.stops,
seed=self.seed,
**kwargs,
)
output = call_api(func)
if output is not None:
if output.choices[0].message.content is None:
# sometimes the model output can get filtered but still return a message
return None
return {
"output": output.choices[0].message.content,
"input_len": output.usage.prompt_tokens,
"output_len": output.usage.completion_tokens,
"input_text": inputs,
"system_fingerprint": output.system_fingerprint,
}
return None
def batch_api(self, inputs, batch_file, **kwargs):
with open(batch_file, "w") as f:
for idx, p in enumerate(inputs):
f.write(json.dumps({
"custom_id": f"{idx}",
"method": "POST",
"url": "/v1/chat/completions",
"body": {
"model": self.model_name,
"messages": p,
"max_tokens": self.generation_max_length,
"temperature": self.temperature if self.do_sample else 0.0,
"top_p": self.top_p,
"stop": self.stops,
"seed": self.seed,
**kwargs,
}
}) + "\n")
upload_file = self.model.files.create(file=open(batch_file, "rb"), purpose="batch")
batch_job = self.model.batches.create(input_file_id=upload_file.id, endpoint="/v1/chat/completions", completion_window='24h')
logger.info(f"Starting batch job: {batch_job.id}")
while batch_job.status != "completed":
if batch_job.status in ['failed', 'expired', 'cancelled']:
logger.error(f"Batch job failed: {batch_job.status}")
raise Exception(f"Batch job {batch_job.id} failed: {batch_job.status}")
time.sleep(5)
batch_job = self.model.batches.retrieve(batch_job.id)
logger.info(batch_job)
result_file_id = batch_job.output_file_id
result = self.model.files.content(result_file_id).content
outputs = [None for _ in inputs]
# save a copy just in case but there may be name collision so we don't read from this file
with open(batch_file+".result", "wb") as f:
f.write(result)
for line in result.decode("utf-8").strip().split("\n"):
output = json.loads(line)
task_id = int(output["custom_id"])
res = output["response"]['body']
if res["choices"][0]["message"]["content"] is not None:
outputs[task_id] = {
"output": res["choices"][0]["message"]["content"],
"input_len": res["usage"]["prompt_tokens"],
"output_len": res["usage"]["completion_tokens"],
"input_text": inputs[task_id],
"system_fingerprint": res["system_fingerprint"],
}
return outputs
def generate_batch(self, inputs=None, prompt=None, **kwargs):
"""
Generate for a batch of inputs.
There are two methods:
1. Use the batch API provided by OpenAI, which involves uploading all requests in a file and getting an output file. This is cheaper and should be faster than just calling the API for each request. To use this, set batch_file to a file path.
2. Use the normal API call for each request with multiple threads for some speedup.
"""
# https://cookbook.openai.com/examples/batch_processing
# https://platform.openai.com/docs/api-reference/batch/create
batch_file = kwargs.pop("batch_file", None)
if batch_file:
# use the batch api, which only supports upto 50k requests/lines and 200MB in size
logger.info(f"Using {batch_file} for batch generation")
if inputs is None:
inputs = [format_chat(p, system_message=self.system_message) for p in prompt]
try:
outputs = self.batch_api(inputs, batch_file, **kwargs)
except Exception as e:
# one possible error is that the file is too large, so we need to split it
batch_size = 100
logger.info(f"Error in batch generation: {e} with size {len(inputs)}, re-running with batch size {batch_size}, you may want to change the batch size if this fails...")
outputs = []
for i in range(0, len(inputs), batch_size):
outputs.extend(self.batch_api(inputs[i:i+batch_size], batch_file, **kwargs))
else:
if inputs is None:
inputs = [None for _ in prompt]
else:
prompt = [None for _ in inputs]
# we don't support kwargs here for now
if len(kwargs) > 0:
logger.warning("kwargs are not supported for batch generation")
# use thread_map instead of process_map since the bottleneck is the api call
outputs = thread_map(self.generate, inputs, prompt, max_workers=32)
return outputs
class AnthropicModel(LLM):
def __init__(
self,
model_name,
temperature=0.9,
top_p=0.9,
max_length=32768,
generation_max_length=2048,
generation_min_length=0,
do_sample=True,
stop_newline=False,
use_chat_template=True,
system_message=None,
**kwargs,
):
super().__init__(
model_name,
temperature=temperature,
top_p=top_p,
max_length=max_length,
generation_max_length=generation_max_length,
generation_min_length=generation_min_length,
do_sample=do_sample,
stop_newline=stop_newline,
use_chat_template=use_chat_template,
system_message=system_message,
)
from anthropic import Anthropic, AnthropicVertex
if "vertex" in model_name:
# region defaults to env var CLOUD_ML_REGION and project_id defaults to ANTHROPIC_VERTEX_PROJECT_ID
self.model = AnthropicVertex()
model_name = model_name[model_name.index("/")+1:]
else:
# remember to set ANTHROPIC_API_KEY environment variable (the default)
self.model = Anthropic()
# Note: the tokenizer was removed since anthropic >= 0.39.0, and it not accurate for the newer models
# however, we still load an older version of the tokenizer for truncation
# https://github.com/anthropics/anthropic-sdk-python/blob/12dbc0c315eee4117c337da99beea5c53d898f9b/src/anthropic/tokenizer.json
from tokenizers import Tokenizer
self.tokenizer = Tokenizer.from_file("claude.tokenizer.json")
self.model_name = model_name
self.temperature = temperature
self.top_p = top_p
self.max_length = max_length
self.generation_max_length = generation_max_length
self.do_sample = do_sample
self.stops = None
if stop_newline: # claude does not support newline
pass
if self.system_message is None:
# claude expects string as system message
self.system_message = ""
def prepare_inputs(self, test_item, data):
buffer = 100
# for anthropic, the system message is passed through the function not in the prompt
prompt = format_chat(data["user_template"].format(**test_item), system_message=None)
inputs = "\n".join([f"Role: {x['role']}\nContent: {x['content']}" for x in prompt])
tokens = self.tokenizer.encode(inputs)
input_len = len(tokens)
if input_len > self.max_length - self.generation_max_length - buffer:
truncate_length = input_len - (self.max_length - self.generation_max_length - buffer)
tokens = self.tokenizer.encode(test_item["context"])
new_context = test_item["context"][:tokens.offsets[-truncate_length-1][1]]
test_item["context"] = new_context
prompt = format_chat(data["user_template"].format(**test_item), system_message=None)
return prompt
def generate(self, inputs=None, prompt=None, **kwargs):
if inputs is None:
inputs = format_chat(prompt, system_message=None)
# kwargs can be used to pass additional parameters to the model: max_tokens, stop, etc.
# Note: in the original paper, we used this system message:
# system="You are a helpful assistant. Make sure your output does not contain new lines."
# To be consistent with the other models, and for future compability, we remove the system message
# We don't expect this to make a significant difference in the results
print(inputs)
func = functools.partial(
self.model.messages.create,
model=self.model_name,
messages=inputs,
max_tokens=self.generation_max_length,
temperature=self.temperature if self.do_sample else 0.0,
top_p=self.top_p,
stop_sequences=self.stops,
system=self.system_message,
**kwargs,
)
output = call_api(func, pause=20)
if output is not None:
return {
"output": output.content[0].text,
"input_len": output.usage.input_tokens,
"output_len": output.usage.output_tokens,
"input_text": inputs,
}
return None
def batch_api(self, inputs, **kwargs):
# this should be faster and costs 50%, but each batch cannot exceed 100k requests or 256MB
# https://docs.anthropic.com/en/docs/build-with-claude/message-batches
from anthropic.types.message_create_params import MessageCreateParamsNonStreaming
from anthropic.types.messages.batch_create_params import Request
requests = []
for idx, p in enumerate(inputs):
requests.append(Request(
custom_id=f"{idx}",
params=MessageCreateParamsNonStreaming(
model=self.model_name,
messages=p,
max_tokens=self.generation_max_length,
temperature=self.temperature if self.do_sample else 0.0,
top_p=self.top_p,
stop_sequences=self.stops,
system=self.system_message,
**kwargs,
)
))
batch_job = self.model.messages.batches.create(requests=requests)
while batch_job.processing_status not in ['succeeded', 'ended']:
if batch_job.processing_status in ['errored', 'cancelled', 'expired']:
logger.error(f"Batch job failed: {batch_job.process_status}")
raise Exception(f"Batch job {batch_job.id} failed: {batch_job.process_status}")
time.sleep(5)
batch_job = self.model.messages.batches.retrieve(batch_job.id)
logger.info(batch_job)
outputs = [None for _ in inputs]
for result in self.model.messages.batches.results(batch_job.id):
if result.result.type == "succeeded":
outputs[int(result.custom_id)] = {
"output": result.result.message.content[0].text,
"input_len": result.result.message.usage.input_tokens,
"output_len": result.result.message.usage.output_tokens,
"input_text": inputs[int(result.custom_id)],
}
return outputs
def generate_batch(self, inputs=None, prompt=None, **kwargs):
batch_file = kwargs.pop("batch_file", None)
if batch_file:
if inputs is None:
inputs = [format_chat(p, system_message=None) for p in prompt]
try:
outputs = self.batch_api(inputs, **kwargs)
except Exception as e:
# one possible error is that the file is too large, so we need to split it
batch_size = 100
logger.info(f"Error in batch generation: {e} with size {len(inputs)}, re-running with batch size {batch_size}, you may want to change the batch size if this fails...")
outputs = []
for i in range(0, len(inputs), batch_size):
outputs.extend(self.batch_api(inputs[i:i+batch_size], batch_file, **kwargs))
else:
if inputs is None:
inputs = [None for _ in prompt]
else:
prompt = [None for _ in inputs]
# we don't support kwargs here for now
if len(kwargs) > 0:
logger.warning("kwargs are not supported for batch generation")
# use thread_map instead of process_map since the bottleneck is the api call
outputs = thread_map(self.generate, inputs, prompt, max_workers=2)
return outputs
class GeminiModel(LLM):
def __init__(
self,
model_name,
temperature=0.9,
top_p=0.9,
max_length=32768,
generation_max_length=2048,
generation_min_length=0,
do_sample=True,
stop_newline=False,
use_chat_template=True,
system_message=None,
**kwargs,
):
super().__init__(
model_name,
temperature=temperature,
top_p=top_p,
max_length=max_length,
generation_max_length=generation_max_length,
generation_min_length=generation_min_length,
do_sample=do_sample,
stop_newline=stop_newline,
use_chat_template=use_chat_template,
system_message=system_message,
)
import google.generativeai as genai
# default env var GOOGLE_API_KEY
genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
import vertexai
vertexai.init() # make sure to set the env var appropriately
from vertexai.preview.tokenization import get_tokenizer_for_model
self.model = genai.GenerativeModel(model_name)
self.tokenizer = get_tokenizer_for_model(model_name)
self.model_name = model_name
if system_message is not None:
logger.warning("system_message is not supported for GeminiModel")
def prepare_inputs(self, test_item, data):
prompt = data["prompt_template"].format(**test_item)
buffer = 100
inputs = self.tokenizer.compute_tokens(prompt).token_info_list()[0].tokens
input_len = len(inputs)
max_length = self.max_length
if input_len > max_length - self.generation_max_length - buffer:
truncate_length = input_len - (max_length - self.generation_max_length - buffer)
# not the most pretty way of doing this but it works...
# the documentation doesn't provide an official way to truncate
new_context = self.tokenizer._sentencepiece_adapter._tokenizer.decode(
self.tokenizer.compute_tokens(test_item["context"]).token_info_list()[0].token_ids[:-truncate_length]
)
test_item['context'] = new_context
prompt = data["prompt_template"].format(**test_item)
return prompt
def generate(self, inputs=None, prompt=None, **kwargs):
import google.generativeai as genai
if inputs is None:
inputs = prompt
generation_config = genai.GenerationConfig(temperature=self.temperature, top_p=self.top_p, max_output_tokens=self.generation_max_length)
func = functools.partial(
self.model.generate_content,
contents=inputs,
generation_config=generation_config
)
output = call_api(func, pause=15)
if output is not None:
try:
# can probably check the output for errors but it's not well documented
output.text
except Exception as e:
logger.error(f"Error in output: {output}; {e}")
return None
return {
"output": output.text,
"input_len": output.usage_metadata.prompt_token_count,
"output_len": output.usage_metadata.candidates_token_count,
"input_text": inputs,
}
return None
def generate_batch(self, inputs=None, prompt=None, **kwargs):
if inputs is None:
inputs = [None for _ in prompt]
else:
prompt = [None for _ in inputs]
# we don't support kwargs here for now
if len(kwargs) > 0:
logger.warning("kwargs are not supported for batch generation")
# use thread_map instead of process_map since the bottleneck is the api call
outputs = thread_map(self.generate, inputs, prompt, max_workers=32)
return outputs
class TogetherModel(LLM):
def __init__(
self,
model_name,
temperature=0.9,
top_p=0.9,
max_length=32768,
generation_max_length=2048,
generation_min_length=0,
do_sample=True,
stop_newline=False,
use_chat_template=True,
system_message=None,
**kwargs,
):
super().__init__(
model_name,
temperature=temperature,
top_p=top_p,
max_length=max_length,
generation_max_length=generation_max_length,
generation_min_length=generation_min_length,
do_sample=do_sample,
stop_newline=stop_newline,
use_chat_template=use_chat_template,
system_message=system_message,
)
from transformers import AutoTokenizer
from together import Together
# default env var TOGETHER_API_KEY
self.model = Together()
self.model_name = model_name.replace("togetherapi/", "")
# you should add the mapping from the TogetherAPI model name to the Hugging Face model name to get the tokenizer
# alternatively, you can use another model with similar tokenizer if the one you are using is not open-source
name_mapping = {
"meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo": "meta-llama/Meta-Llama-3.1-405B-Instruct",
"deepseek-ai/DeepSeek-V3": "deepseek-ai/DeepSeek-V3",
"deepseek-ai/DeepSeek-R1": "deepseek-ai/DeepSeek-R1",
}
self.tokenizer = AutoTokenizer.from_pretrained(name_mapping[self.model_name])
def prepare_inputs(self, test_item, data):
buffer = 100
prompt = format_chat(data["user_template"].format(**test_item), system_message=self.system_message)
tokens = self.tokenizer.apply_chat_template(prompt, tokenize=True, add_generation_prompt=True)
input_len = len(tokens)
max_length = self.max_length
if input_len > max_length - self.generation_max_length - buffer:
truncate_length = input_len - (max_length - self.generation_max_length - buffer)
context_tokens = self.tokenizer(test_item["context"], return_offsets_mapping=True)
new_context = test_item["context"][:context_tokens["offset_mapping"][-truncate_length][0]]
test_item["context"] = new_context
prompt = format_chat(data["user_template"].format(**test_item), system_message=self.system_message)
return prompt
def generate(self, inputs=None, prompt=None, **kwargs):
if inputs is None:
inputs = format_chat(prompt, system_message=self.system_message)
# kwargs can be used to pass additional parameters to the model: max_tokens, stop, etc.
func = functools.partial(
self.model.chat.completions.create,
model=self.model_name,
messages=inputs,
max_tokens=self.generation_max_length,
temperature=self.temperature if self.do_sample else 0.0,
top_p=self.top_p,
stop=self.stops,
**kwargs,
)
output = call_api(func)
if output is not None:
if output.choices[0].message.content is None:
# sometimes the model output can get filtered but sitll return a message
return None
return {
"output": output.choices[0].message.content,
"input_len": output.usage.prompt_tokens,
"output_len": output.usage.completion_tokens,
"input_text": inputs,
}
return None
def generate_batch(self, inputs=None, prompt=None, **kwargs):
if inputs is None:
inputs = [None for _ in prompt]
else:
prompt = [None for _ in inputs]
# we don't support kwargs here for now
if len(kwargs) > 0:
logger.warning("kwargs are not supported for batch generation")
# use thread_map instead of process_map since the bottleneck is the api call
outputs = thread_map(self.generate, inputs, prompt, max_workers=32)
return outputs
def tokenize(
sample: Dict[str, Any],
data: Dict[str, Any],
tokenizer,
max_length: int,
generation_max_length: int,
use_chat_template: bool=False,
continue_final_message: bool=False,
system_message: Optional[str]="You are a helpful assistant.",
):
"""
Tokenize the input for HF-based models.
"""
if continue_final_message:
assert use_chat_template
def format_input(sample):
if use_chat_template:
chat = format_chat(
data["user_template"].format(**sample),
system_message=system_message,
)
if continue_final_message:
chat.append({"role": "assistant", "content": data['system_template'].format(**sample)})
try:
# sometimes the tokenizer doesn't support system message
prompt = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=not continue_final_message, continue_final_message=continue_final_message)
except Exception as e:
# so we exclude the system message
chat = format_chat(data["user_template"].format(**sample), system_message=None)
if continue_final_message:
chat.append({"role": "assistant", "content": data['system_template'].format(**sample)})
prompt = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=not continue_final_message, continue_final_message=continue_final_message)
tokenized_input = tokenizer([prompt], return_tensors="pt", add_special_tokens=False)
else:
prompt = data["prompt_template"].format(**sample)
tokenized_input = tokenizer([prompt], return_tensors="pt")
return tokenized_input
if "Phi3SmallTokenizer" in str(type(tokenizer)):
buffer = 64 if max_length == 131072 else 0 # there is some problem with their rotary emb implementation
else:
buffer = 0
tokenized_input = format_input(sample)
if tokenized_input.input_ids.size(1) > max_length - generation_max_length - buffer:
truncate_length = tokenized_input.input_ids.size(1) - (max_length - generation_max_length - buffer)
# handle non-fast hf tokenizers (e.g., phi-3-small)
if isinstance(tokenizer, PreTrainedTokenizer) and not tokenizer.is_fast:
context_tokens = tokenizer(sample["context"])
new_context = tokenizer.decode(context_tokens["input_ids"][:-truncate_length])
else:
context_tokens = tokenizer([sample["context"]], return_offsets_mapping=True)
new_context = sample["context"][:context_tokens["offset_mapping"][0][-truncate_length][0]]
sample["context"] = new_context
tokenized_input = format_input(sample)
return tokenized_input
class HFModel(LLM):
def __init__(
self,
model_name,
temperature=0.9,
top_p=0.9,
max_length=32768,
generation_max_length=2048,
generation_min_length=0,
do_sample=True,
stop_newline=False,
use_chat_template=False,
system_message=None,
seed=42,
**kwargs,
):
super().__init__(
model_name,
temperature=temperature,
top_p=top_p,
max_length=max_length,
generation_max_length=generation_max_length,
generation_min_length=generation_min_length,
do_sample=do_sample,
stop_newline=stop_newline,
use_chat_template=use_chat_template,
system_message=system_message,
)
set_seed(seed)
import transformers
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig
model_kwargs = {}
from pkg_resources import parse_version
if parse_version(transformers.__version__) <= parse_version("4.34.1"):
model_kwargs["use_flash_attention_2"] = True
else:
model_kwargs["attn_implementation"] = kwargs.get("attn_implementation", "flash_attention_2")
FLASH_ATTN_NOT_SUPPORTED = ["recurrentgemma", "yarn"]
if any([x in model_name.lower() for x in FLASH_ATTN_NOT_SUPPORTED]):
model_kwargs = {}
self.max_length = max_length
self.tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
if self.tokenizer.pad_token is None:
self.tokenizer.pad_token = self.tokenizer.eos_token
self.tokenizer.pad_token_id = self.tokenizer.eos_token_id
self.tokenizer.truncation_side = "left"
self.tokenizer.padding_side = "left"
config = AutoConfig.from_pretrained(model_name, trust_remote_code=True)
if "rope_theta" in kwargs and kwargs["rope_theta"] is not None:
logger.info(f"Override rope theta to {kwargs['rope_theta']}")
config.rope_theta = kwargs["rope_theta"]
self.model = AutoModelForCausalLM.from_pretrained(
model_name,
config=config,
torch_dtype=kwargs.get("torch_dtype", torch.bfloat16),
device_map="auto",
trust_remote_code=True,
**model_kwargs
)
if kwargs.get("torch_compile", True):
self.model = torch.compile(self.model)
# https://huggingface.co/docs/transformers/en/llm_optims?static-kv=basic+usage%3A+generation_config#static-kv-cache-and-torchcompile
# self.model.forward = torch.compile(self.model.forward, mode="reduce-overhead", fullgraph=True)
# use the default if possible, append if necessary
stop_token_ids = self.model.generation_config.eos_token_id
stop_token_ids = [stop_token_ids] if not isinstance(stop_token_ids, list) else stop_token_ids
if stop_newline:
stop = list(set(["\n", "Ċ", "ĊĊ", "<0x0A>"]))
stop_token_ids = list(set([self.tokenizer.convert_tokens_to_ids(stop_token) for stop_token in stop] + stop_token_ids))
if "llama" in model_name.lower():
stop_token_ids.remove(self.tokenizer.unk_token_id)
stop_token_ids = [x for x in stop_token_ids if x is not None]
self.stop_token_ids = stop_token_ids
self.device = self.model.device
self.disable_prefill = False
if "gemma" in model_name.lower():
self.disable_prefill = True
logger.warning("gemma models cannot prefill with past kvs due to cache implementation, need to change the code manually if you need to prefill")
def prepare_inputs(self, test_item, data):
return tokenize(
test_item,
data,
tokenizer=self.tokenizer,
max_length=self.max_length,
generation_max_length=self.generation_max_length,
use_chat_template=self.use_chat_template,
system_message=self.system_message,
)
@torch.no_grad()
def generate(self, inputs=None, prompt=None, **kwargs):
if inputs is None:
assert prompt is not None
if self.use_chat_template and isinstance(prompt, str):
chat = format_chat(prompt, system_message=self.system_message)
inputs = self.tokenizer.apply_chat_template(chat, tokenize=True, add_generation_prompt=True, return_tensors="pt", max_length=self.max_length-self.generation_max_length, truncation=True, padding=True)
else:
inputs = self.tokenizer([prompt], return_tensors="pt", max_length=self.max_length-self.generation_max_length, truncation=True, padding=True)
inputs = inputs.to(self.model.device)
input_len = inputs.input_ids.size(1)
if hasattr(self.model, "model") and not self.disable_prefill:
from transformers import BatchEncoding
# prefill without calculating the logits (save memory for large vocab models)
# one could also do prefilling by chunks, which would save more memory but is more complex and slower
extra = {}
if "jamba" in str(type(self.model)).lower():
from transformers.models.jamba.modeling_jamba import HybridMambaAttentionDynamicCache
cache = HybridMambaAttentionDynamicCache(self.model.config, inputs.input_ids.shape[0], self.model.dtype, device=self.model.device)
extra = {"past_key_values": cache}
prefill = self.model.model(input_ids=inputs.input_ids[..., :-1], attention_mask=inputs.attention_mask[..., :-1], **extra)
past_key_values = prefill.past_key_values
if past_key_values is None:
self.disable_prefill = True
logger.warning("past key values is None, not able to prefill with KVs, disabling...")
else:
inputs = BatchEncoding({"input_ids": inputs.input_ids, "attention_mask": inputs.attention_mask, "past_key_values": past_key_values})
outputs = self.model.generate(
**inputs,
max_new_tokens=self.generation_max_length,
min_new_tokens=self.generation_min_length,
do_sample=self.do_sample,
temperature=self.temperature,
top_p=self.top_p,
eos_token_id=self.stop_token_ids,
pad_token_id=self.tokenizer.pad_token_id,
return_dict_in_generate=True,
output_scores=False,
)
text = self.tokenizer.decode(outputs['sequences'][0, input_len:], skip_special_tokens=True)
save_prompt = self.tokenizer.decode(inputs["input_ids"][0][:500]) + " <skip> " + self.tokenizer.decode(inputs["input_ids"][0][-500:])
output_len = outputs['sequences'].size(1) - input_len
# free up some gpu memory
del inputs
del outputs
return {
"output": text,
"input_len": input_len,
"output_len": output_len,
"input_text": save_prompt,
}
def generate_batch(self, inputs=None, prompt=None, **kwargs):
# there aren't any particular optimizations that I want to do here...
# DDP is possible but won't apply to larger models
# https://huggingface.co/docs/transformers/en/llm_optims?static-kv=advanced+usage:+end-to-end+generate+compilation#static-kv-cache-and-torchcompile
return super().generate_batch(inputs=inputs, prompt=prompt, **kwargs)
class VLLMModel(LLM):
def __init__(
self,
model_name,
temperature=0.9,
top_p=0.9,
max_length=32768,
generation_max_length=2048,
generation_min_length=0,
do_sample=True,
stop_newline=False,
use_chat_template=False,
system_message=None,
seed=42,
):
super().__init__(
model_name,
temperature=temperature,
top_p=top_p,
max_length=max_length,
generation_max_length=generation_max_length,
generation_min_length=generation_min_length,
do_sample=do_sample,
stop_newline=stop_newline,
use_chat_template=use_chat_template,
system_message=system_message,
)
from vllm import LLM
# at the time of testing: note that the max model length is derived from the config file, and if max_length is larger than that length, there will be an error. it appears that vllm does not support positional extrapolation
# there are some work arounds to this, but it may give unexpected results.
self.model = LLM(
model_name,
tensor_parallel_size=torch.cuda.device_count(),
dtype="bfloat16",
trust_remote_code=True,
enforce_eager=True,
seed=seed,
#max_seq_len_to_capture=max_length, # we cannot set unless we are using a constant max length for the run
max_model_len=max_length,
)
self.tokenizer = self.model.get_tokenizer()
if self.tokenizer.pad_token is None:
self.tokenizer.pad_token = self.tokenizer.eos_token
self.tokenizer.pad_token_id = self.tokenizer.eos_token_id
def prepare_inputs(self, test_item, data):
return tokenize(
test_item,
data,
tokenizer=self.tokenizer,
max_length=self.max_length,
generation_max_length=self.generation_max_length,