已合并
support strutured meta #36643
support strutured meta #36643
已合并
maoyuanpeng1创建于 5月25日
maoyuanpeng1成员
5月25日

【合入来源】

https://gitcode.com/Ascend/pytorch/issues/2110

【修改方案】

请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列
如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)

【资料变更】

请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及”

【接口变更】

请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及”

【功能验证】

说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤
新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图

【CheckList】

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

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 maoyuanpeng1 的贡献)
Mmaoyuanpeng1成员
5月25日 创建了 pull request,commit 0a7a7cfb
ascend-robot
ascend-robot成员
5月25日 评论:

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 chujinjin, hbhu_bin (2/2) chujinjin (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
ascend-robotascend-robot成员
5月25日 添加了label:ascend-cla/yes
maoyuanpeng1成员
5月25日 评论:

compile

likedislike
ascend-robot
ascend-robot成员
5月25日 评论:

当前仓库存在以下 保护分支

Protected Branch Version Release
master
v2.7.1
v2.11.0
v2.10.0
v2.9.0
v2.12.0

评论 /sync <branch1> <branch2> ... 可将当前 PR 修改同步到其它分支(创建同步 PR):
a) 如果当前 PR 是 Open 状态,同步操作将延迟到 PR 被合并时执行
b) 如果当前 PR 已经 Merged,将立即执行同步操作

注意:

  1. /sync 命令可以指定同步到多个分支,仅最后一个 /sync 命令生效
  2. 如果创建的同步 PR 不正确,可通过向同步 PR 的源分支提交轻量级 PR 完善,或使用 /close 命令关闭
likedislike
ascend-robotascend-robot成员
5月25日 添加了label:ci-pipeline-running
ascend-robot
ascend-robot成员
5月25日 评论:

Ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
5月25日 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
5月25日 评论:

✅ 跳过 docs ci 检查,没有需要检查的文档文件

likedislike
ascend-robotascend-robot成员
5月25日 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
5月25日 添加了label:docs-ci-pipeline-success
ascend-robot
ascend-robot成员
5月25日 评论:

Ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
5月25日 删除了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
5月25日 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
5月25日 评论:

✅ 跳过 docs ci 检查,没有需要检查的文档文件

likedislike
ascend-robotascend-robot成员
5月25日 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
5月25日 添加了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
5月25日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
5月25日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
5月25日 评论:
流水线 PR-pipeline_pytorch#26674 [ commitID:d82b264f ] 已完成
阶段 任务名 状态 详情
编译构建 Build_X86 >>>
Build_ARM >>>
Build_LibTorch_x86 >>>
Build_LibTorch_ARM >>>
Build_X86_torchair 🛑 >>>
Build_ARM_torchair 🛑 >>>
patch_test 🛑 >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 CodeCheck >>>
check_error >>>
CodeCheck_lintrunner >>>
开源片段检查 SCA >>>
开发者测试 UT_X86_Part_01 🛑 >>>
UT_X86_Part_02 🛑 >>>
UT_ARM_A3_Part_01 🛑 >>>
UT_ARM_A3_Part_02 🛑 >>>
UT_ARM_A2_Part_01 >>>
UT_ARM_A2_Part_02 >>>
UT_ARM_A2_Part_03 >>>
UT_inductor_Part_01 🛑 >>>
UT_inductor_Part_02 🛑 >>>
UT_inductor_Part_03 🛑 >>>
UT_inductor_Part_04 🛑 >>>
UT_DIST_ARM_Part_01 🛑 >>>
UT_DIST_ARM_Part_02 🛑 >>>
UT_DIST_ARM_Part_03 🛑 >>>
UT_DIST_ARM_Part_04 🛑 >>>
流水线 PR-pipeline_pytorch >>>
此流水线已支持下列评论快捷指令,仅PR创建者评论有效
  • compile : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
