大模型微调开源项目全流程

描述

作者:1050Ti全量微调.,东北大学软件工程

微调实战经历

本次微调经验分享依据于我们在2023年参加的“第2届·百度搜索创新大赛——搜索答案组织”整个比赛过程。

我们团队选择的基座模型是ChatGLM3-6B-Base模型,训练数据集为官方提供的数据集(9000条),训练方法为LORA sft 监督微调。

最终结果:score 36.12--ROUGE-L 41.03--BLEU-4 31.22,东三省排名第1名,全国最终排名为44/220.

开始微调(SFT)

准备微调框架

微调框架选择的是github上面的llama-factory开源项目

 

git clone https://github.com/hiyouga/LLaMA-Factory.git
cd LLaMA-Factory
pip install -r requirements.txt

 

推荐python=3.10,如果要在 Windows 平台上开启量化 LoRA(QLoRA),需要安装预编译的 bitsandbytes 库, 支持 CUDA 11.1 到 12.1

 

pip install https://github.com/jllllll/bitsandbytes-windows-webui/releases/download/wheels/bitsandbytes-0.39.1-py3-none-win_amd64.whl

 

加载数据集

在data文件夹下面创建xxx.json命名格式的数据集json文件,本人设置的是baidutrain.json

json文件格式如下

 

[
   {
        "query": "王者荣耀钻石夺宝幸运值满是多少",
        "refs": [
            "积分夺宝幸运值满值为360点,钻石夺宝幸运值满值为200点,但是夺宝幸运值满了之后还需要一抽才能获得水晶,所以积分夺宝361抽必定获得水晶,钻石夺宝201抽必定获得水晶。",
            "王者钻石夺宝幸运值满是200。",
            "1、最高的幸运值上限是200,每抽一次会获得1点幸运值。王者荣耀里面的钻石夺宝幸运值达到200就满了,也就是说当幸运值满200时,再夺宝一次是必出一个王者水晶的,拥有王者水晶可以到水晶商店里面去兑换物品了,兑换的物品都是永久的。",
            "幸运值满值是360,玩家在抽361的时候是保底必出水晶,这个保底是人人都能抽出水晶的,也就是100%可以让玩家得到水晶,荣耀水晶的概率是0.8%,但是玩家们如果在不断增加抽奖的次数,那么抽出水晶的概率也会增加,想要抽出水晶的玩家可以将361次当做保底的次数。",
            "玩家获取王者水晶之后,可以使用该道具兑换王者商店中的奖励,包含了回城特效、英雄、中级品质的皮肤等奖励。"
        ],
        "answer": "《王者荣耀》钻石夺宝幸运值满值为200点。

钻石夺宝幸运值满200后再抽一次必出一个王者水晶,玩家获取王者水晶之后,可以使用该道具兑换王者商店中的奖励,其中包含了回城特效、英雄、中级品质的皮肤等,兑换的物品永久有效。"
    },
    {
        "query": "王手是什么意思",
        "refs": [
            "一、原神王手。 王手这个词出现在PV短片《雪姬逢椿》中。 王手是小神里说的,小神里很可爱,说王手二字的声音也不错。 所以玩家说王手二字让我氪了648。 二、王手原意。 王手是日本将棋的说法,与中国象棋将军的用法相似。 意思是将军(棋步),将军。",
            "一、原神王手 在PV短片——「雪霁逢椿」中出现了王手这个词。 王手是由小神里说出的,小神里十分可爱,说王手二字时声音也好听。 所以玩家就表示王手二字让我氪了648。 二、王手原本意思 王手是日本将棋的说法,和中国象棋将军的用法差不多。 是将军(的棋步),将一军的意思。",
            "王手是日本将棋的说法,与中国象棋将军的用法相似。意思是将军(棋步),将军。",
            "其中的王手是指,如能下了能擒拿对方王将的一手,类似于象棋里的将军。称之为王手。",
            "王手,男,浙江温州市人。1981年开始发表小说。近年小说散见于《收获》《人民文学》《当代》《钟山》《花城》《作家》《山花》等刊,出版中短篇小说集《火药枪》《柯依娜一个人》《狮身人面》"
        ],
        "answer": "王手是指下了这步棋能擒拿对方王将的一手,类似于象棋里的将军,是日本将棋中的一种说法。

此外,王手还是中国作家协会会员,一级作家,出版中短篇小说集《火药枪》《柯依娜一个人》等。"
    }
]

 

