|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +# pyre-strict |
| 9 | + |
| 10 | +import logging |
| 11 | +import unittest |
| 12 | +from unittest import mock |
| 13 | + |
| 14 | +import torch.distributed as dist |
| 15 | + |
| 16 | +from torchrec.distributed.logging_handlers import _log_handlers |
| 17 | +from torchrec.distributed.torchrec_logger import ( |
| 18 | + _DEFAULT_DESTINATION, |
| 19 | + _get_logging_handler, |
| 20 | + _get_msg_dict, |
| 21 | + _get_or_create_logger, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +class TestTorchrecLogger(unittest.TestCase): |
| 26 | + def setUp(self) -> None: |
| 27 | + super().setUp() |
| 28 | + # Save the original _log_handlers to restore it after tests |
| 29 | + self.original_log_handlers = _log_handlers.copy() |
| 30 | + |
| 31 | + # Create a mock logging handler |
| 32 | + self.mock_handler = mock.MagicMock(spec=logging.Handler) |
| 33 | + _log_handlers[_DEFAULT_DESTINATION] = self.mock_handler |
| 34 | + |
| 35 | + # Mock print function |
| 36 | + self.print_patcher = mock.patch("builtins.print") |
| 37 | + self.mock_print = self.print_patcher.start() |
| 38 | + |
| 39 | + def tearDown(self) -> None: |
| 40 | + # Restore the original _log_handlers |
| 41 | + _log_handlers.clear() |
| 42 | + _log_handlers.update(self.original_log_handlers) |
| 43 | + |
| 44 | + # Stop print patcher |
| 45 | + self.print_patcher.stop() |
| 46 | + |
| 47 | + super().tearDown() |
| 48 | + |
| 49 | + def test_get_logging_handler(self) -> None: |
| 50 | + """Test _get_logging_handler function.""" |
| 51 | + # Test with default destination |
| 52 | + handler, name = _get_logging_handler() |
| 53 | + |
| 54 | + self.assertEqual(handler, self.mock_handler) |
| 55 | + self.assertEqual( |
| 56 | + name, f"{type(self.mock_handler).__name__}-{_DEFAULT_DESTINATION}" |
| 57 | + ) |
| 58 | + |
| 59 | + # Test with custom destination |
| 60 | + custom_dest = "custom_dest" |
| 61 | + custom_handler = mock.MagicMock(spec=logging.Handler) |
| 62 | + _log_handlers[custom_dest] = custom_handler |
| 63 | + |
| 64 | + handler, name = _get_logging_handler(custom_dest) |
| 65 | + |
| 66 | + self.assertEqual(handler, custom_handler) |
| 67 | + self.assertEqual(name, f"{type(custom_handler).__name__}-{custom_dest}") |
| 68 | + |
| 69 | + @mock.patch("logging.getLogger") |
| 70 | + def test_get_or_create_logger(self, mock_get_logger: mock.MagicMock) -> None: |
| 71 | + """Test _get_or_create_logger function.""" |
| 72 | + mock_logger = mock.MagicMock(spec=logging.Logger) |
| 73 | + mock_get_logger.return_value = mock_logger |
| 74 | + |
| 75 | + # Test with default destination |
| 76 | + _get_or_create_logger() |
| 77 | + |
| 78 | + # Verify logger was created with the correct name |
| 79 | + handler_name = f"{type(self.mock_handler).__name__}-{_DEFAULT_DESTINATION}" |
| 80 | + mock_get_logger.assert_called_once_with(f"torchrec-{handler_name}") |
| 81 | + |
| 82 | + # Verify logger was configured correctly |
| 83 | + mock_logger.setLevel.assert_called_once_with(logging.DEBUG) |
| 84 | + mock_logger.addHandler.assert_called_once_with(self.mock_handler) |
| 85 | + self.assertFalse(mock_logger.propagate) |
| 86 | + |
| 87 | + # Verify formatter was set on the handler |
| 88 | + self.mock_handler.setFormatter.assert_called_once() |
| 89 | + formatter = self.mock_handler.setFormatter.call_args[0][0] |
| 90 | + self.assertIsInstance(formatter, logging.Formatter) |
| 91 | + |
| 92 | + # Test with custom destination |
| 93 | + mock_get_logger.reset_mock() |
| 94 | + self.mock_handler.reset_mock() |
| 95 | + |
| 96 | + custom_dest = "custom_dest" |
| 97 | + custom_handler = mock.MagicMock(spec=logging.Handler) |
| 98 | + _log_handlers[custom_dest] = custom_handler |
| 99 | + |
| 100 | + _get_or_create_logger(custom_dest) |
| 101 | + |
| 102 | + # Verify logger was created with the correct name |
| 103 | + handler_name = f"{type(custom_handler).__name__}-{custom_dest}" |
| 104 | + mock_get_logger.assert_called_once_with(f"torchrec-{handler_name}") |
| 105 | + |
| 106 | + # Verify custom handler was used |
| 107 | + mock_logger.addHandler.assert_called_once_with(custom_handler) |
| 108 | + |
| 109 | + def test_get_msg_dict_without_dist(self) -> None: |
| 110 | + """Test _get_msg_dict function without dist initialized.""" |
| 111 | + # Mock dist.is_initialized to return False |
| 112 | + with mock.patch("torch.distributed.is_initialized", return_value=False): |
| 113 | + msg_dict = _get_msg_dict("test_func", kwarg1="val1") |
| 114 | + |
| 115 | + # Verify msg_dict contains only func_name |
| 116 | + self.assertEqual(len(msg_dict), 1) |
| 117 | + self.assertEqual(msg_dict["func_name"], "test_func") |
| 118 | + |
| 119 | + def test_get_msg_dict_with_dist(self) -> None: |
| 120 | + """Test _get_msg_dict function with dist initialized.""" |
| 121 | + # Mock dist functions |
| 122 | + with mock.patch.multiple( |
| 123 | + dist, |
| 124 | + is_initialized=mock.MagicMock(return_value=True), |
| 125 | + get_world_size=mock.MagicMock(return_value=4), |
| 126 | + get_rank=mock.MagicMock(return_value=2), |
| 127 | + ): |
| 128 | + # Test with group in kwargs |
| 129 | + mock_group = mock.MagicMock() |
| 130 | + msg_dict = _get_msg_dict("test_func", group=mock_group) |
| 131 | + |
| 132 | + # Verify msg_dict contains all expected keys |
| 133 | + self.assertEqual(len(msg_dict), 4) |
| 134 | + self.assertEqual(msg_dict["func_name"], "test_func") |
| 135 | + self.assertEqual(msg_dict["group"], str(mock_group)) |
| 136 | + self.assertEqual(msg_dict["world_size"], "4") |
| 137 | + self.assertEqual(msg_dict["rank"], "2") |
| 138 | + |
| 139 | + # Verify get_world_size and get_rank were called with the group |
| 140 | + dist.get_world_size.assert_called_once_with(mock_group) |
| 141 | + dist.get_rank.assert_called_once_with(mock_group) |
| 142 | + |
| 143 | + # Test with process_group in kwargs |
| 144 | + dist.get_world_size.reset_mock() |
| 145 | + dist.get_rank.reset_mock() |
| 146 | + |
| 147 | + mock_process_group = mock.MagicMock() |
| 148 | + msg_dict = _get_msg_dict("test_func", process_group=mock_process_group) |
| 149 | + |
| 150 | + # Verify msg_dict contains all expected keys |
| 151 | + self.assertEqual(msg_dict["group"], str(mock_process_group)) |
| 152 | + |
| 153 | + # Verify get_world_size and get_rank were called with the process_group |
| 154 | + dist.get_world_size.assert_called_once_with(mock_process_group) |
| 155 | + dist.get_rank.assert_called_once_with(mock_process_group) |
| 156 | + |
| 157 | + |
| 158 | +if __name__ == "__main__": |
| 159 | + unittest.main() |
0 commit comments