本地AI,太酷了


你不觉得这很酷吗?作为一个ylg,我觉得这真是太酷了,很符合我对未来的想象,科技并带着趣味

使用GLM-6B进行本地部署,GLM算是比较轻量的ai框架了,GLM-6B更是支持中英双语,使用int4量化的训练模型后,GPU部署模式下只需要6GB显存和cuda支持,响应速度1-2秒,可用性很高
CPU模式下对显存没有要求,预计内存占用应该在6-8G左右,响应速度在1分钟左右(zen3+ 8c16t 70%占用 4.10Ghz左右)用e5 12c24t以上等系列效果可能好一点?
第一步:下载所有必要的文件
ChatGLM本体 | INT4量化模型 | TDM-GCC(C语言编译环境)

这里建议安装一个w64devkit用以解决编译环境问题

以上完成后
第二步:安装必要py库

1
pip install -r requirements.txt

默认会安装cpu版本的torch
cpu模式下,使用以下代码来调用

1
2
3
4
5
6
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("INT4量化模型的地址", trust_remote_code=True, revision="")
model = AutoModel.from_pretrained("INT4量化模型的地址",trust_remote_code=True, revision="").float()
model = model.eval()
response, history = model.chat(tokenizer, "你提出的问题", history=[])
print(response)

路径分隔请使用双反斜杠,也就是\\来分隔

如遇以下报错

1
2
3
4
5
6
7
No compiled kernel found.
Compiling kernels : C:\Users\DuFei\.cache\huggingface\modules\transformers_modules\chatglm-6b-int4\quantization_kernels_parallel.c
Compiling gcc -O3 -fPIC -pthread -fopenmp -std=c99 C:\Users\DuFei\.cache\huggingface\modules\transformers_modules\chatglm-6b-int4\quantization_kernels_parallel.c -shared -o C:\Users\DuFei\.cache\huggingface\modules\transformers_modules\chatglm-6b-int4\quantization_kernels_parallel.so
Kernels compiled : C:\Users\DuFei\.cache\huggingface\modules\transformers_modules\chatglm-6b-int4\quantization_kernels_parallel.so
Cannot load cpu kernel, don't use quantized model on cpu.
Using quantization cache
Applying quantization to glm layers

则有可能是编译时出了问题,在量化模型的文件下使用以下命令来手动编译其中的两个c文件

1
2
3
gcc -fPIC -pthread -fopenmp -std=c99 quantization_kernels.c -shared -o quantization_kernels.so

gcc -fPIC -pthread -fopenmp -std=c99 quantization_kernels_parallel.c -shared -o quantization_kernels_parallel.so

然后添加一行来手动指定编译后的文件

1
model = model.quantize(bits=4, kernel_file="INT4量化模型的地址\\quantization_kernels.so")