前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >魔搭社区LLM模型部署实践, 以ChatGLM3为例(二)

魔搭社区LLM模型部署实践, 以ChatGLM3为例(二)

作者头像
Maynor
发布2023-11-12 09:22:08
7890
发布2023-11-12 09:22:08
举报

二 、多端部署-以ChatGLM3+个人Mac电脑为例

魔搭社区和Xinference合作, 提供了模型GGML的部署方式, 以ChatGLM3为例。

img
img

ChatGLM3使用的模型为GGML格式, 模型链接:

https://modelscope.cn/models/Xorbits/chatglm3-ggml/summary

使用方式:

首先在mac上预装Xinference:

pip install x inference[ggml]>=0.4.3

然后本地开启Xinference的实例:

x inference -p 9997

运行如下Python代码, 验证模型推理效果

代码语言:javascript
复制
from xinference.client import Client
client = Client(“http://localhost:9997”)
model_u id = client.launch_model(
model_name=“chatglm3”,
model_format=“ggmlv3”,
model_size_in_billions=6,
quantization=“q4_0”,
)
model = client.get_model(model_u id)
chat_history = []
prompt = "最大的动物是什么? "
model.chat(
prompt,
chat_history,
generate_config={“max_tokens”: 1024}
)

以ChatGLM3为例, 在个人Mac电脑上load模型到完成推理验证, 仅需要10s:

Mac电脑配置:

img
img

推理示例:

img
img

三 、定制化模型部署 - 微调后命令行部署

结合魔搭微调框架swift, 可以实现定制化模型部署。

目前swift支持VLLM框架, chatglm.cpp ,Xinference等推理框架, 具体可以参考文档:

https://github.com/modelscope/swift/blob/main/docs/source/GetStarted/Deployment.md

本文以ChatGLM3模型+chatglm.cpp为例:

该推理优化框架支持:

ChatGLM系列模型

BaiChuan系列模型

CodeGeeX系列模型

chatglm.cpp的github地址是:https://github.com/li-plus/chatglm.cpp

首先初始化对应repo:

代码语言:javascript
复制
git clone --recursive https://g it hub.com/li-plus/chatglm.cpp.g it && cd

chatglm.cpp

python3 -m pip install torch tabulate tqdm transformers accelerate

sentencepiece

cmake -B build

cmake --build build -j --config Release

如果SWIFT训练的是LoRA模型, 需要将LoRA weights合并到原始模型中去:

# 先将文件夹cd到swift根目录中

代码语言:javascript
复制
python tools/merge_lora_weights_to_model.py --model_id_or_path
/dir/to/your/base/model --model_revision master --ck pt_dir
/dir/to/your/lora/model

合并后的模型会输出到 {ckpt_dir}-merged 文件夹中。

之后将上述合并后的 {ckpt_dir}-merged 的模型weights转为cpp支持的bin文件:

# 先将文件夹cd到chatglm.cpp根目录中

python3 chatglm_cpp/convert.py -i {ck pt_dir}-merged -t q4_0 -o chatglm-ggml.bin

chatglm.cpp支持以各种精度转换模型 ,详情请参考: https://github.com/li-

plus/chatglm.cpp#getting-started

之后就可以拉起模型推理:

代码语言:javascript
复制
./build/bin/main -m chatglm-ggml.bin -i
# 以下对话为使用agent数据集训练后的效果
# Prompt > how are you?
# ChatGLM3 > < |startofthink|>```JSON
# {“api_name”: “greeting”, “apimongo_instance”: "ddb1e34-0406-42a3-
a547a220a2", “parameters”: {“text”: “how are # you?”}}}
# ```< |endofthink|>
#
# I 'm an AI assistant and I can only respond to text input. I don 't have the
ability to respond to audio or # video input.
之后启动xinference:
x inference -p 9997

在浏览器界面上选择Register Model选项卡, 添加chatglm.cpp章节中转换成功的ggml模 型:

img
img

注意:

● 模型能力选择Chat

之后再Launch Model中搜索刚刚创建的模型名称, 点击火箭标识运行即可使用。

调用可以使用如下代码:

代码语言:javascript
复制
from xinference.client import Client
client = Client(“http://localhost:9997”)
model_u id = client.launch_model(model_name=“custom-chatglm”)
model = client.get_model(model_u id)
chat_history = []
prompt = “What is the largest animal?”
model.chat(
prompt,
chat_history,
generate_config={“max_tokens”: 1024}
)
# { 'id ': 'chatcmpl-df3c2c28-f8bc-4e79-9c99-2ae3950fd459 ', 'object ':
'chat.completion ', 'created ': 1699367362, 'model ': '021c2b74-7d7a-11ee-b1aa-
ead073d837c1 ', 'choices ': [{ 'index ': 0, 'message ': { 'role ': 'assistant ',
'content ': "According to records kept by the Guinness World Records, the
largest animal in the world is the Blue Whale, specifically, the Right and
Left Whales, which were both caught off the coast of Newfoundland. The two
whales measured a length of 105.63 meters, or approximately 346 feet long, and
had a corresponding body weight of 203,980 pounds, or approximately 101 tons.
It 's important to note that this was an extremely rare event and the whales that size don 't commonly occur."}, 'finish_reason ': None}], 'usage ':
{ 'prompt_tokens ': -1, 'completion_tokens ': -1, 'total_tokens ': -1}}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-11-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档