Skip to content

refactor(dev): delegate DP balancing to TrainableModel and drop recipe-level duplicates - #10502

Open
lincq2000 wants to merge 3 commits into
modelscope:dev-swift-v5from
lincq2000:feat/dp-balance-switch
Open

lincq2000 wants to merge 3 commits into
modelscope:dev-swift-v5from
lincq2000:feat/dp-balance-switch

Conversation

@lincq2000

@lincq2000 lincq2000 commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

PR type

  • Bug Fix
  • New Feature
  • Document Updates
  • More Models or Datasets Support

PR 说明

关联 issue:无。依赖 modelscope/twinkle#367;替换 #10517 / #10546 在 recipe 层的接入。

统一使用 TrainableModel 内置、默认开启的 DP 负载均衡,移除各 recipe 中独立的规划和重排逻辑。本 PR 负责 Swift 接入;planner、后端布局估算、loss 安全检查、引用调度及输出顺序还原统一由 Twinkle 实现。

整体方案

  • 统一开关与构造路径。 TrainConfig.dp_balance 默认 True,统一使用 --dp_balance false 关闭。Transformers 系列及 Megatron builder 均将该配置传给模型,覆盖普通 HF、Unsloth 和 SentenceTransformer 路径;独立的 SentenceTransformer、multi-LoRA SentenceTransformer 构造函数也初始化公共基类状态。共用接口不代表强制对 embedding 等不适用目标重排。
  • 移除 recipe 层重复实现。 删除 recipe/batching.py,以及 GRPO / PPO / RFT / 蒸馏中的 planner 调用、逐样本信号手动重排和 recipe 单独维护的均衡指标。recipe 按原始顺序提交已对齐的 optimizer batch,由模型在 DP 分发前统一规划,并在返回时还原逐样本输出顺序。
  • 移除第二套开关。 删除 RLConfig.balance_batch、对应的仅 Ray / 在线训练可用校验,以及 StepState 中重复的开关状态。原 --balance_batch 用户迁移到 --dp_balance;local / Ray 的适用性由模型、后端和 loss 的公共约束判断。
  • 保留异步长度元数据。 保留 RolloutSample / DistillRow 的 token_lengths,通过轻量公共函数生成模型调用的 input_lengths,不拉取远端编码特征。GRPO、PPO 策略/价值模型、RFT 和蒸馏按同一原始顺序传递元数据与训练信号,引用列表的分片和 worker 侧解析由 Twinkle 负责。
  • 沿用正常指标链路。 model.calculate_metric 返回的 dp_balance/* 指标进入原有训练日志;PPO 原有的价值模型指标收集路径继续保留。
  • 默认开启,安全条件下生效。 开关对所有训练入口默认开启,不只是 RL。按 rank 局部 token 求均值、依赖 batch 统计的 loss,以及缺少长度元数据的远端输入保持原顺序。Megatron 当前仅对等样本数、完整且不打散样本组的固定 micro-batch 布局启用均衡,不改变原有流水线调度和 loss 归一化语义。

各训练任务的生效范围

任务 默认行为
SFT(默认 CE reduction='sum')、RFT 参与均衡
DPO(无 SFT 辅助项)、SimPO、KTO、Reward Model 参与均衡,成对样本不拆开
GRPO、PPO 策略模型(含 dual-clip、overlong 过滤、rollout 重要性采样、off-policy 序列掩码)、CHORD 的 RL 分支 参与均衡
Pointwise reranker 参与均衡
PPO 价值模型、GKD / OPSD / MOPD、GRPO 的 entropy 分位数过滤 / FIPO / SDAR、带 SFT 辅助项的 DPO、InfoNCE 等 embedding / listwise reranker 保持原顺序(重排会改变梯度)

与 #10517 / #10546 的关系

本 PR 替换这两个 PR 在 recipe 层接入的均衡逻辑,同时保留异步样本长度元数据的传递能力,统一为模型层的一套实现和一个开关。迁移应与 Twinkle #367 配套完成,无需单独兼容。

与 #10517 的差异:#10517 对 PPO 价值模型和 GKD / OPSD / MOPD 学生 rollout 也做均衡,但不保证 loss 归一化不变;本 PR 对这些按 rank 局部 token 求均值的 loss 默认保持原顺序,避免开关默认开启时改变训练梯度。

验证

在 swift/dev/tests 下运行(配合 Twinkle #367 分支):

python -m pytest -q \
  component/recipe/test_batch_balancing.py \
  component/recipe/test_grpo_micro_batch.py \
  component/recipe/test_recipe_micro_batch.py \
  component/recipe/test_iteration_metrics.py \
  component/builders/test_dp_balance.py \
  component/config/test_cli_mapping.py \
  feature/rl/test_distill_assembly.py

结果:240 passed。

覆盖默认开启/显式关闭的 CLI 解析,HF / Unsloth / SentenceTransformer / Megatron 构造参数传递,异步引用身份与长度元数据、逐样本训练信号对齐、replay / GA 更新窗口、PPO 策略/价值模型调用、蒸馏组装及既有指标日志路径。git diff --check 通过;相较 rebase 后的分支基线,无新增 Ruff 诊断。

配套 Twinkle PR 的后端布局、安全性、Ray 与 Gloo 相关测试共 292 项通过。本 PR 使用了 Claude Code 辅助完成。

Experiment results

环境:4 × 昇腾 910B,Python 3.12,torch 2.10.0 / torch_npu 2.10.0.post4,transformers 5.14.1,vLLM 0.31.0 / vllm-ascend 0.19.1rc2,megatron-core 0.18.2 / MindSpeed 26.2.0,flash-linear-attention 0.5.2,ray 2.59.0

配置:Qwen3.5-4B GRPO,GSM8K,LoRA(r=16,alpha=32,all-linear),bf16,DP=2。每轮 rollout 8 条 prompt × 8 个采样,每个优化步 32 样本;最大生成长度 2048。FSDP2 使用动态 batching(每个 micro-batch 8192 token);Megatron 使用固定 micro-batch(每 rank 2 行,梯度累积 8 步)。采用同一随机种子,仅变化 dp_balance 。

后端 dp_balance 训练耗时(ms / 1k tokens) 不均衡度(max / mean) iteration 耗时 平均 reward
FSDP2 false 1405 1.171 411.9 s 0.645
FSDP2 true 1158(1.21x) 1.010 414.1 s 0.624
Megatron false 1170 1.175 404.2 s 0.629
Megatron true 1102(1.06x) 1.018 403.6 s 0.632

lincq2000 and others added 2 commits November 12, 2026 17:13
Passes TrainConfig.dp_balance to twinkle's TransformersModel, which by
default reorders each Ray slice_dp batch so the DP ranks get similar
workloads. --dp_balance false turns it off.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
Pass the default-on dp_balance switch through Transformers, Unsloth, SentenceTransformer and Megatron construction. Initialize independent sentence-model constructors with the shared base state.

Remove recipe-level planners, permutations and balance_batch configuration. Forward streaming lengths without fetching feature refs, retain model metric collection, and cover switch propagation and unchanged recipe signal ordering.
@lincq2000
lincq2000 force-pushed the feat/dp-balance-switch branch from 29ac326 to cc7d95e Compare November 12, 2026 09:36
@lincq2000 lincq2000 changed the title feat(dev-swift-v5): Add --dp_balance switch for twinkle DP workload balancing refactor(dev): 统一使用模型层 DP 负载均衡,移除 recipe 层重复实现 Nov 12, 2026
@lincq2000 lincq2000 changed the title refactor(dev): 统一使用模型层 DP 负载均衡,移除 recipe 层重复实现 refactor(dev): delegate DP balancing to TrainableModel and drop recipe-level duplicates Nov 13, 2026
Upstream converged recipe-level balancing into batching.balance_update and
added balance_lanes for the RL lane forwards. This branch keeps balancing in
the Twinkle model instead, so the merge drops recipe/batching.py and
StepState.dp_balance, and the recipes submit every forward in the caller's
order with input_lengths taken from the batch summary's row lengths.

RFT now carries prompt + response row lengths for the update; the response
lengths still feed the reward metric.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants