清华开源语言大模型ChatGLM3部署实战


ChatGLM3 是智谱AI和清华大学 KEG 实验室联合发布的新一代对话预训练模型。
项目库地址:https://github.com/THUDM/ChatGLM3

安装环境

建议使用虚拟环境

git clone https://github.com/THUDM/ChatGLM3
cd ChatGLM3
pip install -r requirements.txt

其中 transformers 库版本推荐为 4.30.2,torch 推荐使用 2.0 及以上的版本,以获得最佳的推理性能。

下载模型文件

git lfs install
git clone https://www.modelscope.cn/ZhipuAI/chatglm3-6b.git

需等待较长时间
在这里插入图片描述

测试是否安装成功

推理时将THUDM/chatglm3-6b改为自己下载模型的路径

gpu推理

推理需13g以上显存

from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm3-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/chatglm3-6b", trust_remote_code=True, device='cuda')
model = model.eval()
response, history = model.chat(tokenizer, "你好", history=[])
print(response)
response, history = model.chat(tokenizer, "晚上睡不着应该怎么办", history=history)
print(response)

cpu推理

推理需32g以上内存

from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm3-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/chatglm3-6b", trust_remote_code=True,.float()
)
model = model.eval()
response, history = model.chat(tokenizer, "你好", history=[])
print(response)
response, history = model.chat(tokenizer, "晚上睡不着应该怎么办", history=history)
print(response)

量化推理

from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm3-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/chatglm3-6b",trust_remote_code=True).quantize(4).cuda()
model = model.eval()
response, history = model.chat(tokenizer, "你好", history=[])
print(response)
response, history = model.chat(tokenizer, "晚上睡不着应该怎么办", history=history)
print(response)

如果遇到什么问题欢迎评论区交流
群内交流更多技术
130856474

猜你喜欢

转载自blog.csdn.net/Silver__Wolf/article/details/134247535