|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | + |
| 8 | +import logging |
| 9 | +from typing import cast |
| 10 | + |
| 11 | +from torchtnt.framework.callback import Callback |
| 12 | +from torchtnt.framework.state import EntryPoint, State |
| 13 | +from torchtnt.framework.unit import AppStateMixin, TEvalUnit, TPredictUnit, TTrainUnit |
| 14 | +from torchtnt.utils.distributed import get_global_rank |
| 15 | + |
| 16 | +logger: logging.Logger = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +class ProgressReporter(Callback): |
| 20 | + """ |
| 21 | + A simple callback which logs the progress at each loop start/end, epoch start/end and step start/end. |
| 22 | + This is useful to debug certain issues, for which the root cause might be unequal progress across ranks, for instance NCCL timeouts. |
| 23 | + If used, it's recommended to pass this callback as the first item in the callbacks list. |
| 24 | + """ |
| 25 | + |
| 26 | + def on_train_start(self, state: State, unit: TTrainUnit) -> None: |
| 27 | + self._log_with_rank_and_unit(state, unit, "on_train_start") |
| 28 | + |
| 29 | + def on_train_epoch_start(self, state: State, unit: TTrainUnit) -> None: |
| 30 | + self._log_with_rank_and_unit(state, unit, "on_train_epoch_start") |
| 31 | + |
| 32 | + def on_train_step_start(self, state: State, unit: TTrainUnit) -> None: |
| 33 | + self._log_with_rank_and_unit(state, unit, "on_train_step_start") |
| 34 | + |
| 35 | + def on_train_step_end(self, state: State, unit: TTrainUnit) -> None: |
| 36 | + self._log_with_rank_and_unit(state, unit, "on_train_step_end") |
| 37 | + |
| 38 | + def on_train_epoch_end(self, state: State, unit: TTrainUnit) -> None: |
| 39 | + self._log_with_rank_and_unit(state, unit, "on_train_epoch_end") |
| 40 | + |
| 41 | + def on_train_end(self, state: State, unit: TTrainUnit) -> None: |
| 42 | + self._log_with_rank_and_unit(state, unit, "on_train_end") |
| 43 | + |
| 44 | + def on_eval_start(self, state: State, unit: TEvalUnit) -> None: |
| 45 | + self._log_with_rank_and_unit(state, unit, "on_eval_start") |
| 46 | + |
| 47 | + def on_eval_epoch_start(self, state: State, unit: TEvalUnit) -> None: |
| 48 | + self._log_with_rank_and_unit(state, unit, "on_eval_epoch_start") |
| 49 | + |
| 50 | + def on_eval_step_start(self, state: State, unit: TEvalUnit) -> None: |
| 51 | + self._log_with_rank_and_unit(state, unit, "on_eval_step_start") |
| 52 | + |
| 53 | + def on_eval_step_end(self, state: State, unit: TEvalUnit) -> None: |
| 54 | + self._log_with_rank_and_unit(state, unit, "on_eval_step_end") |
| 55 | + |
| 56 | + def on_eval_epoch_end(self, state: State, unit: TEvalUnit) -> None: |
| 57 | + self._log_with_rank_and_unit(state, unit, "on_eval_epoch_end") |
| 58 | + |
| 59 | + def on_eval_end(self, state: State, unit: TEvalUnit) -> None: |
| 60 | + self._log_with_rank_and_unit(state, unit, "on_eval_end") |
| 61 | + |
| 62 | + def on_predict_start(self, state: State, unit: TPredictUnit) -> None: |
| 63 | + self._log_with_rank_and_unit(state, unit, "on_predict_start") |
| 64 | + |
| 65 | + def on_predict_epoch_start(self, state: State, unit: TPredictUnit) -> None: |
| 66 | + self._log_with_rank_and_unit(state, unit, "on_predict_epoch_start") |
| 67 | + |
| 68 | + def on_predict_step_start(self, state: State, unit: TPredictUnit) -> None: |
| 69 | + self._log_with_rank_and_unit(state, unit, "on_predict_step_start") |
| 70 | + |
| 71 | + def on_predict_step_end(self, state: State, unit: TPredictUnit) -> None: |
| 72 | + self._log_with_rank_and_unit(state, unit, "on_predict_step_end") |
| 73 | + |
| 74 | + def on_predict_epoch_end(self, state: State, unit: TPredictUnit) -> None: |
| 75 | + self._log_with_rank_and_unit(state, unit, "on_predict_epoch_end") |
| 76 | + |
| 77 | + def on_predict_end(self, state: State, unit: TPredictUnit) -> None: |
| 78 | + self._log_with_rank_and_unit(state, unit, "on_predict_end") |
| 79 | + |
| 80 | + @classmethod |
| 81 | + def _log_with_rank_and_unit( |
| 82 | + cls, state: State, unit: AppStateMixin, hook: str |
| 83 | + ) -> None: |
| 84 | + output_str = f"Progress Reporter: rank {get_global_rank()} at {hook}." |
| 85 | + if state.entry_point == EntryPoint.TRAIN: |
| 86 | + output_str = f"{output_str} Train progress: {cast(TTrainUnit, unit).train_progress.get_progress_string()}" |
| 87 | + |
| 88 | + elif state.entry_point == EntryPoint.EVALUATE: |
| 89 | + output_str = f"{output_str} Eval progress: {cast(TEvalUnit, unit).eval_progress.get_progress_string()}" |
| 90 | + |
| 91 | + elif state.entry_point == EntryPoint.PREDICT: |
| 92 | + output_str = f"{output_str} Predict progress: {cast(TPredictUnit, unit).predict_progress.get_progress_string()}" |
| 93 | + |
| 94 | + elif state.entry_point == EntryPoint.FIT: |
| 95 | + output_str = f"{output_str} Train progress: {cast(TTrainUnit, unit).train_progress.get_progress_string()} Eval progress: {cast(TEvalUnit, unit).eval_progress.get_progress_string()}" |
| 96 | + |
| 97 | + else: |
| 98 | + raise ValueError( |
| 99 | + f"State entry point {state.entry_point} is not supported in ProgressReporter" |
| 100 | + ) |
| 101 | + |
| 102 | + logger.info(output_str) |
0 commit comments