Skip to content

ding.utils.time_helper

ding.utils.time_helper

EasyTimer

Overview

A decent timer wrapper that can be used easily.

Interfaces

__init__, __enter__, __exit__

Example

wait_timer = EasyTimer() with wait_timer: func(...) time_ = wait_timer.value # in second

__init__(cuda=True)

Overview

Init class EasyTimer

Parameters:

Name Type Description Default
- cuda (

obj:bool): Whether to build timer with cuda type

required

__enter__()

Overview

Enter timer, start timing

__exit__(*args)

Overview

Exit timer, stop timing

TimeWrapperTime

Bases: TimeWrapper

Overview

A class method that inherit from TimeWrapper class

Interfaces

start_time, end_time

start_time() classmethod

Overview

Implement and override the start_time method in TimeWrapper class

end_time() classmethod

Overview

Implement and override the end_time method in TimeWrapper class

Returns:

Type Description
  • time(:obj:float): The time between start_time and end_time

WatchDog

Bases: object

Overview

Simple watchdog timer to detect timeouts

Parameters:

Name Type Description Default
- timeout (

obj:int): Timeout value of the watchdog [seconds].

required

.. note:: If it is not reset before exceeding this value, TimeourError raised.

Interfaces

start, stop

Examples:

>>> watchdog = WatchDog(x) # x is a timeout value
>>> ...
>>> watchdog.start()
>>> ... # Some function

__init__(timeout=1)

Overview

Initialize watchdog with timeout value.

Arguments: - timeout (:obj:int): Timeout value of the watchdog [seconds].

start()

Overview

Start watchdog.

stop()

Overview

Stop watchdog with alarm(0), SIGALRM, and SIG_DFL signals.

build_time_helper(cfg=None, wrapper_type=None)

Overview

Build the timehelper

Parameters:

Name Type Description Default
- cfg (

obj:dict): The config file, which is a multilevel dict, have large domain like evaluate, common, model, train etc, and each large domain has it's smaller domain.

required
- wrapper_type (

obj:str): The type of wrapper returned, support ['time', 'cuda']

required

Returns:

Type Description
Callable[[], TimeWrapper]
  • time_wrapper (:obj:TimeWrapper): Return the corresponding timewrapper, Reference: ding.utils.timehelper.TimeWrapperTime and ding.utils.timehelper.get_cuda_time_wrapper.

Full Source Code

../ding/utils/time_helper.py

