P-Tuning v2 在 3 亿到 100 亿参数的广阔参数规模上,均能仅以 0.1%~3% 的微调参数量,取得和精调方法媲美的迁移效果,并大大超越前代 P-Tuning 算法;在文本分类和问答到序列标注的广泛任务类型上,P-Tuning v2 也总能取得和精调方法接近的表现水平。
一,下载模型
项目地址
https://github.com/THUDM/ChatGLM-6B
模型文件
https://huggingface.co/THUDM/chatglm-6b
https://huggingface.co/THUDM/chatglm-6b-int4
https://cloud.tsinghua.edu.cn/d/fb9f16d6dc8f482596c2/
数据文件(ADGEN数据集)
# 下载 AdvertiseGen.tar.gz
https://cloud.tsinghua.edu.cn/f/b3f119a008264b1cabd1/?dl=1
二、项目部署
1、环境准备
# 创建虚拟环境
conda create --name chatglm6b python=3.10
To activate this environment, use:
> conda activate chatglm6b
To deactivate an active environment, use:
> conda deactivate
#进入虚拟环境
conda activate chatglm6b
2,安装部署
#克隆项目
git clone https://github.com/THUDM/ChatGLM-6B.git
说明:ChatGLM-6B项目安装部署等参考《CentOS7安装ChatGLM-6B》(获取方法:关注本公众号,输入关键字"chatglm6b"获取)
# 进入微调目录
cd ChatGLM-6B/ptuning
# 安装依赖
pip install rouge_chinese nltk jieba datasets
#数据准备
下载处理好的 ADGEN 数据集,将解压后的 AdvertiseGen 目录放到ChatGLM-6B/ptuning目录下
wget https://cloud.tsinghua.edu.cn/f/b3f119a008264b1cabd1/?dl=1
tar -zxvf AdvertiseGen.tar.gz
三、微调训练
1,微调训练:使用AdvertiseGen全量数据
# 修改代码
vi train.sh
#修改模型地址
```python
PRE_SEQ_LEN=128
LR=2e-2
--train_file AdvertiseGen/train.json \
--validation_file AdvertiseGen/dev.json \
--model_name_or_path /home/admin/ai/chatglm-6b-model/chatglm-6b \
--output_dir output/adgen-chatglm-6b-pt-$PRE_SEQ_LEN-$LR \
--max_steps 3000 \
--logging_steps 10 \
--save_steps 1000 \
```
参数说明:
PRE_SEQ_LEN:训练soft prompt长度
LR:训练学习率
train_file:训练数据文件位置
validation_file:验证数据文件位置
model_name_or_path:原始ChatGLM-6B模型文件路径
output_dir:输出模型文件路径
#开启模型训练
./train.sh
训练过程:以TeslaP40的性能,默认配置下,总需时间近18小时
特别说明:使用AdvertiseGen全量数据集默认参数下最大3000步训练,TeslaP40预估显示要近18个小时,这里对数据做裁剪,并对训练参数做调整。(GPU资源充裕的同学就不用考虑裁剪了)
2,微调训练:使用AdvertiseGen前5条测试数据
# 训练数据和参数调整
# 调整训练数据:截取5条训练数据为测试数据集
head -n 5 AdvertiseGen/train.json > AdvertiseGen/train5.json
head -n 5 AdvertiseGen/train.json > AdvertiseGen/dev5.json
# 调整参数:减少soft prompt长度、训练学习率、最大步数等
vi train.sh
使用新截取的测试数据集训练,并调整最大步数和保存间隔步数
```python
PRE_SEQ_LEN=8
LR=1e-2
--train_file AdvertiseGen/train5.json \
--validation_file AdvertiseGen/dev5.json \
/home/admin/ai/chatglm-6b-model/chatglm-6b \
--output_dir output/adgen-chatglm-6b-pt-$PRE_SEQ_LEN-$LR \
--max_steps 300 \
--logging_steps 10 \
--save_steps 100 \
```
# 重新训练:5条数据300步
./train.sh
修改后5条数据300步大概需要21分钟
#训练结果:5条数据300步实际花27分钟
#训练完成后生成对应的模型文件(位置:output/adgen-chatglm-6b-pt-8-1e-2)
(chatglm6b) [admin@git ptuning]$ ls -l output/adgen-chatglm-6b-pt-8-1e-2/
总用量 24
-rw-rw-r-- 1 admin admin 193 5月 11 18:55 all_results.json
drwxrwxr-x 2 admin admin 4096 5月 11 18:37 checkpoint-100
drwxrwxr-x 2 admin admin 4096 5月 11 18:46 checkpoint-200
drwxrwxr-x 2 admin admin 4096 5月 11 18:55 checkpoint-300
-rw-rw-r-- 1 admin admin 4061 5月 11 18:55 trainer_state.json
-rw-rw-r-- 1 admin admin 193 5月 11 18:55 train_results.json
四、推理过程
# 修改代码
vi evaluate.sh
修改PRE_SEQ_LEN、CHECKPOINT、STEP、model_name_or_path参数
```python
#PRE_SEQ_LEN=128
#CHECKPOINT=adgen-chatglm-6b-pt-128-2e-2
#STEP=3000
PRE_SEQ_LEN=8
CHECKPOINT=adgen-chatglm-6b-pt-8-1e-2
STEP=300
CUDA_VISIBLE_DEVICES=0 python3 main.py \
--do_predict \
--validation_file AdvertiseGen/dev5.json \
--test_file AdvertiseGen/dev5.json \
--overwrite_cache \
--prompt_column content \
--response_column summary \
--model_name_or_path /home/admin/ai/chatglm-6b-model/chatglm-6b \
--ptuning_checkpoint ./output/$CHECKPOINT/checkpoint-$STEP \
--output_dir ./output/$CHECKPOINT \
--overwrite_output_dir \
--max_source_length 64 \
--max_target_length 64 \
--per_device_eval_batch_size 1 \
--predict_with_generate \
--pre_seq_len $PRE_SEQ_LEN \
--quantization_bit 4
```
#执行推理
chmod +x evaluate.sh
./evaluate.sh
执行结果:
评测指标为中文 Rouge score 和 BLEU-4,生成的结果保存在
output/adgen-chatglm-6b-pt-8-1e-2/generated_predictions.txt
(chatglm6b) [admin@git ptuning]$ ls -l output/adgen-chatglm-6b-pt-8-1e-2/
总用量 32
-rw-rw-r-- 1 admin admin 449 5月 11 19:33 all_results.json
drwxrwxr-x 2 admin admin 4096 5月 11 18:37 checkpoint-100
drwxrwxr-x 2 admin admin 4096 5月 11 18:46 checkpoint-200
drwxrwxr-x 2 admin admin 4096 5月 11 18:55 checkpoint-300
-rw-rw-r-- 1 admin admin 3176 5月 11 19:33 generated_predictions.txt
-rw-rw-r-- 1 admin admin 258 5月 11 19:33 predict_results.json
-rw-rw-r-- 1 admin admin 4061 5月 11 18:55 trainer_state.json
-rw-rw-r-- 1 admin admin 193 5月 11 18:55 train_results.json
查看生成的结果:
vi output/adgen-chatglm-6b-pt-8-1e-2/generated_predictions.tx
# 查看后退出
五、微调效果测试
1,修改微调参数脚本
# 修改脚本:设置模型地址、微调后模型地址
vi web_demo.sh
```shell
#PRE_SEQ_LEN=128
PRE_SEQ_LEN=8
CUDA_VISIBLE_DEVICES=0 python3 web_demo.py \
--model_name_or_path /home/admin/ai/chatglm-6b-model/chatglm-6b \
--ptuning_checkpoint output/adgen-chatglm-6b-pt-8-1e-2/checkpoint-300 \
--pre_seq_len $PRE_SEQ_LEN
```
2,运行微调WEB测试
# 修改代码:设置外部访问:
# 注意:此处是ChatGLM-6B/ptuning目录下
vi web_demo.py
启动脚本
# 注意:此处是ChatGLM-6B/ptuning目录下
./web_demo.sh
3, 浏览器访问:对比测试
微调后测试:
微调前测试:
六、GPU监控
watch -n 1 -d nvidia-smi
说明:5条数据300步训练花费27分钟,GPU负载97%,GPU内存占用5G
本文原创公众号:超世先锋,授权转载请联系微信(minggeliuliuliua),授权后,请在原创发表48小时后转载。