Closed
[RFC] DeepSeek-V4 GraphTrainer NPU 适配 #137
zhangjiansheAdd on  Sep 24Closed at  Sep 29
zhangjianshe
zhangjiansheCollaborators
Sep 24 Create

状态:草案

目标分支:cann/torchtitan-npu:master

regional 基础适配:已合入,默认路径

full Inductor 适配:规划中

1. 摘要

本 RFC 讨论 DeepSeek-V4(DSV4)在 Ascend NPU 上接入 TorchTitan GraphTrainer 的整体适配,范围包括当前默认的 regional 路径和后续增强的 full 路径:

  • regional 路径:mode=aot_fx_trace + inductor_compilation=regional,作为当前默认 GraphTrainer 编译路径,基础适配已经合入。
  • full 路径:mode=aot_fx_trace + inductor_compilation=full,作为后续增强目标,需在 regional 基础上分阶段打通和验收。

GraphTrainer 的核心价值是把 forward、loss、backward 捕获为联合 FX 图,并在同一张图上执行内存策略、bucketing、SimpleFSDP、EP overlap、Inductor 编译等 pass。DSV4 在 NPU 上引入了 packed attention metadata、AscendC metadata、MoE/EP dispatcher、NPU fused op、dynamic shape 和 torch-npu runtime 等额外约束,因此需要模型层、并行化层、算子层和 GraphTrainer compile 层一起适配。

本 RFC 的目标是明确 DSV4 GraphTrainer NPU 适配的能力边界、已合入 regional 基础、full Inductor 后续计划、验证标准、回滚方式和上游化原则。性能数据暂不写入本 RFC;regional 与 full 的收益、显存和编译成本应在可复现实验完成后单独补充。

2. 动机与现状

2.1 GraphTrainer 对模型与运行时的要求

GraphTrainer 与普通 eager/torch.compile 路径不同。它先通过 aot_fx_trace 捕获 forward + loss + backward,再在 FX 图上执行 pass:

DSV4 model + loss
  -> aot_fx_trace / minimal_fx_tracer
  -> GraphTrainer graph passes
  -> memory policy / bucketing / FSDP / optional EP overlap
  -> Inductor strategy: regional or full
  -> NPU codegen/runtime execution

这条链路对模型实现、动态形状和运行时行为有几项前置要求:

方面 要求
Trace safety forward/loss/backward 中的数据结构必须能被 pytree 展平和 FakeTensor trace
Dynamic shape 数据相关尺寸必须保持符号化,不能被首个 batch 固化
Mutation/recompute 参与重计算的路径不能依赖不可重放的 in-place side effect
Distributed graph FSDP/EP 通信需要在图内可见,供 pass 重排和编译
NPU runtime CUDA graph 假设不适用于 NPU,cudagraph_pass 必须禁用

2.2 DSV4 为什么需要专门适配

DSV4 不是一个普通 dense Transformer。它同时包含 sparse attention、compressed varlen metadata、MoE routed experts、EP token dispatcher 和多类 AscendC fused op。GraphTrainer 捕获联合图后,这些对象会出现在 placeholder、node metadata、custom op backward 或 graph pass 依赖中。

主要差异包括:

  • packed document 边界会生成 batch 相关的 attention metadata,部分 tensor 首维需要保留 dynamic shape。
  • AscendC metadata wrapper 需要注册为 pytree node,否则 GraphTrainer tracer 无法稳定处理其结构。
  • MoE/EP 路径中的 routed-token 数可能是数据相关符号,不能被转换为 Python int。
  • 部分 eager 友好的 in-place 或 shape 推导写法,在 AOT trace 和 recompute 中不安全。
  • NPU Inductor runtime 无 CUDA driver bandwidth 查询,需要 NPU 侧 runtime estimation patch。

这些问题不是 full Inductor 独有;regional 默认路径也必须先解决它们。因此 regional 基础适配是本 RFC 的第一阶段,也是 full 后续工作的前提。

2.3 当前状态

当前 DSV4 GraphTrainer NPU 适配的状态如下:

