Co-authored-by: liujiacheng_2026<laneljc@qq.com> # message auto-generated for no-merge-commit merge: !42006 merge test-get-available-device-type-master into master test: add NPU coverage for torch._utils._get_available_device_type Created-by: liujiacheng_2026 Commit-by: liujiacheng_2026 Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> # 【合入来源】 https://gitcode.com/Ascend/pytorch/issues/2924 - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 # 【修改方案】 ## 一、API 功能 `torch._utils._get_available_device_type` 查询当前 PyTorch 环境可用的加速设备类型;Torch-NPU 环境预期返回 `"npu"`。 ## 二、PyTorch 社区用例情况与新增必要性 已在 PyTorch 官方社区和 Torch-NPU 原有测试中检索 `torch._utils._get_available_device_type`,没有发现针对该 API 返回值的直接断言。该 API 是私有工具函数,不涉及 Tensor 或算子计算。 因此本 PR 在 torch-npu `test` 目录新增独立、可扩展的测试文件: ```text 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 私有接口,按照任务规范无需补充资料。 # 【接口变更】 无客户可见接口变更,仅新增直接测试。 # 【功能验证】 验证环境:master、Ascend910B4。 ```bash python test/test_utils_api.py -v ``` ```text test_get_available_device_type (__main__.TestTorchUtilsAPIs.test_get_available_device_type) ... ok ---------------------------------------------------------------------- Ran 1 test in 0.026s OK ``` 返回值为 `"npu"`,表明测试实际加载了 NPU PrivateUse1 后端;没有跳过新增测试。 # 【CheckList】 - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!42006
34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
# Copyright (c) 2026 Huawei Technologies Co., Ltd
|
|
# All rights reserved.
|
|
#
|
|
# Licensed under the BSD 3-Clause License (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# https://opensource.org/licenses/BSD-3-Clause
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
"""
|
|
Add validation cases for torch._utils APIs on NPU:
|
|
1. PyTorch community lacks sufficient direct validation for some APIs on NPU.
|
|
2. This file validates torch._utils._get_available_device_type (extendable).
|
|
"""
|
|
|
|
import torch
|
|
from torch.testing._internal.common_utils import TestCase, run_tests
|
|
|
|
|
|
class TestTorchUtilsAPIs(TestCase):
|
|
|
|
def test_get_available_device_type(self):
|
|
self.assertEqual(torch._utils._get_available_device_type(), "npu")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run_tests()
|