ALL INTELLIGENCE
蚂蚁 inclusionAI 官方仓库T1·Open Source

Ant Group Open-Sources dFactory for Diffusion LLM Fine-Tuning

By inclusionAIOriginal title: ⭐ inclusionAI/dFactory (274 stars)
62AI Score

inclusionAI has open-sourced dFactory, a fine-tuning framework for diffusion language models (dLLMs) that supports supervised fine-tuning with block-diffusion and full-attention variants. It ships with adapters for LLaDA2.0-mini (16B) and LLaDA2.0-flash (100B) instruction-tuned models, filling a gap in engineering tooling for diffusion-based LLMs.

Full Report

Contents12 sections
<h4 align="center"> </h4>

dFactory:简单高效的 dLLM 微调

特性

  • 多种模型: LLaDA2.0-mini(16B)、LLaDA2.0-flash(100B)
  • 集成方法:(连续)监督微调(块扩散、全注意力)等

支持的模型

模型 ID描述规模配置路径Hugging Face 链接
inclusionAI/LLaDA2.0-mini指令微调模型,可直接用于下游应用。16Bconfigs/model_configs/llada2_mini/🤗 模型卡片
inclusionAI/LLaDA2.0-flash指令微调模型,可直接用于下游应用。100Bconfigs/model_configs/llada2_flash/🤗 模型卡片
inclusionAI/LLaDA2.1-mini即将推出。16Bconfigs/model_configs/llada2_mini/🤗 模型卡片
inclusionAI/LLaDA2.1-flash即将推出。16Bconfigs/model_configs/llada2_flash/🤗 模型卡片
inclusionAI/LLaDA2.2-flash即将推出。16Bconfigs/model_configs/llada2_flash/🤗 模型卡片

TODO

我们正在积极为项目添加新功能并持续改进。近期路线图包括:

  • [☑️] 完善文档:完整的文档站点正在建设中,将包含深入教程、API 参考和最佳实践。
  • [☑️] 可训练并行解码:集成对可训练并行解码的支持,以支持更高级的使用场景。

敬请期待这些更新!

快速开始

0. 环境搭建

方式 A:使用 uv(推荐)

BASH
# Install uv if not already installed
curl -LsSf https://astral.sh/uv/install.sh | sh

git clone https://github.com/inclusionAI/dFactory.git --recursive
cd dFactory/VeOmni

# Install dependencies
uv sync --extra gpu

# Activate environment
source .venv/bin/activate

# Back to our workdir
cd ..

方式 B:使用 pip

BASH
git clone https://github.com/inclusionAI/dFactory.git --recursive
cd dFactory
pip install -e VeOmni/

1. 下载并合并模型权重

我们的训练脚本需要“合并专家”(merged-expert)格式的模型权重,以获得最佳性能。开始之前,你必须下载标准权重并进行转换。

1. 下载原始模型: 我们提供了一个辅助脚本,用于从 Hugging Face Hub 下载权重。

BASH
# Choose a destination for the original model files
python ./scripts/download_hf_model.py \
  --repo_id inclusionAI/LLaDA2.0-mini-preview \
  --local_dir /path/to/separate_expert_model

2. 转换为合并格式: 运行以下脚本,生成训练所需的合并检查点。

BASH
# Use the path from the previous step as the source
python scripts/moe_convertor.py \
  --input-path /path/to/separate_expert_model \
  --output-path /path/to/save/merged_model \
  --mode merge

目录 /path/to/save/merged_model 就是你在训练脚本中要使用的路径。更多细节请参见 MoE 专家合并与拆分工具

2. 准备训练数据

训练之前,必须先准备好数据集。本教程使用 openai/gsm8k 数据集,并演示如何将其转换为对话格式。

我们为此提供了一个示例脚本 ./scripts/build_gsm8k_dataset.py。你可以改编该脚本或编写自己的脚本来处理其他数据集。

运行以下命令即可执行该脚本。它会将 "question" 和 "answer" 字段转换为对话式的 messages 字段。处理后的数据集随后会保存到 ./gsm8k_datasets/ 目录下,并拆分为两个独立文件:用于训练的 train.jsonl 和用于评估的 test.jsonl。

BASH
python ./scripts/build_gsm8k_dataset.py

3. 修改训练配置

编辑 configs/sft/llada2_mini_bd_sft.yaml:

YAML
model:
  model_path: "/your/model/path"
data:
  train_path: "/your/data/path"
train:
  output_dir: "/your/output/path"

4. 运行训练

所有准备工作完成后,你现在可以通过一条命令启动微调流程:

BASH
PYTHONPATH=$(pwd)/VeOmni:$PYTHONPATH sh train.sh tasks/train_llada2_bd.py configs/sft/llada2_mini_bd_sft.yaml

5. 与微调后的模型交互

要与微调后的模型交互,你必须完成两个主要步骤:转换 checkpoint 和复制建模文件。

步骤 1:转换 Checkpoint

首先,你需要将 checkpoint 从训练时使用的合并格式转换回标准的混合专家(MoE)结构。

重要:找到正确的输入路径

转换脚本的 --input-path 是保存的 Hugging Face checkpoint 的路径,而不是你在训练时指定的根输出目录。该 checkpoint 通常位于类似下面的子目录中:

TRAIN_OUTPUT_DIR/checkpoints/global_step_XXX/hf_ckpt/

运行以下命令执行转换:

BASH
python scripts/moe_convertor.py \
  --input-path /path/to/merged_model \
  --output-path /path/to/save/separate_expert_model \
  --mode split

步骤 2:复制建模文件

转换完成后,还需要进行最后一步手动操作。你必须将模型的架构文件(例如 modeling_llada2_moe.py)复制到新创建的 separate_expert_model 目录中。

该文件必须来自你的原始基础模型目录——即在进行任何合并或训练操作之前所使用的那个模型。训练和转换过程只会更新模型权重,而不会更新架构文件,这就是为什么需要使用原始版本。

BASH
# 示例:从初始的、合并前的模型目录复制
cp /path/to/original_base_model/modeling_llada2_moe.py /path/to/save/separate_expert_model/

模型已完成转换,建模文件也已就位,现在你可以开始对话了!请按照官方模型卡上的说明,开始与你的模型进行对话。

(注:更多技术实现细节与完整 API 文档请查阅原项目 README)