baichuan2的13B Lora微调

本次实验旨在参考MindFormers的官方套件运行baichuan2的Lora 13B微调,为此,我们首先需要准备以下资源和环境:
MindFormers官方套件:这包括针对baichuan2的Lora微调的相关代码、模型和工具。可以直接访问MindFormers的官网,当前的环境是(21e46b88d14dcddadb232c5aaf9af66d45b568c5)

硬件资源:为了运行baichuan2的Lora微调,我们需要具备以下硬件资源:

  • Atlas800-9000训练服务器
  • 各地计算中心(昇腾)
  • 有昇腾训练卡的服务器
  • 硬盘大于500G

软件环境:除了硬件资源外,我们还需要准备以下软件环境
参考文章:https://gitee.com/mindspore/mindformers/blob/dev/research/baichuan2/baichuan2.md

  • 全量模型下载地址:https://ascend-repo-modelzoo.obs.cn-east-2.myhuaweicloud.com/MindFormers/baichuan2/Baichuan2_13B_Base.ckpt

  • tokenizer:https://ascend-repo-modelzoo.obs.cn-east-2.myhuaweicloud.com/MindFormers/baichuan2/tokenizer.model

  • 数据集:https://github.com/baichuan-inc/Baichuan2/blob/main/fine-tune/data/belle_chat_ramdon_10k.json

  • BMS(AICC)镜像:swr.cn-central-221.ovaijisuan.com/mindformers/mindformers0.8.0_mindspore2.2.0:aarch_20231025
    说明:底层驱动和固件需的版本需要大于等于C84的版本。(目前各地的计算中心的底层驱动大多数为C81–2023-10-31)

    1、首先是环境准备:
    使用上面的镜像,地址通过docker pull下载镜像。如下图所示在这里插入图片描述
    2、数据集准备

  • 当前提供belle_chat_ramdon数据集的预处理和微调样例,用于对Baichuan2-7B-Base,Baichuan2-13B-Base模型进行微调。数据集下载链接如下:belle_chat_ramdon_10k

  • 执行belle_preprocess.py,进行数据预处理、Mindrecord数据生成,将带有prompt模板的数据转换为mindrecord格式。

# 脚本路径:research/baichuan2/belle_preprocess.py
python research/baichuan2/belle_preprocess.py \
--input_glob /{path}/belle_chat_ramdon_10k.json \
--model_file /{path}/tokenizer.model \
--output_file /{path}/belle_512.mindrecord \
--seq_length 512

生成如下数据集文件
在这里插入图片描述

3、权重文件下载和准备

  • 下载权重文件权重文件
  • 下载tokenizer文件
    下载文件
  • 将权重文件配置成成多卡的路径如下图所示,我有八张卡所以分成八份。
    在这里插入图片描述
    4、配置RANK_TABLE_FILE:
  • 单机8卡
    运行mindformers/tools/hccl_tools.py,生成RANK_TABLE_FILE文件
    运行如下命令,生成当前机器的RANK_TABLE_FILE的json文件
python ./mindformers/tools/hccl_tools.py --device_num "[0,8)"
  • 若使用ModelArts的notebook环境,可从 /user/config/jobstart_hccl.json 路径下直接获取rank table,无需手动生成
    如下图:
    在这里插入图片描述
    5、lora微调配置
    Baichuan2-13B-Base用于Lora微调,seq_length默认为512。Lora微调支持910A/B,配置文件基本相同。官方给出910B的默认配置文件run_baichuan2_13b_lora_910b.yaml适合910A和910B两种卡,但是有部分参数有区别。修改run_baichuan2_13b_lora_910b.yaml中相关配置,默认使用完整权重,开启自动权重转换。
load_checkpoint: 'model_dir'    # 使用完整权重,权重按照`model_dir/rank_0/xxx.ckpt`格式存放,如我的应该改为:/work/DataDisk0/baichuan2/models/ckpt
auto_trans_ckpt: True           # 打开自动权重转换
use_parallel: True
run_mode: 'finetune'
# dataset
train_dataset: &train_dataset
  data_loader:
    type: MindDataset
    dataset_dir: "dataset_dir"  # 配置训练数据集文件夹路径
    shuffle: True
  input_columns: ["input_ids", "labels"]
# 指令微调时(如belle数据集),input_columns: ["input_ids", "labels"]
# 继续预训练时(如wikitext2数据集),input_columns: ["input_ids"]

# 8卡分布式策略配置
parallel_config:
  data_parallel: 8
  model_parallel: 1
  pipeline_stage: 1
  micro_batch_num: 1
  vocab_emb_dp: True
  gradient_aggregation_group: 4

# 权重保存配置参考
parallel:
  strategy_ckpt_config:
    save_file: "./ckpt_strategy.ckpt"
    only_trainable_params: False # 配置为False,保存完整权重

# model增加pet_config
model:
  model_config:
    pet_config:
      pet_type: lora
      # configuration of lora
      lora_rank: 1
      lora_alpha: 32
      lora_dropout: 0.1
      target_modules: '.*wq|.*wk|.*wv'
  • 若要在910A上运行,只需修改配置文件如下:
# research/baichuan2/run_baichuan2_13b_lora_910b.yaml
max_device_memory: "31GB"    # 910A将最大内存改为31GB即可

# 910A的8卡分布式策略配置
parallel_config:
  data_parallel: 2
  model_parallel: 1
  pipeline_stage: 4
  micro_batch_num: 4
  vocab_emb_dp: True
  gradient_aggregation_group: 4
  • 启动lora微调
    • 在终端或者是编写个train.sh脚本,添加如下内容到train.sh中,然后运行。
cd mindformers/research
bash run_singlenode.sh \
"python baichuan2/run_baichuan2.py \
--config baichuan2/run_baichuan2_13b_lora_910b.yaml \
--load_checkpoint model_dir \
--auto_trans_ckpt True \
--use_parallel True \
--run_mode finetune \
--train_data dataset_dir" \
/work/DataDisk0/mindformers/hccl_8p.json [0,8] 8

# 参数说明
config: 配置文件路径
load_checkpoint: 权重文件夹路径,权重按照'model_dir/rank_x/xxx.ckpt'格式存放,如我的路径为:/work/DataDisk0/baichuan2/models/ckpt
auto_trans_ckpt: 自动权重转换开关
run_mode: 运行模式,微调时设置为finetune
train_data: 训练数据集文件夹路径,如我的路径为:/work/DataDisk0/baichuan2/data/belle_512.mindrecord"

微调完成,耗时大概为1h左右。有任何问题,可以在评论区评论或者私信。
感谢mindformers提供的强大的大模型套件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值