chatglm2微调—Lora

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

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

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

chatglm2微调—Lora_第1张图片

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

chatglm2微调—Lora_第2张图片

3.Lora微调训练

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

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

chatglm2微调—Lora_第3张图片

所以还是用example/train_sft.sh

chatglm2微调—Lora_第4张图片

可设置的主要参数包括:

  • 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模型评估预测

5.Lora模型合并及使用

(1)模型合并

(2)加载并进行推理

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

from peft import PeftModel
from transformers import AutoTokenizer, AutoModel
import torch

device = torch.device(1)
# 加载原始 LLM
model_path = "THUDM/chatglm-6b"
model = AutoModel.from_pretrained(model_path, trust_remote_code=True).half().to(device)
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model.chat(tokenizer, "你好", history=[])


# 给原始 LLM 安装上你的 LoRA tool
model = PeftModel.from_pretrained(model, "model/chatglm2_lora").half()
model.chat(tokenizer, "你好", history=[])

你可能感兴趣的:(深度学习)