能力 状态 说明
aot_fx_trace 捕获 已适配 GraphTrainer 默认 trace mode
regional Inductor 已合入,默认路径 inductor_compilation=regional
full Inductor 规划中 inductor_compilation=full,显式 opt-in
DSV4 GraphTrainer config factory 已适配 graph_trainer_deepseek_v4_*
DSV4 GraphTrainer model wrapper 已适配 处理 lazy parametrization / trace 初始化
DSV4 GraphTrainer parallelize 已适配 MoE/FQN 标注、SimpleFSDP、compile 接入
AscendC metadata pytree 已适配 保留 metadata 结构和 dynamic shape 语义
NPU runtime estimation patch 已适配 NPU bandwidth estimate 与 standalone_compile 调整
cudagraph_pass 禁用 NPU 路径不支持 CUDA graph capture
Context Parallel 未支持 DSV4 GraphTrainer 稀疏注意力编译路径未适配 CP
spmd_types backend 未支持 当前 GraphTrainer 路径限定 default/partial DTensor 语义

3. 目标与边界

3.1 目标

  • 让 DSV4 在 Ascend NPU 上具备可用的 GraphTrainer regional 默认路径。
  • 将 DSV4 模型层、metadata、MoE/EP、SimpleFSDP 和 NPU fused op 调整到 GraphTrainer 可追踪、可重算、可编译的要求。
  • 保持 regional 作为默认和回归基线。
  • 在 regional 基础上规划 full Inductor 显式 opt-in 路径,建立分阶段验收标准。
  • 保留 packed metadata 和 routed-token 相关 shape 的动态语义,不把首个 batch 的尺寸固化进捕获到的训练图。
  • 明确 NPU 侧禁用 cudagraph_pass、不支持 CP/spmd_types 的当前边界。
  • 将性能数据从 RFC 主体中移除,后续以独立实验记录补充。

3.2 非目标

  • 不把 full Inductor 设置为默认路径。
  • 不宣称 regional 或 full 已获得固定性能收益。
  • 不重新设计 TorchTitan GraphTrainer 的 trace、SimpleFSDP、bucketing 或 EP overlap 算法。
  • 不在本 RFC 中支持 Context Parallel 或 spmd_types backend。
  • 不通过删除 dynamic shape 标记、绕过 pass 校验或静默回退 eager 来规避 GraphTrainer 问题。
  • 不把 torch-npu/CANN codegen 问题长期维护为 DSV4 私有逻辑;应按组件归属跟踪。

4. 方案设计

4.1 总体责任边界

flowchart TB
    A["DSV4 GraphTrainer config"] --> B["GraphTrainerDeepSeekV4Model"]
    B --> C["trace-safe model init"]
    C --> D["packed / AscendC metadata pytree"]
    C --> E["MoE / EP graph annotations"]
    D --> F["aot_fx_trace"]
    E --> F
    F --> G["GraphTrainer passes"]
    G --> H["memory policy / bucketing / SimpleFSDP"]
    H --> I{"Inductor strategy"}
    I -->|"regional default"| J["tagged regions -> regional_inductor"]
    I -->|"full opt-in"| K["whole graph tagging -> full_inductor_compilation_pass"]
    J --> L["NPU execution"]
    K --> L

代码责任分为四类:

层级 责任
配置层 提供 graph_trainer_deepseek_v4_* 配置,并固定 GraphTrainer compile 默认行为
模型层 处理 lazy state 初始化、metadata pytree、dynamic shape、可追踪 forward
并行层 提供 DSV4 GraphTrainer 专属 parallelize,接入 MoE/FQN 标注和 SimpleFSDP
编译/runtime 层 禁用 cudagraph_pass,接入 regional/full Inductor 和 NPU runtime patch

4.2 Regional 默认路径

Regional 路径是本 RFC 的已合入基础能力,也是后续 full 工作的回归基线。

配置约定:

compile.enable = true
compile.mode = aot_fx_trace
compile.inductor_compilation = regional
compile.memory_policy = full
compile.disable_passes includes cudagraph_pass

Regional 路径的设计原则:

  • 只编译被标记的区域,未标记部分仍保持 GraphTrainer 解释/执行语义。
  • 保持数值和调试边界相对保守,适合作为 full 的对照组。
  • DSV4 GraphTrainer factories 默认使用 regional,不要求用户额外指定 full。
  • NPU fused op 与 AscendC metadata 只在显式 override 或模型路径中生效,不改变 eager 默认行为。

4.3 Full Inductor 扩展路径

Full 路径是 regional 之后的增强目标。它保持 mode=aot_fx_trace 不变,仅切换:

--compile.inductor_compilation full

上游 full_inductor_compilation_pass 的语义是:

  1. 遍历 GraphModule 和子 GraphModule。
  2. 将所有非 placeholder/output 节点标记为 compile_with_inductor。
  3. 复用 regional_inductor_pass / standalone_compile 完成整图编译。
  4. 将 full 编译前的 cudagraph compatibility verdict 写入结果 metadata。

