Windows本地部署ChatGLM-6B模型并用CPU运行

下载模型代码

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

相关代码会下载到ChatGLM2-6B文件夹中

创建虚拟环境

cmd 打开 ChatGLM2-6B文件夹

powershell 复制代码
python -m venv venv  # 创建了一个叫做 venv 的虚拟环境,会出现一个 venv 的文件夹
venv\Scripts\activate.bat # 来激活虚拟环境,如果报错,就先cd到Scripts文件夹,执行activate.bat
pip install -r requirements.txt -i https://pypi.douban.com/simple # 安装所需要的包

下载模型

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

从这个网站上下载模型相关的文件

在ChatGLM2-6B文件夹下创建THUDM/chatglm-6b-int4文件夹,将下载的文件放到这个文件夹中

运行

python 复制代码
from transformers import AutoTokenizer, AutoModel

# 绝对路径
tokenizer = AutoTokenizer.from_pretrained("D:\ChatGLM\ChatGLM2-6B\THUDM\chatglm2-6b-int4", trust_remote_code=True)
model = AutoModel.from_pretrained("D:\ChatGLM\ChatGLM2-6B\THUDM\chatglm2-6b-int4", trust_remote_code=True).float()
model = model.eval()
input = "我该怎么训练你"
response, history = model.chat(tokenizer, input, history=[])
print(response)

运行结果

问题

1、报错 No module named 'transformers_modules.THUDM/chatglm-6b'

win系统下子文件夹的路径应该写成\("THUDM\chatglm-6b"), 原代码写的是linux文件系统用的/ ("THUDM/chatglm-6b")。

2、quantization_kernels_parallel.so 加载失败

这是因为ctypes在Windows环境下的bug还没修复,python3.10目前还有问题。需要对quantization.py 中的 ctypes.cdll.LoadLibrary相关代码 进行处理:

python 复制代码
将
kernels = ctypes.cdll.LoadLibrary(kernel_file)
修改为
kernels = ctypes.CDLL(kernel_file, winmode = 0)

也有人会手动编译,步骤如下,也可以尝试

(1)手动编译,在模型path下

cpp 复制代码
gcc -fPIC -pthread -fopenmp -std=c99 quantization_kernels_parallel.c -shared -o quantization_kernels_parallel.so
gcc -fPIC -pthread -fopenmp -std=c99 quantization_kernels.c -shared -o quantization_kernels.so

(2)然后在原先模型加载后手动加载一下手动编译的kernel

python 复制代码
model = AutoModel.from_pretrained("THUDM/chatglm-6b-int4",trust_remote_code=True).float()
model = model.quantize(bits=4, kernel_file="Your Kernel Path")

参考文献

https://blog.csdn.net/u013817537/article/details/131500501

https://blog.csdn.net/chaishen10000/article/details/131274928

https://zhuanlan.zhihu.com/p/640677084

相关推荐
开飞机的舒克_34 分钟前
FastAPI 实战入门:从路由、参数校验到依赖注入的后端开发指南
python·fastapi
霸道流氓气质1 小时前
Kiro 中反编译 JAR 包并分析字节码的流程指南
chrome·python·jar
人工智能时代 准备好了吗1 小时前
AI回答内容进入率监测:引用识别、文本匹配与语义判断
开发语言·人工智能·python
言乐62 小时前
Python实现建造微服务商城后台
开发语言·python·算法·微服务·架构
fenglllle3 小时前
chromadb emmbedding 向量检索
人工智能·python·embedding
cui_ruicheng3 小时前
Python从入门到实战(六):非序列容器
开发语言·python
百里香酚兰3 小时前
【python学习笔记】pyttsx3库疑似只播报一次语音
笔记·python·学习
飞猪~3 小时前
Langchain python版本 LLM 重要函数invoke,ainvoke,stream,astream,batch,abatch
python·langchain·batch
沙蒿同学4 小时前
当古诗词遇上 AI:从 38 万句诗词中取一个好名字
python·算法·架构
xxie1237944 小时前
Python装饰器与语法糖
开发语言·python