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

相关推荐
曲幽2 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
敏编程7 小时前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪7 小时前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
databook7 小时前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效
花酒锄作田20 小时前
使用 pkgutil 实现动态插件系统
python
前端付豪1 天前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽1 天前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战1 天前
Pydantic配置管理最佳实践(一)
python
阿尔的代码屋1 天前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python
AI探索者2 天前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python