这说明 full 不是替代 regional 基础设施的全新后端,而是“整图打标 + regional_inductor 复用”。因此 full 适配应尽量复用 regional 已建立的 DSV4/NPU 追踪、形状和运行时约束。

4.4 NPU 侧约束

约束 决议
Trace mode 固定 aot_fx_trace
Regional 默认启用,作为已合入路径和回归基线
Full 显式 opt-in,待分阶段验收
CUDA graph 禁用 cudagraph_pass
Backend 当前不支持 spmd_types
CP 当前不支持 Context Parallel
EP overlap 可作为独立组合能力验证,不能与 regional/full 本身混淆归因
性能结论 不写入 RFC,单独记录可复现实验

5. 实施映射

5.1 相关文件与职责

能力 相关文件 说明
GraphTrainer 配置工厂 torchtitan_npu/models/deepseek_v4/config_registry.py graph_trainer_deepseek_v4_* factories,regional 默认
GraphTrainer 模型变体 torchtitan_npu/models/deepseek_v4/model.py GraphTrainerDeepSeekV4Model 处理 trace 初始化
GraphTrainer 并行化 torchtitan_npu/models/deepseek_v4/parallelize.py MoE/FQN 标注、SimpleFSDP、GraphTrainer compile 接入
NPU GraphTrainer 扩展 torchtitan_npu/extensions/graph_trainer.py GraphTrainerEx 结合 TrainerEx 与 GraphTrainer 行为
AscendC metadata torchtitan_npu/override/deepseek_v4/sparse_attn/ascendc.py metadata extension、pytree、dynamic shape 标记
NPU runtime patch torchtitan_npu/patches/torch_npu/inductor_runtime_estimation.py NPU runtime estimation 与 standalone_compile 行为调整
Regional/full pass torchtitan/experiments/graph_trainer/inductor_passes.py 上游 regional/full Inductor pass
Pass 选择 torchtitan/experiments/graph_trainer/passes.py 根据 compile.inductor_compilation 选择 regional/full

5.2 配置入口

当前 DSV4 GraphTrainer 入口覆盖以下模型族:

GraphTrainer 配置 对应 eager 配置 用途
graph_trainer_deepseek_v4_debugmodel deepseek_v4_debugmodel debug / 最小验证
graph_trainer_deepseek_v4_flash deepseek_v4_flash flash 真实配置
graph_trainer_deepseek_v4_flash_43layers_16experts deepseek_v4_flash_43layers_16experts 冒烟/缩放验证
graph_trainer_deepseek_v4_pro deepseek_v4_pro pro 真实配置
graph_trainer_deepseek_v4_pro_61layers_32experts deepseek_v4_pro_61layers_32experts 冒烟/缩放验证

初期不新增 *_full factory。Full 通过 CLI opt-in:

--compile.inductor_compilation full

6. 分阶段计划

R0:regional 基础适配固化(已合入)

范围:

  • GraphTrainer config factories。
  • GraphTrainer model wrapper。
  • DSV4 GraphTrainer parallelize。
  • metadata pytree / dynamic shape 语义。
  • NPU runtime estimation patch。
  • cudagraph_pass disabled。

验收标准:

  • regional 路径可作为默认 GraphTrainer 编译路径。
  • 与 eager/regional 对照时无明显正确性回归。
  • 当前限制(无 CP、无 spmd_types、禁用 CUDA graph)在文档中明确。

R1:regional 回归与文档补齐

范围:

  • 补齐 regional 配置、限制、override 选择、调试方式和已知风险说明。
  • 明确 regional 是 full 的回归基线。
  • 将性能数据移出 RFC,放入可复现实验记录或特性文档。

验收标准:

  • 用户能按文档启动 DSV4 GraphTrainer regional。
  • 文档不暗示 full 已稳定或默认启用。
  • 不再保留未经复验的性能结论。

F1:full debugmodel 链路打通

范围:

  • 在 graph_trainer_deepseek_v4_debugmodel 上启用 --compile.inductor_compilation full。
  • 保持 mode=aot_fx_trace、cudagraph_pass disabled。
  • 确认 full pass 被实际执行。

验收标准:

  • full 编译产物成功生成并加载。
  • 少量训练 step 可正常执行并 clean exit。
  • loss、grad norm、runtime 状态无 NaN/Inf。
  • 记录 trace、graph pass、Inductor compile、kernel build 的阶段耗时,用于后续工程化分析。

