|
| 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 unittest |
| 11 | +from unittest import mock |
| 12 | +from unittest.mock import MagicMock |
| 13 | + |
| 14 | +from torchtnt.framework.callbacks.slow_rank_detector import ( |
| 15 | + _get_min_max_indices, |
| 16 | + SlowRankDetector, |
| 17 | +) |
| 18 | + |
| 19 | +from torchtnt.framework.state import State |
| 20 | +from torchtnt.framework.unit import TrainUnit |
| 21 | +from torchtnt.utils.distributed import get_global_rank, spawn_multi_process |
| 22 | +from torchtnt.utils.loggers.logger import MetricLogger |
| 23 | +from torchtnt.utils.progress import Progress |
| 24 | +from torchtnt.utils.test_utils import skip_if_not_distributed, skip_if_not_gpu |
| 25 | + |
| 26 | + |
| 27 | +class SlowRankDetectorTest(unittest.TestCase): |
| 28 | + |
| 29 | + @skip_if_not_distributed |
| 30 | + @skip_if_not_gpu |
| 31 | + def test_sync_times(self) -> None: |
| 32 | + spawn_multi_process(2, "nccl", self._test_sync_times) |
| 33 | + |
| 34 | + @staticmethod |
| 35 | + def _test_sync_times() -> None: |
| 36 | + tc = unittest.TestCase() |
| 37 | + rank = get_global_rank() |
| 38 | + logger = MagicMock(spec=MetricLogger) |
| 39 | + |
| 40 | + with mock.patch("time.perf_counter", return_value=rank + 1), tc.assertLogs( |
| 41 | + level="INFO" |
| 42 | + ) as log: |
| 43 | + slow_rank_detector = SlowRankDetector(logger=logger) |
| 44 | + slow_rank_detector._sync_times(1, 1) |
| 45 | + tc.assertEqual( |
| 46 | + log.output, |
| 47 | + [ |
| 48 | + "INFO:torchtnt.framework.callbacks.slow_rank_detector:Time difference between fastest rank (0: 1.0 sec) and slowest rank (1: 2.0 sec) is 1.0 seconds after 1 epochs and 1 steps." |
| 49 | + ], |
| 50 | + ) |
| 51 | + if rank == 0: |
| 52 | + logger.log.assert_called_once_with( |
| 53 | + "Difference between fastest/slowest rank (seconds)", 1.0, 1 |
| 54 | + ) |
| 55 | + else: |
| 56 | + logger.log.assert_not_called() |
| 57 | + |
| 58 | + def test_get_min_max_indices(self) -> None: |
| 59 | + min_index, max_index = _get_min_max_indices([5.0, 2.0, 3.5]) |
| 60 | + self.assertEqual(min_index, 1) |
| 61 | + self.assertEqual(max_index, 0) |
| 62 | + |
| 63 | + min_index, max_index = _get_min_max_indices([1.0]) |
| 64 | + self.assertEqual(min_index, 0) |
| 65 | + self.assertEqual(max_index, 0) |
| 66 | + |
| 67 | + min_index, max_index = _get_min_max_indices([2.0, 3.0, 2.0]) |
| 68 | + self.assertEqual(min_index, 0) |
| 69 | + self.assertEqual(max_index, 1) |
| 70 | + |
| 71 | + def test_invalid_initialization_params(self) -> None: |
| 72 | + with self.assertRaisesRegex( |
| 73 | + ValueError, |
| 74 | + "At least one of check_every_n_steps or check_every_n_epochs must be specified.", |
| 75 | + ): |
| 76 | + SlowRankDetector(check_every_n_steps=None, check_every_n_epochs=None) |
| 77 | + |
| 78 | + with self.assertRaisesRegex( |
| 79 | + ValueError, |
| 80 | + "check_every_n_steps must be a positive integer. Value passed is 0", |
| 81 | + ): |
| 82 | + SlowRankDetector(check_every_n_steps=0) |
| 83 | + |
| 84 | + with self.assertRaisesRegex( |
| 85 | + ValueError, |
| 86 | + "check_every_n_epochs must be a positive integer. Value passed is 0", |
| 87 | + ): |
| 88 | + SlowRankDetector(check_every_n_epochs=0) |
| 89 | + |
| 90 | + def test_sync_times_frequency(self) -> None: |
| 91 | + slow_rank_detector = SlowRankDetector( |
| 92 | + check_every_n_steps=2, check_every_n_epochs=2 |
| 93 | + ) |
| 94 | + unit = MagicMock(spec=TrainUnit) |
| 95 | + unit.train_progress = Progress(num_epochs_completed=1, num_steps_completed=1) |
| 96 | + state = MagicMock(spec=State) |
| 97 | + with mock.patch.object(slow_rank_detector, "_sync_times") as sync_times_mock: |
| 98 | + # first step shouldn't trigger time sync |
| 99 | + slow_rank_detector.on_train_step_end(state, unit) |
| 100 | + sync_times_mock.assert_not_called() |
| 101 | + |
| 102 | + # second step should trigger time sync |
| 103 | + unit.train_progress.increment_step() |
| 104 | + slow_rank_detector.on_train_step_end(state, unit) |
| 105 | + sync_times_mock.assert_called_once() |
| 106 | + |
| 107 | + # third step shouldn't trigger time sync |
| 108 | + unit.train_progress.increment_step() |
| 109 | + sync_times_mock.reset_mock() |
| 110 | + slow_rank_detector.on_train_step_end(state, unit) |
| 111 | + sync_times_mock.assert_not_called() |
| 112 | + |
| 113 | + # first epoch shouldn't trigger time sync |
| 114 | + slow_rank_detector.on_train_epoch_end(state, unit) |
| 115 | + sync_times_mock.assert_not_called() |
| 116 | + |
| 117 | + # second epoch should trigger time sync |
| 118 | + unit.train_progress.increment_epoch() |
| 119 | + slow_rank_detector.on_train_epoch_end(state, unit) |
| 120 | + sync_times_mock.assert_called_once() |
| 121 | + |
| 122 | + # third epoch shouldn't trigger time sync |
| 123 | + unit.train_progress.increment_epoch() |
| 124 | + sync_times_mock.reset_mock() |
| 125 | + slow_rank_detector.on_train_epoch_end(state, unit) |
| 126 | + sync_times_mock.assert_not_called() |
0 commit comments