XTuner 概述
一个大语言模型微调工具箱。由 MMRazor 和 MMDeploy *联合开发。
- 🤓 傻瓜化: 以 配置文件 的形式封装了大部分微调场景,0基础的非专业人员也能一键开始微调。
- 🍃 轻量级: 对于 7B 参数量的LLM,微调所需的最小显存仅为 8GB : 消费级显卡✅,colab✅*
XTuner依赖于mmengine等
支持的开源LLM (2023.11.01)
微调算法
全量无监督微调
SFT:全量有监督微调
LoRA/ QLoRA:基于低秩分解的高效微调( PEFT)
增量无监督(预训练)微调
指令微调
作用
使模型遵从指令
LLM——>Instructed LLM
微调原理
对话模板
模板是在微调时使用的,用户使用时不需要输入模板。
推理时,用户输入的部分是自动放入User部分,模板的System部分则已经指定(当然也可以切换或者自己定义)。
LoRA
LoRA: Low-Rank adapation of large language models
LLM 的参数量主要集中在模型中的 Linear,训练这些参数会耗费大量的显存
LoRA 通过在原本的 Linear 旁,新增一个支路,包含两个连续的小 Linear.新增的这个支路通常叫做Adapter
Adapter 参数量远小于原本的 Linear,能大幅降低训练的显存消耗
Adapter训练后可以合并,也可以不合并旁路分开运行。
LoRA/ QLoRA:基于低秩分解的高效微调
XTuner快速上手
XTuner简介
微调操作概览
自带训练模板
自定义训练模板
微调模型加载对话
加载Adapater文件
基于插件的对话
工具/plugin对话
msagent
MSAgent-Bench 数据集
数据引擎
数据处理流程
数据格式推荐使用Json/Jsonl
数据映射
样本拼接/切割(高效利用显存)
修改为自定义数据集
默认配置(8GB显存微调)
deepspeed不是默认启动,需要额外的参数启动
实战—:医疗问答 Medication QA
数据概览
问题 | 答案 |
---|---|
What are ketorolac eye drops?(什么是酮咯酸滴眼液?) | Ophthalmic ketorolac is used to treat itchy eyes caused by allergies. It also is used to treat swelling and redness (inflammation) that can occur after cataract surgery. Ketorolac is in a class of medications called nonsteroidal anti-inflammatory drugs (NSAIDs). It works by stopping the release of substances that cause allergy symptoms and inflammation. |
What medicines raise blood sugar? (什么药物会升高血糖?) | Some medicines for conditions other than diabetes can raise your blood sugar level. This is a concern when you have diabetes. Make sure every doctor you see knows about all of the medicines, vitamins, or herbal supplements you take. This means anything you take with or without a prescription. Examples include: Barbiturates. Thiazide diuretics. Corticosteroids. Birth control pills (oral contraceptives) and progesterone. Catecholamines. Decongestants that contain beta-adrenergic agents, such as pseudoephedrine. The B vitamin niacin. The risk of high blood sugar from niacin lowers after you have taken it for a few months. The antipsychotic medicine olanzapine (Zyprexa). |
格式转化和数据划分
原格式:(.xlsx)
问题 | 药物类型 | 问题类型 | 回答 | 主题 | URL |
---|---|---|---|---|---|
aaa | bbb | ccc | ddd | eee | fff |
目标格式:(.jsonL)
[{
"conversation":[
{
"system": "xxx",
"input": "xxx",
"output": "xxx"
}
]
},
{
"conversation":[
{
"system": "xxx",
"input": "xxx",
"output": "xxx"
}
]
}]
通过 python脚本:将 .xlsx
中的 问题 和 回答 两列 提取出来,再放入 .jsonL
文件的每个 conversation 的 input 和 output 中。
同时对数据进行训练集和测试集的分割
执行 python 脚本,获得格式化后的数据集:
python xlsx2jsonl.py
修改配置文件
# 复制配置文件到当前目录
xtuner copy-cfg internlm_chat_7b_qlora_oasst1_e3 .
# 改个文件名
mv internlm_chat_7b_qlora_oasst1_e3_copy.py internlm_chat_7b_qlora_medqa2019_e3.py
减号代表要删除的行,加号代表要增加的行。
# 修改import部分
- from xtuner.dataset.map_fns import oasst1_map_fn, template_map_fn_factory
+ from xtuner.dataset.map_fns import template_map_fn_factory
# 修改模型为本地路径
- pretrained_model_name_or_path = 'internlm/internlm-chat-7b'
+ pretrained_model_name_or_path = './internlm-chat-7b'
# 修改训练数据为 MedQA2019-structured-train.jsonl 路径
- data_path = 'timdettmers/openassistant-guanaco'
+ data_path = './MedQA2019/MedQA2019-structured-train.jsonl'
# 修改 train_dataset 对象
train_dataset = dict(
type=process_hf_dataset,
- dataset=dict(type=load_dataset, path=data_path),
+ dataset=dict(type=load_dataset, path='json', data_files=dict(train=data_path)),
tokenizer=tokenizer,
max_length=max_length,
- dataset_map_fn=alpaca_map_fn,
+ dataset_map_fn=None,
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
remove_unused_columns=True,
shuffle_before_pack=True,
pack_to_max_length=pack_to_max_length)
进行微调
xtuner train internlm_chat_7b_qlora_medqa2019_e3.py
超参数:
GPU使用情况:
微调过程
生成微调模型文件
将得到的 PTH 模型转换为 HuggingFace 模型,即:生成 Adapter 文件夹
xtuner convert pth_to_hf ${CONFIG_NAME_OR_PATH} ${PTH_file_dir} ${SAVE_PATH}
在本示例中,为:
mkdir hf
cd hf
mkdir epoch50
export MKL_SERVICE_FORCE_INTEL=1
xtuner convert pth_to_hf internlm_chat_7b_qlora_medqa_e3.py ./work_dirs/internlm_chat_7b_qlora_medqa_e3/epoch_50.pth ./hf/epoch50
目标文件夹中生成的就是的huggingface格式
|-- hf
| |-- adapter_config.json
| |-- adapter_model.bin
此时,hf 文件夹即为我们平时所理解的所谓 “LoRA 模型文件”
可以简单理解:LoRA 模型文件 = Adapter
合并后使用
xtuner convert merge
xtuner convert merge /root/model/Singa_Model/internlm-chat-7b ./hf/epoch50 ./merge/epoch50 --max-shard-size 2GB
xtuner chat
# 加载 Adapter 模型对话(Float 16)
xtuner chat ./merged --prompt-template internlm_chat
-
xtuner chat参数
注意:
使用xtuner chat的模型路径
使用xtuner chat时模型路径必须为一层路径,不能有多层,否则会识别为线上的repo
即必须要在当前模型文件夹必须在当前目录