已合并
[sync] PR-39678: revert: detect Ascend950 SoC for TORCH_NPU_USE_COMPATIBLE_IMPL default #39724
[sync] PR-39678: revert: detect Ascend950 SoC for TORCH_NPU_USE_COMPATIBLE_IMPL default #39724
已合并
ascend-robot创建于 6月30日
ascend-robot
ascend-robot成员
6月30日

1. Origin pull request:

https://gitcode.com/Ascend/pytorch/merge_requests/39678

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

Sha Datetime Message
8539c290 2026-06-30 21:08:41 +0800 CST Revert "feat: detect Ascend950 SoC for TORCH_NPU_USE_COMPATIBLE_IMPL default"

This reverts commit acebf35339791ea6fcac6f116acdb242a1c09096.

Removes the _is_ascend950() SoC auto-detection and the SoC-based default-value assignment for TORCH_NPU_USE_COMPATIBLE_IMPL in torch_npu/init.py, and drops test_compatible_impl_soc_detect.py.

Co-Authored-By: Claude noreply@anthropic.com
likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 ascend-robot 的贡献)
ascend-robotascend-robot成员
6月30日 创建了 pull request,commit e8230cb3
ascend-robotascend-robot成员
6月30日 关联了issue:[Bug]:一致性路径路由环境变量默认值回退
ascend-robotascend-robot成员
6月30日 添加了label:ascend-cla/yes
ascend-robot
ascend-robot成员
6月30日 评论:

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 liangsongwei, ffmh (2/2) liangsongwei (1/1)
test liangsongwei, ffmh (2/2) liangsongwei (1/1)

💡 Tip:

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

CLA Signature Pass

ascend-ds-bot, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
ascend-robotascend-robot成员
6月30日 添加了label:ci-pipeline-running
atomgit-bot
atomgit-bot
6月30日 评论:

变更摘要

本 PR 回退了之前的一个功能特性:移除了对 Ascend950 SoC 的自动检测逻辑,以及基于 SoC 版本自动设置 TORCH_NPU_USE_COMPATIBLE_IMPL 环境变量默认值的机制。同时删除了对应的单元测试文件。

