Merged
feat: adapt GraphTrainer EP overlap for DeepSeek-V4 #695
feat: adapt GraphTrainer EP overlap for DeepSeek-V4 #695
Merged
wanlinanAdd on Sep 3
wanlinan
wanlinanCollaborators
Sep 3

上游 experiments/graph_trainer 的 EP overlap pass 在 DeepSeek-V4 图上无法运行:
四处上游缺陷会在 chunk / 调度阶段直接抛错或产出成环的调度图。本提交补齐这些缺陷并提供可 trace 的 token dispatcher,使 EP overlap 可在 Ascend NPU 上启用。

patches/torchtitan/ 新增四个临时补丁(随 import torchtitan_npu 自动 apply):

  • graph_trainer_ep_chunk_concretization:把形状符号 hint 收集限制在 EP chunk符号可达的连通分量内。原实现为所有 placeholder 符号收集 hint,DSV4 的数据相关输入元数据要么推导不出 base hint 而抛错,要么把 per-batch 尺寸冻结进图。
  • graph_trainer_ep_overlap_shape_queries:剥离 token exchange 输出上 sym_size查询误继承的 EP_token_exchange 标注,否则调度器判定为非法标注节点并抛错。
  • graph_trainer_ep_ready_nodes_dedup:_ready_nodes 不去重,同一节点可同时出现在两个 chunk 的候选集中,重复项被串成顺序边后使调度图成环、拓扑排序失败。
  • graph_trainer_ep_shape_live_out:all-to-all 作为被切分区域 live-out 且使用者均为首维 size query 时,chunk pass 无法重建完整值;改为路由到 chunk 侧,残余情况沿 token 维 cat(token 数在 chunk 间可加)。
likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for wanlinan's contribution.)
wanlinanwanlinanCollaborators
Sep 3 Created pull request, commit e3d2e03b
atomgit-bot
atomgit-botCollaborators
Sep 3 Comments:

变更摘要

本 PR 旨在让上游 experiments/graph_trainer 的 EP overlap pass 能在 Ascend NPU 上支撑 DeepSeek-V4 图:上游存在四处缺陷,会在 chunk / 调度阶段直接抛错或产出成环的调度图。本提交在 torchtitan_npu/patches/torchtitan/ 下新增四个临时补丁并在 patches/torchtitan/__init__.py 中注册(随 import torchtitan_npu 自动 apply),同时新增可 trace 的 token dispatcher 并配套将 moe_token_unpermute 反向改为 v2 内核,使 EP overlap 可在 NPU 上启用。

主要改动

  • 新增可 trace 的 EP token dispatcher:在 torchtitan_npu/override/common/token_dispatcher.py 中新增 NPUTraceableLocalDispatchMetadata / NPUTraceableAllToAllDispatchMetadata 元数据类与 NPUTraceableAllToAllTokenDispatcher——dispatch 通过稳定的 argsort/gather 计算 inverse_indices_N、保持 token 数符号化,combine 复用 npu_moe_token_unpermute 完成 local combine,并提供 override 入口 npu_graph_trainer_token_dispatcher 供配置选用。
  • moe_token_unpermute 反向适配 v2 内核:_npu_moe_token_unpermute_backward 改用 torch_npu.npu_moe_token_unpermute_grad_v2,probs 非 None 时恒保存 permuted_tokens,并记录 permuted_tokens_size_0 / permuted_tokens_dtype,使 permuted 行数在 chunk 后为符号量时仍可 trace(原零占位、按 probs.requires_grad 分支的写法被移除)。
  • 补丁 graph_trainer_ep_chunk_concretization:把 placeholder 符号 hint 收集限定在与 EP chunk 符号连通的连通分量内,对无法推导 base hint 的 extent 跳过而非抛错(真实 hint 冲突仍抛错),并只改写含 chunk 符号的值,避免将 per-batch 尺寸冻结进图或对无关动态维抛错。
  • 补丁 graph_trainer_ep_overlap_shape_queries 与 graph_trainer_ep_ready_nodes_dedup:前者在调度前剥离 token exchange 输出上 aten.sym_size.int 查询误继承的 EP_token_exchange 标注(否则被调度器判为非法标注节点而抛错);后者在 _ready_nodes 中引入 selected 集合按节点去重,避免同一节点同时进入两个 chunk 候选集后被串成顺序边、导致调度图成环使拓扑排序失败。
  • 补丁 graph_trainer_ep_shape_live_out:当 dispatch all-to-all 为被切分区域 live-out 且使用者均为首维 size query 时,把位于 producer region root 内的 dim-0 size query 用户路由到 chunk 侧并跳过物化;残余场景改用 torch.ops.aten.cat 沿 token 维拼接两个 chunk 值重建(token 数在 chunk 间可加),并写入 chunked_materialization_proof 元信息。
likedislike
Incorrect?
atomgit-bot
atomgit-botCollaborators
Sep 3 Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Sep 3 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Sep 3 Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
repo-cann/torchtitan-npu ✅ panchao-gitcode, 张德鹏 (2/2) ✅ 张德鹏 (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

wanlinan, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
120 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Sep 17 Added label: lgtm
CANN-robotCANN-robotCollaborators
Sep 17 Merged pull request
CANN-robot
CANN-robotCollaborators
Sep 17 Comments:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
刘原辰刘原辰Collaborators
Oct 31 Associated issue: [RFC] DeepSeek-V4 GraphTrainer EP overlap 适配方案
CANN-robot
CANN-robotCollaborators
Oct 31 Comments:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike