ChatGLM-6B 大模型的前世今生( 四 )


# INT8 量化的模型将"THUDM/chatglm-6b-int4"改为"THUDM/chatglm-6b-int8"model = AutoModel.from_pretrained("THUDM/chatglm-6b-int4", trust_remote_code=True).half().cuda()
量化模型的参数文件也可以从这里手动下载 。
CPU 部署
如果你没有 GPU 硬件的话,也可以在 CPU 上进行推理,但是推理速度会更慢 。使用方法如下(需要大概 32GB 内存)
model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).float()
如果你的内存不足,可以直接加载量化后的模型:
# INT8 量化的模型将"THUDM/chatglm-6b-int4"改为"THUDM/chatglm-6b-int8"model = AutoModel.from_pretrained("THUDM/chatglm-6b-int4",trust_remote_code=True).float()
如果遇到了报错 Could not find'.dll' 或者 ::(MacOS),请从本地加载模型
Mac 部署
对于搭载了 Apple或者 AMD GPU 的Mac,可以使用 MPS 后端来在 GPU 上运行 -6B 。需要参考 Apple 的 官方说明 安装 -(正确的版本号应该是2.1.0.,而不是2.0.0) 。
目前在 MacOS 上只支持从本地加载模型 。将代码中的模型加载改为从本地加载,并使用 mps 后端:
model = AutoModel.from_pretrained("your local path", trust_remote_code=True).half().to('mps')
加载半精度的 -6B 模型需要大概 13GB 内存 。内存较小的机器(比如 16GB 内存的Pro),在空余内存不足的情况下会使用硬盘上的虚拟内存,导致推理速度严重变慢 。此时可以使用量化后的模型如 -6b-int4 。因为 GPU 上量化的是使用 CUDA 编写的,因此无法在 MacOS 上使用,只能使用 CPU 进行推理 。
# INT8 量化的模型将"THUDM/chatglm-6b-int4"改为"THUDM/chatglm-6b-int8"model = AutoModel.from_pretrained("THUDM/chatglm-6b-int4",trust_remote_code=True).float()
为了充分使用 CPU 并行,还需要单独安装。
多卡部署
如果你有多张 GPU,但是每张 GPU 的显存大小都不足以容纳完整的模型,那么可以将模型切分在多张GPU上 。首先安装 : pip,然后通过如下方法加载模型:
from utils import load_model_on_gpusmodel = load_model_on_gpus("THUDM/chatglm-6b", num_gpus=2)
即可将模型部署到两张 GPU 上进行推理 。你可以将改为你希望使用的 GPU 数 。默认是均匀切分的,你也可以传入参数来自己指定 。
高效参数微调
基于 P- v2 的高效参数微调 。具体使用方法详见 /.md 。
-6B 示例
以下是一些使用 .py 得到的示例截图 。更多 -6B 的可能,等待你来探索发现!
局限性
由于 -6B 的小规模,其能力仍然有许多局限性 。以下是我们目前发现的一些问题:
协议
本仓库的代码依照 -2.0 协议开源,-6B 模型的权重的使用则需要遵循 Model。-6B 权重对学术研究完全开放,在填写问卷进行登记后亦允许免费商业使用 。
引用
如果你觉得我们的工作有帮助的话,请考虑引用下列论文
@article{zeng2022glm,title={Glm-130b: An open bilingual pre-trained model},author={Zeng, Aohan and Liu, Xiao and Du, Zhengxiao and Wang, Zihan and Lai, Hanyu and Ding, Ming and Yang, Zhuoyi and Xu, Yifan and Zheng, Wendi and Xia, Xiao and others},journal={arXiv preprint arXiv:2210.02414},year={2022}}
【ChatGLM-6B 大模型的前世今生】@inproceedings{du2022glm,title={GLM: General Language Model Pretraining with Autoregressive Blank Infilling},author={Du, Zhengxiao and Qian, Yujie and Liu, Xiao and Ding, Ming and Qiu, Jiezhong and Yang, Zhilin and Tang, Jie},booktitle={Proceedings of the 60th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)},pages={320--335},year={2022}}