首页 > 其他分享 >在MacM1上运行ChatGLM-6B推理

在MacM1上运行ChatGLM-6B推理

时间:2023-06-14 18:23:46浏览次数:70  
标签:6B 模型 ChatGLM 6b MacM1 int4 chatglm

1. 简介

ChatGLM 6B是清华大学和智谱合作的一个62亿参数的大语言模型。基于清华的GLM模型开发。和Meta的LLaMA模型还不是一种模型。

由于LLaMA缺乏中文语料,中文能力不佳。在中文大模型中,ChatGLM 6B参数较小,运行硬件要求较低。而表现可谓出色。所以这里作为一个基础模型先让他运行起来看看有多大的能力。

2. 准备环境

在这里我们一般使用miniconda来做python的包管理。

新建一个Python3.10环境,环境名叫chatglm-6b

conda create -n chatglm-6b python=3.10

激活这个环境,从此之后都在这个环境chatglm-6b中操作

conda activate chatglm-6b

 

3. 下载代码

执行下列命令

git clone https://github.com/THUDM/ChatGLM-6B

这里没有什么花哨的地方,下载下来就可以了。

 

4. 安装Python依赖

python环境切到chatglm-6b, 在上面克隆下来的ChatGLM-6B代码目录中执行下列命令

pip install -r requirements.txt

根据ChatGLM-6B的官方文档,需要安装Pytorch Nightly(似乎不安装nightly也可以)。所以我们在装完上面的依赖包之后,删除torch,重新安装pytorch nightly。

安装方法:链接

执行命令

pip uninstall torch
pip install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu

 

然后,我们看下下载下来的pytorch是不是支持m1的gpu加速

执行python进入python命令行

输入命令

import torch; torch.backends.mps.is_available()

输出True即为可以用M1的GPU加速。但是这里int4量化后的版本不支持MPS GPU加速。而非量化的版本虽然支持MPS GPU加速,但是如果没有32G内存的话,内存不足导致执行速度非常的慢。所以在用int4量化后的模型后,有没有MPS GPU加速都会使用CPU推理。

Python 3.10.11 (main, Apr 20 2023, 13:58:42) [Clang 14.0.6 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch; torch.backends.mps.is_available()
True
>>> 

 

安装icetk

奇怪的是运行web_demo.py需要icetk包,但是没有写在requirements.txt里

补上

pip install icetk

这样就把包依赖安装好了。

 

5. 使用模型推理

5.1. 使用int4量化模型

Mac M1上如果没有32G及32G以上内存,一般还是int4量化模型会快一点。

5.1.1. 下载模型到本地

先运行huggingface.co下的ChatGLM 6B 4int量化后的模型。这个模型推理需要6G内存,finetune需要7G内存。对于小内存用户来说是最小硬件需求的模型。

huggingface.co克隆git repo需要git-lfs

所以先运行

git lfs install

如果没有装过git-lfs,可以看这个链接安装git-lfs

克隆int4量化model

git clone https://huggingface.co/THUDM/chatglm-6b-int4

Mac上没有CUDA可以用,而量化模型是基于CUDA开发的。所以M1/M2的MPS GPU加速也不可用,这里只能用CPU推理。CPU推理需要安装g++和openmp

由于本机安装的g++版本是14.x,所以可以按照链接的指示安装openmp

然后把THUDM/chatglm-6b-int4拷贝到代码的ChatGLM-6B下。所以在本地克隆下来的代码目录下,模型放在

代码目录/THUDM/chatglm-6b-int4下

5.1.2. 修改代码

打开web_demo.py,修改代码。

把下面代码

tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).half().cuda()

改为

tokenizer = AutoTokenizer.from_pretrained("./THUDM/chatglm-6b-int4/", trust_remote_code=True)
model = AutoModel.from_pretrained("./THUDM/chatglm-6b-int4/", trust_remote_code=True).float()

5.1.3. 执行推理

执行命令

python web_demo.py

输出

Arguments: (RuntimeError('Unknown platform: darwin'),)
No compiled kernel found.
Compiling kernels : ...
Compiling clang -O3 -fPIC -pthread -Xclang -fopenmp -lomp -std=c99 /Users/heye/.cache/huggingface/modules/transformers_modules/quantization_kernels_parallel.c -shared -o ...
Load kernel : ...
Setting CPU quantization kernel threads to 4
Using quantization cache
Applying quantization to glm layers
Running on local URL:  http://127.0.0.1:7860

自动弹出浏览器:http://127.0.0.1:7860/

就可以用了。

可以试试输入几个问题

1. 方程和函数有什么区别

