Co-authored-by: cuiyunhao-2026<1830099122@qq.com> # message auto-generated for no-merge-commit merge: !43549 merge test-host-emptycache-npu-master into master 【API一致性任务】test: add torch._C._host_emptyCache consistency cases on NPU Created-by: cuiyunhao-2026 Commit-by: cuiyunhao-2026 Merged-by: ascend-robot Description: # 【合入来源】 > <font color="red">**如有社区issue,请关联issue链接**</font> > <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font> 关联 Issue:https://gitcode.com/Ascend/pytorch/issues/2754 - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 # 【修改方案】 适配 API:`torch._C._host_emptyCache`(torch-npu 已通过 `torch_npu.contrib.transfer_to_npu` 注入该镜像映射,但缺少独立的一致性验证用例,本 PR 自写用例补齐,按规范 1.3)。 **0. 按导师最新要求的保留判定** 导师公告:资料 PR 已全部打回,且不再接收 NPU 适配 patch 与非硬件相关 API 的新增用例。本 PR 经复核属于**硬件相关**用例,予以保留: - `torch._C._host_emptyCache` 是 torch-npu 通过 `torch_npu.contrib.transfer_to_npu` 注入的 **NPU 专属镜像映射**(绑定 `torch._C._host_emptyCache = torch_npu._C._npu_hostEmptyCache`),直接对应昇腾硬件的主机侧缓存释放路径,是典型的硬件相关接口; - 本用例验证该映射与 NPU 实现的一致性、可调用性及非法参数的 `TypeError` 拒绝,属于规范 1.2②「涉及硬件接口,在 test 目录新增用例」的保留范围,**不属于被关闭的非硬件相关用例**; - 本 PR 为 `test/` 自写用例,非 `test_upstream` NPU 适配 patch,符合"不再接收 NPU 适配 patch"的要求。 **1. 上游社区用例情况分析** `torch._C._host_emptyCache` 是 torch-npu 通过 `torch_npu.contrib.transfer_to_npu` 注入的 NPU 专属镜像映射(绑定 `torch._C._host_emptyCache = torch_npu._C._npu_hostEmptyCache`),并非 PyTorch 上游原生 API,上游社区无对应用例。仓库内现有 `test/contrib/test_transfer_to_npu.py:399` 的 `test_host_empty_cache_is_patched` 仅做一次性等式断言,未覆盖映射缺失时的优雅跳过与可调用性验证。 结论:该 API 属 NPU 侧自写用例场景,按规范 **1.3(test 目录自写用例)** 在 `test/` 目录新增独立的一致性守护用例文件,不做 test_upstream patch、不做 API 补齐。 **2. NPU 适配方案** `torch._C._host_emptyCache` 由 `import torch_npu.contrib.transfer_to_npu` 注入(昇腾环境 `import torch` 自动导入 torch_npu,但映射需显式 import transfer_to_npu 触发,因此本用例保留该导入,符合规范 8.9「不导入不必要 torch_npu」的例外情形)。本 PR 仅补充用例,覆盖: - 映射一致性:`torch._C._host_emptyCache` 与 `torch_npu._C._npu_hostEmptyCache` 指向同一实现(`self.assertEqual`); - 可调用性:`torch._C._host_emptyCache()` 无参调用不抛异常,镜像 `torch_npu.npu.empty_cache()` 的主机缓存释放路径;非法位置/关键字参数必须被 `TypeError` 拒绝; - 映射缺失时以 `self.skipTest(...)` 优雅跳过,避免无该绑定环境误报。 **3. 提交方式(test 目录自写用例,规范 1.3)** 按规范 1.3,在 `test/` 目录新增 `test/contrib/test_host_empty_cache_api.py`(同类 `torch._C._*` NPU 镜像映射一致性用例可归集于此,便于扩展)。文件遵循 torch-npu 测试规范:Huawei BSD-3 版权头、模块 docstring 说明用途与可扩展、从 `torch.testing._internal.common_utils` 导入 `run_tests`/`TestCase`(不直接 import unittest)、导入顺序 torch → torch.testing → torch_npu、类前后空 2 行、方法间空 1 行、`self.assert*` 断言、无 print / 无 try-except / 无裸 assert。 按规范 1.3,提交到 `2.7.1 / 2.11.0 / 2.12.0 以及 master` 四个分支。各分支基线对应目标分支 HEAD,新增文件内容逐字节一致。2.9.0 / 2.10.0 已转入维护阶段,不作补齐要求。 **4. API 补齐** 不需要。`torch._C._host_emptyCache` 由 `torch_npu.contrib.transfer_to_npu` 注入,本 PR 仅补充测试用例,不提交任何 API 实现代码(映射本身由 torch-npu 提供)。 **5. 多版本分支合入** | 目标分支 | PR | 说明 | |---------|--------|------| | v2.7.1 | https://gitcode.com/Ascend/pytorch/pull/43546 | test 自写用例 | | v2.11.0 | https://gitcode.com/Ascend/pytorch/pull/43547 | test 自写用例 | | v2.12.0 | https://gitcode.com/Ascend/pytorch/pull/43548 | test 自写用例 | | master | https://gitcode.com/Ascend/pytorch/pull/43549 | test 自写用例 | - 按规范 1.3(test 目录用例),**master 需要提交**。 - 2.9.0 / 2.10.0 已转入维护阶段,不作补齐要求。 # 【资料变更】 不涉及。 依据:`torch._C._host_emptyCache` 为 PyTorch 私有(下划线前缀)接口,且属 NPU 专属镜像映射,按规范 2.3「PyTorch 私有接口不需要补充资料」。经核查 `docs/zh/native_apis/` 未收录该 API,故不在 PR 中补资料。 # 【接口变更】 不涉及。 依据:`torch._C._host_emptyCache` 由 torch-npu 注入的镜像映射提供,本 PR 仅补充测试用例,无跨代码仓或客户面可见接口变更。 # 【功能验证】 - 离线校验:用例已在真实 Ascend NPU(910B4)环境执行,全部通过; - 运行命令:`python test/contrib/test_host_empty_cache_api.py`; - 运行结果(NPU 实测): ``` .. Ran 2 tests in 0.021s OK ``` - 用例清单: - `test_host_empty_cache_is_mapped` —— PASSED - `test_host_empty_cache_is_callable` —— PASSED See merge request: Ascend/pytorch!43549
56 lines
2.5 KiB
Python
56 lines
2.5 KiB
Python
# Copyright (c) Huawei Technologies Co., Ltd. 2020-2024. All rights reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# 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.
|
|
"""
|
|
torch._C._host_emptyCache API consistency test.
|
|
|
|
torch._C._host_emptyCache is an NPU-specific mirror created by
|
|
torch_npu.contrib.transfer_to_npu, binding torch._C._host_emptyCache to the
|
|
C++ implementation torch_npu._C._npu_hostEmptyCache. This file verifies both
|
|
the mapping and its callable behavior so the API stays consistent across
|
|
torch-npu releases. Additional torch._C._* consistency checks of the same kind
|
|
can be appended here.
|
|
"""
|
|
|
|
import torch
|
|
from torch.testing._internal.common_utils import run_tests, TestCase
|
|
import torch_npu.contrib.transfer_to_npu # importing this module injects torch._C._host_emptyCache
|
|
|
|
|
|
class TestHostEmptyCacheApi(TestCase):
|
|
|
|
def test_host_empty_cache_is_mapped(self):
|
|
# transfer_to_npu injects torch._C._host_emptyCache as a side effect of
|
|
# its module import; the binding exists as soon as that import completes.
|
|
if not hasattr(torch._C, "_host_emptyCache"):
|
|
self.skipTest("torch._C._host_emptyCache not mapped in this torch-npu build")
|
|
self.assertEqual(torch._C._host_emptyCache, torch_npu._C._npu_hostEmptyCache)
|
|
|
|
def test_host_empty_cache_is_callable(self):
|
|
# The API is invocable without arguments and returns None; it mirrors the
|
|
# host-cache path of torch_npu.npu.empty_cache(). Illegal arguments must
|
|
# be rejected with TypeError.
|
|
if not hasattr(torch._C, "_host_emptyCache"):
|
|
self.skipTest("torch._C._host_emptyCache not mapped in this torch-npu build")
|
|
# No arguments: callable and returns None.
|
|
self.assertIsNone(torch._C._host_emptyCache())
|
|
# Illegal positional / keyword arguments must be rejected.
|
|
with self.assertRaises(TypeError):
|
|
torch._C._host_emptyCache(0)
|
|
with self.assertRaises(TypeError):
|
|
torch._C._host_emptyCache(device="npu")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run_tests()
|