XTuner 模型微调,打造个人小助手

XTuner 微调实践微调

在这里插入图片描述

本文档将介绍 InternLM 个人小助手认知

写在前面

微调内容需要使用 30% A100 才能完成。
本次实战营的微调内容包括了以下两个部分:

  1. SFT 数据的获取
  2. 使用 InternLM2.5-7B-Chat 模型微调

这节课你会收获:

  • 针对业务场景(如特殊自我认知的机器人)的微调能力
  • 一个属于自己的语言聊天机器人

XTuner 文档链接:XTuner-doc-cn

环境配置与数据准备

本节中,我们将演示如何安装 XTuner。
推荐使用 Python-3.10 的 conda 虚拟环境安装 XTuner。

步骤 0. 使用 conda 先构建一个 Python-3.10 的虚拟环境

cd ~
#git clone 本repo
git clone https://github.com/InternLM/Tutorial.git -b camp4
mkdir -p /root/finetune && cd /root/finetune
conda create -n xtuner-env python=3.10 -y
conda activate xtuner-env

步骤 1. 安装 XTuner

此处推荐用我 freeze 的 requirements.txt,更多的安装方法请回到前面看 XTuner 文档

cd /root/Tutorial/docs/L1/XTuner
pip install -r requirements.txt
如果安装过程出现错误,请参考以下解决方案: > WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1007)'))': /pypi/simple/bitsandbytes/

Could not fetch URL https://mirrors.aliyun.com/pypi/simple/bitsandbytes/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host=‘mirrors.aliyun.com’, port=443): Max retries exceeded with url: /pypi/simple/bitsandbytes/ (Caused by SSLError(SSLCertVerificationError(1, ‘[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1007)’))) - skipping

INFO: pip is looking at multiple versions of xtuner to determine which version is compatible with other requirements. This could take a while.

ERROR: Could not find a version that satisfies the requirement bitsandbytes>=0.40.0.post4 (from xtuner) (from versions: none),可以 Ctrl + C 退出后换成 pip install --trusted-host mirrors.aliyun.com -e '.[deepspeed]' -i https://mirrors.aliyun.com/pypi/simple/

验证安装

为了验证 XTuner 是否安装正确,我们将使用命令打印配置文件。

打印配置文件: 在命令行中使用 xtuner list-cfg 验证是否能打印配置文件列表。

xtuner list-cfg
输出没有报错则为此结果

xtuner list-cfg
CONFIGS=
baichuan2_13b_base_full_custom_pretrain_e1
baichuan2_13b_base_qlora_alpaca_e3
baichuan2_13b_base_qlora_alpaca_enzh_e3
baichuan2_13b_base_qlora_alpaca_enzh_oasst1_e3

internlm2_1_8b_full_alpaca_e3
internlm2_1_8b_full_custom_pretrain_e1
internlm2_1_8b_qlora_alpaca_e3
internlm2_20b_full_custom_pretrain_e1
internlm2_20b_full_finetune_custom_dataset_e1
internlm2_20b_qlora_alpaca_e3
internlm2_20b_qlora_arxiv_gentitle_e3
internlm2_20b_qlora_code_alpaca_e3
internlm2_20b_qlora_colorist_e5
internlm2_20b_qlora_lawyer_e3
internlm2_20b_qlora_msagent_react_e3_gpu8
internlm2_20b_qlora_oasst1_512_e3
internlm2_20b_qlora_oasst1_e3
internlm2_20b_qlora_sql_e3
internlm2_5_chat_20b_alpaca_e3
internlm2_5_chat_20b_qlora_alpaca_e3
internlm2_5_chat_7b_full_finetune_custom_dataset_e1
internlm2_5_chat_7b_qlora_alpaca_e3
internlm2_5_chat_7b_qlora_oasst1_e3
internlm2_7b_full_custom_pretrain_e1
internlm2_7b_full_finetune_custom_dataset_e1
internlm2_7b_full_finetune_custom_dataset_e1_sequence_parallel_4
internlm2_7b_qlora_alpaca_e3
internlm2_7b_qlora_arxiv_gentitle_e3
internlm2_7b_qlora_code_alpaca_e3
internlm2_7b_qlora_colorist_e5
internlm2_7b_qlora_json_e3
internlm2_7b_qlora_lawyer_e3
internlm2_7b_qlora_msagent_react_e3_gpu8
internlm2_7b_qlora_oasst1_512_e3
internlm2_7b_qlora_oasst1_e3
internlm2_7b_qlora_sql_e3

