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

相关推荐
lifallen1 分钟前
Flink Agent 与 Checkpoint:主循环闭环与 Mailbox 事件驱动模型
java·大数据·人工智能·python·语言模型·flink
平安的平安1 分钟前
Python 构建AI多智能体系统:让三个 AI 协作完成复杂任务
开发语言·人工智能·python
曲幽5 分钟前
FastAPI + Vue 前后端分离实战:我的项目结构“避坑指南”
python·vue·fastapi·web·vite·proxy·cors·env
Kapaseker8 分钟前
Python 提速 — 惰性导入
python
码上掘金11 分钟前
基于深度学习与大语言模型的皮肤病智能辅助诊断系统
人工智能·深度学习·语言模型
杜子不疼.13 分钟前
Python + Ollama 本地跑大模型:零成本打造私有 AI 助手
开发语言·c++·人工智能·python
李昊哲小课13 分钟前
pip缓存配置
python·缓存·pip
belldeep13 分钟前
python:介绍 UV 安装,如何使用 UV 安装配置 OpenHarness
windows·python·环境变量·uv
这张生成的图像能检测吗17 分钟前
(论文速读)FD-LLM:将振动信号编码为文本表示来将振动信号与大型语言模型进行对齐
人工智能·深度学习·语言模型·智能制造·故障诊断
理想三旬18 分钟前
Numpy 数据库
python·机器学习·numpy