文章目录
环境背景:Ubuntu24.04 系统 + Python3.10 + Cuda12.4 + Cudnn9.1 + torch2.6.0 + transformers4.48
测试环境 requirements.txt 下载
一、介绍
Xtuner 是一款用来微调大语言模型(LLM)的工具。在微调的过程中,用户可以直观地看到验证数据的训练情况。因此,Xtuner 更适合用来微调主观类的数据。
二、安装使用
2.1 构建虚拟环境
推荐使用 Python-3.10 的 conda 虚拟环境安装 XTuner
2.1.1 创建虚拟环境
conda create -n xtuner python=3.10 -y && conda activate xtuner
2.1.2 安装Xtuner
- 方案a:通过 pip 直接安装
pip install -U 'xtuner[deepspeed]'
- 方案b:通过源码安装【推荐】
# 拉取 XTuner
git clone <https://github.com/InternLM/xtuner.git>
cd xtuner
pip install -e '.[all]'
2.2 下载模型
此文以从 modelscope 平台下载 Qwen/Qwen1.5-0.5B-Chat 模型为例
from modelscope import snapshot_download
model_dir = snapshot_download('Qwen/Qwen1.5-0.5B-Chat',cache_dir='/root/work/ai/models/modelscope')
2.3 微调准备
2.3.1 创建微调训练配置文件
在 xtuner 的文件夹下找到 xtuner/xtuner/configs/qwen/qwen1_5/qwen1_5_0_5b_chat/qwen1_5_0_5b_chat_qlora_alpaca_e3.py 文件,将该文件复制一份至 xtuner/ 根目录(如果使用其他模型,需要找到对应模型的配置文件)
配置文件名中,有形如 <model>_full_*.py、<model>_qlora_*.py、<model>_int8_lora*.py 的不同文件,作以下说明:
-
full:表示对整个模型的所有参数进行微调
-
qlora:基于量化+LoRA的低资源微调
model = dict( type=SupervisedFinetune, use_varlen_attn=use_varlen_attn, llm=dict( type=AutoModelForCausalLM.from_pretrained, pretrained_model_name_or_path=pretrained_model_name_or_path, trust_remote_code=True, torch_dtype=torch.float16, quantization_config=dict( type=BitsAndBytesConfig, load_in_4bit=True, # 是否开启4位QLoRA量化 load_in_8bit=False, # 是否开启8位QLoRA量化 l

最低0.47元/天 解锁文章
2940

被折叠的 条评论
为什么被折叠?



