已合并
feat(_inductor): add ascendc backend support for aclgraph capture/replay #40263
feat(_inductor): add ascendc backend support for aclgraph capture/replay #40263
已合并
dingdairong创建于 7月7日
dingdairong成员
7月7日

【合入来源】

如有社区issue,请关联issue链接
请勿携带内部流程信息(需求链接、问题单、内部issue等)

  • [.] 问题单
  • [.] issue/工单
  • [.] 重构优化
  • [.] 资料更新

【修改方案】

支持npu_backend option配置为ascendc
compiled_model = torch.compile(
test_add_sum,
backend="inductor", # 指定后端为 Inductor
options={"npu_backend": "ascendc", "triton.cudagraphs": True},
)

【资料变更】

涉及新增

【接口变更】

不涉及

【功能验证】

新增st

【CheckList】

PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 dingdairong 的贡献)
Ddingdairong成员
7月7日 创建了 pull request,commit b4afe26c
atomgit-bot
atomgit-bot
7月7日 评论:

变更摘要

此 PR 主要为 torch_npu/_inductor 模块新增 AscendC 后端(aclgraphtree)支持,同时重构后端加载流程,将通用 patch 抽取为 _apply_common_patches(),消除 config.py 对 triton 的硬依赖,使得在没有 triton 的环境中也能正常导入配置模块并使用 AscendC 后端。

主要改动

  • 新增 _load_ascendc_backend() 与后端注册:在 __init__.py 中添加 _load_ascendc_backend() 函数,通过 from . import ascendc 触发 AscendC 后端注册,并在 _BACKEND_LOADERS 中绑定 "ascendc" 键,使环境变量 TORCHINDUCTOR_NPU_BACKEND=ascendc 可路由到该后端。
  • 抽取 _apply_common_patches() 解耦 triton/mlir 公共逻辑:将原先顶层无条件执行的 patch_has_triton、patch_is_gpu、patch_device_supports_tma、patch_codegen_with_cpp_wrapper、patch_cache_base_get_system 封装到 _apply_common_patches(),仅在 mlir/dvm/triton 后端加载时调用,AscendC 后端不再执行这些 triton 相关 patch。
  • 顶层提前执行设备级 patch:npu_patch_meta() 和 register_device_op_overrides_npu() 保持在任何后端加载前无条件执行,确保所有后端(含 AscendC)都能获得 NPU 设备级基础注册。
  • config.py 去除 triton 依赖:移除 from triton.runtime.driver import driver,改用 torch.npu.current_device() / torch.npu.get_device_properties() 获取 NPU 硬件参数,将 prop["num_aicore"] 替换为 prop.cube_core_num / prop.vector_core_num,target.arch 替换为 prop.name,使配置模块在纯 AscendC 环境可独立导入。
likedislike
不准确?
atomgit-bot
atomgit-bot
7月7日 评论:

代码审查

审查总结

文件 审查结果
torch_npu/_inductor/__init__.py 发现 3 个问题 (P1×1, P2×1, P3×1)
torch_npu/_inductor/config.py 发现 1 个问题 (P3×1)

按优先级统计

  • P1: 1 个 — _load_ascendc_backend 引用不存在的 ascendc 包,选择该后端将导致 ImportError
  • P2: 1 个 — _load_ascendc_backend 跳过了 _apply_common_patches(),可能缺失 patch_is_gpu() 等关键注册
  • P3: 2 个 — 注释不准确、Ascend910B 冗余覆盖

整体风险评估

中等风险。config.py 的 triton 依赖移除修改是正确的,硬件参数获取从 triton driver 切换为 torch.npu API 是可验证的改进。但 __init__.py 中新增的 ascendc 后端注册引用了尚不存在的 Python 包(P1),在 ascendc 包就绪之前选择该后端会直接崩溃。建议将 _load_ascendc_backend 与 ascendc 包的实现一并合入,或在函数中添加明确的可用性检查。

类型 数量
🔴 阻塞 1
🟡 建议 0

⛔ 需要修改

likedislike
不准确?
ascend-robotascend-robot成员
7月7日 添加了label:ascend-cla/yes
此处折叠了90条消息 查看更多
weizhan4成员
7月8日 评论:

/lgtm

likedislike
ascend-robotascend-robot成员
7月8日 添加了label:lgtm
ascend-robotascend-robot成员
7月8日 合入了pull request
ascend-robot
ascend-robot成员
7月8日 评论:

Pull Request 已合并或已关闭。

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

likedislike
ascend-robot
ascend-robot成员
7月8日 评论:
流水线 pytorch_gitcode_PR_multiVersion#12229 [ commitID:9f1fcb61 ] 已完成
likedislike