已合并
feat(_inductor): add ascendc backend support for aclgraph capture/replay #40263
dingdairong创建于 7月7日
feat(_inductor): add ascendc backend support for aclgraph capture/replay #40263
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 dingdairong 的贡献)7月7日 创建了 pull request,commit b4afe26c
atomgit-bot
7月7日 评论:
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 环境可独立导入。


不准确?
ascend-robot
7月7日 评论:
7月7日 评论:
atomgit-bot
7月7日 评论:
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 |
⛔ 需要修改


不准确?
7月7日 添加了label:ascend-cla/yes
此处折叠了90条消息 查看更多
7月8日 添加了label:lgtm
7月8日 合入了pull request
ascend-robot
7月8日 评论:
7月8日 评论:
Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


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


【合入来源】
【修改方案】
支持npu_backend option配置为ascendc
compiled_model = torch.compile(
test_add_sum,
backend="inductor", # 指定后端为 Inductor
options={"npu_backend": "ascendc", "triton.cudagraphs": True},
)
【资料变更】
涉及新增
【接口变更】
不涉及
【功能验证】
新增st
【CheckList】