方程和函数有什么区别


 

 

2.

6. 性能

由于用CPU推理,一个词一个词往外蹦,每个词耗时大约是3-4秒。

如果在Windows下用GPU推理的话速度大约会提升十倍。

量化和非量化版本在表现上差异很少。这点比LLaMA要好很多。LLaMA的7Bint4量化版本表现比较差。

7. 评价

这个模型部署的坑非常少,清大的工程能力可谓出色。整体完成度很高。

 

标签:6B,模型,ChatGLM,6b,MacM1,int4,chatglm
From: https://www.cnblogs.com/smartheye/p/17481047.html

相关文章

  • ChatGLM简介和SSE聊天接口测试效果
    开发公司智谱AI是由清华大学计算机系技术成果转化而来的公司,致力于打造新一代认知智能通用模型。公司合作研发了双语千亿级超大规模预训练模型GLM-130B,并构建了高精度通用知识图谱,形成数据与知识双轮驱动的认知引擎,基于此模型打造了ChatGLM(chatglm.cn)。此外,智谱AI还推出了认知大......
  • CodeForces - 616B Dinner with Emma (模拟)水
    TimeLimit: 1000MS MemoryLimit: 262144KB 64bitIOFormat: %I64d&%I64uCodeForces-616BDinnerwithEmmaSubmit StatusDescriptionJackdecidestoinviteEmmaoutforadinner.Jackisamodeststudent,hedoesn'twanttogotoanexpensiveres......
  • chatglm+langchain本地cpu实战.
    #Setupenvirnment#大概使用59个G内存.condacreate-nlangchainpython=3.8.1-ycondaactivatelangchain#拉取仓库gitclonehttps://github.com/imClumsyPanda/langchain-ChatGLM.git#安装依赖cdlangchain-ChatGLMpython3-mpipinstall-rrequirements.txtpy......
  • chatglm_langchain_demo
    #Setupenvirnmentcondacreate-nlangchainpython=3.8.1-ycondaactivatelangchain#拉取仓库gitclonehttps://github.com/imClumsyPanda/langchain-ChatGLM.git#安装依赖cdlangchain-ChatGLMpython3-mpipinstall-rrequirements.txtpython3-mpipinstal......
  • chatglm_langchain
    #Setupenvirnment!condacreate-nlangchainpython=3.8.1-y!condaactivatelangchain#拉取仓库#!gitclonehttps://github.com/imClumsyPanda/langchain-ChatGLM.git!cp-r/kaggle/input/langchain-chatglm//kaggle/working/langchain-ChatGLM#安装依赖%cd/ka......
  • ChatGLM-6B int4的本地部署与初步测试
    0.前言本次笔记是对于实习初期,初次接触到LLM大模型的一些记录。内容主要集中在对于环境的配置的模型的运行。本人的硬软件配置如下:GPU:RTX30606GB显存内存:32GB系统:Windows111.Anaconda3+Pycharm的环境搭建我使用的是Anaconda3+PyCharm的环境搭建。首先下......
  • ps 2023版本更新?支持M1、神经滤镜,最新版 Photoshop 2023 (ps 2023) for Mac v24.5/24.
    Photoshop2023是一款功能强大、易于操作的图像处理软件,可以帮助用户实现对图像的全方位处理,提高工作效率和图像质量。它拥有丰富的工具和素材、高质量的输出、简单易用的操作界面、对AI和3D的支持以及云端集成等特点...Mac版详情:Photoshop2023 Photoshop2023拥有以下特点:......
  • Firms sign investment deals worth $8.6b at Invest Beijing Global Summit
    Beijingwillcontinuetobeanattractiveinvestmentdestinationforforeigninvestors,asthecityisattheforefrontoftechnologicalinnovation,hasasoundbusinessenvironment,andmakesgreatereffortsindrivingreformandopening-up,saidexecuti......
  • langchain-ChatGLM调研
    https://github.com/imClumsyPanda/langchain-ChatGLM 1.确定显卡规格lspci|grep-invidia00:07.03Dcontroller:NVIDIACorporationGV100GL[TeslaV100SXM232GB](reva1)2.确定显卡运行状况nvidia-smi如果有问题,需要先装显卡的驱动aptsearchnvidia-drive......
  • 基于motorcad设计的外转子发电机,磁钢采用FB6B铁氧体 ,不等匝绕组,输出功率2.3KW 定子外
    基于motorcad设计的外转子发电机,磁钢采用FB6B铁氧体,不等匝绕组,输出功率2.3KW定子外径1563200RPM,18极27槽永磁同步发电机(PMSG)设计案例.ID:2750641231489906......