文章目录
前言
ChatGLM-6B ChatGLM-6B 是一个开源的、支持中英双语的对话语言模型,基于 General Language Model (GLM) 架构,具有 62 亿参数。ChatGLM-6B是本人尝试使用和微调的第一个大语言模型,自我感觉该模型很适合作为大语言模型的入门级选手,无论是部署配置还是推理微调都十分方便。本文主要介绍如何配置部署ChatGLM-6B,以及ChatGLM-6B推理和P-tuning v2微调基本步骤,希望可以帮助大家使用ChatGLM-6B。
一、ChatGLM-6B安装
1.1 下载
ChatGLM-6B项目仓库地址为 GitHub,模型文件下载地址为Huggingface,将下载好的模型文件chatglm-6b文件放至项目仓库中的ptuning文件目录下(如下图所示)。整个下载时间的长短根据网速和是否使用远程服务器因人而异,本人因使用的是远程服务器,下载时间共约5个小时。
1.2 环境安装
服务器的版本为RTX 3090,内存为24GB。Python版本为3.8.16,ubuntu的版本为20.04,Cuda的版本为11.6。
库名 | 版本 |
---|---|
transformers | 4.27.1 |
torch | 1.13.1 |
详情可见requirements.txt,其中gradio库有的时候会安装失败,如果后续不考虑前端交互的平台的构建,此库可以先不安装,并不影响模型推理和微调。环境配置步骤如下代码所示:
conda create -n test python=3.8.16 -y
source activate test
pip install -r requirements.txt
cd ChatGLM
二、ChatGLM-6B推理
ChatGLM-6B推理部分,只要找到cli_demo.py文件运行即可。
tokenizer = AutoTokenizer.from_pretrained("ptuning/chatglm-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("ptuning/chatglm-6b", trust_remote_code=True).half().cuda()
model = model.eval()
以下是推理部分的展示:
当然我们也想要批量式询问ChatGLM-6B,这里我自己写了一个批量调用的py文件:
import torch
from transformers import AutoTokenizer, AutoModel
import torch
import sys
import pandas as pd
model_path="ptuning/chatglm-6b"
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model = AutoModel.from_pretrained("ptuning/chatglm-6b",trust_remote_code=True).float()
#model =model.to("cpu")
model = model.eval()
data = pd.read_csv('Q1.csv')
MC = data['Question']