Mmaoyuanpeng1成员
5月26日 update merge request[project id: 7404318, iid: 36643, commit_id: f8a6439540344d313b764ef9c2b711e1b4097ca9] virtual merging success
Mmaoyuanpeng1成员
5月26日 强制推送  1 个提交:56b99e2c-support strutured meta
Mmaoyuanpeng1成员
5月26日 update merge request[project id: 7404318, iid: 36643, commit_id: bfd5148592481c3b80c0bab9dcbed7bcb68a38b6] virtual merging success
ascend-robotascend-robot成员
5月26日 删除了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
5月26日 评论:

Notification

This pull request source branch has changed, so removes the following label(s): ci-pipeline-passed.

likedislike
maoyuanpeng1成员
5月26日 评论:

compile

likedislike
ascend-robotascend-robot成员
5月26日 添加了label:ci-pipeline-running
ascend-robotascend-robot成员
5月26日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
5月26日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
5月26日 评论:
流水线 PR-pipeline_pytorch#26967 已完成
阶段 任务名 状态 详情
编译构建 Build_X86 >>>
Build_ARM >>>
Build_LibTorch_x86 >>>
Build_LibTorch_ARM >>>
Build_X86_torchair 🛑 >>>
Build_ARM_torchair 🛑 >>>
patch_test 🛑 >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 CodeCheck >>>
check_error >>>
CodeCheck_lintrunner >>>
开源片段检查 SCA >>>
开发者测试 UT_X86_Part_01 🛑 >>>
UT_X86_Part_02 🛑 >>>
UT_ARM_A3_Part_01 🛑 >>>
UT_ARM_A3_Part_02 🛑 >>>
UT_ARM_A2_Part_01 >>>
UT_ARM_A2_Part_02 >>>
UT_ARM_A2_Part_03 >>>
UT_inductor_Part_01 🛑 >>>
UT_inductor_Part_02 🛑 >>>
UT_inductor_Part_03 🛑 >>>
UT_inductor_Part_04 🛑 >>>
UT_DIST_ARM_Part_01 🛑 >>>
UT_DIST_ARM_Part_02 🛑 >>>
UT_DIST_ARM_Part_03 🛑 >>>
UT_DIST_ARM_Part_04 🛑 >>>
流水线 PR-pipeline_pytorch >>>
likedislike
Mmaoyuanpeng1成员
5月26日 修改了pull request 的描述
Mmaoyuanpeng1成员
5月26日 修改了pull request 的描述
huangyunlong成员5月27日进行代码检视5
torchnpugen/gen_npu_c_shim.py
@@ -62,3 +58,1 @@
62- structured_func_group_dict[func.structured_delegate]
63- )
64- ):
58+ if backend_indices[dk].has_kernel(func):
huangyunlong5月27日评论:

这里为啥要删除

likedislike
maoyuanpeng1成员
5月27日 评论:

删除 _get_backend_index_for_npu 中通过 structured_delegate 查询 structured group kernel 的兜底逻辑,是为了避免 AOTInductor C shim为 out-only structured 算子生成 functional ABI。

在本次 structured meta 支持中,NPU backend index 改为 use_out_as_primary=True,并且 structured 算子的 backend metadata 标在 structured out variant 上。这个变化后,backend_index.has_kernel(NativeFunctionsGroup) 的语义会从查询 functional variant 变成查询 out variant。

原逻辑中:

backend_indices[dk].has_kernel(func) or (
func.structured_delegate is not None
and func.structured_delegate in structured_func_group_dict
and backend_indices[dk].has_kernel(
structured_func_group_dict[func.structured_delegate]
)
)

第二段会在当前 func 是 functional variant 时,通过 structured_delegate 找到它所属的 structured group,并查询该 group 是否有 NPU kernel。由于当前 has_kernel(group) 会命中 out variant,这会把“只支持 xxx.out”误判为“functional xxx 也支持”。

例如 NPU yaml 中只声明了:

  • func: cumprod.out(...)

没有声明:

  • func: cumprod(...)

在新语义下,恢复旧逻辑会导致:

has_kernel(cumprod functional) -> False
has_kernel(cumprod structured group) -> has_kernel(cumprod.out) -> True

