实战之快速完成 ChatGLM3-6B 在 GPU-8G的 INT4 量化和本地部署

ChatGLM3 (ChatGLM3-6B)

项目地址

https://github.com/THUDM/ChatGLM3

大模型是很吃CPU和显卡的,所以,要不有一个好的CPU,要不有一块好的显卡,显卡尽量13G+,内存基本要32GB+。
清华大模型分为三种(ChatGLM3-6B-Base,ChatGLM3-6B,ChatGLM3-6B-32K)
dd032091c280d455facca120b02fcd28.png
从上图也可以看到,ChatGLM3-6B-32K的话是最高配的模型,而ChatGLM3-6B-Base是最低配的模型。
一般会选择 ChatGLM3-6B普通模型来使用,当然,如果配置高,可以用32K的,会更好。

使用方式

环境安装

首先需要下载本仓库:

# 可以访问github的话,直接git clone即可
git clone https://github.com/THUDM/ChatGLM3
# 如果不能访问的话,利用代理加速, 使用下面命令即可
git clone https://www.gitclone.com/github.com/THUDM/ChatGLM3
cd ChatGLM3

我是安装在Conda虚拟环境下, Conda的搭建安装可以参考我的【Win安装Conda及其环境配置(包含pip、conda换源)】

第一步,创建虚拟环境

请安装 Anaconda,然后用下面的命令创建名为 chatglm3 的虚拟环境:

conda create --name chatglm3 python=3.10
conda activate chatglm3
第二步,然后使用 pip 安装依赖:
pip install -r requirements.txt
# 国内建议使用指定国内镜像进行安装
pip install -r requirements.txt -i https://mirror.sjtu.edu.cn/pypi/web/simple
第三步,安装 pytorch

为了保证 torch 的版本正确,请严格按照 官方文档 的说明安装。
查看本机显卡驱动和cuda版本

nvcc --version

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Wed_Sep_21_10:41:10_Pacific_Daylight_Time_2022
Cuda compilation tools, release 11.8, V11.8.89
Build cuda_11.8.r11.8/compiler.31833905_0

nvidia-smi

Thu May 23 09:57:00 2024
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 551.61                 Driver Version: 551.61         CUDA Version: 12.4     |
|-----------------------------------------+------------------------+----------------------+
| GPU  Name                     TCC/WDDM  | Bus-Id          Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |           Memory-Usage | GPU-Util  Compute M. |
|                                         |                        |               MIG M. |
|=========================================+========================+======================|
|   0  NVIDIA GeForce GTX 1070 Ti   WDDM  |   00000000:01:00.0  On |                  N/A |
|  0%   50C    P8              9W /  180W |     777MiB /   8192MiB |      6%      Default |
|                                         |                        |                  N/A |
+-----------------------------------------+------------------------+----------------------+
# 安装对应pytorch版本命令
conda install pytorch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 pytorch-cuda=11.8 -c pytorch -c nvidia

# 查找torch是否安装成功
import torch
print(torch.__version__) 
print(torch.cuda.is_available()) # True
print(torch.version.cuda) 
第四步,下载模型

可以从Huggingface, Modelsope, SwanHub三个平台下载模型。
使用 Huggingface 下载模型

git lfs install
git clone https://huggingface.co/THUDM/chatglm3-6b.git

使用 Modelscope 下载模型,没工具的情况下,用Modelscope下载最快

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

使用 SwanHub 下载模型

git lfs install
git clone https://swanhub.co/ZhipuAI/chatglm3-6b.git
第五步,使用本地模型运行示例
5.1、基本对话示例

模型路径: E:\AI\code\models\chatglm3-6b,修改basic_demo目录中的cli_demo.py、web_demo_gradio.py、web_demo_streamlit.py模型路径代码。

# cli_demo.py、web_demo_gradio.py、web_demo_streamlit.py
MODEL_PATH = os.environ.get('MODEL_PATH', 'E:\\AI\\code\\models\\chatglm3-6b')

5.2、低成本部署
模型量化

默认情况下,模型以 FP16 精度加载,运行上述代码需要大概 13GB 显存。如果你的 GPU 显存有限,可以尝试以量化方式加载模型,使用方法如下:

model = AutoModel.from_pretrained("THUDM/chatglm3-6b", trust_remote_code=True).quantize(4).cuda()

模型量化会带来一定的性能损失,经过测试,ChatGLM3-6B 在 4-bit 量化下仍然能够进行自然流畅的生成。

CPU 部署

如果你没有 GPU 硬件的话,也可以在 CPU 上进行推理,但是推理速度会更慢。使用方法如下(需要大概 32GB 内存)

model = AutoModel.from_pretrained("THUDM/chatglm3-6b", trust_remote_code=True).float()
Mac 部署

对于搭载了 Apple Silicon 或者 AMD GPU 的 Mac,可以使用 MPS 后端来在 GPU 上运行 ChatGLM3-6B。需要参考 Apple 的 官方说明 安装 PyTorch-Nightly(正确的版本号应该是2.x.x.dev2023xxxx,而不是 2.x.x)。
目前在 MacOS 上只支持从本地加载模型。将代码中的模型加载改为从本地加载,并使用 mps 后端:

model = AutoModel.from_pretrained("your local path", trust_remote_code=True).to('mps')

加载半精度的 ChatGLM3-6B 模型需要大概 13GB 内存。内存较小的机器(比如 16GB 内存的 MacBook Pro),在空余内存不足的情况下会使用硬盘上的虚拟内存,导致推理速度严重变慢。

5.3、命令行demo
python basic_demo/cli_demo.py

猜你喜欢

转载自blog.csdn.net/lvyuanj/article/details/139162784