输出内容为 XTuner 支持微调的模型

修改提供的数据

步骤 0. 创建一个新的文件夹用于存储微调数据

mkdir -p /root/finetune/data && cd /root/finetune/data
cp -r /root/Tutorial/data/assistant_Tuner.jsonl  /root/finetune/data
此时 `finetune` 文件夹下应该有如下结构
finetune
├── data
│   └── assistant_Tuner.jsonl
└── xtuner

步骤 1. 创建修改脚本

我们写一个脚本生成修改我们需要的微调训练数据,在当前目录下创建一个 change_script.py 文件,内容如下:

# 创建 `change_script.py` 文件
touch /root/finetune/data/change_script.py

打开该change_script.py文件后将下面的内容复制进去。</

### 使用 XTuner 进行大模型微调的方法教程 #### 1. 构建数据集 为了使模型能够学习特定的任务或行为,首先需要准备高质量的数据集。根据需求,可以创建一个对话形式的数据集,其中包含提问和对应的理想回答。这些数据将用于指导模型的学习方向[^1]。 ```bash # 数据集应保存为 JSONL 文件格式,每条记录是一个独立的对话样本。 { "instruction": "解释什么是机器学习", "input": "", "output": "机器学习是一种通过算法让计算机从经验中自动改进的技术..." } ``` #### 2. 配置文件编写 XTuner 的配置文件定义了训练的具体参数以及使用的模型架构等信息。以 `internlm_chat_7b` 模型为例,可以通过修改现有的 `.py` 配置模板来适配自己的任务场景[^3]。 以下是简化版配置文件的一个片段: ```python from xtuner.engine import Config model = dict( type='InternLM', version='chat-7b' ) train_dataset_type = 'CustomDataset' data_root = '/path/to/dataset' max_epochs = 3 per_device_train_batch_size = 4 gradient_accumulation_steps = 8 learning_rate = 5e-5 weight_decay = 0.01 warmup_ratio = 0.05 lr_scheduler_type = 'cosine' logging_dir = './logs' save_total_limit = 3 checkpointing_steps = 1000 fp16 = True bf16 = False deepspeed_config_path = "/root/deepspeed_zero2.json" work_dir = "./work_dirs/assistTuner" cfg = Config(locals()) ``` #### 3. 开始微调过程 利用 XTuner 提供的命令行工具可以直接运行指定配置下的微调流程。下面展示了一个典型的执行指令案例: ```bash xtuner train ./config/internlm2_5_chat_7b_qlora_alpaca_e3_copy.py \ --deepspeed deepspeed_zero2 \ --work-dir ./work_dirs/assistTuner ``` 此命令会加载预设好的超参设置并基于 DeepSpeed 技术加速计算效率,在工作目录下逐步存储中间结果与最终完成后的权重文件。 #### 4. 转换至 Hugging Face 格式 当微调完成后如果希望分享成果或者进一步部署应用,则可能需要用到标准框架支持的形式。XTuner 支持把内部生成的结果导出成兼容 Hugging Face Transformers 库的标准结构化存档[^2]。 转换操作如下所示: ```bash xtuner convert-to-hf-model \ ./work_dirs/assistTuner/best_model.pth \ /root/output/huggingface_model/ ``` 这样就可以轻松地与其他依赖该生态系统的项目集成起来了。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值