已合并
[feature] add torchnpugen packages #30023
梁松伟创建于 1月26日
[feature] add torchnpugen packages #30023
已合并
梁松伟创建于 1月26日
梁松伟
梁松伟成员
1月26日

What type of PR is this?

Uncomment only one /kind <> line, hit enter to put that in a new line, and remove leading whitespaces from that line:

/kind bug
/kind task
/kind feature

What does this PR do / why do we need it:

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewers:

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 梁松伟 的贡献)
梁松伟梁松伟成员
1月26日 创建了 pull request,commit fbf33fd2
ascend-robot
ascend-robot成员
1月26日 评论:

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

likedislike
ascend-robot
ascend-robot成员
1月26日 评论:

以下是根据您提交的修改文件推荐的Reviewer和Committer序列,需各模块评审通过后方可合入

Module List Reviewers Committers
repo-Ascend/pytorch li_jing_hw, kuhn7, huangyunlong2022, medivh-x, liangsongwei shaoyf, wasd1111222, li_jing_hw, dilililiwhy, chenhao_1209
likedislike
ascend-robotascend-robot成员
1月26日 添加了label:ascend-cla/yes
ascend-robot
ascend-robot成员
1月26日 评论:

CLA Signature Pass

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

likedislike
梁松伟
梁松伟成员
1月26日 评论:

compile

likedislike
ascend-robotascend-robot成员
1月26日 添加了label:ci-pipeline-running
AtlasAccount
AtlasAccount成员1月26日进行代码检视1
generate_code.sh
@@ -23,19 +23,19 @@ testing_source_yaml="$CDIR/test/ops_unsupport_list.yaml"
2323 
2424op_plugin_functions_yaml_path="$op_plugin_config_path/npu_native_functions.yaml"
2525 
26-${python_execute} -m codegen.gen_backend_stubs \
AtlasAccount
AtlasAccount1月26日评论:

代码维护性: 代码中使用了硬编码的模块路径 torchnpugen.gen_backend_stubs。虽然从上下文看,这可能是从 codegen.gen_backend_stubs 重命名而来,但硬编码的模块名降低了脚本的灵活性。如果未来模块结构或命名再次发生变化,需要修改此脚本。结合第32行和第38行的类似改动,这属于批量重命名操作,但未考虑使用变量或参数来配置模块路径,使得脚本与特定的代码组织结构耦合。

问题类型: 代码维护性
文件路径: generate_code.sh
行号: 26
问题代码:

${python_execute} -m torchnpugen.gen_backend_stubs  \

修改建议:

建议将模块路径定义为变量,例如 `GEN_BACKEND_STUBS_MODULE="torchnpugen.gen_backend_stubs"`,然后在命令中引用该变量。这样,当模块路径需要调整时,只需修改变量的值,提高了脚本的可维护性和可配置性。

此评论由代码审查工具自动生成

likedislike
AtlasAccount
AtlasAccount成员1月26日进行代码检视1
generate_code.sh
@@ -30,4 +30,3 @@
3030 --op_plugin_impl_path="$CDIR/third_party/op-plugin/op_plugin/ops" \
3131 --op_plugin_yaml_path="$op_plugin_config_path/op_plugin_functions.yaml"
3232 
33-${python_execute} -m codegen.autograd.gen_autograd \
AtlasAccount
AtlasAccount1月26日评论:

代码维护性: 代码中使用了硬编码的模块路径 torchnpugen.autograd.gen_autograd 和目录路径 $CDIR/torchnpugen/autograd。与第26行的问题类似,这属于从 codegen.autograd.gen_autograd$CDIR/codegen/autograd 重命名。硬编码的路径使得脚本依赖于特定的项目目录结构,如果未来目录结构调整,需要同步修改此脚本。虽然当前改动是统一的(从 codegen 改为 torchnpugen),但未采用更灵活的设计。

问题类型: 代码维护性
文件路径: generate_code.sh
行号: 33
问题代码:

${python_execute} -m torchnpugen.autograd.gen_autograd \
  --out_dir="$CDIR/torch_npu/csrc/aten" \
  --autograd_dir="$CDIR/torchnpugen/autograd" \

修改建议:

建议将模块路径和目录路径定义为变量,例如 `GEN_AUTOGRAD_MODULE="torchnpugen.autograd.gen_autograd"` 和 `AUTOGRAD_DIR="$CDIR/torchnpugen/autograd"`。在命令中引用这些变量。这样,路径配置集中管理,便于维护和适应变化。

此评论由代码审查工具自动生成

likedislike
AtlasAccount
AtlasAccount成员1月26日进行代码检视1
generate_code.sh
@@ -36,3 +35,3 @@
35+ --autograd_dir="$CDIR/torchnpugen/autograd" \
3636 --npu_native_function_dir="$source_yaml"
3737 
38-${python_execute} -m codegen.codegen_ops_info
AtlasAccount
AtlasAccount1月26日评论:

