已合并
build: 升级C++编译标准至c++20 #3478
chz34创建于 20 天前
build: 升级C++编译标准至c++20 #3478
已合并
chz34创建于 20 天前
chz34
chz34
20 天前

背景

新版 PyTorch 头文件强制要求 C++20,torchair 仍以 C++17 编译时,构建 torch_npu 目标报错:

ATen/ATen.h:5: #error C++20 or later compatible compiler is required to use ATen.
torch/all.h:5: #error C++20 or later compatible compiler is required to use PyTorch.

torch_npu_stub.cpp 经 GetCANNInfo.h 引入 torch 头,因编译标准为 C++17 触发上述 #error。

[2026/07/30 20:12:06.576 GMT+08:00] [ 56%] Built target _abi_compat_ge_apis
[2026/07/30 20:12:06.576 GMT+08:00] [ 57%] Building CXX object torchair/CMakeFiles/_torchair.dir/cann_ir_ability/cann_ir_ability.cpp.o
[2026/07/30 20:12:06.576 GMT+08:00] [ 61%] Building CXX object torchair/CMakeFiles/_torchair.dir/concrete_graph/export.cpp.o
[2026/07/30 20:12:06.576 GMT+08:00] [ 61%] Building CXX object torchair/CMakeFiles/_torchair.dir/concrete_graph/executor.cpp.o
[2026/07/30 20:12:06.576 GMT+08:00] [ 63%] Building CXX object torchair/CMakeFiles/_torchair.dir/concrete_graph/concrete_graph.cpp.o
[2026/07/30 20:12:06.576 GMT+08:00] [ 64%] Building CXX object torchair/CMakeFiles/_torchair.dir/concrete_graph/hdc_channel.cpp.o
[2026/07/30 20:12:06.576 GMT+08:00] [ 64%] Building CXX object torchair/CMakeFiles/_torchair.dir/concrete_graph/logger.cpp.o
[2026/07/30 20:12:06.576 GMT+08:00] [ 66%] Building CXX object torchair/CMakeFiles/_torchair.dir/concrete_graph/npu_aoe.cpp.o
[2026/07/30 20:12:06.576 GMT+08:00] In file included from /opt/_internal/cpython-3.10.20/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/torch.h:4,
[2026/07/30 20:12:06.576 GMT+08:00]                  from /workspace/CODE/third_party/torchair/torchair/torchair/include/executor.h:10,
[2026/07/30 20:12:06.576 GMT+08:00]                  from /workspace/CODE/third_party/torchair/torchair/torchair/concrete_graph/executor.cpp:1:
[2026/07/30 20:12:06.576 GMT+08:00] /opt/_internal/cpython-3.10.20/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/all.h:5:2: error: #error C++20 or later compatible compiler is required to use PyTorch.
[2026/07/30 20:12:06.576 GMT+08:00]     5 | #error C++20 or later compatible compiler is required to use PyTorch.
[2026/07/30 20:12:06.576 GMT+08:00]       |  ^~~~~
[2026/07/30 20:12:06.577 GMT+08:00] In file included from /opt/_internal/cpython-3.10.20/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/torch.h:4,
[2026/07/30 20:12:06.577 GMT+08:00]                  from /workspace/CODE/third_party/torchair/torchair/torchair/include/executor.h:10,
[2026/07/30 20:12:06.577 GMT+08:00]                  from /workspace/CODE/third_party/torchair/torchair/torchair/include/concrete_graph.h:8,
[2026/07/30 20:12:06.577 GMT+08:00]                  from /workspace/CODE/third_party/torchair/torchair/torchair/concrete_graph/concrete_graph.cpp:3:
[2026/07/30 20:12:06.577 GMT+08:00] /opt/_internal/cpython-3.10.20/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/all.h:5:2: error: #error C++20 or later compatible compiler is required to use PyTorch.
[2026/07/30 20:12:06.577 GMT+08:00]     5 | #error C++20 or later compatible compiler is required to use PyTorch.
[2026/07/30 20:12:06.577 GMT+08:00]       |  ^~~~~
[2026/07/30 20:12:06.577 GMT+08:00] In file included from /opt/_internal/cpython-3.10.20/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/torch.h:4,
[2026/07/30 20:12:06.577 GMT+08:00]                  from /workspace/CODE/third_party/torchair/torchair/torchair/include/executor.h:10,
[2026/07/30 20:12:06.577 GMT+08:00]                  from /workspace/CODE/third_party/torchair/torchair/torchair/include/concrete_graph.h:8,
[2026/07/30 20:12:06.577 GMT+08:00]                  from /workspace/CODE/third_party/torchair/torchair/torchair/concrete_graph/export.cpp:4:
[2026/07/30 20:12:06.577 GMT+08:00] /opt/_internal/cpython-3.10.20/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/all.h:5:2: error: #error C++20 or later compatible compiler is required to use PyTorch.
[2026/07/30 20:12:06.577 GMT+08:00]     5 | #error C++20 or later compatible compiler is required to use PyTorch.
[2026/07/30 20:12:06.577 GMT+08:00]       |  ^~~~~
[2026/07/30 20:12:06.577 GMT+08:00] [ 67%] Building CXX object torchair/CMakeFiles/_torchair.dir/concrete_graph/session.cpp.o
[2026/07/30 20:12:06.577 GMT+08:00] In file included from /opt/_internal/cpython-3.10.20/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/types.h:4,
[2026/07/30 20:12:06.577 GMT+08:00]                  from /opt/_internal/cpython-3.10.20/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/dataloader_options.h:5,
[2026/07/30 20:12:06.577 GMT+08:00]                  from /opt/_internal/cpython-3.10.20/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/dataloader/base.h:4,
[2026/07/30 20:12:06.577 GMT+08:00]                  from /opt/_internal/cpython-3.10.20/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/dataloader/stateful.h:5,
[2026/07/30 20:12:06.577 GMT+08:00]                  from /opt/_internal/cpython-3.10.20/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/dataloader.h:4,
[2026/07/30 20:12:06.577 GMT+08:00]                  from /opt/_internal/cpython-3.10.20/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data.h:4,
[2026/07/30 20:12:06.577 GMT+08:00]                  from /opt/_internal/cpython-3.10.20/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/all.h:10:
[2026/07/30 20:12:06.577 GMT+08:00] /opt/_internal/cpython-3.10.20/lib/python3.10/site-packages/torch/include/ATen/ATen.h:5:2: error: #error C++20 or later compatible compiler is required to use ATen.
[2026/07/30 20:12:06.577 GMT+08:00]     5 | #error C++20 or later compatible compiler is required to use ATen.

