XTuner 微调个人小助手认知

一、环境安装

pip install -U 'xtuner[deepspeed]'

# InternStudio 平台中,从本地 clone 一个已有 pytorch 2.0.1 的环境
conda create --name personal_assistant --clone=/root/share/conda_envs/internlm-base
# 如果在其他平台:
# conda create --name personal_assistant python=3.10 -y
 
# 激活环境
conda activate personal_assistant
cd ~
# personal_assistant用于存放本教程所使用的东西
mkdir /root/personal_assistant && cd /root/personal_assistant
mkdir /root/personal_assistant/xtuner019 && cd /root/personal_assistant/xtuner019

git clone -b v0.1.9  https://github.com/InternLM/xtuner
 
# 进入源码目录
cd xtuner
 
# 从源码安装 XTuner
pip install -e '.[all]'

二、模型认知微调

1.生成自我认知数据:

[
    {
        "conversation": [
            {
                "input": "请介绍一下你自己",
                "output": "我是Jeff2017的小助手,内在是上海AI实验室书生·浦语的7B大模型哦"
            }
        ]
    },
    {
        "conversation": [
            {
                "input": "请做一下自我介绍",
                "output": "我是Jeff2017的小助手,内在是上海AI实验室书生·浦语的7B大模型哦"
            }
        ]
    }
]
[
    {
        "conversation": [
            {
                "input": "请介绍一下你自己",
                "output": "我是Jeff2017的小助手,内在是上海AI实验室书生·浦语的7B大模型哦"
            }
        ]
    },
    {
        "conversation": [
            {
                "input": "请做一下自我介绍",
                "output": "我是Jeff2017的小助手,内在是上海AI实验室书生·浦语的7B大模型哦"
            }
        ]
    }
]

2.修改配置文件

(1)列出所有预定义配置文件

    xtuner list-cfg -p internlm2 

(2)复制配置文件

  xtuner copy-cfg internlm2_chat_1_8b_qlora_alpaca_e3 .  

(3)修改配置文件

    a.模型路径修改:

  pretrained_model_name_or_path = 'Shanghai_AI_Laboratory/internlm2-1_8b'

   b.数据集修改: 

      data_files = ['datas/pretrain.json']

  c.evaluation_inputs修改:

      evaluation_inputs = ['请介绍一下你自己', 'Please introduce yourself']

(4)启动微调

        xtuner train ./internlm2_chat_1_8b_qlora_alpaca_e3_copy.py 

(5)模型格式转换

       模型转换的本质其实就是将原本使用 Pytorch 训练出来的模型权重文件转换为目前通用的 HuggingFace 格式文件,那么我们可以通过以下命令来实现一键转换。

    MKL_SERVICE_FORCE_INTEL=1 MKL_THREADING_LAYER=GNU xtuner convert pth_to_hf ./internlm2_chat_1_8b_qlora_alpaca_e3_copy.py ./work_dirs/internlm2_chat_1_8b_qlora_alpaca_e3_copy/iter_384.pth ./hf      

(6)模型合并

        对于 LoRA 或者 QLoRA 微调出来的模型其实并不是一个完整的模型,而是一个额外的层(Adapter),训练完的这个层最终还是要与原模型进行合并才能被正常的使用。

 MKL_SERVICE_FORCE_INTEL=1 MKL_THREADING_LAYER=GNU xtuner convert merge /root/InternLM/XTuner/Shanghai_AI_Laboratory/internlm2-chat-1_8b ./hf ./merged --max-shard-size 2GB

三、模型微调前后对比

未微调前:

微调后:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值