You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

_time_monitor.py 1.8 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """TimeMonitor Callback class."""
  16. import time
  17. from mindspore import log as logger
  18. from ._callback import Callback
  19. class TimeMonitor(Callback):
  20. """
  21. Monitor the time in training.
  22. Args:
  23. data_size (int): Dataset size. Default: None.
  24. """
  25. def __init__(self, data_size=None):
  26. super(TimeMonitor, self).__init__()
  27. self.data_size = data_size
  28. def epoch_begin(self, run_context):
  29. self.epoch_time = time.time()
  30. def epoch_end(self, run_context):
  31. epoch_seconds = (time.time() - self.epoch_time) * 1000
  32. step_size = self.data_size
  33. cb_params = run_context.original_args()
  34. if hasattr(cb_params, "batch_num"):
  35. batch_num = cb_params.batch_num
  36. if isinstance(batch_num, int) and batch_num > 0:
  37. step_size = cb_params.batch_num
  38. if not isinstance(step_size, int) or step_size < 1:
  39. logger.error("data_size must be positive int.")
  40. return
  41. step_seconds = epoch_seconds / step_size
  42. print("epoch time: {:5.3f} ms, per step time: {:5.3f} ms".format(epoch_seconds, step_seconds), flush=True)