一定要将数据集所在的json文件加载到dataset_info.json中!!!!!!!!

 

"baidu_train":{
    "file_name": "baidutrain.json",
    "columns": {
      "prompt": "", #系统prompt
      "query": "refs"+"query", # 输入
      "response": "answer", # 输出
      "history": "" #历史对话
    }
  }

 

启动微调

使用sft进行微调

 

CUDA_VISIBLE_DEVICES=0 python work/jwx/ChatGLM-Efficient-Tuning-main/src/train_bash.py 
    --stage sft  #微调方式
    --model_name_or_path work/jwx/ChatGLM3-main/model  #模型路径
    --do_train  
    --dataset_dir work/jwx/ChatGLM-Efficient-Tuning-main/data  #数据集文件夹
    --dataset baidu_dev,baidu_train  # 数据集
    --finetuning_type lora  #微调方法
    --max_source_length 2048  #最大长度
    --max_target_length 512 
    --learning_rate 3e-05  #学习率
    --num_train_epochs 3.0  #学习轮数
    --max_samples 10000 
    --lora_rank 8  #lora训练的秩
    --lora_dropout 0.1 
    --lora_target q_proj,v_proj  #lora作用层
    --resume_lora_training True 
    --output_dir saves/output  #输出微调的文件夹
    --fp16 True  #开启fp16微调
    --per_device_train_batch_size 4 
    --gradient_accumulation_steps 4  
    --lr_scheduler_type cosine 
    --max_grad_norm 1.0 
    --logging_steps 5 
    --save_steps 1000 

 

对于所有“基座”(Base)模型,–template 参数可以是 default, alpaca, vicuna 等任意值。但“对话”(Chat)模型请务必使用对应的模板。

更多参数信息:

 

https://github.com/hiyouga/ChatGLM-Efficient-Tuning/wiki/%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95

 

合并模型并导出

 

python src/export_model.py 
    --model_name_or_path work/jwx/ChatGLM3-main/model 
    --adapter_name_or_path saves/output 
    --template default 
    --finetuning_type lora 
    --export_dir path_to_export 
    --export_size 7 
    --export_legacy_format False

 

RW

奖励模型训练

数据准备

 

奖励模型,作为RL中的critic,应该让模型知道什么是正确的答案和什么是错误的答案,因此需要在数据集上进行对于不同答案的一个打分情况,来让机器进行学习。格式如下:

[
   0:  {
   input:  "",
   instruction:  "What are the three primary colors?",
   output:  [
   0:  "The three primary colors are red, blue, and yellow. These colors are called primary because they cannot be created by mixing other colors and all other colors can be made by combining them in various proportions. In the additive color system, used for light, the primary colors are red, green, and blue (RGB)." ,
   1:  "Red, Yellow, and Green."
   ]
 } ,
  1:  {
  input:  "4/16",
  instruction:  "Explain why the following fraction is equivalent to 1/4",
  output:  [
   0:  "The fraction 4/16 is equivalent to 1/4 because both numerators and denominators are divisible by 4. Dividing both the top and bottom numbers by 4 yields the fraction 1/4." ,
   1:  "1/4 is the same as 1/4."
  ]
 }
]

 

运行微调

微调之后的奖励模型不可以直接进行使用,因为rw模型的输出是一个打分,相当于RL中的critic。

 

CUDA_VISIBLE_DEVICES=0 python src/train_bash.py 
    --stage rm 
    --do_train 
    --model_name_or_path path_to_llama_model 
    --adapter_name_or_path path_to_sft_checkpoint  #sft模型微调的权重文件,可加可不加
    --create_new_adapter 
    --dataset comparison_gpt4_zh 
    --template default 
    --finetuning_type lora 
    --lora_target q_proj,v_proj 
    --output_dir path_to_rm_checkpoint 
    --per_device_train_batch_size 2 
    --gradient_accumulation_steps 4 
    --lr_scheduler_type cosine 
    --logging_steps 10 
    --save_steps 1000 
    --learning_rate 1e-6 
    --num_train_epochs 1.0 
    --plot_loss 
    --fp16

 

