Source code for mindspore.train.callback._time_monitor

# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""TimeMonitor Callback class."""

import time

from ._callback import Callback


[docs]class TimeMonitor(Callback): """ Monitor the time in training. Args: data_size (int): How many steps are the intervals between print information each time. if the program get `batch_num` during training, `data_size` will be set to `batch_num`, otherwise `data_size` will be used. Default: None. Raises: ValueError: If data_size is not positive int. """ def __init__(self, data_size=None): super(TimeMonitor, self).__init__() self.data_size = data_size self.epoch_time = time.time()
[docs] def epoch_begin(self, run_context): """ Record time at the beginning of epoch. Args: run_context (RunContext): Context of the process running. """ self.epoch_time = time.time()
[docs] def epoch_end(self, run_context): """ Print process cost time at the end of epoch. Args: run_context (RunContext): Context of the process running. """ epoch_seconds = (time.time() - self.epoch_time) * 1000 step_size = self.data_size cb_params = run_context.original_args() if hasattr(cb_params, "batch_num"): batch_num = cb_params.batch_num if isinstance(batch_num, int) and batch_num > 0: step_size = cb_params.batch_num if not isinstance(step_size, int) or step_size < 1: raise ValueError("For 'TimeMonitor', the argument 'data_size' must be positive integer, " "but got {}.".format(step_size)) step_seconds = epoch_seconds / step_size print("epoch time: {:5.3f} ms, per step time: {:5.3f} ms".format(epoch_seconds, step_seconds), flush=True)