代码维护性: 代码中使用了硬编码的模块路径 torchnpugen.codegen_ops_info。这是从 codegen.codegen_ops_info 重命名而来。同样的问题,硬编码模块名降低了脚本的灵活性。结合第26行和第33行的改动,这三次修改都是将 codegen 重命名为 torchnpugen,但脚本本身没有提供任何机制来适应这种重命名,而是直接写死新名称。

问题类型: 代码维护性
文件路径: generate_code.sh
行号: 38
问题代码:

${python_execute} -m torchnpugen.codegen_ops_info

修改建议:

建议将模块路径定义为变量,例如 `CODEGEN_OPS_INFO_MODULE="torchnpugen.codegen_ops_info"`,然后在命令中引用。或者,考虑在脚本开头定义一个基础包名变量(如 `BASE_PKG="torchnpugen"`),用于拼接出完整的模块路径,这样重命名时只需修改一个地方。

此评论由代码审查工具自动生成

likedislike
AtlasAccount
AtlasAccount成员1月26日进行代码检视1
torchnpugen/__init__.py
@@ -8,1 +1,0 @@
8-def _write_if_changed_security(self, filename: str, contents: str) -> None:
9- old_contents: Optional[str]
AtlasAccount
AtlasAccount1月26日评论:

类型注解与代码可读性: 变量 old_contents 被声明为 Optional[str] 类型,但未从 typing 模块导入 Optional。虽然代码可能在其他地方已导入,但在此片段中缺少显式导入,会影响代码的清晰度和类型检查工具(如 mypy)的正确运行。

问题类型: 类型注解与代码可读性
文件路径: torchnpugen/__init__.py
行号: 9
问题代码:

    old_contents: Optional[str]

修改建议:

在函数内部或模块顶部添加 `from typing import Optional`,以确保类型注解的完整性和可移植性。

此评论由代码审查工具自动生成

likedislike
AtlasAccount
AtlasAccount成员
1月26日 评论:

问题/功能描述

本次PR是一次系统性的代码模块导入路径重构,旨在解决代码库中模块命名空间不一致和依赖关系不清晰的问题。核心功能是将所有对旧模块codegen及其子模块的引用,统一迁移至新的torchnpugen模块,以提升代码结构的清晰度、可维护性和可移植性。此外,PR还修复了因重构不完整导致的ModuleNotFoundError问题,并新增了用于为自定义硬件后端(如NPU)生成PyTorch算子存根代码和功能化支持模板的脚本与工具。

修改方案描述

修改方案涉及多个Python脚本、配置文件和模板。核心修改包括:1) 将所有from codegen.xxx import yyy及相关的相对路径导入语句,统一替换为from torchnpugen.xxx import yyy,确保导入路径的一致性;2) 扩展并提供了新的代码生成脚本(gen_backend_stubs.py),用于解析PyTorch算子定义并生成支持自定义后端的C++注册、分发及功能化代码;3) 提供了一系列配套的C++/Python代码生成模板,用于自动化生成算子注册、重分发和功能化转换所需的骨架代码;4) 修复了重构过程中遗漏的模块导入路径,确保所有功能正常运行。本次变更属于代码重构和工具增强,不涉及核心业务逻辑的修改。

likedislike
ascend-robotascend-robot成员
1月26日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
1月26日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
1月26日 评论:
流水线 PR-pipeline_pytorch#4571 已完成
阶段 任务名 状态 详情
编译构建 Build_X86 >>>
Build_LibTorch >>>
Build_ARM >>>
Build_ARM_inductor 🛑 >>>
Build_X86_py311 🛑 >>>
Build_ARM_py311 🛑 >>>
dist_test_or_not >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 CodeCheck >>>
check_error >>>
开源片段检查 SCA >>>
开发者测试 UT_DIST_X86 🛑 >>>
UT_X86_Part_01 >>>
UT_X86_Part_02 >>>
UT_ARM_A2_Part_01 🛑 >>>
UT_ARM_A2_Part_02 🛑 >>>
UT_inductor_arm 🛑 >>>
流水线 PR-pipeline_pytorch >>>
likedislike
shaoyf成员
1月26日 评论:

/approve

likedislike
ascend-robotascend-robot成员
1月26日 添加了label:lgtm
王超成员
1月26日 评论:

/approve

likedislike
ascend-robotascend-robot成员
1月26日 添加了label:approved
ascend-robot
ascend-robot成员
1月26日 评论:

Review Guide

This Pull-Request Passes Review.
Committers who wrote a comment of /approve are: wangchao430, shaoyf.
Reviewers who wrote a comment of /lgtm are: wangchao430, shaoyf.

likedislike
ascend-robotascend-robot成员
1月26日 合入了pull request