Xtuner

A Next-Generation Training Engine Built for Ultra-Large MoE Models

Last verified:

Visit Xtuner

What is Xtuner?

XTuner is an efficient, flexible, and full-featured lightweight toolkit for fine-tuning large language models (LLMs) and multimodal vision-language models (VLMs), developed by the InternLM team at Shanghai AI Laboratory. It supports both pre-training and lightweight fine-tuning for various model types, including continuous pre-training, instruction fine-tuning, and agent fine-tuning.

Key features include support for multiple fine-tuning algorithms such as QLoRA, LoRA, and full-parameter fine-tuning, allowing users to choose the optimal approach for their needs. XTuner is compatible with DeepSpeed and automatically optimizes performance using high-performance operators like FlashAttention and Triton kernels. It provides built-in configuration files for common scenarios, model conversion tools (PTH to HuggingFace format), LoRA merging capabilities, and a built-in chat interface for testing fine-tuned models.

XTuner is designed for AI researchers, machine learning engineers, developers, and anyone interested in adapting large models to specific tasks. It supports both single GPU setups (can fine-tune a 7B LLM on just 8GB VRAM) and multi-node distributed training for models exceeding 70B parameters. The toolkit integrates seamlessly with deployment tools like LMDeploy and evaluation libraries such as OpenCompass and VLMEvalKit.

The tool supports various open-source models including InternLM series, Llama, and can load models/datasets from both HuggingFace and ModelScope. It supports multiple data formats including Alpaca format and conversation format, making it versatile for different fine-tuning scenarios.

Xtuner pricing

Pricing model: Freemium

XTuner是开源免费的工具库,通过pip安装使用。没有付费计划或商业版本。安装命令为pip install xtuner或pip install -U 'xtuner[deepspeed]>=0.1.22'(集成DeepSpeed版本)。所有功能包括多种微调算法、DeepSpeed兼容、模型转换工具、chat工具等均为免费提供。

Xtuner pros

  • 支持在8GB显存下微调7B模型,适合消费级显卡
  • 支持QLoRA、LoRA和全量参数微调等多种算法
  • 兼容DeepSpeed,支持ZeRO-1/2/3等多种优化策略
  • 自动优化使用FlashAttention和Triton kernels加速训练
  • 提供多个开箱即用的预置配置文件
  • 支持单卡和单机多卡训练,以及SLURM分布式训练
  • 支持连续预训练、指令微调和agent微调
  • 内置模型转换工具,可将PTH格式转为HuggingFace格式
  • 内置LoRA权重合并工具,方便模型部署
  • 提供与微调后模型对话的chat工具
  • 支持HuggingFace和ModelScope模型/数据集加载
  • 支持多种数据格式包括Alpaca和conversation格式
  • 支持多模态图文模型(VLM)的预训练及微调
  • 支持NVIDIA 20系以上所有显卡
  • 训练模型可无缝接入LMDeploy部署和OpenCompass评测
  • 支持通过MMEngine进行训练指标可视化跟踪
  • 配置文件形式封装,零基础用户也能一键微调
  • 支持自定义优化器参数和训练配置修改
  • 支持调整最大epoch、checkpoint保存策略等训练参数

Xtuner cons

  • 主要提供中文文档,英文文档相对较少
  • 需要手动修改配置文件适配特定模型和数据集
  • QLoRA训练速度比全量参数训练慢
  • 初次使用需要理解配置文件结构有一定学习曲线
  • PTH格式转换和LoRA合并需要额外步骤
  • 对非常新的模型可能需要手动配置prompt_template
  • 训练日志输出较 verbose,信息量大
  • 多卡训练需要配置启动命令而非配置文件

Frequently asked questions about Xtuner

XTuner是什么?

XTuner是由上海AI实验室的InternLM团队推出的一款高效、灵活、全能的轻量化大模型微调工具库。它支持大语言模型(LLM)和多模态图文模型(VLM)的预训练及轻量级微调,可在8GB显存下微调7B模型,也支持多节点跨设备微调70B+更大尺度模型。

XTuner支持哪些微调算法?

XTuner支持多种微调算法,包括QLoRA、LoRA和全量参数微调。用户可以根据具体需求选择最优方案。QLoRA通过将模型量化为4bit并智能调度到CPU,大幅降低显存消耗;LoRA在保持原模型参数不变的情况下新增可训练分支;全量参数微调则训练所有参数。

如何在单卡上启动微调?

使用命令'xtuner train ${CONFIG_NAME_OR_PATH}'即可在单卡上启动微调。例如:'xtuner train internlm2_5_chat_7b_qlora_oasst1_e3 --deepspeed deepspeed_zero2'。可以添加--deepspeed参数使用DeepSpeed优化训练过程。

XTuner支持多卡训练吗?

是的,XTuner支持多卡训练。使用DIST方式:'NPROC_PER_NODE=${GPU_NUM} xtuner train ${CONFIG}';使用SLURM方式:'srun ${SRUN_ARGS} xtuner train ${CONFIG} --launcher slurm'。多卡训练由启动命令决定,而非配置文件。

训练完成后如何转换模型格式?

训练完成后会得到.pth文件,需要使用'xtuner convert pth_to_hf ${CONFIG_NAME_OR_PATH} ${PTH} ${SAVE_PATH}'将PTH模型转换为HuggingFace格式。然后使用'xtuner convert merge'将LoRA权重合并到原模型中。

XTuner支持哪些大模型?

XTuner支持与InternLM(书生·浦语)、Llama等多款开源大模型的适配。可以通过修改配置文件中的pretrained_model_name_or_path来使用其他LLM模型。支持加载HuggingFace和ModelScope的模型。

XTuner支持哪些数据格式?

XTuner支持多种数据格式,包括Alpaca格式(包含instruction、input、output字段)和对话格式(conversation数组,包含role和content)。用户可以根据数据集特点选择合适的格式。

如何与微调后的模型对话?

使用'xtuder chat ${NAME_OR_PATH_TO_LLM}'命令。可以指定--prompt-template指定对话模板,--system-template指定System Prompt。既可以与合并权重后的模型对话,也可以直接与LLM + LoRA Adapter对话。

XTuner的硬件要求是什么?

XTuner最低只需8GB显存即可微调7B参数量的LLM,满足大部分消费级显卡(NVIDIA 20系以上所有显卡)。同时支持多节点跨设备微调更大尺度模型(70B+),适用于传统数据中心如Tesla T4、A100等。

XTuner如何与部署和评测工具集成?

训练所得模型可无缝接入部署工具库LMDeploy、大规模评测工具库OpenCompass及VLMEvalKit。XTuner还支持通过MMEngine使用SwanLab进行在线跟踪,可以跟踪和可视化损失、显存占用等指标。

Categories

Use cases

Browse all AI tools on NeedAnAI