【书生·浦语大模型实战营第二期】XTuner微调LLM:1.8B、多模态、Agent——学习笔记4

本文详细介绍了XTuner工具的使用,包括前期准备、配置文件选择、模型微调步骤(如数据格式转换、模型选择、训练与部署)、以及多模态LLMLaVA的实操过程。着重展示了DeepSpeed在加速训练中的应用和微调后模型的性能提升。
摘要由CSDN通过智能技术生成

XTuner微调个人小助手

教程学习

Xtuner工具的运行原理:
image.png
总结来说模型微调基础步骤

  1. 前期准备:数据采集和数据格式转换、微调方法选择、微调模型选择、配置文件选择
  2. 配置文件准备(修改配置文件):修改模型及数据集地址、修改load_dataset方式、promp_template选择、修改dataset_map_fn
  3. 模型训练:deepspeed使用、模型续训、权重保存
  4. 训练完成后:模型转换及整合、模型测试、模型部署

动手实操

前期准备

使用数据为openai格式的数据

data = [
    {
        "messages": [
            {
                "role": "user",
                "content": "请做一下自我介绍"
            },
            {
                "role": "assistant",
                "content": "我是{}的小助手,内在是上海AI实验室书生·浦语的1.8B大模型哦".format(name)
            }
        ]
    }

tips代码

# 文件结构目录打印 tree.py
import os
import argparse

def print_dir_tree(startpath, prefix=''):
    """递归地打印目录树结构。"""
    contents = [os.path.join(startpath, d) for d in os.listdir(startpath)]
    directories = [d for d in contents if os.path.isdir(d)]
    files = [f for f in contents if os.path.isfile(f)]

    if files:
        for f in files:
            print(prefix + '|-- ' + os.path.basename(f))
    if directories:
        for d in directories:
            print(prefix + '|-- ' + os.path.basename(d) + '/')
            print_dir_tree(d, prefix=prefix + '    ')

def main():
    parser = argparse.ArgumentParser(description='打印目录树结构')
    parser.add_argument('folder', type=str, help='要打印的文件夹路径')

    args = parser.parse_args()

    print('|-- ' + os.path.basename(args.folder) + '/')
    print_dir_tree(args.folder, '    ')

if __name__ == "__main__":
    main()

配置文件选择

# 查询与internlm2-1.8b模型相关的config文件
xtuner list-cfg -p internlm2_1_8b
# 复制配置文件到指定位置
xtuner copy-cfg internlm2_1_8b_qlora_alpaca_e3 /root/ft/config

关于配置文件

1. part 1 Settings: 模型的基本设置,预训练模型的选择、数据集信息和训练过程中的一些基本参数(如批大小、学习率等)
2. part2 Model&Tokenizer: 指定了用于训练的模型和分词器的具体类型及其配置
3. part3 Dataset&Dataloader: 数据处理的细节,包括如何加载数据集、预处理步骤、批处理大小等
4. part4 Scheduler&Optimizer: 优化过程中的关键参数,如学习率调度策略和优化器的选择
5. part5 Runtime: 训练过程中的额外设置,如日志记录、模型保存策略和自定义钩子等

模型训练
常规训练:

xtuner train /root/ft/config/internlm2_1_8b_qlora_alpaca_e3_copy.py --work-dir /root/ft/train

deepspeed加速训练

xtuner train /root/ft/config/internlm2_1_8b_qlora_alpaca_e3_copy.py --work-dir /root/ft/train_deepspeed --deepspeed deepspeed_zero2

关于DeepSpeed

一个深度学习优化库,提高大规模模型训练的效率和速度,包括这样几个关键技术:模型分割、梯度累积、以及内存和带宽优化等
参数说明:`zero`即Zero Redundancy Optimizer, 降低训练大型模型所需内存占用的优化器
ZeRO分级
deepspeed_zero1: 优化了模型参数的存储,使得每个GPU只存储一部分参数以减少内存的使用
deepspeed_zero2:进一步优化了梯度和优化器状态的存储,分散存储到不同的GPU上
deepspeed_zero3: 进一步减少了激活函数的内存占用, 不存储激活

如何解决模型训练过程出现的过拟合问题

  1. 减少保存权重的间隔,增加权重文件保存的上限:结合评估问题从保存的模型中选择最优的
  2. 增加常规对话数据集以稀释原本数据

模型续训
使用--resume{checkpoint_path}实现模型续训

xtuner train /root/ft/config/internlm2_1_8b_qlora_alpaca_e3_copy.py --work-dir /root/ft/train --resume /root/ft/train/iter_600.pth

实操截图展示
image.png

一段时间后
image.png

模型转换、整合、测试和部署

.pth模型转化为hf格式模型

# xtuner convert pth_to_hf ${配置文件地址} ${权重文件地址} ${转换后模型保存地址}, 得到Huggingface中常用的.bin格式文件
xtuner convert pth_to_hf /root/ft/train/internlm2_1_8b_qlora_alpaca_e3_copy.py /root/ft/train/iter_768.pth /root/ft/huggingface

image.png

模型整合
LoRA或者QLoRA微调得到的模型为一个额外的层(adapter),而非一个完整的模型,需要和原来的模型组合之后才能使用

# XTuner中的一键整合指令
xtuner convert merge /root/ft/model /root/ft/huggingface /root/ft/final_model

对话测试
使用整合后的final_model,借助XTuner提供的prompt-template进行快速测试

# 与模型进行对话,这里选择的模板是internlm2_chat
xtuner chat /root/ft/final_model --prompt-template internlm2_chat
# 测试原模型
xtuner chat /root/ft/model --prompt-template internlm2_chat
# 使用adapter参数提前测试找到最优的adapter模型
xtuner chat /root/ft/model --adapter /root/ft/huggingface --prompt-template internlm2_chat

XTuner微调多模态LLM

多模态LLM原理示例图:
image.png

LLaVA介绍

训练得到LLaVA模型
使用<question text><image> -- <answer text>数据对+文本单模态LLM训练得到一个Image Projector,这个Image Projector即LLaVA模型
image.png
如何使用LLaVA模型
image.png
LLaVA方案的基本思路
分为两个阶段,在pretrain阶段使用<image><caption>数据训练一个理解图片总体特征的模型;在fintune阶段,使用<image><complex text>使得模型具有足够强悍的视觉能力
image.png
pretrain阶段需要8卡算力,溜号了

finetune阶段介绍
数据格式

 {
    "id": "000000033471",
    "image": "coco/train2017/000000033471.jpg",
    "conversations": [
      {
        "from": "human",
        "value": "<image>\nWhat are the colors of the bus in the image?"
      },
      {
        "from": "gpt",
        "value": "The bus in the image is white and red."
      },
      {
        "from": "human",
        "value": "What feature can be seen on the back of the bus?"
      },
      {
        "from": "gpt",
        "value": "The back of the bus features an advertisement."
      },
      {
        "from": "human",
        "value": "Is the bus driving down the street or pulled off to the side?"
      },
      {
        "from": "gpt",
        "value": "The bus is driving down the street, which is crowded with people and other vehicles."
      }
    ]
  },

配置文件准备

# 查询xtuner内置配置文件
xtuner list-cfg -p llava_internlm2_chat_1_8b

# 拷贝配置文件到当前目录
xtuner copy-cfg llava_internlm2_chat_1_8b_qlora_clip_vit_large_p14_336_lora_e1_gpu8_finetune /root/tutorial/xtuner/llava

动手实操结果展示

微调启动!
image.png

带了deepspeed的就是快
image.png

训练完成
image.png

finetuning前的模型性能展示
image.png

finetuning后的模型性能展示
image.png

参考资料

  1. XTuner微调LLM: 1.8B模型,LLaVA方案
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值