已合并
feat: [graph partition] aclgraph support graph partition #35325
feat: [graph partition] aclgraph support graph partition #35325
已合并
luochao60创建于 5月11日
luochao60成员
5月11日

【合入来源】

(如有)请关联需求文档/issue链接
关联 issue: #1911 (https://gitcode.com/Ascend/pytorch/issues/1911)

【修改方案】

  1. torch_npu/utils/_graph_tree.py — 新增 get_manager monkey-patch
    上游 torch._inductor.output_code.maybe_handle_backward_generationtorch._dynamo.backends.cudagraphs 会直接调用 torch._inductor.cudagraph_trees.get_manager,依赖它返回真实的 manager 实例来推进 backward 的 cudagraph 状态机。NPU 把自己的 NPUGraphTreeManager 注册在独立 registry(torch_npu.npu._graph_tree),导致上游那两条路径在 NPU 设备上拿不到 manager,触发 AssertionErrorAttributeError。在 _apply_npugraph_tree_methods() 末尾把 torch._inductor.cudagraph_trees.get_manager 重指到 NPU 的 get_manager(签名一致、duck-typing 兼容),打通 backward 状态机闭环。

  2. torch_npu/_inductor/codegen/wrapper.py — wrapper 体系重构以支持 graph partition
    提取 _NPUKernelCodegenMixin 混入类,把 NPU 特化逻辑(define_kerneluser_autotune → user_autotune_npuPrecomputedGrid → PrecomputedGridNpuFixedGrid → FixedGridNpu 替换;get_next_kernel_suffixmake_buffer_free 等)从原 NPUWrapperCodeGen 上移;新增 NPUSubgraphWrapperCodegen(_NPUKernelCodegenMixin, SubgraphPythonWrapperCodegen) 让 subgraph wrapper 共享 NPU 适配;NPUWrapperCodeGen.create()is_subgraph=True 时返回新的 subgraph wrapper。

  3. torch_npu/_inductor/lowering_op_list.py — graph partition 所需算子注册
    GENERATE_LIST 新增 prims.device_putaten.unbindtorch.ops.higher_order.cond

  4. torch_npu/csrc/core/npu/NPUHooksInterface.{h,cpp} — Pinned memory 接口实现
    override 上游 at::PrivateUse1HooksInterface 新增的虚函数:isPinnedPtr()CachingHostAllocator_isPinnedgetPinnedMemoryAllocator() 返回 getPinnedMemoryAllocator()。同时新增 CachingHostAllocator.h 包含。

  5. test/_inductor/test_inductor_graph_partition.py — graph partition 测试集
    新增 graph partition 场景的系列用例:dynamic shapes、condition op、custom op 拆分、subgraph wrapper user_autotune 兜底、forward cudagraph + backward fallback 等。

【资料变更】

不涉及

【接口变更】

不涉及。C++ 层 NPUHooksInterface::isPinnedPtrgetPinnedMemoryAllocator 是对上游 PrivateUse1HooksInterface 已有虚函数的 override 实现,非新增对外 API。

【功能验证】

test/_inductor/test_inductor_graph_partition.py 中新增覆盖 graph partition 场景的用例:

  • test_graph_partition_dynamic_shapes:动态 shape 下产生 3 个 npugraph
  • test_graph_partition_condition_op:cond / higher-order op 切图
  • test_graph_partition_custom_op:cudagraph_unsafe 自定义算子切分边界
  • test_graph_partition_subgraph_wrapper_user_autotune:子图 wrapper 的 user_autotune 替换路径
  • forward cudagraph + backward fallback 的混合路径

通过 python test/_inductor/test_inductor_graph_partition.py 在 NPU 设备上跑通全部用例。

【CheckList】

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

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 luochao60 的贡献)
Lluochao60成员
5月11日 创建了 pull request,commit 7f1e2af4
openLiBingCI成员
5月11日 评论:

本PR中共发现代码检查告警抑制1处,请Committer检视合理性