1import signal 2import time 3from typing import Any, Callable 4 5import torch 6from easydict import EasyDict 7from .time_helper_base import TimeWrapper 8from .time_helper_cuda import get_cuda_time_wrapper 9 10 11def build_time_helper(cfg: EasyDict = None, wrapper_type: str = None) -> Callable[[], 'TimeWrapper']: 12 """ 13 Overview: 14 Build the timehelper 15 16 Arguments: 17 - cfg (:obj:`dict`): 18 The config file, which is a multilevel dict, have large domain like 19 evaluate, common, model, train etc, and each large domain 20 has it's smaller domain. 21 - wrapper_type (:obj:`str`): The type of wrapper returned, support ``['time', 'cuda']`` 22 23 Returns: 24 - time_wrapper (:obj:`TimeWrapper`): 25 Return the corresponding timewrapper, Reference: ``ding.utils.timehelper.TimeWrapperTime`` 26 and ``ding.utils.timehelper.get_cuda_time_wrapper``. 27 """ 28 # Note: wrapper_type has higher priority 29 if wrapper_type is not None: 30 time_wrapper_type = wrapper_type 31 elif cfg is not None: 32 time_wrapper_type = cfg.common.time_wrapper_type 33 else: 34 raise RuntimeError('Either wrapper_type or cfg should be provided.') 35 36 if time_wrapper_type == 'time': 37 return TimeWrapperTime 38 elif time_wrapper_type == 'cuda': 39 if torch.cuda.is_available(): 40 # lazy initialize to make code runnable locally 41 return get_cuda_time_wrapper() 42 else: 43 return TimeWrapperTime 44 else: 45 raise KeyError('invalid time_wrapper_type: {}'.format(time_wrapper_type)) 46 47 48class EasyTimer: 49 """ 50 Overview: 51 A decent timer wrapper that can be used easily. 52 53 Interfaces: 54 ``__init__``, ``__enter__``, ``__exit__`` 55 56 Example: 57 >>> wait_timer = EasyTimer() 58 >>> with wait_timer: 59 >>> func(...) 60 >>> time_ = wait_timer.value # in second 61 """ 62 63 def __init__(self, cuda=True): 64 """ 65 Overview: 66 Init class EasyTimer 67 68 Arguments: 69 - cuda (:obj:`bool`): Whether to build timer with cuda type 70 """ 71 if torch.cuda.is_available() and cuda: 72 time_wrapper_type = "cuda" 73 else: 74 time_wrapper_type = "time" 75 self._timer = build_time_helper(wrapper_type=time_wrapper_type) 76 self.value = 0.0 77 78 def __enter__(self): 79 """ 80 Overview: 81 Enter timer, start timing 82 """ 83 self.value = 0.0 84 self._timer.start_time() 85 86 def __exit__(self, *args): 87 """ 88 Overview: 89 Exit timer, stop timing 90 """ 91 self.value = self._timer.end_time() 92 93 94class TimeWrapperTime(TimeWrapper): 95 """ 96 Overview: 97 A class method that inherit from ``TimeWrapper`` class 98 99 Interfaces: 100 ``start_time``, ``end_time`` 101 """ 102 103 # overwrite 104 @classmethod 105 def start_time(cls): 106 """ 107 Overview: 108 Implement and override the ``start_time`` method in ``TimeWrapper`` class 109 """ 110 cls.start = time.time() 111 112 # overwrite 113 @classmethod 114 def end_time(cls): 115 """ 116 Overview: 117 Implement and override the end_time method in ``TimeWrapper`` class 118 119 Returns: 120 - time(:obj:`float`): The time between ``start_time`` and end_time 121 """ 122 cls.end = time.time() 123 return cls.end - cls.start 124 125 126class WatchDog(object): 127 """ 128 Overview: 129 Simple watchdog timer to detect timeouts 130 131 Arguments: 132 - timeout (:obj:`int`): Timeout value of the ``watchdog [seconds]``. 133 134 .. note:: 135 If it is not reset before exceeding this value, ``TimeourError`` raised. 136 137 Interfaces: 138 ``start``, ``stop`` 139 140 Examples: 141 >>> watchdog = WatchDog(x) # x is a timeout value 142 >>> ... 143 >>> watchdog.start() 144 >>> ... # Some function 145 146 """ 147 148 def __init__(self, timeout: int = 1): 149 """ 150 Overview: 151 Initialize watchdog with ``timeout`` value. 152 Arguments: 153 - timeout (:obj:`int`): Timeout value of the ``watchdog [seconds]``. 154 """ 155 156 self._timeout = timeout + 1 157 self._failed = False 158 159 def start(self): 160 """ 161 Overview: 162 Start watchdog. 163 """ 164 signal.signal(signal.SIGALRM, self._event) 165 signal.alarm(self._timeout) 166 167 @staticmethod 168 def _event(signum: Any, frame: Any): 169 """ 170 Overview: 171 Event handler for watchdog. 172 Arguments: 173 - signum (:obj:`Any`): Signal number. 174 - frame (:obj:`Any`): Current stack frame. 175 """ 176 177 raise TimeoutError() 178 179 def stop(self): 180 """ 181 Overview: 182 Stop watchdog with ``alarm(0)``, ``SIGALRM``, and ``SIG_DFL`` signals. 183 """ 184 signal.alarm(0) 185 signal.signal(signal.SIGALRM, signal.SIG_DFL)