已合并
【API一致性任务】test: add torch._C._host_emptyCache consistency cases on NPU #43549
【API一致性任务】test: add torch._C._host_emptyCache consistency cases on NPU #43549
已合并
cuiyunhao-2026创建于 8月1日
cuiyunhao-2026
8月1日

【合入来源】

如有社区issue,请关联issue链接
请勿携带内部流程信息(需求链接、问题单、内部issue等)

关联 Issue:https://gitcode.com/Ascend/pytorch/issues/2754

【修改方案】

适配 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:399test_host_empty_cache_is_patched 仅做一次性等式断言,未覆盖映射缺失时的优雅跳过与可调用性验证。

结论:该 API 属 NPU 侧自写用例场景,按规范 1.3(test 目录自写用例)test/ 目录新增独立的一致性守护用例文件,不做 test_upstream patch、不做 API 补齐。

2. NPU 适配方案

torch._C._host_emptyCacheimport torch_npu.contrib.transfer_to_npu 注入(昇腾环境 import torch 自动导入 torch_npu,但映射需显式 import transfer_to_npu 触发,因此本用例保留该导入,符合规范 8.9「不导入不必要 torch_npu」的例外情形)。本 PR 仅补充用例,覆盖:

  • 映射一致性:torch._C._host_emptyCachetorch_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_emptyCachetorch_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
likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 cuiyunhao-2026 的贡献)
cuiyunhao-2026cuiyunhao-2026
8月1日 创建了 pull request,commit 7ef3cd0a
atomgit-bot
atomgit-bot
8月1日 评论:

变更摘要

本 PR 新增了 NPU 环境下 torch._C._host_emptyCache 一致性测试用例文件 test/contrib/test_host_empty_cache_api.py。该接口是 torch_npu.contrib.transfer_to_npu 为 NPU 创建的镜像,将 torch._C._host_emptyCache 绑定到 C++ 实现 torch_npu._C._npu_hostEmptyCache。新增测试验证了该映射关系及其可调用行为,以确保该 API 在 torch-npu 各版本间保持一致,并可供后续同类 torch._C._* 一致性检查扩展复用。

主要改动

  • 新增测试类 TestHostEmptyCacheApi:在 test/contrib/test_host_empty_cache_api.py 中定义,继承 TestCase,作为该 API 一致性测试的载体。
  • 新增 test_host_empty_cache_is_mapped 用例:验证导入 transfer_to_nputorch._C._host_emptyCache 存在且等于 torch_npu._C._npu_hostEmptyCache,不满足映射条件时通过 skipTest 跳过。
  • 新增 test_host_empty_cache_is_callable 用例:验证 torch._C._host_emptyCache() 可无参调用且在 NPU 环境中不抛异常,对应 torch_npu.npu.empty_cache() 的 host 缓存路径行为。
likedislike
不准确?
atomgit-bot
atomgit-bot
8月1日 评论:

代码审查

✅ 未发现问题

likedislike
不准确?
ascend-robotascend-robot成员
8月1日 添加了label:ascend-cla/yes
此处折叠了565条消息 查看更多
梁松伟
梁松伟成员
8 天前 评论:

/approve

likedislike
ascend-robotascend-robot成员
8 天前 添加了label:approvedlgtm
ascend-robotascend-robot成员
8 天前 合入了pull request
ascend-robot
ascend-robot成员
8 天前 评论:

Pull Request 已合并或已关闭。

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

likedislike
AtlasAccount
AtlasAccount成员
8 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#14765 [ commitID:fdf0059f ] 已完成
likedislike