于是 AOTInductor C shim 会错误生成:

AOTI_TORCH_EXPORT AOTITorchError aoti_torch_npu_cumprod(...);

类似还会多生成 _addmm_activation、gcd 等 functional shim,触发 c_shim_npu.h 的 ABI diff 检查失败。

因此 _get_backend_index_for_npu 只能按当前 NativeFunction 本身判断:

if backend_indices[dk].has_kernel(func):
return backend_indices[dk]

这样 AOTI C shim 只会为当前 schema 自身在 backend index 中存在的 op 生成导出函数,不会把 structured out variant 的支持扩散到 functional variant,避免错误扩大 AOTI C ABI。

huangyunlong成员
5月28日 评论:

这里的目的其实就是为了通过structure进行支持的

maoyuanpeng1成员
5月28日 评论:

但是我们很多算子(在pytorch中是structured算子)只实现了out版本,没有实现functional版本。这与structured算子本来就是背道而驰。

huangyunlong成员
5月28日 评论:

后续支持后,这里需要补齐下

huangyunlong成员5月27日进行代码检视2
torchnpugen/templates/NPUStructuredNativeFunctions.cpp
@@ -0,0 +1,0 @@
1+// ${generated_comment}
huangyunlong5月27日评论:

这些模板不能复用的原因是什么

likedislike
maoyuanpeng1成员
5月27日 评论:

可以直接覆盖该模板,当时只是觉得会重复写相应字符串。

huangyunlong成员5月28日进行代码检视2
torchnpugen/gen_backend_stubs.py
@@ -561,29 +742,30 @@
742+ return std::nullopt;
743+}
744+""",
745+ ]
746+ 
747+ 
561748def gen_dispatcher_registrations(
562749 fm: FileManager,
563750 class_name: str,
@@ -588,6 +775,7 @@ def gen_dispatcher_registrations(
588775#include "torch_npu/csrc/aten/NPUNativeFunctions.h"
589776#include "torch_npu/csrc/framework/interface/EnvVariables.h"
590777#include "torch_npu/csrc/aten/NPUOpApiNativeFunctions.h"
778+#include "torch_npu/csrc/aten/NPUStructuredNativeFunctions.h"
591779"""
592780 
593781 # 根据环境变量决定是否包含FormatHelper.h
@@ -636,7 +824,7 @@ $dispatch_registrations_body
636824 backend_index, per_operator_headers=False, rocm=False
637825 ),
638826 "ops_headers": "",
639- "dispatch_helpers": dest.gen_registration_helpers(backend_index),
827+ "dispatch_helpers": gen_npu_structured_registration_helpers(backend_index),
640828 "dispatch_definitions": fm.substitute_with_template(
641829 "RegisterDispatchDefinitions.ini",
642830 lambda: {
@@ -686,6 +874,309 @@ $dispatch_registrations_body
686874 )
687875 
688876 
877+def get_supported_structured_groups(
huangyunlong5月28日评论:

这里新增的有没有可以复用上游社区的,
如果针对structured的代码比较多的话,是不是可以单独抽出一个文件,那样理解起来是不是简单些

likedislike
maoyuanpeng1成员
5月28日 评论:

这里看个人理解,当然可以抽出成单独文件。 当时考虑非structured算子代码生成也写在了这个文件中,所以添加到了此处。

Hhuangyunlong成员
5月28日 解决了最后一个问题
chujinjin成员
5月28日 评论:

/approve

likedislike
ascend-robotascend-robot成员
5月28日 添加了label:approved
hbhu_bin成员
5月28日 评论:

/lgtm

likedislike
ascend-robotascend-robot成员
5月28日 添加了label:lgtm
ascend-robot
ascend-robot成员
5月28日 评论:

Review Guide

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

likedislike
ascend-robotascend-robot成员
5月28日 合入了pull request
ascend-robot
ascend-robot成员
5月28日 评论:
流水线 pytorch_gitcode_PR_multiVersion#9526 [ commitID:56b99e2c ] 已完成
likedislike