已合并
test(logging): add testcase for torch._logging.set_logs #42018
木路折创建于 7月18日
test(logging): add testcase for torch._logging.set_logs #42018
已合并
共 1 个文件变更+108-0
| @@ -0,0 +1,108 @@ | |||
| 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 | +# Owner(s): ["module: library"] | ||
| 17 | + | ||
| 18 | +""" | ||
| 19 | +Add validation cases for torch._logging.set_logs API: | ||
| 20 | +1. PyTorch community lacks sufficient and direct API validations for | ||
| 21 | + torch._logging.set_logs, so this file is added. | ||
| 22 | +2. This file validates the log level configuration, artifact enabling, | ||
| 23 | + modules parameter handling, invalid input validation, and environment | ||
| 24 | + variable precedence for torch._logging.set_logs (extendable). | ||
| 25 | +""" | ||
| 26 | + | ||
| 27 | +import logging | ||
| 28 | +import os | ||
| 29 | +import unittest.mock | ||
| 30 | + | ||
| 31 | +import torch | ||
| 32 | +import torch._logging._internal | ||
| 33 | +from torch.testing._internal.common_utils import run_tests, TestCase | ||
| 34 | +from torch.testing._internal.logging_utils import preserve_log_state | ||
| 35 | + | ||
| 36 | + | ||
| 37 | +class TestLibraryLogging(TestCase): | ||
| 38 | + """Test torch._logging.set_logs.""" | ||
| 39 | + | ||
| 40 | + def test_set_logs_default_clears_state(self): | ||
| 41 | + """Calling set_logs() with no arguments resets the logging state.""" | ||
| 42 | + with preserve_log_state(): | ||
| 43 | + torch._logging.set_logs(dynamo=logging.DEBUG, graph_code=True) | ||
| 44 | + self.assertTrue( | ||
| 45 | + torch._logging._internal.log_state.is_artifact_enabled("graph_code") | ||
| 46 | + ) | ||
| 47 | + torch._logging.set_logs() | ||
| 48 | + self.assertFalse( | ||
| 49 | + torch._logging._internal.log_state.is_artifact_enabled("graph_code") | ||
| 50 | + ) | ||
| 51 | + self.assertEqual( | ||
| 52 | + list(torch._logging._internal.log_state.get_log_level_pairs()), [] | ||
| 53 | + ) | ||
| 54 | + | ||
| 55 | + def test_set_logs_enable_component(self): | ||
| 56 | + """set_logs can set the log level for registered components.""" | ||
| 57 | + with preserve_log_state(): | ||
| 58 | + torch._logging.set_logs(dynamo=logging.DEBUG) | ||
| 59 | + pairs = dict(torch._logging._internal.log_state.get_log_level_pairs()) | ||
| 60 | + self.assertIn("torch._dynamo", pairs) | ||
| 61 | + self.assertEqual(pairs["torch._dynamo"], logging.DEBUG) | ||
| 62 | + self.assertEqual(logging.getLogger("torch._dynamo").level, logging.DEBUG) | ||
| 63 | + | ||
| 64 | + def test_set_logs_enable_artifact(self): | ||
| 65 | + """set_logs can enable registered artifacts.""" | ||
| 66 | + with preserve_log_state(): | ||
| 67 | + torch._logging.set_logs(graph_code=True) | ||
| 68 | + self.assertTrue( | ||
| 69 | + torch._logging._internal.log_state.is_artifact_enabled("graph_code") | ||
| 70 | + ) | ||
| 71 | + | ||
| 72 | + def test_set_logs_modules(self): | ||
| 73 | + """set_logs supports registered aliases through the modules argument.""" | ||
| 74 | + with preserve_log_state(): | ||
| 75 | + torch._logging.set_logs(modules={"dynamo": logging.INFO}) | ||
| 76 | + pairs = dict(torch._logging._internal.log_state.get_log_level_pairs()) | ||
| 77 | + self.assertIn("torch._dynamo", pairs) | ||
| 78 | + self.assertEqual(pairs["torch._dynamo"], logging.INFO) | ||
| 79 | + | ||
| 80 | + def test_set_logs_invalid_artifact_value(self): | ||
| 81 | + """Passing a non-bool value for an artifact raises ValueError.""" | ||
| 82 | + with preserve_log_state(): | ||
| 83 | + with self.assertRaises(ValueError): | ||
| 84 | + torch._logging.set_logs(graph_code=5) | ||
| 85 | + | ||
| 86 | + def test_set_logs_invalid_log_level(self): | ||
| 87 | + """Passing an unrecognized log level raises ValueError.""" | ||
| 88 | + with preserve_log_state(): | ||
| 89 | + with self.assertRaises(ValueError): | ||
| 90 | + torch._logging.set_logs(dynamo=999) | ||
| 91 | + | ||
| 92 | + def test_set_logs_invalid_module_name(self): | ||
| 93 | + """Passing an unrecognized module name via modules raises ValueError.""" | ||
| 94 | + with preserve_log_state(): | ||
| 95 | + with self.assertRaises(ValueError): | ||
| 96 | + torch._logging.set_logs(modules={"not_a_real_thing": logging.INFO}) | ||
| 97 | + | ||
| 98 | + def test_set_logs_env_var_precedence(self): | ||
| 99 | + """When TORCH_LOGS is set, set_logs does nothing.""" | ||
| 100 | + with unittest.mock.patch.dict(os.environ, {"TORCH_LOGS": "dynamo"}): | ||
| 101 | + with preserve_log_state(): | ||
| 102 | + torch._logging.set_logs(dynamo=logging.DEBUG) | ||
| 103 | + pairs = dict(torch._logging._internal.log_state.get_log_level_pairs()) | ||
| 104 | + self.assertNotIn("torch._dynamo", pairs) | ||
| 105 | + | ||
| 106 | + | ||
| 107 | +if __name__ == "__main__": | ||
| 108 | + run_tests() | ||