-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
171 lines (158 loc) · 5.64 KB
/
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
'''
load models and tokenizers from locals; generation configs
'''
import os
import re
import torch
from transformers import (
BertTokenizer,
BertLMHeadModel,
GPT2Tokenizer,
GPT2LMHeadModel,
GPTNeoForCausalLM,
LlamaTokenizer,
LlamaForCausalLM,
AutoTokenizer,
AutoModelForCausalLM
)
# locate model from local path
model_dir = "/root/autodl-tmp/zhaoyi/huggingface_models" # change the path to yours
def model_name2path(model_name:str)->str:
if model_name == "gpt2-xl":
return model_dir + "/gpt2-xl"
if model_name == "gpt2-medium":
return model_dir + "/gpt2-medium"
elif model_name == "gpt-j-6b":
return model_dir + "/gpt-j-6b"
elif model_name == "openalpaca-3b":
return model_dir + "/openalpaca3b"
elif model_name == "llama2-7b":
return model_dir + "/llama2-7b-hf"
elif model_name == "llama2-13b":
return model_dir + "/llama2-13b-hf"
elif model_name == "redpajama-7b":
return model_dir + "/RedPajama-INCITE-7B-Instruct"
elif model_name == "qwen-7b":
return model_dir + "/qwen-7b"
def get_lm_type(model_name:str)->str:
if "gpt" in model_name:
# GPT2-small,base,large,xl; GPT-J-6B
lm_type = "gpt"
elif "llama" in model_name or "alpaca" in model_name:
# OpenAlpaca-3B, LLaMA-2-7B
lm_type = "llama"
elif "qwen" in model_name:
lm_type = "qwen"
else:
raise Exception("model:{} is currently not covered in the model list.".format(model_name))
return lm_type
def make_inputs(tokenizer, prompts, device="cuda"):
token_lists = [tokenizer.encode(p) for p in prompts]
maxlen = max(len(t) for t in token_lists)
if "[PAD]" in tokenizer.all_special_tokens:
pad_id = tokenizer.all_special_ids[tokenizer.all_special_tokens.index("[PAD]")]
else:
pad_id = 0
input_ids = [[pad_id] * (maxlen - len(t)) + t for t in token_lists]
# position_ids = [[0] * (maxlen - len(t)) + list(range(len(t))) for t in token_lists]
attention_mask = [[0] * (maxlen - len(t)) + [1] * len(t) for t in token_lists]
return dict(
input_ids=torch.tensor(input_ids).to(device),
# position_ids=torch.tensor(position_ids).to(device),
attention_mask=torch.tensor(attention_mask).to(device),
)
class ModelAndTokenizer:
"""
An object to hold on to (or automatically download and hold)
a GPT-style language model and tokenizer. Counts the number
of layers.
"""
def __init__(
self,
model_name=None,
model_type=None,
model=None,
tokenizer=None,
low_cpu_mem_usage=False,
torch_dtype=None,
):
if tokenizer is None:
assert model_name is not None
assert model_type is not None
if model_type == "llama":
if "llama2" in model_name:
if "13b" in model_name:
tokenizer = AutoTokenizer.from_pretrained(model_name)
else:
tokenizer = AutoTokenizer.from_pretrained(model_name)
else:
tokenizer = LlamaTokenizer.from_pretrained(model_name)
elif model_type == "gpt":
tokenizer = AutoTokenizer.from_pretrained(model_name)
elif model_type == "qwen":
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
else:
raise Exception("model type:{} is currently not covered in the model type list.".format(model_type))
if model is None:
assert model_name is not None
assert model_type is not None
if model_type == "llama":
model = LlamaForCausalLM.from_pretrained(model_name,torch_dtype=torch_dtype)
elif model_type == "gpt":
model = AutoModelForCausalLM.from_pretrained(
model_name,
low_cpu_mem_usage=low_cpu_mem_usage,
torch_dtype=torch_dtype
)
elif model_type == "qwen":
model = AutoModelForCausalLM.from_pretrained(
model_name,
low_cpu_mem_usage=low_cpu_mem_usage,
torch_dtype=torch_dtype,
trust_remote_code=True
)
else:
raise Exception("model type:{} is currently not covered in the model type list.".format(model_type))
# nethook.set_requires_grad(False, model)
model.eval().cuda()
self.tokenizer = tokenizer
self.model = model
self.model_type = model_type
self.layer_names = [
n
for n, m in model.named_modules()
if (re.match(r"^(transformer|gpt_neox|model)\.(h|layers)\.\d+$", n))
]
# for n, m in model.named_modules():
# print(n)
# for n in self.layer_names:
# print(n)
# raise Exception("debug")
self.num_layers = len(self.layer_names)
def __repr__(self):
return (
f"ModelAndTokenizer(model: {type(self.model).__name__} "
f"[{self.num_layers} layers], "
f"tokenizer: {type(self.tokenizer).__name__})"
)
GEN_CONFIGS = dict()
GEN_CONFIGS["llama2-7b"]={
"bos_token_id": 1,
"do_sample": True,
"eos_token_id": 2,
"pad_token_id": 0,
"temperature": 0.6,
"max_length": 50,
"top_p": 0.9,
"transformers_version": "4.31.0.dev0"
}
GEN_CONFIGS["llama2-13b"]={
"bos_token_id": 1,
"do_sample": True,
"eos_token_id": 2,
"pad_token_id": 0,
"temperature": 0.6,
"max_length": 50,
"top_p": 0.9,
"transformers_version": "4.32.0.dev0"
}