已合并
test: add NPU coverage for torch._utils._get_available_device_type #42005
test: add NPU coverage for torch._utils._get_available_device_type #42005
已合并
Lane0218创建于 7月17日
Lane0218
Lane0218
7月17日

【合入来源】

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

【修改方案】

一、API 功能

torch._utils._get_available_device_type 查询当前可用加速设备类型;Torch-NPU 环境预期返回 "npu"

二、PyTorch 社区用例情况与新增必要性

已在 PyTorch 官方社区和 Torch-NPU 原有测试中检索 torch._utils._get_available_device_type,没有发现针对该 API 返回值的直接断言。该 API 是私有工具函数,不涉及 Tensor 或算子计算。

因此本 PR 在 torch-npu test 目录新增独立、可扩展的测试文件:

test/test_utils_api.py::TestTorchUtilsAPIs::test_get_available_device_type

用例只直接断言 torch._utils._get_available_device_type() 返回 "npu",不增加冗余环境探测、Tensor 创建或异常包装。

三、NPU 适配

3.1 API 适配

Torch-NPU 已将 PrivateUse1 后端注册为 npu,API 实际返回 "npu",无需修改 torch_npu/ 源码。

3.2 测试用例适配

该 API 是非计算类工具函数,不涉及 Tensor,因此新增测试不需要创建 NPU Tensor;测试通过返回值直接验证 NPU 后端注册结果。

【资料变更】

无资料文件变更。该 API 是 PyTorch 私有接口,按照任务规范无需补充资料。

【接口变更】

无客户可见接口变更,仅新增直接测试。

【功能验证】

验证环境:PyTorch 2.12.0、torch-npu 对应版本、Ascend910B4。

python test/test_utils_api.py -v
test_get_available_device_type (__main__.TestTorchUtilsAPIs.test_get_available_device_type) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.205s

OK

返回值为 "npu",表明测试实际加载了 NPU PrivateUse1 后端;没有跳过新增测试。

【CheckList】

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 Lane0218 的贡献)
Lane0218Lane0218
7月17日 创建了 pull request,commit 58b43a56
Lane0218Lane0218
7月17日 关联了issue:【社区任务】7月社区任务第二期-Ascend for PyTorch API 一致性开发(14)
atomgit-bot
atomgit-bot
7月17日 评论:

变更摘要

此 PR 为 Torch-NPU 补充了 torch._utils._get_available_device_type 私有 API 的单元测试覆盖。该 API 在 NPU 环境下预期返回 "npu",但此前缺少直接的返回值断言用例。变更仅在 test/test_utils.pyTestExtensionUtils 类中新增一个最小化测试方法,无需修改源码或引入额外依赖。

主要改动

  • 新增 test_get_available_device_type 测试方法:在 TestExtensionUtils 类中添加了一个测试用例,通过 assertEqual 断言 torch._utils._get_available_device_type() 的返回值为 "npu"
  • 零依赖、零副作用:该测试不涉及张量创建、不新增文件导入、不做冗余环境判断,仅对 API 返回值做直接校验。
likedislike
atomgit-bot
atomgit-bot
7月17日 评论:

代码审查

Closing Summary

  • Changed file reviewed: test/test_utils.py — 1 finding
  • Findings by priority: P3: 1
  • Overall risk judgment: 低风险。新增测试本身逻辑正确、简洁,但暴露了同类 tearDown 清理不完整这一既存问题——test_external_module_and_backend_register 修改的全局状态(privateuse1 后端名 "foo" 及对应设备模块)未被 tearDown 清理,在特定测试执行顺序下可能使新测试产生不确定的结果。当前因 _get_available_device_type 的模块迭代顺序使 "npu" 先被命中而暂时安全,但属脆弱依赖,建议补充 tearDown 清理逻辑以消除隐患。

⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。

likedislike
此处折叠了136条消息 查看更多
ascend-robot
ascend-robot成员
7 天前 评论:
流水线 PR-pipeline_pytorch#60364 [ commitID:f84bec28 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 Build_X86 >>>
Build_ARM >>>
Build_LibTorch_x86 >>>
Build_LibTorch_ARM >>>
Build_X86_torchair 🛑 >>>
Build_ARM_torchair 🛑 >>>
patch_test 🛑 >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 codecheck_pre-commit >>>
check_error >>>
lintrunner >>>
开源片段检查 SCA >>>
开发者测试 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, fanglanyue0916, hhz0, LiNuohang, taohuoquan, Jesse, WSs_321]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
renyujin成员
7 天前 评论:

/lgtm
/approve

likedislike
ascend-robotascend-robot成员
7 天前 添加了label:approvedlgtm
ascend-robotascend-robot成员
7 天前 合入了pull request
ascend-robot
ascend-robot成员
7 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#13972 [ commitID:f84bec28 ] 已完成
likedislike