已合并
test: add test cases for torch.set_warn_always.The current test cases for torch.set_warn_always fail to cover all essential scenarios and must be completed. #43575
创建于 20 天前
test: add test cases for torch.set_warn_always.The current test cases for torch.set_warn_always fail to cover all essential scenarios and must be completed. #43575
已合并
创建于 20 天前
已删除 :test_set_warn_always_v2.7.1合入到Ascend/pytorchv2.7.1
1 个文件变更+114-0
Atest/npu/test_set_warn_always.py+114-0
@@ -0,0 +1,114 @@
1+# Copyright (c) 2026 Huawei Technologies Co., Ltd
2+# All rights reserved.
3+#
4+# Licensed under the BSD 3-Clause License (the "License");
5+# you may not use this file except in compliance with the License.
6+# You may obtain a copy of the License at
7+#
8+# https://opensource.org/licenses/BSD-3-Clause
9+#
10+# Unless required by applicable law or agreed to in writing, software
11+# distributed under the License is distributed on an "AS IS" BASIS,
12+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+# See the License for the specific language governing permissions and
14+# limitations under the License.
15+ 
16+"""
17+Add validation cases for torch.set_warn_always on NPU:
18+1. PyTorch community lacks complete and direct validation for this API.
19+2. This file validates state switching, warning behavior, and invalid inputs (extendable).
20+"""
21+ 
22+import subprocess
23+import sys
24+import textwrap
25+import warnings
26+ 
27+import numpy as np
28+import torch
29+from torch.testing._internal.common_utils import TestCase, run_tests
30+ 
31+ 
32+class TestSetWarnAlways(TestCase):
33+ 
34+ def setUp(self):
35+ super().setUp()
36+ self.original_state = torch.is_warn_always_enabled()
37+ self.addCleanup(torch.set_warn_always, self.original_state)
38+ 
39+ def test_state_switching_and_return_value(self):
40+ result = torch.set_warn_always(True)
41+ self.assertIsNone(result)
42+ self.assertTrue(torch.is_warn_always_enabled())
43+ 
44+ result = torch.set_warn_always(False)
45+ self.assertIsNone(result)
46+ self.assertFalse(torch.is_warn_always_enabled())
47+ 
48+ def test_warn_always_emits_repeated_warnings(self):
49+ array = np.arange(10)
50+ array.flags.writeable = False
51+ message = "not writable"
52+ torch.set_warn_always(True)
53+ 
54+ with warnings.catch_warnings(record=True) as records:
55+ warnings.simplefilter("always")
56+ # NumPy conversion is CPU-only and provides a stable TORCH_WARN_ONCE source.
57+ torch.from_numpy(array)
58+ torch.from_numpy(array)
59+ 
60+ matched = [record for record in records if message in str(record.message)]
61+ self.assertEqual(len(matched), 2)
62+ torch.set_warn_always(False)
63+ 
64+ def test_warn_once_behavior_when_disabled(self):
65+ code = textwrap.dedent(
66+ """
67+ import warnings
68+ 
69+ import numpy as np
70+ import torch
71+ 
72+ array = np.arange(10)
73+ array.flags.writeable = False
74+ torch.set_warn_always(False)
75+ with warnings.catch_warnings(record=True) as records:
76+ warnings.simplefilter("always")
77+ # NumPy conversion is CPU-only and provides a stable TORCH_WARN_ONCE source.
78+ torch.from_numpy(array)
79+ torch.from_numpy(array)
80+ 
81+ message = "not writable"
82+ matched = [record for record in records if message in str(record.message)]
83+ raise SystemExit(0 if len(matched) == 1 else 1)
84+ """
85+ )
86+ process = subprocess.run(
87+ [sys.executable, "-c", code],
88+ capture_output=True,
89+ check=False,
90+ text=True,
91+ )
92+ 
93+ self.assertEqual(process.returncode, 0, process.stderr)
94+ 
95+ def test_invalid_inputs_preserve_state(self):
96+ torch.set_warn_always(True)
97+ 
98+ for value in (1, 0, None, "true", object()):
99+ with self.subTest(value=value):
100+ with self.assertRaises((RuntimeError, TypeError)):
101+ torch.set_warn_always(value)
102+ self.assertTrue(torch.is_warn_always_enabled())
103+ 
104+ with self.assertRaises(TypeError):
105+ torch.set_warn_always(b=False)
106+ with self.assertRaises(TypeError):
107+ torch.set_warn_always()
108+ with self.assertRaises(TypeError):
109+ torch.set_warn_always(True, False)
110+ self.assertTrue(torch.is_warn_always_enabled())
111+ 
112+ 
113+if __name__ == "__main__":
114+ run_tests()