本评论自动扫描PR中使用的开源代码检查工具(ruff、clang-tidy、CodeQL等)的屏蔽注释。
这些屏蔽注释会阻止开源代码检查工具对特定代码区域的检测,可能导致潜在问题被忽略。

点击下载完整报告

文件路径 行号 屏蔽类型 代码片段 工具名称
torch_npu/utils/_graph_tree.py 399 行级屏蔽 395: # forward was cudagraph'd but backward is not, to drive the cudagraph
396: # generation state machine) and _dynamo/backends/cudagraphs.py. NPU
397: # registers its manager under torch_npu.npu._graph_tree, so without
398: # this forward those upstream paths raise AttributeError or return None.
399: import torch._inductor.cudagraph_trees as _upstream_cgt # noqa: F401
400: from torch_npu.npu._graph_tree import get_manager as _npu_get_manager
401: _upstream_cgt.get_manager = _npu_get_manager
flake8
likedislike
ascend-robot
ascend-robot成员
5月11日 评论:

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


PR Approval Progress

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

Module Approval Details

module lgtm status approve status
repo-Ascend/pytorch adrian07110, chujinjin (2/2) adrian07110, chujinjin (2/1)
test adrian07110, chujinjin (2/2) adrian07110, chujinjin (2/1)
torch_npu/_inductor adrian07110, chujinjin (2/2) adrian07110, chujinjin (2/1)
torch_npu/distributed adrian07110, chujinjin (2/2) adrian07110, chujinjin (2/1)
torch_npu/npu adrian07110, chujinjin (2/2) adrian07110, chujinjin (2/1)
torch_npu/utils adrian07110, chujinjin (2/2) adrian07110, chujinjin (2/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
ascend-robotascend-robot成员
5月11日 添加了label:ascend-cla/yes
此处折叠了428条消息 查看更多
ascend-robotascend-robot成员
5月14日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
5月14日 评论:
流水线 PR-pipeline_pytorch#22239 (重试第2次)已完成
阶段 任务名 状态 详情
编译构建 Build_X86 >>>
Build_ARM >>>
Build_LibTorch_x86 >>>
Build_LibTorch_ARM >>>
Build_X86_torchair 🛑 >>>
Build_ARM_torchair 🛑 >>>
patch_test 🛑 >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 CodeCheck >>>
check_error >>>
开源片段检查 SCA >>>
开发者测试 UT_X86_Part_01 🛑 >>>
UT_X86_Part_02 🛑 >>>
UT_ARM_A3_Part_01 🛑 >>>
UT_ARM_A3_Part_02 🛑 >>>
UT_DIST_X86_Part_01 >>>
UT_DIST_X86_Part_02 >>>
UT_DIST_X86_Part_03 >>>
UT_DIST_X86_Part_04 >>>
UT_ARM_A2_Part_01 >>>
UT_ARM_A2_Part_02 >>>
UT_ARM_A2_Part_03 >>>
UT_inductor_Part_01_pool >>>
UT_inductor_Part_02_pool >>>
UT_inductor_Part_03_pool >>>
UT_inductor_Part_04_pool >>>
UT_DIST_ARM_Part_01 🛑 >>>
UT_DIST_ARM_Part_02 🛑 >>>
UT_DIST_ARM_Part_03 🛑 >>>
UT_DIST_ARM_Part_04 🛑 >>>
流水线 PR-pipeline_pytorch >>>
likedislike
ascend-robot
ascend-robot成员
5月14日 评论:

Review Guide

This pull-request passes review.
Committers who wrote a comment of /approve are: adrian07110, chujinjin.
Reviewers who wrote a comment of /lgtm are: adrian07110, chujinjin.

likedislike
ascend-robotascend-robot成员
5月14日 合入了pull request
ascend-robot
ascend-robot成员
5月15日 评论:
流水线 pytorch_gitcode_PR_multiVersion#9160 [ commitID:3ec33b8d ] 已完成
likedislike