主要改动

  • 移除 _is_ascend950() 函数: 从 torch_npu/__init__.py 中删除了通过 libascendcl.soaclrtGetSocName 接口检测 SoC 是否以 "Ascend950" 开头的完整函数实现(共约 12 行代码)
  • 移除 TORCH_NPU_USE_COMPATIBLE_IMPL 自动赋值逻辑: 删除了在 C++ 初始化前根据 _is_ascend950() 返回值自动设置环境变量默认值的代码块(当环境变量未被预设置时,Ascend950 设为 "1",否则为 "0"
  • 删除测试文件: 整个 test/test_compatible_impl_soc_detect.py 文件被删除(42 行),该文件包含 4 个测试用例,分别验证 _is_ascend950() 返回值类型、与 C++ GetSocVersion() 的一致性、默认值与 SoC 匹配、以及用户预设值的保留行为
likedislike
atomgit-bot
atomgit-bot
6月30日 评论:

代码审查

审查总结

本次 PR 对 2 个文件进行了变更:

文件 审查结果
torch_npu/__init__.py 发现问题 — 删除了 _is_ascend950() 函数及其相关的 TORCH_NPU_USE_COMPATIBLE_IMPL 默认值设定逻辑(共 -19 行),但未同步清理调用方
test/test_compatible_impl_soc_detect.py 无问题 — 该测试文件随所测功能一并删除,清理干净,且未被其他文件引用

按严重程度统计

严重程度 数量
P0 0
P1 1
P2 0
P3 0

总体风险判断

中等风险。回退逻辑本身正确(移除了脆弱的 SoC 探测和隐式环境变量改写),符合 Issue #2561 的期望。但回退不完整:benchmarks/torchbench/torchbench.py:50 仍依赖已删除的 torch_npu._is_ascend950(),会在 benchmark 运行时因 AttributeError 崩溃。建议在该 PR 中一并修复该调用方。

类型 数量
🔴 阻塞 1
🟡 建议 0

⛔ 需要修改

likedislike
atomgit-bot
atomgit-bot6月30日进行代码检视1
torch_npu/__init__.py
@@ -10,25 +8,25 @@
10- import ctypes
11- lib = ctypes.CDLL("libascendcl.so")
12- lib.aclrtGetSocName.restype = ctypes.c_char_p
13- soc_name = lib.aclrtGetSocName()
14- if soc_name and soc_name.decode().startswith("Ascend950"):
15- return True
16- except Exception as e:
17- import logging
18- logging.getLogger("torch_npu").debug(
19- f"Failed to detect SoC version via aclrtGetSocName: {e}")
20- return False
21- 
22- 
23-# Set TORCH_NPU_USE_COMPATIBLE_IMPL default based on SoC version before C++ init
24-if "TORCH_NPU_USE_COMPATIBLE_IMPL" not in os.environ:
25- os.environ["TORCH_NPU_USE_COMPATIBLE_IMPL"] = "1" if _is_ascend950() else "0"
26- 
278# Disable autoloading before running 'import torch' to avoid circular dependencies
289ORG_AUTOLOAD = os.getenv("TORCH_DEVICE_BACKEND_AUTOLOAD", "1")
2910os.environ["TORCH_DEVICE_BACKEND_AUTOLOAD"] = "0"
@@ -41,7 +22,7 @@
4122 error_code_msg = "feature not supported"
4223 submodule_name = "PTA"
4324 raise RuntimeError(
4425 f"Two accelerators cannot be used at the same time "
atomgit-bot
atomgit-bot6月30日评论:

🟠 High Priority

diff 删除了 torch_npu/__init__.py 中的 _is_ascend950() 函数(第8-20行),但未同步清理调用方 benchmarks/torchbench/torchbench.py:50,该处仍通过 torch_npu._is_ascend950() 调用已不存在的函数。

失败路径:

  1. torch_npu 可正常 import(不会触发 ImportError
  2. 执行 torch_npu._is_ascend950() 时抛出 AttributeError
  3. 由于外层 try...except ImportError 只捕获 ImportErrorAttributeError 不会被捕获
  4. 异常向上传播导致 benchmark 脚本崩溃

受影响的代码在 benchmarks/torchbench/torchbench.py:452 使用 is_ascend950 判断 Ascend950 上的容差策略,该变量在崩溃路径上不会被赋值。

建议:在同一个 PR 中也回退 benchmarks/torchbench/torchbench.py 中对 torch_npu._is_ascend950() 的调用,改为使用已存在的内部 API(如 torch_npu._inductor.config.is_ascend950)或从 torch_npu._C._npu_get_soc_version() 自行判断 soc_version >= 260,并保留 except ImportError 回退逻辑。

likedislike
ascend-robotascend-robot成员
6月30日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
6月30日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
6月30日 评论:
流水线 PR-pipeline_pytorch#40309 [ commitID:5511a222 ] 已完成
>>>代码风格自动修复执行失败,具体请查看日志,不影响流水线执行及PR合入
阶段 任务名 状态 详情
编译构建 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 🛑 >>>
UT_ARM_A2_Select_Part_01 >>>
UT_ARM_A2_Select_Part_02 >>>
流水线 PR-pipeline_pytorch >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员[wujinyuan1, huangjingwei, liangsongwei, yashi999, culechan, Dring, wuyouqi1, L1919_snow, qq_52711437, WhiteNight12, nomiz, xiu_21, ffmh, wanglijun55, hss-shuai, husichao, smallsilly, lanshaozuishuai, jimmyisme1, lzy0920232, alpha-junh, Sunshine_Youngster, wei_zhuoyi, zhangyihuiben, zyw-hw, zzzkeke, rmch, yangch0324, LucciC, AACAES, renyujin, wjlflyer, senzhen-town, pengjingyou, qsc97, limuan, yule100, xiaoqi-zhou, kuhn7, chenxingying, hanye02, zichun_ye, anyrenwei, kkjocker, wangzili121, Lu_G, yvjc, puddingfjz, HandsoemLemon, bigprestigee1, huawuyi, zhenyu10, dairenjie, du-jin-hang, zou-jieyu, adelaideliu, TrHan, wanlinan, Windwindzzz, pengqihw, kisnwang, yuheng_wang, honghao_wang, jizewei, zhangguoguang, sunyu-xuan, chenrayray, hbhu_bin, liujunzhu, c_34, LiNuoh, maoyuanpeng1, zzhongmin, zhaoyu65, bellatan, jiabaolin, zhuofanshen, wencaiwen, lu_zhuge, caoshuyang, molly12, lyx324521, LQ1206, gitcode-chenjiao, cai-weiwei1989, CHDong, ogqin, yuanlipingGit, xuqinglin1, lqz2, zouwei1, chaoluoa, paradox325, jackzhang1116, yaoyao, akh, yujiacheng, dengjie0116, Hubert11111, Shine_Ws, wslhj555, longqiand, OYtao666, JiaqingQiang, luyyyy, Kingbelial, zhanghaiyu0101, wenxp1018, yanliu-luoluo, ksun_sekiro, liyong328, wgzheng, tangky, vivi_is_coding, aoiaoisola, weixin_44494597, wangmengmengwang65667, hid57809721, qq_35468730, comeonup, C547032, gcw_m5OQChA4, yao_yao_ling_xian, cnnbwcy, szqfes_12, cora_19, cann_lilin, can, shawnylee233]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
梁松伟
梁松伟成员
6月30日 评论:

/approve

likedislike
ascend-robotascend-robot成员
6月30日 添加了label:approved
ffmh
ffmh成员
6月30日 评论:

/lgtm

likedislike
ascend-robotascend-robot成员
6月30日 添加了label:lgtm
ascend-robotascend-robot成员
6月30日 解决了最后一个问题
ascend-robotascend-robot成员
6月30日 合入了pull request
ascend-robot
ascend-robot成员
6月30日 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
ascend-robot
ascend-robot成员
6月30日 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
ascend-robot
ascend-robot成员
6月30日 评论:
流水线 pytorch_gitcode_PR_multiVersion#11623 [ commitID:5511a222 ] 已完成
likedislike