跳到主要内容

模型微调:基于DeepSpeed

1. 演练目标与架构​

本节基于摩尔线程MTT S5000 GPU 完成 DeepSpeed 的微调,采用 BF16 精度。

2. 环境准备​

机器信息​

在 MTT S5000 上进行 DeepSpeed 微调。宿主机使用 Linux Driver 3.3.10,容器内使用 MUSA SDK 4.3.10。

使用摩尔线程预训练容器 training-suite:v2.2.0。v2.2.0 配套 MT-Megatron 0.19.0、MT-TransformerEngine 2.13.0 和 MT-DeepSpeed 0.19.3;PyTorch 与 torch_musa 版本以容器内检查结果为准。

创建并启动微调容器​

# 创建容器
sudo docker create \
--privileged \
--env MTHREADS_VISIBLE_DEVICES=all \
--net host \
-v /data/:/data/ \
--name deepspeed \
registry.mthreads.com/mcconline/training-suite:v2.2.0 \
sleep infinity

# 启动并进入容器
sudo docker start deepspeed
sudo docker exec -it deepspeed bash

# 在容器里,启动ssh服务
service ssh restart

GPU 识别验证​

进入容器后,验证MTT S5000 GPU 的识别状态与拓扑连接:

# 查看GPU硬件识别与状态
mthreads-gmi

3. 检查 DeepSpeed​

在容器里,已经内置 DeepSpeed 0.19.3。执行 ds_report:

# ds_report
[2026-02-28 15:47:47,251] [INFO] [real_accelerator.py:258:get_accelerator] Setting ds_accelerator to musa (auto detect)
[2026-02-28 15:47:48,310] [INFO] [logging.py:107:log_dist] [Rank -1] [TorchCheckpointEngine] Initialized with serialization = False
--------------------------------------------------
DeepSpeed C++/CUDA extension op report
--------------------------------------------------
NOTE: Ops not installed will be just-in-time (JIT) compiled at
runtime if needed. Op compatibility means that your system
meet the required dependencies to JIT install the op.
--------------------------------------------------
JIT compiled ops requires ninja
ninja .................. [OKAY]
--------------------------------------------------
op name ................ installed .. compatible
--------------------------------------------------
cpu_adam ............... [YES] ...... [OKAY]
musa_fused_adam ........ [YES] ...... [OKAY]
--------------------------------------------------
DeepSpeed general environment info:
torch install path ............... ['/usr/local/lib/python3.12/dist-packages/torch']
torch version .................... 2.9.0.post1
deepspeed info ................... 0.19.3, branch/v0.19.3
deepspeed wheel compiled w. ...... torch 2.7
shared memory (/dev/shm) size .... 1.00 GB

4. 下载模型、数据​

下载模型​

可以使用 modelscope 下载模型到本地:

# 安装modelscope
pip install modelscope
# 下载模型到本地
modelscope download --model Qwen/Qwen3-4B --local_dir /data/models/Qwen3-4B

5. Qwen3 系列模型的 SFT 微调方案​

DeepSpeed 作为底层分布式训练引擎,仅提供 ZeRO 优化器、数据/模型并行等并行策略支持,并不包含模型定义、数据预处理及微调逻辑等上层训练代码。因此,针对 Qwen3 系列模型的 SFT 微调,需基于 Transformers 等框架开发偏上层的训练脚本(如 train_qwen3_sft_deepspeed.py),通过 TrainingArguments(deepspeed=...) 将 DeepSpeed 作为后端加速插件集成。

在此基础上,建议采用统一代码骨架 + 差异化配置策略的设计范式:维护单一训练脚本作为 Dense 和 MoE 模型的基础骨架,通过外部化 DeepSpeed 配置实现模型特异性适配,例如为 4B 规模配置 ZeRO-2,并通过启动脚本调整 per_device_batch_size、gradient_accumulation_steps 和学习率。统一代码骨架不等于所有模型自动获得支持,新模型仍需完成数据和并行链路验证。

v2.2.0 的 AutoEP、Qwen3.5-35B-A3B 多模态训练和 ZeRO-3 初始化修复使用新的 MUSA 测试链路,详见MT-DeepSpeed 新特性与 AutoEP。本文的 Qwen3 SFT 示例与上述新特性示例需要分别核对启动脚本和配置。

通用 SFT 训练文件​

针对本文示例使用的 Qwen3 模型,创建通用 SFT 训练文件。在 examples/qwen3/ 目录中创建 train_qwen3_sft_deepspeed.py。该脚本作为统一代码骨架,通过外部配置文件(ds_config_qwen3_4b_zero2.json)实现差异化策略。

下载脚本​

下载示例数据,这个数据集中包含多轮对话格式的训练/验证数据,适合快速测试。

cd /home/DeepSpeed/examples/
wget -c https://mt-ai-infra.tos-cn-beijing.volces.com/deepspeed_qwen3.tgz
tar zxvf deepspeed_qwen3.tgz

SFT 微调 Qwen3-4B​

examples/qwen3/
├── train_qwen3_sft_deepspeed.py # 通用训练脚本
├── run_4b_sft.sh # 4B 启动脚本
└── configs/
└── ds_config_qwen3_4b_zero2.json # 4B 专用 ZeRO-2 配置

DS 配置文件

创建 configs/ds_config_qwen3_4b_zero2.json:

{
"bf16": {
"enabled": true
},
"fp16": {
"enabled": false
},
"train_batch_size": "auto",
"train_micro_batch_size_per_gpu": "auto",
"gradient_accumulation_steps": "auto",
"gradient_clipping": "auto",
"zero_optimization": {
"stage": 2,
"offload_optimizer": {
"device": "none",
"pin_memory": true
},
"allgather_partitions": true,
"allgather_bucket_size": 200000000,
"overlap_comm": true,
"reduce_scatter": true,
"reduce_bucket_size": 200000000,
"contiguous_gradients": true
},
"wall_clock_breakdown": false
}

微调脚本

创建微调脚本:run_4b_sft.sh

cd /home/DeepSpeed/examples/qwen3/
bash run_4b_sft.sh