-
Notifications
You must be signed in to change notification settings - Fork 10
/
model.py
61 lines (46 loc) · 1.67 KB
/
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
import sys
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModelForCausalLM
from config import BASE_MODELS
def get_device():
if torch.cuda.is_available():
device = "cuda"
else:
device = "cpu"
return device
def load_tokenizer_and_model(model_name, base_model=None, device=None):
if base_model is None:
if model_name in BASE_MODELS:
base_model = BASE_MODELS[model_name]
assert base_model is not None, "Please assign the corresponding base model to the argument 'base_model'."
tokenizer = AutoTokenizer.from_pretrained(base_model)
tokenizer.padding_side = 'left'
tokenizer.pad_token = '<pad>'
tokenizer.sep_token = '<unk>'
tokenizer.cls_token = '<unk>'
tokenizer.mask_token = '<unk>'
if device is None:
device = get_device()
if device == "cuda":
model = AutoModelForCausalLM.from_pretrained(
base_model,
torch_dtype=torch.bfloat16,
device_map="auto",
)
model = PeftModelForCausalLM.from_pretrained(
model,
model_name,
torch_dtype=torch.bfloat16,
)
else:
raise NotImplementedError("No implementation for loading model on CPU yet.")
model = model.merge_and_unload()
# unwind broken decapoda-research config
model.config.pad_token_id = tokenizer.pad_token_id
model.config.bos_token_id = tokenizer.bos_token_id
model.config.eos_token_id = tokenizer.eos_token_id
model.eval()
if torch.__version__ >= "2" and sys.platform != "win32":
model = torch.compile(model)
return tokenizer, model