1,項目地址
https://github.com/li-plus/chatglm.cpp.git
這個項目和llama.cpp 項目類似,使用C++ 去運行模型的。
項目使用了 ggml 這個核心模塊,去運行的。
可以支持在 cpu 上面跑模型。
ChatGLM2-6B 是開源中英雙語對話模型 ChatGLM-6B 的第二代版本,在保留了初代模型對話流暢、部署門檻較低等眾多優(yōu)秀特性的基礎之上,ChatGLM2-6B 引入了如下新特性:
更強大的性能: 基于 ChatGLM 初代模型的開發(fā)經(jīng)驗,我們?nèi)嫔壛?ChatGLM2-6B 的基座模型。ChatGLM2-6B 使用了 GLM 的混合目標函數(shù),經(jīng)過了 1.4T 中英標識符的預訓練與人類偏好對齊訓練,評測結果顯示,相比于初代模型,ChatGLM2-6B 在 MMLU(+23%)、CEval(+33%)、GSM8K(+571%) 、BBH(+60%)等數(shù)據(jù)集上的性能取得了大幅度的提升,在同尺寸開源模型中具有較強的競爭力。
更長的上下文: 基于 FlashAttention 技術,我們將基座模型的上下文長度(Context Length)由 ChatGLM-6B 的 2K 擴展到了 32K,并在對話階段使用 8K 的上下文長度訓練,允許更多輪次的對話。但當前版本的 ChatGLM2-6B 對單輪超長文檔的理解能力有限,我們會在后續(xù)迭代升級中著重進行優(yōu)化。
更高效的推理: 基于 Multi-Query Attention 技術,ChatGLM2-6B 有更高效的推理速度和更低的顯存占用:在官方的模型實現(xiàn)下,推理速度相比初代提升了 42%,INT4 量化下,6G 顯存支持的對話長度由 1K 提升到了 8K。
2,準備環(huán)境,使用python的docker進行安裝
下載代碼:
git clone --recursive https://github.com/li-plus/chatglm.cpp.git
要是超時可以分開下載:
git clone https://github.com/li-plus/chatglm.cpp.git
cd chatglm.cpp/third_party
git clone https://github.com/ggerganov/ggml.git
git clone https://github.com/pybind/pybind11.git
git clone https://github.com/google/sentencepiece.git
要是網(wǎng)絡不好可以這樣下載,速度也快:
git clone https://ghproxy.com/https://github.com/li-plus/chatglm.cpp.git
cd chatglm.cpp/third_party
git clone https://ghproxy.com/https://github.com/ggerganov/ggml.git
git clone https://ghproxy.com/https://github.com/pybind/pybind11.git
git clone https://ghproxy.com/https://github.com/google/sentencepiece.git
然后運行docker 并配置python 的源:
docker run -itd --name python -p 8000:8000 -p 7860:7860 -v `pwd`/chatglm.cpp:/data python:slim-bullseye
docker exec -it python bash
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
pip config set install.trusted-host mirrors.aliyun.com/pypi/simple/
echo "deb https://mirrors.aliyun.com/debian/ bullseye main contrib non-free" > /etc/apt/sources.list && \
echo "deb https://mirrors.aliyun.com/debian/ bullseye-updates main contrib non-free" >> /etc/apt/sources.list && \
echo "deb https://mirrors.aliyun.com/debian/ bullseye-backports main contrib non-free" >> /etc/apt/sources.list && \
echo "deb https://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list
3,安裝依賴包,使用特殊命令安裝pytorch的cpu版本
只安裝 cpu 版本的 pytorch ,可以減少鏡像大小。
特別注意pytorch2.0 只支持 3.10 的最低版本,其他版本安裝不上。
apt-get update && apt-get -y install g++ cmake
# 只是安裝 cpu 的版本:
pip3 install torch==2.0.1+cpu torchvision==0.15.2+cpu torchaudio==2.0.2 --index-url https://download.pytorch.org/whl/cpu
pip3 install uvicorn fastapi==0.92.0 sse_starlette chatglm-cpp tabulate tqdm gradio transformers==4.30.2
4,進行模型轉(zhuǎn)換,把chatglm2-6b模型轉(zhuǎn)換下
需要下載模型,安裝git-lfs 把模型下載即可
cd /data
apt-get install git-lfs
# 下載模型
git clone https://huggingface.co/THUDM/chatglm2-6b-int4
# 然后就可以轉(zhuǎn)換模型了,chatglm2-6b-int4 是下載的模型文件夾
python3 convert.py -i chatglm2-6b-int4 -t q4_0 -o chatglm2-ggml.bin
# 稍等下,如果沒有報錯信息,說明轉(zhuǎn)換成功。會有個 chatglm2-ggml.bin 文件
3.3G chatglm-ggml.bin # 說明轉(zhuǎn)換成了。
ChatGLM2-6B,各種尺寸的模型,需要消耗的資源:
Q4_0 | Q4_1 | Q5_0 | Q5_1 | Q8_0 | F16 | F32 | |
---|---|---|---|---|---|---|---|
ms/token (CPU @ Platinum 8260) | 64 | 71 | 79 | 83 | 106 | 189 | 372 |
ms/token (CUDA @ V100 SXM2) | 9.7 | 9.4 | 10.3 | 10.2 | 14.0 | 19.1 | 33.0 |
ms/token (MPS @ M2 Ultra) | 11.0 | 11.7 | N/A | N/A | N/A | 32.1 | N/A |
file size | 3.3GB | 3.7GB | 4.0GB | 4.4GB | 6.2GB | 12GB | 24GB |
mem usage | 3.4GB | 3.8GB | 4.1GB | 4.5GB | 6.2GB | 12GB | 23GB |
5,啟動web demo 界面,啟動api 接口
需要修改下 web_demo.py 的最后一行:
因為是docker 做端口映射,需要把 IP 修改成 0.0.0.0 本機就可以訪問了。
demo.queue().launch(share=False, inbrowser=True,server_name="0.0.0.0", server_port=7860)
cd /data/examples
python3 web_demo.py
Running on local URL: http://0.0.0.0:7860
To create a public link, set `share=True` in `launch()`.
如果沒有報錯,說明啟動成功了,端口是7860 ,直接通過web訪問即可。
啟動 api 接口:
python3 api_demo.py
INFO: Started server process [5843]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
執(zhí)行命令測試接口,和chatgpt 的接口是一樣的。
curl http://127.0.0.1:8000/v1/completions -H 'Content-Type: application/json' -d '{"prompt": "你好"}'
{
"object":"text_completion",
"response":"你好??!我是人工智能助手 ChatGLM2-6B,很高興見到你,歡迎問我任何問題。",
"model":"chatglm2-6b",
"choices":[
{
"text":"你好??!我是人工智能助手 ChatGLM2-6B,很高興見到你,歡迎問我任何問題。",
"index":0,
"finish_reason":"stop"
}
],
"usage":{
}
}
6,使用docker 把鏡像的運行打包,在CPU下運行環(huán)境搭建
dockerfile
# 構建 python
# FROM python:slim-bullseye 使用最新的slim 版本。
# docker build . -t chatglm.cpp:latest
FROM python:slim-bullseye as builder
RUN echo "deb https://mirrors.aliyun.com/debian/ bullseye main contrib non-free" > /etc/apt/sources.list && \
echo "deb https://mirrors.aliyun.com/debian/ bullseye-updates main contrib non-free" >> /etc/apt/sources.list && \
echo "deb https://mirrors.aliyun.com/debian/ bullseye-backports main contrib non-free" >> /etc/apt/sources.list && \
echo "deb https://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list && \
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ && \
pip config set install.trusted-host mirrors.aliyun.com/pypi/simple/
RUN apt-get update && apt-get -y install g++ cmake && \
pip3 install torch==2.0.1+cpu torchvision==0.15.2+cpu torchaudio==2.0.2 \
--index-url https://download.pytorch.org/whl/cpu && \
pip3 install uvicorn fastapi==0.92.0 sse_starlette chatglm-cpp tabulate tqdm gradio transformers==4.30.2
# 拷貝本地文件到目錄
COPY . /data
# service
FROM python:slim-bullseye
# 直接使用基礎鏡像然后拷貝 site-packages 安裝包即可。
COPY --from=builder /data/examples /data/examples
COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
WORKDIR /data
# 設置python 的環(huán)境變量和 fask app文件。
ENV LC_ALL="C.UTF-8" LANG="C.UTF-8"
ENV PYTHONPATH="/data"
EXPOSE 8000 7860
ENTRYPOINT ["/data/examples/docker-entrypoint.sh"]
CMD ["/bin/sh"]
其中啟動腳本 docker-entrypoint.sh 是:
#!/bin/sh
ls -lh
echo "############# start python3 web_demo.py #############"
cd /data/examples
python3 web_demo.py
sleep 99999d
執(zhí)行打包命令:
docker build . -t chatglm.cpp:latest
然后就可以啟動了,必須注意不能掛載當前的源代碼文件夾了,否則會報錯:
ModuleNotFoundError: No module named ‘chatglm_cpp._C’
https://github.com/li-plus/chatglm.cpp/issues/91
嘗試下cd到別的路徑下運行,在chatglm.cpp目錄下執(zhí)行,包名會跟倉庫里的chatglm_cpp文件夾沖突
這樣啟動就可以了:
docker run -itd --name chatglm -p 8000:8000 -p 7860:7860 -v `pwd`/chatglm.cpp/chatglm-ggml.bin:/data/chatglm-ggml.bin chatglm.cpp:latest
然后就可以訪問web 界面了。文章來源:http://www.zghlxwxcb.cn/news/detail-721470.html
7,總結
也可以支持英文,但是最后幾個字有點問題。最后有點亂碼,不知道是不是因為模型裁剪的問題。
同時也有可能是原始素材就有這個問題。
可以使用docker 在 CPU上面運行 chatglm ,同時安裝了 pytorch 的CPU 版本,鏡像縮小到 1.5 G了。
并且速度也是非常的快了。可以在非GPU的機器上面運行了。可以解決很多問題呢。文章來源地址http://www.zghlxwxcb.cn/news/detail-721470.html
到了這里,關于【chatglm2】使用Python在CPU環(huán)境中運行 chatglm.cpp 可以實現(xiàn)本地使用CPU運行chatglm2模型,速度也特別的快可以本地部署,把現(xiàn)有項目進行AI的改造。的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!