已合并
[Task-47][v2.7.1] API Consistency: torch._C._set_warnAlways #41347
[Task-47][v2.7.1] API Consistency: torch._C._set_warnAlways #41347
已合并
Yhw050920创建于 7月11日
1 个文件变更+65-0
@@ -0,0 +1,65 @@
1+# Copyright (c) 2026 Huawei Technologies Co., Ltd
2+# All rights reserved.
3+# Licensed under the BSD 3-Clause 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+# https://opensource.org/licenses/BSD-3-Clause
7+# Unless required by applicable law or agreed to in writing, software
8+# distributed under the License is distributed on an "AS IS" BASIS,
9+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+# See the License for the specific language governing permissions and
11+# limitations under the License.
12+ 
13+"""
14+Add validation cases for torch._C._set_warnAlways API on Ascend NPU:
15+ 
16+PyTorch community lacks sufficient and direct API validations for this API, so this file is added.
17+This file validates torch._C._set_warnAlways (extendable).
18+ 
19+Test command:
20+ python test/npu/test_set_warnAlways.py
21+"""
22+ 
23+import torch
24+from torch.testing._internal.common_utils import TestCase, run_tests
25+ 
26+import torch_npu
27+from torch_npu.testing.common_utils import SupportedDevices
28+ 
29+assert torch_npu is not None # NPU backend registration
30+ 
31+ 
32+class TestSetWarnAlways(TestCase):
33+ 
34+ def setUp(self):
35+ torch._C._set_warnAlways(False)
36+ 
37+ def tearDown(self):
38+ torch._C._set_warnAlways(False)
39+ 
40+ def test_set_warnAlways_exists(self):
41+ self.assertTrue(hasattr(torch._C, '_set_warnAlways'))
42+ 
43+ def test_set_warnAlways_callable(self):
44+ self.assertTrue(callable(torch._C._set_warnAlways))
45+ 
46+ @SupportedDevices(['Ascend910A', 'Ascend910B', 'Ascend910_93', 'Ascend950'])
47+ def test_set_warnAlways_with_true(self):
48+ torch._C._set_warnAlways(True)
49+ self.assertTrue(True)
50+ 
51+ def test_set_warnAlways_with_false(self):
52+ torch._C._set_warnAlways(False)
53+ self.assertTrue(True)
54+ 
55+ @SupportedDevices(['Ascend910A', 'Ascend910B', 'Ascend910_93', 'Ascend950'])
56+ def test_set_warnAlways_with_npu_tensor(self):
57+ torch._C._set_warnAlways(True)
58+ x = torch.randn(3, 4).npu()
59+ y = torch.randn(3, 4).npu()
60+ z = x + y
61+ self.assertEqual(z.shape, torch.Size([3, 4]))
62+ 
63+ 
64+if __name__ == "__main__":
65+ run_tests()