改动

  • CMake 构建:CMAKE_CXX_STANDARD 17 → 20(CMakeLists.txt、tests/CMakeLists.txt),torch_npu_local_stub 等 stub 目标随之以 C++20 编译。
  • 运行时 g++ 编译:-std=c++17 → -std=c++20(custom_op_compiler.py、inductor_npu_ext compiler/revert_ascir、示例 setup.py)。
  • 测试:torchair_customop_compiler_st.py 断言同步更新为 -std=c++20。
likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 chz34 的贡献)
chz34chz34
20 天前 创建了 pull request,commit 6a50ae39
atomgit-bot
atomgit-bot
20 天前 评论:

变更摘要

此 PR 将项目中所有显式指定的 C++ 编译标准从 -std=c++17 统一升级为 -std=c++20,涉及示例扩展模块的构建脚本、Inductor NPU 编译器后端、自定义算子编译器及其单元测试等 8 个文件,变更均为编译标志字符串的版本号替换。

主要改动

  • 示例模块 setup.py 编译标志升级examples/_kernel_extension_aclgraph/pybind/setup.pytorch_library/setup.py 中的 AscendBuildExtension 将 g++ 编译参数 -std=c++17 替换为 -std=c++20
  • Inductor NPU 编译器后端升级experimental/_inductor_npu_ext 下的 revert_ascir.pyAscCompilerStub 类的三处 g++ 命令)与 _compiler.py_build_cppcompile_wrappercompile_tiling_wrapper 函数)均将 -std=c++17 改为 -std=c++20
  • 自定义算子编译器升级python/torchair/_ge_concrete_graph/custom_op_compiler.pybuild_gcc_command 函数将编译标志从 -std=c++17 改为 -std=c++20
  • 单元测试同步更新tests/st/torchair_customop_compiler_st.pyTestBuildGccCommand 的断言字符串同步从 -std=c++17 更新为 -std=c++20,保持测试与实现一致。
