当前位置:   article > 正文

chatglm2微调—Lora_lora 微调 chatglm2

lora 微调 chatglm2

1.使用ChatGLM-Efficient-Tuning框架

官网下载https://github.com/hiyouga/ChatGLM-Efficient-Tuning

或者国内镜像https://gitee.com/mirrors/chatglm-efficient-tuning

推荐一些写的不错的链接以及官网readme

ChatGLM2-6B微调 - 掘金 (juejin.cn)

基于 PEFT 的高效 ChatGLM2-6B 微调 - 简书 (jianshu.com)

【CHATGLM】ChatGLM2-6B--LoRA微调--(02) - 知乎 (zhihu.com)

【微调】CHATGLM2-6B LoRA 微调 - 知乎 (zhihu.com)

2.配置ChatGLM-Efficient-Tuning

  1. cd ChatGLM-Effi-Tuning
  2. pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
  3. #必要可以加清华源

目前主流对大模型进行微调方法有三种:Freeze方法、P-Tuning方法和Lora方法

 LoRA: 在大型语言模型上对指定参数(权重矩阵)并行增加额外的低秩矩阵,并在模型训练过程中,仅训练额外增加的并行低秩矩阵的参数,冻结其他参数。 当“秩值”远小于原始参数维度时,新增的低秩矩阵参数量也就很小。在下游任务tuning时,仅须训练很小的参数,但能获取较好的表现结果。

3.Lora微调训练

推荐使用项目下example/train_sft.sh

网络上复制的一直报错export_model.py: error: the following arguments are required: --output_dir.明明指定了输出路径,甚至放在根目录或者绝对路径还报错,直至看到项目下example/train_sft.sh文件发现复制命令的有些换行/,正常是黄色,这样可能导致换行失效了。不知道为什么?

所以还是用example/train_sft.sh

可设置的主要参数包括:

  • dataset, 分词后的数据集,即在 data/ 地址下的文件夹名称
  • lora_rank, 设置 LoRA 的秩,推荐为4或8,默认8
  • per_device_train_batch_size, 每块 GPU 上的 batch size,显存不大尽量1-2
  • gradient_accumulation_steps, 梯度累加,可以在不提升显存占用的情况下增大 batch size
  • save_steps, 多少步保存一次
  • save_total_limit, 保存多少个checkpoint
  • learning_rate, 学习率
  • output_dir, 模型文件保存地址

训练过程

4.Lora模型评估预测

评估:

  1. CUDA_VISIBLE_DEVICES=0 python ../src/train_bash.py \
  2. --stage sft \
  3. --do_eval \
  4. --model_name_or_path /home/xx/ChatGLM2-6B/model \
  5. --dataset alpaca_gpt4_zh \
  6. --dataset_dir ../data \
  7. --finetuning_type lora \
  8. --checkpoint_dir /home/xx/ChatGLM-Efficient-Tuning/output/lora_ckp \
  9. --output_dir /home/xx/ChatGLM-Efficient-Tuning/eval/lora \
  10. --overwrite_cache \
  11. --per_device_eval_batch_size 8 \
  12. --max_samples 50 \
  13. --predict_with_generate

预测:

  1. CUDA_VISIBLE_DEVICES=0 python ../src/train_bash.py \
  2. --stage sft \
  3. --do_predict \
  4. --model_name_or_path /home/lenovo/ChatGLM2-6B/model \
  5. --dataset alpaca_gpt4_zh \
  6. --dataset_dir ../data \
  7. --finetuning_type lora \
  8. --checkpoint_dir /home/lenovo/ChatGLM-Efficient-Tuning/output/lora_ckp \
  9. --output_dir /home/lenovo/ChatGLM-Efficient-Tuning/eval/lora \
  10. --overwrite_cache \
  11. --per_device_eval_batch_size 8 \
  12. --max_samples 50 \
  13. --predict_with_generate

5.Lora模型合并及使用

(1)模型合并

  1. CUDA_VISIBLE_DEVICES=0 python ../src/export_model.py \
  2. --model_name_or_path /home/xx/ChatGLM2-6B/model \
  3. --finetuning_type lora \
  4. --dataset_dir ../data \
  5. --checkpoint_dir /home/xx/ChatGLM-Efficient-Tuning/output/lora_ckp \
  6. --output_dir /home/xx/ChatGLM-Efficient-Tuning/output/lora_combine_model \

(2)加载并进行推理(有点问题待解决)

把训练的Lora模型打包带走,假设文件夹为 model/chatglm2_lora, 其中(至少)包含 adapter_model.bin 和 adapter_config.json 两个文件,加载及推理代码如下:

  1. from peft import PeftModel
  2. from transformers import AutoTokenizer, AutoModel
  3. import torch
  4. device = torch.device(1)
  5. # 加载原始 LLM
  6. model_path = "THUDM/chatglm-6b"
  7. model = AutoModel.from_pretrained(model_path, trust_remote_code=True).half().to(device)
  8. tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
  9. model.chat(tokenizer, "你好", history=[])
  10. # 给原始 LLM 安装上你的 LoRA tool
  11. model = PeftModel.from_pretrained(model, "model/chatglm2_lora").half()
  12. model.chat(tokenizer, "你好", history=[])
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/57798
推荐阅读
  

闽ICP备14008679号