Files
TorchNPU/generate_code.sh
T
c_34 46936d18a1 feat(c_shim): version AOTInductor NPU C shim header per torch version
Co-authored-by: chz34<chenhaozhe1@huawei.com>



# message auto-generated for no-merge-commit merge:
!40590 merge compat/c-shim into master

feat(c_shim): version AOTInductor NPU C shim header per torch version

Created-by: c_34
Commit-by: chz34
Merged-by: ascend-robot
Description: <!--
PR描述模板更新日期:20260203
-->

# 【合入来源】
> <font color="red">**如有社区issue,请关联issue链接**</font>\
> <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font>

- [ ] 需求
- [ ] 问题单
- [x] issue/工单 https://gitcode.com/Ascend/pytorch/issues/2568
- [x] 重构优化
- [ ] 资料更新

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

根据多个版本分别在不同的目录存储`c_shim_npu.h`,构建过程中根据构建命令(实际参考version.txt)选择不同的版本进行校验。

构建中固定生成一份新的`c_shim_npu.h`文件,存放在原有的位置。

对于开发中的上游最新版本,对应版本目录缺失或者文件缺失,进行软报错,打印提示但不中断构建,用于放行上游的主动改变。

对于已经稳定的上游版本,根据对应目录进行强校验,不一致则拦截,主要控制PTA与op-plugin的内部修改。

详情参考issue内容:https://gitcode.com/Ascend/pytorch/issues/2568

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

不涉及

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

不涉及

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

针对现有功能的多版本兼容重构,基于现有用例进行看护

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

- [x] 代码注释完备,正确记录错误日志
- [x] 代码实现进行了返回值、空指针等校验
- [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等
- [x] PR持续集成流水线(CI)执行通过,代码检查无异常


See merge request: Ascend/pytorch!40590
2026-07-10 15:30:36 +08:00

83 lines
2.6 KiB
Bash

#!/bin/bash
set -e
CDIR="$(cd "$(dirname "$0")" ; pwd -P)"
cd $CDIR
python_execute="$1"
pytorch_version="$2"
IFS='.' read -ra version_parts <<< "$pytorch_version"
pytorch_dir="v${version_parts[0]}r${version_parts[1]}"
# Fetch ACL headers from submodule paths
ACL_DEST="$CDIR/third_party/acl/inc/acl"
echo " --- Fetching ACL headers from submodules..."
ACL_SRC="$CDIR/third_party/acl_src"
# Copy runtime headers (lower priority, copied first)
if [ -d "$ACL_SRC/runtime/include/external/acl" ]; then
mkdir -p "$ACL_DEST"
cp -r "$ACL_SRC/runtime/include/external/acl/"* "$ACL_DEST/"
echo " --- Copied runtime acl headers"
fi
# Copy ge headers (higher priority, overwrites runtime)
if [ -d "$ACL_SRC/ge/inc/external/acl" ]; then
mkdir -p "$ACL_DEST"
cp -r "$ACL_SRC/ge/inc/external/acl/"* "$ACL_DEST/"
echo " --- Copied ge acl headers"
fi
# Copy super_kernel.h from graph-autofusion
SUPER_KERNEL_SRC="$ACL_SRC/graph-autofusion/super_kernel/include/super_kernel/super_kernel.h"
if [ -f "$SUPER_KERNEL_SRC" ]; then
cp "$SUPER_KERNEL_SRC" "$ACL_DEST/super_kernel.h"
echo " --- Copied super_kernel.h"
fi
# Clean up submodule working directories
rm -rf "$ACL_SRC"
echo " --- Cleaned up acl_src submodule directories"
echo " --- ACL headers fetched successfully"
file=$CDIR/third_party/op-plugin/gencode.sh
if [ -f "${file}" ]; then
bash ${file} ${pytorch_version} ${python_execute}
fi
op_plugin_config_path=$CDIR/third_party/op-plugin/op_plugin/config/$pytorch_dir
source_yaml="$CDIR/torch_npu/csrc/aten/npu_native_functions.yaml"
testing_source_yaml="$CDIR/test/ops_unsupport_list.yaml"
op_plugin_functions_yaml_path="$op_plugin_config_path/npu_native_functions.yaml"
${python_execute} -m torchnpugen.gen_backend_stubs \
--output_dir="torch_npu/csrc/aten" \
--source_yaml="$source_yaml" \
--impl_path="$CDIR/torch_npu/csrc/aten" \
--op_plugin_impl_path="$CDIR/third_party/op-plugin/op_plugin/ops" \
--op_plugin_yaml_path="$op_plugin_config_path/op_plugin_functions.yaml" \
--pytorch_version_dir="$pytorch_dir"
${python_execute} -m torchnpugen.autograd.gen_autograd \
--out_dir="$CDIR/torch_npu/csrc/aten" \
--autograd_dir="$CDIR/torchnpugen/autograd" \
--npu_native_function_dir="$source_yaml"
${python_execute} -m torchnpugen.codegen_ops_info
if [ -f $CDIR/third_party/op-plugin/codegen/templates/_op_plugin_docs.py ]; then
if [ -f $CDIR/torch_npu/_op_plugin_docs.py ]; then
# remove _op_plugin_docs.py
rm $CDIR/torch_npu/_op_plugin_docs.py
echo "Existing _op_plugin_docs.py in torch_npu deleted."
fi
cp -f $CDIR/third_party/op-plugin/codegen/templates/_op_plugin_docs.py $CDIR/torch_npu/_op_plugin_docs.py
fi