F2:full kernel 构建与缓存稳定化

范围:

  • 梳理 AscendC Codegen、CANN AutoFuse 和 fallback 节点。
  • 验证冷启动、缓存命中、多 rank 并发构建。
  • 明确失败日志和临时产物清理策略。

验收标准:

  • debugmodel 可连续多 step 稳定运行。
  • 二次运行能解释 cache 命中或未命中的原因。
  • 多 rank 不因构建并发产生互相污染或半成品复用。

F3:真实规模 full 验证

范围:

  • 在 flash/pro 冒烟或真实配置上运行 full。
  • 与 regional 使用相同 seed 和 batch 形态进行对照。
  • 分别记录正确性、稳定性、显存、编译成本和稳态吞吐。

验收标准:

  • 多 rank 训练可完成预设 step 数并 clean exit。
  • full 与 regional 对照无明显正确性回归。
  • 性能结论必须基于排除 trace/warmup 后的稳态区间。
  • 只有收益超过编译和维护成本后,才讨论默认启用;否则 full 保持 opt-in。

7. 验证与验收

7.1 总体验收矩阵

层级 Regional Full
配置解析 默认路径可用 CLI opt-in 可用
Trace aot_fx_trace 稳定 复用 regional 的追踪要求
Graph pass memory/bucketing/FSDP 正常 full pass terminal 语义明确
Inductor 标记区域编译 整图打标编译
Runtime NPU 执行稳定 kernel build/cache 稳定
对照 eager/regional 正确性对照 regional/full 正确性对照
文档 限制清楚 opt-in 与风险清楚

7.2 当前证据缺口

  • regional 已作为默认路径合入,但仍应补齐可复现实验入口和限制说明。
  • full 路径缺少从 debugmodel 到真实规模的分阶段记录。
  • full 编译的 kernel build/cache 行为尚未形成工程化结论。
  • full 与 EP overlap 的组合关系需要独立拆分验证。
  • 性能数据暂不写入 RFC,后续通过独立实验记录补充。

8. 迁移、回滚与上游计划

8.1 迁移原则

Regional 路径作为默认配置,无需用户额外切换。Full 路径仅通过显式配置启用:

--compile.inductor_compilation full

该能力不需要 checkpoint 迁移,不新增模型参数,也不改变 state-dict key。

8.2 回滚边界

  • regional 出现问题时,可回退到 DSV4 eager 路径。
  • full 出现问题时,应回退到 regional:
--compile.inductor_compilation regional

不应通过删除 dynamic shape 标记、重新开启 cudagraph_pass、关闭 GraphTrainer 校验或静默回退 eager 来掩盖问题。

8.3 上游化

GraphTrainer regional/full Inductor pass 属于 torchtitan 上游能力。本仓应只承载 DSV4/NPU 特有适配。若适配过程中暴露 GraphTrainer 通用问题,应拆成最小图测试并向上游提交;若暴露 torch-npu/CANN codegen 问题,应按 torch-npu 或 CANN 组件单独跟踪。

9. 评审记录与未决问题

9.1 当前决议

事项 决议
RFC 范围 DSV4 GraphTrainer NPU 整体适配,包括 regional 和 full
默认路径 regional
full 状态 显式 opt-in,待分阶段验收
性能数据 暂不写入 RFC,后续单独补充
CP / spmd_types 当前不支持
cudagraph_pass NPU GraphTrainer 路径继续禁用

9.2 未决问题

  • Regional 默认路径是否需要补充独立的 CI smoke 或 nightly NPU 验证?
  • Full 相对 regional 的稳态收益是否足以覆盖编译成本和维护成本?
  • Full 编译下 AscendC Codegen、CANN AutoFuse 和 fallback 节点的边界如何稳定观测?
  • 多 rank full 编译是否需要额外的 cache 隔离、锁或构建并发控制?
  • Full 与 EP overlap 同时启用时,收益和风险应如何拆分归因?
likedislike
hanboyouhanboyouCollaborators
Sep 29 changed custom state from 待办的 to 已解决
hanboyouhanboyouCollaborators
Sep 29 Close issue
hanboyouhanboyouCollaborators
Sep 29 Associated pull request
hanboyouhanboyouCollaborators
Sep 29 Deleted associated pull request: [feat] Add DSV4 GraphTrainer SAC memory policy
hanboyouhanboyouCollaborators
Sep 29 Associated pull request
CANN-robotCANN-robotCollaborators
Sep 29 Added label: resolved