Langchain-Chatchat/models/chatglm_llm.py

195 lines
7.1 KiB
Python
Raw Normal View History

2023-04-15 14:43:12 +08:00
import json
2023-03-31 20:09:40 +08:00
from langchain.llms.base import LLM
2023-05-03 11:29:59 +08:00
from typing import List, Dict, Optional
2023-04-15 14:43:12 +08:00
from transformers import AutoTokenizer, AutoModel, AutoConfig
import torch
from configs.model_config import *
from utils import torch_gc
DEVICE_ = LLM_DEVICE
2023-04-11 21:55:42 +08:00
DEVICE_ID = "0" if torch.cuda.is_available() else None
DEVICE = f"{DEVICE_}:{DEVICE_ID}" if DEVICE_ID else DEVICE_
2023-03-31 20:09:40 +08:00
def auto_configure_device_map(num_gpus: int) -> Dict[str, int]:
# transformer.word_embeddings 占用1层
# transformer.final_layernorm 和 lm_head 占用1层
# transformer.layers 占用 28 层
# 总共30层分配到num_gpus张卡上
num_trans_layers = 28
per_gpu_layers = 30 / num_gpus
# bugfix: 在linux中调用torch.embedding传入的weight,input不在同一device上,导致RuntimeError
# windows下 model.device 会被设置成 transformer.word_embeddings.device
# linux下 model.device 会被设置成 lm_head.device
# 在调用chat或者stream_chat时,input_ids会被放到model.device上
# 如果transformer.word_embeddings.device和model.device不同,则会导致RuntimeError
# 因此这里将transformer.word_embeddings,transformer.final_layernorm,lm_head都放到第一张卡上
device_map = {'transformer.word_embeddings': 0,
'transformer.final_layernorm': 0, 'lm_head': 0}
used = 2
gpu_target = 0
for i in range(num_trans_layers):
if used >= per_gpu_layers:
gpu_target += 1
used = 0
assert gpu_target < num_gpus
device_map[f'transformer.layers.{i}'] = gpu_target
used += 1
return device_map
2023-03-31 20:09:40 +08:00
class ChatGLM(LLM):
max_token: int = 10000
2023-04-11 21:55:42 +08:00
temperature: float = 0.01
2023-03-31 20:09:40 +08:00
top_p = 0.9
2023-04-26 22:29:20 +08:00
# history = []
tokenizer: object = None
model: object = None
history_len: int = 10
2023-03-31 20:09:40 +08:00
def __init__(self):
super().__init__()
@property
def _llm_type(self) -> str:
return "ChatGLM"
def _call(self,
prompt: str,
2023-04-26 22:29:20 +08:00
history: List[List[str]] = [],
streaming: bool = STREAMING): # -> Tuple[str, List[List[str]]]:
if streaming:
2023-04-26 23:19:11 +08:00
for inum, (stream_resp, _) in enumerate(self.model.stream_chat(
2023-04-26 22:29:20 +08:00
self.tokenizer,
prompt,
2023-04-26 23:19:11 +08:00
history=history[-self.history_len:-1] if self.history_len > 0 else [],
2023-04-26 22:29:20 +08:00
max_length=self.max_token,
temperature=self.temperature,
2023-04-26 23:19:11 +08:00
)):
2023-05-04 20:48:36 +08:00
torch_gc()
2023-04-26 23:19:11 +08:00
if inum == 0:
history += [[prompt, stream_resp]]
else:
history[-1] = [prompt, stream_resp]
2023-04-26 22:29:20 +08:00
yield stream_resp, history
2023-05-04 20:48:36 +08:00
torch_gc()
2023-04-23 22:13:20 +08:00
else:
response, _ = self.model.chat(
self.tokenizer,
prompt,
history=history[-self.history_len:] if self.history_len > 0 else [],
max_length=self.max_token,
temperature=self.temperature,
2023-04-23 22:13:20 +08:00
)
2023-05-04 20:48:36 +08:00
torch_gc()
history += [[prompt, response]]
yield response, history
2023-05-04 20:48:36 +08:00
torch_gc()
2023-04-26 22:29:20 +08:00
# def chat(self,
# prompt: str) -> str:
# response, _ = self.model.chat(
# self.tokenizer,
# prompt,
# history=self.history[-self.history_len:] if self.history_len > 0 else [],
# max_length=self.max_token,
# temperature=self.temperature,
# )
# torch_gc()
# self.history = self.history + [[None, response]]
# return response
2023-04-22 12:20:08 +08:00
2023-04-13 23:01:52 +08:00
def load_model(self,
model_name_or_path: str = "THUDM/chatglm-6b",
llm_device=LLM_DEVICE,
2023-04-19 23:02:47 +08:00
use_ptuning_v2=False,
device_map: Optional[Dict[str, int]] = None,
**kwargs):
self.tokenizer = AutoTokenizer.from_pretrained(
model_name_or_path,
trust_remote_code=True
)
2023-04-15 14:43:12 +08:00
model_config = AutoConfig.from_pretrained(model_name_or_path, trust_remote_code=True)
if use_ptuning_v2:
try:
prefix_encoder_file = open('ptuning-v2/config.json', 'r')
prefix_encoder_config = json.loads(prefix_encoder_file.read())
prefix_encoder_file.close()
model_config.pre_seq_len = prefix_encoder_config['pre_seq_len']
model_config.prefix_projection = prefix_encoder_config['prefix_projection']
2023-04-22 21:20:18 +08:00
except Exception as e:
print(e)
2023-04-15 14:43:12 +08:00
print("加载PrefixEncoder config.json失败")
2023-04-13 23:01:52 +08:00
if torch.cuda.is_available() and llm_device.lower().startswith("cuda"):
# 根据当前设备GPU数量决定是否进行多卡部署
num_gpus = torch.cuda.device_count()
if num_gpus < 2 and device_map is None:
self.model = (
AutoModel.from_pretrained(
2023-04-19 23:02:47 +08:00
model_name_or_path,
config=model_config,
2023-04-22 12:20:08 +08:00
trust_remote_code=True,
**kwargs)
.half()
.cuda()
)
else:
from accelerate import dispatch_model
2023-04-26 22:29:20 +08:00
model = (
AutoModel.from_pretrained(
model_name_or_path,
trust_remote_code=True,
config=model_config,
**kwargs)
.half())
# 可传入device_map自定义每张卡的部署情况
if device_map is None:
device_map = auto_configure_device_map(num_gpus)
self.model = dispatch_model(model, device_map=device_map)
2023-04-11 21:55:42 +08:00
else:
self.model = (
AutoModel.from_pretrained(
model_name_or_path,
2023-04-15 14:43:12 +08:00
config=model_config,
2023-04-26 22:29:20 +08:00
trust_remote_code=True,
**kwargs)
2023-04-11 21:55:42 +08:00
.float()
2023-04-13 23:01:52 +08:00
.to(llm_device)
2023-04-11 21:55:42 +08:00
)
2023-04-15 14:43:12 +08:00
if use_ptuning_v2:
try:
prefix_state_dict = torch.load('ptuning-v2/pytorch_model.bin')
new_prefix_state_dict = {}
for k, v in prefix_state_dict.items():
if k.startswith("transformer.prefix_encoder."):
new_prefix_state_dict[k[len("transformer.prefix_encoder."):]] = v
self.model.transformer.prefix_encoder.load_state_dict(new_prefix_state_dict)
self.model.transformer.prefix_encoder.float()
2023-04-22 12:20:08 +08:00
except Exception as e:
print(e)
2023-04-15 14:43:12 +08:00
print("加载PrefixEncoder模型参数失败")
2023-04-11 21:55:42 +08:00
self.model = self.model.eval()
if __name__ == "__main__":
llm = ChatGLM()
llm.load_model(model_name_or_path=llm_model_dict[LLM_MODEL],
llm_device=LLM_DEVICE, )
last_print_len=0
for resp, history in llm._call("你好", streaming=True):
print(resp[last_print_len:], end="", flush=True)
last_print_len = len(resp)
for resp, history in llm._call("你好", streaming=False):
print(resp)
pass