RLHF

机器学习

准备actor和critic

actor:对应的是sft输出的权重文件

critic:对应的是RW输出的权重文件

运行RLHF

 

CUDA_VISIBLE_DEVICES=0 python src/train_bash.py 
    --stage ppo 
    --do_train True 
    --model_name_or_path baichuan-inc/baichuan-7B 
    --adapter_name_or_path saves/Baichuan-7B-Base/lora/sft  #sft输出的权重文件
    --finetuning_type lora 
    --template default 
    --dataset_dir data 
    --dataset alpaca_gpt4_en 
    --cutoff_len 1024 
    --learning_rate 5e-05 
    --num_train_epochs 3.0 
    --max_samples 100000 
    --per_device_train_batch_size 4 
    --gradient_accumulation_steps 4 
    --lr_scheduler_type cosine 
    --max_grad_norm 1.0 
    --logging_steps 5 
    --save_steps 100 
    --warmup_steps 0 
    --lora_rank 8 
    --lora_dropout 0.1 
    --lora_target W_pack 
    --create_new_adapter True 
    --output_dir saves/Baichuan-7B-Base/lora/train_2024-03-01-09-49-43 
    --fp16 True 
    --reward_model saves/Baichuan-7B-Base/lora/rw   #rw输出的权重文件
    --reward_model_type lora 
    --plot_loss True

 

多卡训练

使用accelerate进行训练

 

accelerate config # 首先配置分布式环境
accelerate launch src/train_bash.py 
    --stage sft  #微调方式
    --model_name_or_path work/jwx/ChatGLM3-main/model  #模型路径
    --do_train  
    --dataset_dir work/jwx/ChatGLM-Efficient-Tuning-main/data  #数据集文件夹
    --dataset baidu_dev,baidu_train  # 数据集
    --finetuning_type lora  #微调方法
    --max_source_length 2048  #最大长度
    --max_target_length 512 
    --learning_rate 3e-05  #学习率
    --num_train_epochs 3.0  #学习轮数
    --max_samples 10000 
    --lora_rank 8  #lora训练的秩
    --lora_dropout 0.1 
    --lora_target q_proj,v_proj  #lora作用层
    --resume_lora_training True 
    --output_dir saves/output  #输出微调的文件夹
    --fp16 True  #开启fp16微调
    --per_device_train_batch_size 4 
    --gradient_accumulation_steps 4  
    --lr_scheduler_type cosine 
    --max_grad_norm 1.0 
    --logging_steps 5 
    --save_steps 1000 # 参数同上

 

使用deepspeed进行训练

 

deepspeed --num_gpus 8 --master_port=9901 src/train_bash.py 
    --deepspeed ds_config.json 
    --stage sft  #微调方式
    --model_name_or_path work/jwx/ChatGLM3-main/model  #模型路径
    --do_train  
    --dataset_dir work/jwx/ChatGLM-Efficient-Tuning-main/data  #数据集文件夹
    --dataset baidu_dev,baidu_train  # 数据集
    --finetuning_type lora  #微调方法
    --max_source_length 2048  #最大长度
    --max_target_length 512 
    --learning_rate 3e-05  #学习率
    --num_train_epochs 3.0  #学习轮数
    --max_samples 10000 
    --lora_rank 8  #lora训练的秩
    --lora_dropout 0.1 
    --lora_target q_proj,v_proj  #lora作用层
    --resume_lora_training True 
    --output_dir saves/output  #输出微调的文件夹
    --fp16 True  #开启fp16微调
    --per_device_train_batch_size 4 
    --gradient_accumulation_steps 4  
    --lr_scheduler_type cosine 
    --max_grad_norm 1.0 
    --logging_steps 5 
    --save_steps 1000 

 

心得分享

下面我将本次比赛微调训练的得分的过程分享给大家

机器学习

审核编辑:黄飞

 

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分