已合并
【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日
1 个文件变更+55-0
@@ -0,0 +1,55 @@
1+# Copyright (c) Huawei Technologies Co., Ltd. 2020-2024. All rights reserved.
2+#
3+# Licensed under the Apache License, Version 2.0 (the "License");
4+# you may not use this file except in compliance with the License.
5+# You may obtain a copy of the License at
6+#
7+# http://www.apache.org/licenses/LICENSE-2.0
8+#
9+# Unless required by applicable law or agreed to in writing, software
10+# distributed under the License is distributed on an "AS IS" BASIS,
11+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+# See the License for the specific language governing permissions and
13+# limitations under the License.
14+"""
15+torch._C._host_emptyCache API consistency test.
16+ 
17+torch._C._host_emptyCache is an NPU-specific mirror created by
18+torch_npu.contrib.transfer_to_npu, binding torch._C._host_emptyCache to the
19+C++ implementation torch_npu._C._npu_hostEmptyCache. This file verifies both
20+the mapping and its callable behavior so the API stays consistent across
21+torch-npu releases. Additional torch._C._* consistency checks of the same kind
22+can be appended here.
23+"""
24+ 
25+import torch
26+from torch.testing._internal.common_utils import run_tests, TestCase
27+import torch_npu.contrib.transfer_to_npu # importing this module injects torch._C._host_emptyCache
28+ 
29+ 
30+class TestHostEmptyCacheApi(TestCase):
31+ 
32+ def test_host_empty_cache_is_mapped(self):
33+ # transfer_to_npu injects torch._C._host_emptyCache as a side effect of
34+ # its module import; the binding exists as soon as that import completes.
35+ if not hasattr(torch._C, "_host_emptyCache"):
36+ self.skipTest("torch._C._host_emptyCache not mapped in this torch-npu build")
37+ self.assertEqual(torch._C._host_emptyCache, torch_npu._C._npu_hostEmptyCache)
38+ 
39+ def test_host_empty_cache_is_callable(self):
40+ # The API is invocable without arguments and returns None; it mirrors the
41+ # host-cache path of torch_npu.npu.empty_cache(). Illegal arguments must
42+ # be rejected with TypeError.
43+ if not hasattr(torch._C, "_host_emptyCache"):
44+ self.skipTest("torch._C._host_emptyCache not mapped in this torch-npu build")
45+ # No arguments: callable and returns None.
46+ self.assertIsNone(torch._C._host_emptyCache())
47+ # Illegal positional / keyword arguments must be rejected.
48+ with self.assertRaises(TypeError):
49+ torch._C._host_emptyCache(0)
50+ with self.assertRaises(TypeError):
51+ torch._C._host_emptyCache(device="npu")
52+ 
53+ 
54+if __name__ == "__main__":
55+ run_tests()