Skip to content

ding.framework.middleware.functional.timer

ding.framework.middleware.functional.timer

epoch_timer(print_per=1, smooth_window=10)

Overview

Print time cost of each epoch.

Arguments: - print_per (:obj:int): Print each N epoch. - smooth_window (:obj:int): The window size to smooth the mean.

Full Source Code

../ding/framework/middleware/functional/timer.py

1import numpy as np 2from collections import deque 3from ditk import logging 4from time import time 5 6from ding.framework import task 7from typing import TYPE_CHECKING 8if TYPE_CHECKING: 9 from ding.framework.context import Context 10 11 12def epoch_timer(print_per: int = 1, smooth_window: int = 10): 13 """ 14 Overview: 15 Print time cost of each epoch. 16 Arguments: 17 - print_per (:obj:`int`): Print each N epoch. 18 - smooth_window (:obj:`int`): The window size to smooth the mean. 19 """ 20 records = deque(maxlen=print_per * smooth_window) 21 22 def _epoch_timer(ctx: "Context"): 23 start = time() 24 yield 25 time_cost = time() - start 26 records.append(time_cost) 27 if ctx.total_step % print_per == 0: 28 logging.info( 29 "[Epoch Timer][Node:{:>2}]: Cost: {:.2f}ms, Mean: {:.2f}ms".format( 30 task.router.node_id or 0, time_cost * 1000, 31 np.mean(records) * 1000 32 ) 33 ) 34 35 return _epoch_timer