-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
363 lines (326 loc) · 13.8 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
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
import os, torch, time
import ahocorasick
from peft import PeftModel
from loguru import logger
from transformers import (
AutoModel,
AutoModelForCausalLM,
AutoTokenizer,
GenerationConfig,
)
from param_model import ChatMessage
from typing import List, Dict
from transformers_stream_generator.main import NewGenerationMixin
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
MODEL_BASE_DIR = os.path.join(BASE_DIR, "llms")
def auto_download(model_type: str, revision: str = None, size: str = None, repair_name: str = None):
def wait_for_rename(old_name, new_name, retries=10, delay=2):
if os.path.exists(old_name):
os.rename(old_name, new_name)
for _ in range(retries):
if os.path.exists(new_name):
return True
time.sleep(delay)
return False
else:
return False
from modelscope import snapshot_download
cache_dir = f"{MODEL_BASE_DIR}/{model_type}"
if model_type == "chatglm":
model_id = "ZhipuAI/chatglm3-6b"
elif model_type == "baichuan":
model_id = f"baichuan-inc/Baichuan2-{size}-Chat"
elif model_type == "qwen":
model_id = f"qwen/Qwen1.5-{size}-Chat"
elif model_type == "llama":
model_id = f"LLM-Research/Meta-Llama-3-{size}-Instruct"
elif model_type == "embedding":
model_id = "AI-ModelScope/bge-large-zh-v1.5"
elif model_type == "reranker":
model_id = "quietnight/bge-reranker-large"
else:
raise ValueError(f'Unsupported model type {model_type} yet.')
out_dir = snapshot_download(model_id, revision=revision, cache_dir=cache_dir)
if repair_name is not None:
from pathlib import Path
model_path = Path(out_dir)
repair_path = Path(repair_name)
if not model_path.exists():
raise ValueError(f'Model path {model_path} is not exists.')
wait_for_rename(model_path.absolute(), os.path.join(model_path.parent.absolute(), repair_path.name))
if repair_path.parent.name:
parent_repair = Path(os.path.join(cache_dir, repair_path.parent.name))
if not os.path.exists(parent_repair):
wait_for_rename(model_path.parent.absolute(), parent_repair)
def get_bge_large_zh():
model_type = "embedding"
model_name_or_path = f"{model_type}/BAAI/bge-large-zh-v1.5"
if not os.path.exists(os.path.join(MODEL_BASE_DIR, model_name_or_path)):
auto_download(model_type, repair_name="BAAI/bge-large-zh-v1.5")
logger.info(f'正在加载模型>>>>{model_name_or_path}\n')
from sentence_transformers import SentenceTransformer
model = SentenceTransformer(os.path.join(MODEL_BASE_DIR, model_name_or_path))
model = model.eval()
tokenizer = AutoTokenizer.from_pretrained(os.path.join(MODEL_BASE_DIR, model_name_or_path))
return {
"model_type": model_type,
'model': model,
'tokenizer': tokenizer,
}
def get_bge_reranker_large():
model_type = "reranker"
model_name_or_path = f"{model_type}/BAAI/bge-reranker-large"
if not os.path.exists(os.path.join(MODEL_BASE_DIR, model_name_or_path)):
auto_download(model_type, repair_name="BAAI/bge-reranker-large")
logger.info(f'正在加载模型>>>>{model_name_or_path}\n')
from sentence_transformers import CrossEncoder
model = CrossEncoder(os.path.join(MODEL_BASE_DIR, model_name_or_path))
return {
"model_type": model_type,
'model': model,
}
def get_llama3(size="8B"):
model_type = 'llama'
assert size in ['8B', '70B']
model_name_or_path = f"{model_type}/MetaAI/Meta-Llama-3-{size}-Instruct"
if not os.path.exists(os.path.join(MODEL_BASE_DIR, model_name_or_path, 'config.json')):
auto_download(model_type, size=size, repair_name=f'MetaAI/Meta-Llama-3-{size}-Instruct')
SFT_MODEL_DIR = os.path.join(MODEL_BASE_DIR, f"{model_type}/ft_models")
SFT_MODELS = ['baseline']
logger.info(f'正在加载模型>>>>{model_name_or_path}\n')
if torch.cuda.is_available():
model = AutoModelForCausalLM.from_pretrained(
os.path.join(MODEL_BASE_DIR, model_name_or_path),
torch_dtype=torch.bfloat16,
device_map="auto"
)
else:
model = AutoModelForCausalLM.from_pretrained(
os.path.join(MODEL_BASE_DIR, model_name_or_path))
tokenizer = AutoTokenizer.from_pretrained(
os.path.join(MODEL_BASE_DIR, model_name_or_path))
generation_config = GenerationConfig.from_pretrained(
os.path.join(MODEL_BASE_DIR, model_name_or_path))
generation_config.max_length = 8192
model.generation_config = generation_config
model.__class__.generate_stream = NewGenerationMixin.generate
model.__class__.sample_stream = NewGenerationMixin.sample_stream
if os.path.exists(SFT_MODEL_DIR):
logger.info(f"==== 正在加载sft模型 ====\n")
sft_models = os.listdir(SFT_MODEL_DIR)
for model_name in sft_models:
model_id = os.path.join(SFT_MODEL_DIR, model_name)
try:
if isinstance(model, PeftModel):
model.load_adapter(model_id, adapter_name=model_name)
else:
model = PeftModel.from_pretrained(model, model_id, adapter_name=model_name)
except Exception as e:
logger.error(f"---> 加载 {model_id} 失败:{str(e)}\n")
else:
SFT_MODELS.append(model_name)
logger.info(f"---> 加载 {model_id} 成功\n")
model = model.eval()
logger.info(f"==== sft_models {SFT_MODELS} ====\n")
return {
"model_type": model_type,
'model': model,
'tokenizer': tokenizer,
'adapters': SFT_MODELS
}
def get_chatglm3():
model_type = "chatglm"
model_name_or_path = f"{model_type}/ZhipuAI/chatglm3-6b"
if not os.path.exists(os.path.join(MODEL_BASE_DIR, model_name_or_path, 'config.json')):
auto_download(model_type, revision="v1.0.2")
SFT_MODEL_DIR = os.path.join(MODEL_BASE_DIR, f"{model_type}/ft_models")
SFT_MODELS = ['baseline']
logger.info(f'正在加载模型>>>>{model_name_or_path}\n')
if torch.cuda.is_available():
model = AutoModel.from_pretrained(
os.path.join(MODEL_BASE_DIR, model_name_or_path),
trust_remote_code=True,
torch_dtype=torch.bfloat16,
device_map="auto"
)
else:
model = AutoModel.from_pretrained(
os.path.join(MODEL_BASE_DIR, model_name_or_path),
trust_remote_code=True
).float()
tokenizer = AutoTokenizer.from_pretrained(
os.path.join(MODEL_BASE_DIR, model_name_or_path),
trust_remote_code=True)
model.generation_config = GenerationConfig(
max_length=8192,
num_beams=1,
do_sample=True,
top_p=0.8,
temperature=0.8
)
model.__class__.generate_stream = NewGenerationMixin.generate
model.__class__.sample_stream = NewGenerationMixin.sample_stream
if os.path.exists(SFT_MODEL_DIR):
logger.info(f"==== 正在加载sft模型 ====\n")
sft_models = os.listdir(SFT_MODEL_DIR)
for model_name in sft_models:
model_id = os.path.join(SFT_MODEL_DIR, model_name)
try:
if isinstance(model, PeftModel):
model.load_adapter(model_id, adapter_name=model_name)
else:
model = PeftModel.from_pretrained(model, model_id, adapter_name=model_name)
except Exception as e:
logger.error(f"---> 加载 {model_id} 失败:{str(e)}\n")
else:
SFT_MODELS.append(model_name)
logger.info(f"---> 加载 {model_id} 成功\n")
model = model.eval()
logger.info(f"==== sft_models {SFT_MODELS} ====\n")
return {
"model_type": model_type,
"model": model,
'tokenizer': tokenizer,
'adapters': SFT_MODELS
}
def get_baichuan2(size='7B'):
'''加载百川基座模型'''
model_type = 'baichuan'
assert size in ['7B', '14B']
model_name_or_path = f"{model_type}/baichuan-inc/Baichuan2-{size}-Chat"
if not os.path.exists(os.path.join(MODEL_BASE_DIR, model_name_or_path, 'config.json')):
auto_download(model_type, revision="v2.0.1", size=size)
SFT_MODEL_DIR = os.path.join(MODEL_BASE_DIR, f"{model_type}/ft_models")
SFT_MODELS = ['baseline']
logger.info(f'正在加载模型>>>>{model_name_or_path}\n')
if torch.cuda.is_available():
model = AutoModelForCausalLM.from_pretrained(
os.path.join(MODEL_BASE_DIR, model_name_or_path),
trust_remote_code=True,
torch_dtype=torch.bfloat16,
device_map="auto"
)
else:
model = AutoModelForCausalLM.from_pretrained(
os.path.join(MODEL_BASE_DIR, model_name_or_path),
trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained(
os.path.join(MODEL_BASE_DIR, model_name_or_path),
trust_remote_code=True)
generation_config = GenerationConfig.from_pretrained(
os.path.join(MODEL_BASE_DIR, model_name_or_path),
trust_remote_code=True)
model.generation_config = generation_config
if os.path.exists(SFT_MODEL_DIR):
logger.info(f"==== 正在加载sft模型 ====\n")
sft_models = os.listdir(SFT_MODEL_DIR)
for model_name in sft_models:
model_id = os.path.join(SFT_MODEL_DIR, model_name)
try:
if isinstance(model, PeftModel):
model.load_adapter(model_id, adapter_name=model_name)
else:
model = PeftModel.from_pretrained(model, model_id, adapter_name=model_name)
except Exception as e:
logger.error(f"---> 加载 {model_id} 失败:{str(e)}\n")
else:
SFT_MODELS.append(model_name)
logger.info(f"---> 加载 {model_id} 成功\n")
model = model.eval()
logger.info(f"==== sft_models {SFT_MODELS} ====\n")
return {
"model_type": model_type,
'model': model,
'tokenizer': tokenizer,
'adapters': SFT_MODELS
}
def get_qwen2(size='7B'):
'''加载千问大模型'''
model_type = "qwen"
assert size in ['0.5B', '1.8B', '4B', '7B', '14B', '32B', '72B', '110B']
model_name_or_path = f"{model_type}/qwen/Qwen1.5-{size}-Chat"
if not os.path.exists(os.path.join(MODEL_BASE_DIR, model_name_or_path, 'config.json')):
auto_download(model_type, size=size, repair_name=f"qwen/Qwen1.5-{size}-Chat")
SFT_MODEL_DIR = os.path.join(MODEL_BASE_DIR, f"{model_type}/ft_models")
SFT_MODELS = ['baseline']
logger.info(f'正在加载模型>>>>{model_name_or_path}\n')
if torch.cuda.is_available():
model = AutoModelForCausalLM.from_pretrained(
os.path.join(MODEL_BASE_DIR, model_name_or_path),
torch_dtype=torch.bfloat16,
device_map="auto"
)
else:
model = AutoModelForCausalLM.from_pretrained(
os.path.join(MODEL_BASE_DIR, model_name_or_path))
tokenizer = AutoTokenizer.from_pretrained(
os.path.join(MODEL_BASE_DIR, model_name_or_path))
generation_config = GenerationConfig.from_pretrained(
os.path.join(MODEL_BASE_DIR, model_name_or_path))
generation_config.max_length = 8192
model.generation_config = generation_config
model.__class__.generate_stream = NewGenerationMixin.generate
model.__class__.sample_stream = NewGenerationMixin.sample_stream
if os.path.exists(SFT_MODEL_DIR):
logger.info(f"==== 正在加载sft模型 ====\n")
sft_models = os.listdir(SFT_MODEL_DIR)
for model_name in sft_models:
model_id = os.path.join(SFT_MODEL_DIR, model_name)
try:
if isinstance(model, PeftModel):
model.load_adapter(model_id, adapter_name=model_name)
else:
model = PeftModel.from_pretrained(model, model_id, adapter_name=model_name)
except Exception as e:
logger.error(f"---> 加载 {model_id} 失败:{str(e)}\n")
else:
SFT_MODELS.append(model_name)
logger.info(f"---> 加载 {model_id} 成功\n")
model = model.eval()
logger.info(f"==== sft_models {SFT_MODELS} ====\n")
return {
"model_type": model_type,
'model': model,
'tokenizer': tokenizer,
'adapters': SFT_MODELS
}
def process_msg(messages: List[ChatMessage]):
_messages = messages
messages = []
for i, m in enumerate(_messages):
if i != 0 and m.role == "system":
continue
# if i == 0 and m.role == "system":
# messages.append({
# 'role': m.role,
# 'content': f'\n你是小花,一个由小花AI训练的大语言模型。\n知识截至日期:2023-04\n当前型号:Baichuan2-13B-Chat\n当前时间:{datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")}\n\n'
# })
# messages.append({
# 'role': m.role,
# 'content': ''
# })
# else:
messages.append({
"role": m.role,
"content": m.content
})
return messages
def load_completions(file) -> ahocorasick.Automaton:
# 加载数据到AC自动机
A = ahocorasick.Automaton()
with open(file, 'r', encoding='utf8') as f:
lines = f.readlines()
for line in lines:
line = line.strip()
a, b = line.split()
A.add_word(a, f'{a}&&{b}')
A.make_automaton()
return A
def enhance_text(A: ahocorasick.Automaton, text: str) -> str:
for _, words in A.iter(text):
src, dst = words.split("&&")
text = text.replace(src, dst)
return text
if __name__ == '__main__':
A = load_completions("completions.txt")
enhance_text(A, "你擅长什么呢?当冲对技术的要求比较高")