likedislike
atomgit-bot
atomgit-bot
20 天前 评论:

代码审查

✅ 未发现问题

likedislike
ascend-robotascend-robot成员
20 天前 添加了label:smoke-pipeline-running
ascend-robotascend-robot成员
20 天前 添加了label:ascend-cla/no
ascend-robot
ascend-robot成员
20 天前 评论:

CLA Signature Guide

@c_34 , thanks for your pull request.

The following commit(s) have not signed Contributor License Agreement (CLA).

Commit Reason
387f115e build: 升级C++编译标准至c... the email is not signed for CLA! please check if it is correct and the same as your signed email.

If you need to sign CLA, you can click here to sign the CLA.

If you need to check if the email is set up correctly, you can click here to do it in the FAQs.

After signing the CLA or updating the email, you must comment /check-cla to check the CLA status again.

likedislike
ascend-robot
ascend-robot成员
20 天前 评论:

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/torchair fu-hao-huawei, XDaoHong (2/2) fu-hao-huawei (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
ascend-robot
ascend-robot成员
20 天前 评论:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
20 天前 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
20 天前 评论:

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

likedislike
ascend-robotascend-robot成员
20 天前 删除了label:docs-ci-pipeline-running
此处折叠了7条事件消息 查看更多
ascend-robotascend-robot成员
20 天前 添加了label:ascend-cla/yes
ascend-robot
ascend-robot成员
20 天前 评论:
流水线 PR-pipeline_torchair_smoke#344 [ commitID:387f115e ] 已完成
⚠️ 流水线执行结果已过期,当前 PR 最新 commit 为 30a9bd08,请重新运行流水线
阶段 任务名 状态 详情
编译构建 Build_x86 >>>
Build_ARM >>>
开发者测试 smoke >>>
流水线 PR-pipeline_torchair_smoke >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员评论有效
  • smoke : 运行流水线
likedislike
chz34
chz34
20 天前 评论:

smoke

likedislike
ascend-robotascend-robot成员
20 天前 删除了label:smoke-pipeline-passed
ascend-robotascend-robot成员
20 天前 添加了label:smoke-pipeline-running
ascend-robot
ascend-robot成员
20 天前 评论:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
20 天前 删除了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
20 天前 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
20 天前 评论:

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

likedislike
ascend-robotascend-robot成员
20 天前 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
20 天前 添加了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
20 天前 删除了label:smoke-pipeline-running
ascend-robotascend-robot成员
20 天前 添加了label:smoke-pipeline-passed
ascend-robot
ascend-robot成员
20 天前 评论:
流水线 PR-pipeline_torchair_smoke#345 [ commitID:30a9bd08 ] 已完成
阶段 任务名 状态 详情
编译构建 Build_x86 >>>
Build_ARM >>>
开发者测试 smoke >>>
流水线 PR-pipeline_torchair_smoke >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员评论有效
  • smoke : 运行流水线
likedislike
chz34chz34
20 天前 修改了pull request 的描述
chz34
chz34
20 天前 评论:

compile

likedislike
ascend-robotascend-robot成员
20 天前 添加了label:ci-pipeline-running
ascend-robot
ascend-robot成员
20 天前 评论:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
20 天前 删除了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
20 天前 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
20 天前 评论:

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

likedislike
ascend-robotascend-robot成员
20 天前 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
20 天前 添加了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
20 天前 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
20 天前 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
20 天前 评论:
流水线 PR-pipeline_torchair#3569 [ commitID:30a9bd08 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 Build_x86 >>>
Build_ARM >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 CodeCheck >>>
CodeCheck_pre-commit >>>
开源片段检查 SCA >>>
流水线 PR-pipeline_torchair >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员评论有效
  • compile : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
fu_hao
fu_hao成员
20 天前 评论:

/approve

likedislike
ascend-robotascend-robot成员
20 天前 添加了label:approved
XDaoHong
XDaoHong成员
20 天前 评论:

/lgtm

likedislike
ascend-robotascend-robot成员
20 天前 添加了label:lgtm
ascend-robotascend-robot成员
20 天前 合入了pull request
XDaoHong
XDaoHong成员
20 天前 评论:

【MR评价】评价分数:3,
评价意见:
编码规范遵守度:无编码规范问题;
代码设计:不涉及代码设计;
DT质量:不涉及;

likedislike