Xtuner:用于微调大型模型
一.概述
本教程旨在帮助用户快速上手使用 Xtuner 用于微调大型模型。
XTuner 是一个高效、灵活且功能齐全的工具包,用于微调大型模型。它在几乎所有 GPU 上支持 LLM、VLM 预训练/微调,并且与 DeepSpeed 🚀 兼容,轻松利用各种 ZeRO 优化技术。
本教程示例:
示例1:InternLM2.5-Chat-7B 微调
示例2:phi3_mini_4k_instruct 微调
通过本教程,用户将:
- 学会如何使用 Xtuner 开始任务
- 使用相关命令进行模型微调
- 利用 Xtuner 部署语言模型
本教程适合希望快速上手微调LLM(InternLM2、Llama3、Phi3、Qwen、Mistral 等)模型的开发者和研究人员。
二.快速开始
2.1 环境准备
本教程运行硬件环境:
类别 | 详细信息 |
---|---|
CPU | 16 vCPU Intel(R) Xeon(R) Platinum 8481C |
GPU | RTX 4090 |
GPU 显存 | 24GB |
CUDA 版本 | 12.1 |
操作系统 | Linux 5.15.0-94-generic |
Python 版本 | 3.10.16 |
PyTorch 版本 | 2.4.1 |
创建一个 conda 环境 xtuner-env ,并激活环境
conda create --name xtuner-env python=3.10 -y
conda activate xtuner-env
2.2 安装依赖(3选1)
2.21 通过 pip 安装 XTuner
mkdir run-xtuner
cd run-xtuner
pip install -U xtuner
再安装:
pip install deepspeed==0.15.4
conda install mpi4py -y
2.22 DeepSpeed 集成
pip install -U 'xtuner[deepspeed]'
2.23 从源安装 XTuner(推荐)
mkdir run-xtuner
cd run-xtuner
wget https://mirrors.aheadai.cn/scripts/xtuner_example.zip
unzip xtuner_example.zip
cd xtuner_example
部署 xtuner:
pip install -e '.[all]'
pip install deepspeed==0.15.4
conda install mpi4py -y
pip install torch==2.4.1 torchvision==0.19.1 torchaudio==2.4.1
三.训练教程
3.1 模型配置准备
XTuner 提供了许多现成的配置,根据读者需要选择相应配置。
我们可以通过以下方式查看所有配置:
xtuner list-cfg
可以得到如下结果(仅展示部分可用配置):
baichuan2_13b_base_full_custom_pretrain_e1
baichuan2_13b_base_qlora_alpaca_e3st1_e3
baichuan2_13b_base_qlora_oasst1_512_e3
chatglm2_6b_qlora_medical_e1
chatglm2_6b_qlora_oasst1_512_e3e3
chatglm2_6b_qlora_openorca_e1
deepseekcoder_6_7b_instruct_qlora_custom_sft_e1
deepspeed_zero3
deepspeed_zero3_offload
gemma_2b_full_alpaca_e3
gemma_2b_full_custom_pretrain_e1
gemma_2b_it_full_alpaca_e3
gemma_2b_it_qlora_alpaca_e3
gemma_2b_it_qlora_custom_sft_e1
gemma_7b_qlora_alpaca_e3
gemma_7b_qlora_custom_sft_e1
internlm2_1_8b_full_alpaca_e3
internlm2_1_8b_full_custom_pretrain_e11
internlm2_chat_7b_qlora_lawyer_e3
internlm2_chat_7b_qlora_oasst1_512_e3
internlm2_chat_7b_qlora_oasst1_e3
internlm_20b_qlora_alpaca_e3
internlm_20b_qlora_alpaca_enzh_e3
internlm_20b_qlora_alpaca_enzh_oasst1_e3
internvl_v2_phi3_4b_finetune
internvl_v2_phi3_4b_lora_finetune
internvl_v2_phi3_4b_qlora_finetune
llama2_70b_full_alpaca_enzh_128k_sp8
llama2_70b_full_alpaca_enzh_256k_sp16
llama2_70b_full_alpaca_enzh_32k_sp4
.........
或者,如果提供的配置无法满足要求,请将提供的配置复制到指定目录,并通过:
xtuner copy-cfg ${CONFIG_NAME} ${SAVE_PATH}
vi ${SAVE_PATH}/${CONFIG_NAME}_copy.py
3.2 数据集准备
3.21 HuggingFace 数据集
可以快速使用 HuggingFace Hub 上的数据集,比如:
tatsu-lab/alpaca
OpenAssistant/oasst1
3.22 其余方式
Arxiv Gentitle**
Arxiv 数据集未在 HuggingFace Hub 上发布,但可以从 Kaggle 下载:
从 https://kaggle.com/datasets/Cornell-University/arxiv 下载原始数据后,以如下方式处理数据:
xtuner preprocess arxiv ${DOWNLOADED_DATA} ${SAVE_DATA_PATH} --categories cs.AI cs.CL cs.CV --start-date 2020-01-01
可以移动和重命名数据,或更改配置.
MOSS-003-SFT**
MOSS-003-SFT 数据集可以从 https://huggingface.co/datasets/fnlp/moss-003-sft-data 下载:
# Make sure you have git-lfs installed (https://git-lfs.com)
git lfs install
git clone https://huggingface.co/datasets/fnlp/moss-003-sft-data
cd moss-003-sft-data
unzip moss-003-sft-no-tools.jsonl.zip
unzip moss-003-sft-with-tools-no-text2image.zip
RefCOCO dataset**
文件结构:
./data
├── refcoco_annotations
│ ├── refcoco
│ │ ├── instances.json
│ │ ├── refs(google).p
│ │ └── refs(unc).p
│ ├── refcoco+
│ │ ├── instances.json
│ │ └── refs(unc).p
│ └── refcocog
│ ├── instances.json
│ ├── refs(google).p
│ └─── refs(und).p
├── coco_images
| ├── *.jpg
...
使用以下链接下载 RefCOCO、RefCOCO+、RefCOCOg 注释文件。 coco train 2017 和 2014 coco_images 均有效:
Image source | Download path |
---|---|
RefCOCO | https://bvisionweb1.cs.unc.edu/licheng/referit/data/refcoco.zip |
RefCOCO+ | https://bvisionweb1.cs.unc.edu/licheng/referit/data/refcoco+.zip |
RefCOCOg | https://bvisionweb1.cs.unc.edu/licheng/referit/data/refcocog.zip |
下载注释后,解压缩文件并将其放在目录中。 使用以下命令将注释转换为 json 格式:
xtuner preprocess refcoco --ann-path $RefCOCO_ANN_PATH --image-path $COCO_IMAGE_PATH \
--save-path $SAVE_PATH # ./data/llava_data/RefCOCOJson/
3.3 模型训练
3.32 模型训练实例
xtuner train ${CONFIG_NAME_OR_PATH}
3.31 InternLM2.5-Chat-7B 微调
可以通过以下方式使用 oasst1 数据集启动 InternLM2.5-Chat-7B 的 QLoRA 微调 :
单GPU微调:
xtuner train internlm2_5_chat_7b_qlora_oasst1_e3 --deepspeed deepspeed_zero2
多GPU微调:
(DIST) NPROC_PER_NODE=${GPU_NUM} xtuner train internlm2_5_chat_7b_qlora_oasst1_e3 --deepspeed deepspeed_zero2
(SLURM) srun ${SRUN_ARGS} xtuner train internlm2_5_chat_7b_qlora_oasst1_e3 --launcher slurm --deepspeed deepspeed_zero2
--deepspeed
意味着使用 DeepSpeed 来优化训练。XTuner 带有多种集成策略,包括 ZeRO-1、ZeRO-2 和 ZeRO-3。如果想禁用此功能,只需删除此参数即可。
3.32 phi3_mini_4k_instruct 微调
可以通过以下方式使用 alpaca 数据集启动 phi3_mini_4k_instruct 的 QLoRA 微调 :
单GPU微调:
xtuner train phi3_mini_4k_instruct_qlora_alpaca_e3 --deepspeed deepspeed_zero2
需要使用其余模型则改为对应配置!已有配置在 xtuner_example/xtuner/config 下。
**可能的问题:
Error1 : ''Adafactor is already registered in optimizer at torch.optim''
command error: ''Adafactor is already registered in optimizer at torch.optim''
解决方案:降级 pytorch 版本到 2.4.1
pip install torch==2.4.1 torchvision==0.19.1 torchaudio==2.4.1
评论