基于 PEFT 的高效 ChatGLM2-6B 微调

参考

hiyouga/ChatGLM-Efficient-Tuning
LLMsNineStoryDemonTower-PEFT_finetune

恢复环境

环境来自 百度飞桨的aistudio,部署glm2-6b推理测试

硬件原V100 16GB fp16报错OOM,参考如下硬件需求,改为V100 32GB环境。


V100-32GB
硬件需求
注:r 为LoRA 维数大小,p 为前缀词表大小,l 为微调层数,ex/s 为每秒训练的样本数。gradient_accumulation_steps 参数设置为 1。上述结果均来自于单个 Tesla V100(32GB) GPU,仅供参考。

1. 创建3.10环境

conda create -n py310 python=3.10
conda init

2. 关闭shell再打开,激活310

conda activate py310
python -V

3. 修改site packages

vi /home/aistudio/.conda/envs/py310/lib/python3.10/site.py
  "/home/aistudio/work/pip/lib/python310/site-packages"
image.png

4. 测试环境

cd ~/work/ChatGLM2-6B
python helloworld_glm2.py


image.png

微调训练

微调方法:

  • LoRA:仅微调低秩适应器。
  • P-Tuning V2:仅微调前缀编码器。
  • Freeze :仅微调后几层的全连接层。

1. 准备微调训练

代码clone

git clone https://github.com/hiyouga/ChatGLM-Efficient-Tuning.git

安装依赖

cd ChatGLM-Efficient-Tuning
pip install -r requirements.txt

2. freeze方式微调

ChatGLM2-6B 模型的微调。需要使用--use_v2 参数来进行训练。
本地模型的微调,需要使用--model_name_or_path参数来指定。
不联网情况下微调训练
alpaca_gpt4_zh要用10个小时,换self_cognition数据集30秒训练完成。

mkdir output
CUDA_VISIBLE_DEVICES=0 python src/train_bash.py \
    --do_train \
    --model_name_or_path /home/aistudio/work/chatglm2-6b \
    --dataset self_cognition \
    --dataset_dir data \
    --finetuning_type freeze \
    --output_dir output/freeze_sft_checkpoint \
    --overwrite_cache \
    --per_device_train_batch_size 2 \
    --gradient_accumulation_steps 2 \
    --lr_scheduler_type cosine \
    --logging_steps 10 \
    --save_steps 1000 \
    --learning_rate 5e-5 \
    --num_train_epochs 3.0 \
    --plot_loss \
    --fp16   
freeze finetune

对话测试:

python src/cli_demo.py  --checkpoint_dir output/freeze_sft_checkpoint --model_name_or_path /home/aistudio/work/chatglm2-6b

3. p_tuning方式微调

CUDA_VISIBLE_DEVICES=0 python src/train_bash.py \
    --do_train \
    --model_name_or_path /home/aistudio/work/chatglm2-6b \
    --dataset self_cognition \
    --dataset_dir data \
    --finetuning_type p_tuning \
    --output_dir output/p_tuning_sft_checkpoint  \
    --overwrite_cache \
    --per_device_train_batch_size 2 \
    --gradient_accumulation_steps 2 \
    --lr_scheduler_type cosine \
    --logging_steps 10 \
    --save_steps 1000 \
    --learning_rate 5e-5 \
    --num_train_epochs 3.0 \
    --plot_loss

4. lora方式微调

CUDA_VISIBLE_DEVICES=0 python src/train_bash.py \
    --stage sft \
    --do_train \
    --model_name_or_path /home/aistudio/work/chatglm2-6b \
    --dataset self_cognition \
    --dataset_dir data \
    --finetuning_type lora \
    --output_dir output/lora_sft_checkpoint \
    --overwrite_cache \
    --per_device_train_batch_size 2 \
    --gradient_accumulation_steps 2 \
    --lr_scheduler_type cosine \
    --logging_steps 10 \
    --save_steps 1000 \
    --learning_rate 1e-3 \
    --num_train_epochs 3.0 \
    --fp16
image.png

对话测试

python src/cli_demo.py  --checkpoint_dir output/lora_sft_checkpoint --model_name_or_path /home/aistudio/work/chatglm2-6b

向微调后的 ChatGLM-6B 模型问一些自我认知问题,可以发现它能够给出我们期望的回答。同时,还测试了两个额外的问题,验证结果说明模型的原本知识并没有被严重破坏。

image.png

ChatGLM2-6B 模型 多 GPU 分布式微调

配置 分布式环境

accelerate config # 首先配置分布式环境
accelerate launch src/train_bash.py ...  # 微调,参数同上

注:注意:若您使用 LoRA 方法进行微调,请指定以下参数 --ddpfindunused_parameters False 来避免报错。

模型部署

部署在项目框架中,请使用 export_model.py 将微调后的权重合并到 ChatGLM-6B 模型中并导出完整模型。

python src/export_model.py \
    --checkpoint_dir cognition \
    --output_dir path_to_save_model

通过类似如下代码的调用方式,您可以在任何项目中独立部署微调后的模型。

from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained(path_to_save_model, trust_remote_code=True)
model = AutoModel.from_pretrained(path_to_save_model, trust_remote_code=True).half().cuda()
response, history = model.chat(tokenizer, "你是谁", history=[])
print(response)

ChatGLM2-6B 评估预测

1. ChatGLM2-6B 指标评估(BLEU分数和汉语ROUGE分数)

ChatGLM2-6B 指标评估(BLEU分数和汉语ROUGE分数)

CUDA_VISIBLE_DEVICES=0 python examples/finetune.py \
    --do_eval \
    --dataset alpaca_gpt4_zh \
    --checkpoint_dir path_to_checkpoint \
    --output_dir path_to_eval_result \
    --per_device_eval_batch_size 8 \
    --max_samples 50 \
    --predict_with_generate

2. ChatGLM2-6B 模型预测

  CUDA_VISIBLE_DEVICES=0 python examples/finetune.py \
    --do_predict \
    --dataset alpaca_gpt4_zh \
    --checkpoint_dir path_to_checkpoint \
    --output_dir path_to_predict_result \
    --per_device_eval_batch_size 8 \
    --max_samples 50 \
    --predict_with_generate

3 ChatGLM2-6B 模型推理

  CUDA_VISIBLE_DEVICES=0 python src/infer.py  --checkpoint_dir path_to_checkpoint --quantization_bit=4

问题

微调显存问题

问题描述:在 执行 微调命令时,爆 显存不足
解决方法:在 trainsft.sh 中 添加 参数 --quantizationbit=8,该命令的作用时 进行 8bit 量化

使用 P-Tuning v2 进行 微调时 AssertionError: Please disable fp16 training while using the P-Tuning v2 method.

问题描述:使用 freeze 进行 量化微调时出错
$ AssertionError: Please disable fp16 training while using the P-Tuning v2 method.
问题原因:P-Tuning v2 微调 不支持 fp16
解决方法:删除 --fp16 即可

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容