本文档描述 HyperParallel Expert Parallel 与 FSDP(Fully Sharded Data Parallelism)的组合验证、以及 ExpertParallel 在流水线并行多微批次场景下的状态管理安全问题,不包含具体代码实现,侧重背景、职责边界、接口契约、测试与验收。
ExpertParallel
大规模 MoE 模型训练通常需要同时启用多种并行策略:
三者叠加时,EP 所在的 ExpertParallel 并行样式需要在多种 hook 执行序列、参数 unshard/shard 生命周期、以及多微批次并发调用的环境中保持正确性。
hyper_parallel/core/expert_parallel/expert_parallel.py 的 ExpertParallel 通过 distribute_module 在 GroupedExperts 子模块上注册:
hyper_parallel/core/expert_parallel/expert_parallel.py
distribute_module
GroupedExperts
_token_dispatch
self._input_splits
self._output_splits
self._input_shape
self._permuted_indices
_token_combine
文档 docs/expert_parallel.md 已提及与 FSDP 的兼容性意图,但缺乏分布式集成测试与对状态安全的显式保证。
docs/expert_parallel.md
_token_dispatch 将本次 forward 的中间状态写入 self._* 实例变量,_token_combine 在同一次 forward 结束时消费这些变量。在单微批次场景下,两个 hook 严格配对,行为正确。
self._*
在流水线并行多微批次场景下:
N
N+1
FSDP 对被包装模块注册自身的 pre-hook(参数 unshard)和 post-hook(参数 re-shard)。ExpertParallel 的 _token_dispatch / _token_combine 同样通过 pre/post hook 接入。两者叠加时:
上述顺序在当前代码中均未经过明确测试。
FSDP 通过 reduce-scatter 聚合梯度,EP 的 combine 反向(即 differentiable_all_to_all_single 的反向传播)也产生针对 GroupedExperts 权重的梯度贡献。两者之间是否会在某些分片维度上产生重复归约,尚未有实测验证。
reduce-scatter
differentiable_all_to_all_single
当前:_token_dispatch 写 self._*,_token_combine 读 self._*。
目标:
(transformed_inputs, dispatch_ctx)
dispatch_ctx
(module, output, dispatch_ctx, device_mesh)
要求:
self
MoE.forward
ExpertParallel.apply
MoE
MoE.experts
上述顺序约束需在 docs/expert_parallel.md 中以使用注意事项形式明确说明,并在测试中断言。
ExpertParallel、ExpertTensorParallel、TensorParallel 的 apply(module, device_mesh) 接口签名保持不变;内部状态传递方式的变更对调用方不可见。
ExpertTensorParallel
TensorParallel
apply(module, device_mesh)
functools.partial
ExpertParallel._token_dispatch
hyper_parallel/core/fully_shard/
FSDP + EP 组合支持与 Pipeline 微批次状态安全
本文档描述 HyperParallel Expert Parallel 与 FSDP(Fully Sharded Data Parallelism)的组合验证、以及
ExpertParallel在流水线并行多微批次场景下的状态管理安全问题,不包含具体代码实现,侧重背景、职责边界、接口契约、测试与验收。1. 背景
1.1 训练配置演进
大规模 MoE 模型训练通常需要同时启用多种并行策略:
三者叠加时,EP 所在的
ExpertParallel并行样式需要在多种 hook 执行序列、参数 unshard/shard 生命周期、以及多微批次并发调用的环境中保持正确性。1.2 现有实现
hyper_parallel/core/expert_parallel/expert_parallel.py的ExpertParallel通过distribute_module在GroupedExperts子模块上注册:_token_dispatch(pre-hook):执行计数 all-to-all、token all-to-all、permute,将中间状态存入 实例变量self._input_splits、self._output_splits、self._input_shape、self._permuted_indices;_token_combine(post-hook):读取上述实例变量执行 unpermute 和反向 all-to-all。文档
docs/expert_parallel.md已提及与 FSDP 的兼容性意图,但缺乏分布式集成测试与对状态安全的显式保证。2. 已知问题
2.1 实例变量状态在多微批次场景下不安全
_token_dispatch将本次 forward 的中间状态写入self._*实例变量,_token_combine在同一次 forward 结束时消费这些变量。在单微批次场景下,两个 hook 严格配对,行为正确。在流水线并行多微批次场景下:
N个微批次的_token_combine执行之前,即发起第N+1个微批次的_token_dispatch;N个微批次的_token_combine读取到错误状态;2.2 FSDP 与
distribute_moduleHook 顺序未验证FSDP 对被包装模块注册自身的 pre-hook(参数 unshard)和 post-hook(参数 re-shard)。
ExpertParallel的_token_dispatch/_token_combine同样通过 pre/post hook 接入。两者叠加时:_token_dispatch之后执行,则 dispatch 阶段可能访问尚未 unshard 的专家权重;_token_combine之前触发,combine 结果写入时权重已被释放。上述顺序在当前代码中均未经过明确测试。
2.3 FSDP 梯度归约与 EP Combine 反向梯度的重叠
FSDP 通过
reduce-scatter聚合梯度,EP 的 combine 反向(即differentiable_all_to_all_single的反向传播)也产生针对GroupedExperts权重的梯度贡献。两者之间是否会在某些分片维度上产生重复归约,尚未有实测验证。3. 目标与非目标
3.1 目标
_token_dispatch与_token_combine之间的中间状态从实例变量迁移为调用栈局部传递(dispatch 返回 context 对象,combine 接收该 context),从根本上消除多微批次覆写风险;docs/expert_parallel.md中明确 FSDP 与 EP hook 的预期执行顺序,并在测试中断言该顺序。3.2 非目标
4. 接口与契约
4.1 状态传递方式变更
当前:
_token_dispatch写self._*,_token_combine读self._*。目标:
_token_dispatch返回(transformed_inputs, dispatch_ctx),其中dispatch_ctx是携带本次 forward 所有中间状态的不可变对象(或具名元组);_token_combine接收(module, output, dispatch_ctx, device_mesh),不再从实例变量读取。要求:
dispatch_ctx不持有对self的引用,生命周期与调用栈绑定;distribute_module的 hook 签名需支持此状态透传机制(若现有接口不支持,需评估是否扩展distribute_moduleAPI 或改用显式 wrapper 模式);MoE.forward+ExpertParallel.apply)透明,不改变外部使用接口。4.2 FSDP + EP 的组合约束
MoE,EP 应用于MoE.experts子模块distribute_module调用应在 FSDP 包裹之后GroupedExperts的参数在 EP dispatch 前必须已 unshard上述顺序约束需在
docs/expert_parallel.md中以使用注意事项形式明确说明,并在测试中断言。4.3 对外 API 不变
ExpertParallel、ExpertTensorParallel、TensorParallel的apply(module, device_mesh)接口签名保持不变;内部状态传递方式的变更对调用方不可见。5. 测试设计
5.1 状态安全单元测试
_token_dispatch返回后、_token_combine执行前,插入第 2 个微批次的_token_dispatchdispatch_ctx的生命周期:combine 后 ctx 不被ExpertParallel持有5.2 FSDP + EP 分布式测试
GroupedExperts各参数的梯度非 None 且有限5.3 Hook 顺序断言
_token_dispatch的执行顺序5.4 回归
6. 风险与开放问题
distribute_module当前 hook 签名不支持 context 透传functools.partial或 wrapper class 在不修改distribute_module接口的前提下实现透传reduce-scatter与 EP combine 反向的梯度维度重叠7. 验收标准
8. 参考
hyper_parallel/core/expert_parallel/expert_parallel.py—ExpertParallel._token_dispatch、_token_combine;hyper_parallel/core/fully_shard/— FSDP 参数生命周期与 hook 注册;docs/expert_parallel.md— 现有 EP 使用说明。