已合并
test: add NPU coverage for torch._utils._get_available_device_type #42005
Lane0218创建于 7月17日
test: add NPU coverage for torch._utils._get_available_device_type #42005
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 Lane0218 的贡献)7月17日 关联了issue:【社区任务】7月社区任务第二期-Ascend for PyTorch API 一致性开发(14)
atomgit-bot
7月17日 评论:
7月17日 评论:
变更摘要
此 PR 为 Torch-NPU 补充了 torch._utils._get_available_device_type 私有 API 的单元测试覆盖。该 API 在 NPU 环境下预期返回 "npu",但此前缺少直接的返回值断言用例。变更仅在 test/test_utils.py 的 TestExtensionUtils 类中新增一个最小化测试方法,无需修改源码或引入额外依赖。
主要改动
- 新增
test_get_available_device_type测试方法:在TestExtensionUtils类中添加了一个测试用例,通过assertEqual断言torch._utils._get_available_device_type()的返回值为"npu"。 - 零依赖、零副作用:该测试不涉及张量创建、不新增文件导入、不做冗余环境判断,仅对 API 返回值做直接校验。


ascend-robot
7月17日 评论:
7月17日 评论:
atomgit-bot
7月17日 评论:
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清理逻辑以消除隐患。
⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。


此处折叠了136条消息 查看更多
ascend-robot
7 天前 评论:
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 | ✅ | >>> |
- compile、compile_inductor、compile_torchair : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


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


【合入来源】
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目录新增独立、可扩展的测试文件:用例只直接断言
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返回值为
"npu",表明测试实际加载了 NPU PrivateUse1 后端;没有跳过新增测试。【CheckList】