Safemotion Lib
Loading...
Searching...
No Matches
Public Member Functions | Protected Attributes | List of all members
fastreid.engine.hooks.IterationTimer Class Reference
Inheritance diagram for fastreid.engine.hooks.IterationTimer:
fastreid.engine.train_loop.HookBase

Public Member Functions

 __init__ (self, warmup_iter=3)
 
 before_train (self)
 
 after_train (self)
 
 before_step (self)
 
 after_step (self)
 

Protected Attributes

 _warmup_iter
 
 _step_timer
 
 _start_time
 
 _total_timer
 

Detailed Description

Track the time spent for each iteration (each run_step call in the trainer).
Print a summary in the end of training.
This hook uses the time between the call to its :meth:`before_step`
and :meth:`after_step` methods.
Under the convention that :meth:`before_step` of all hooks should only
take negligible amount of time, the :class:`IterationTimer` hook should be
placed at the beginning of the list of hooks to obtain accurate timing.

Definition at line 78 of file hooks.py.

Constructor & Destructor Documentation

◆ __init__()

fastreid.engine.hooks.IterationTimer.__init__ ( self,
warmup_iter = 3 )
Args:
    warmup_iter (int): the number of iterations at the beginning to exclude
        from timing.

Definition at line 89 of file hooks.py.

89 def __init__(self, warmup_iter=3):
90 """
91 Args:
92 warmup_iter (int): the number of iterations at the beginning to exclude
93 from timing.
94 """
95 self._warmup_iter = warmup_iter
96 self._step_timer = Timer()
97

Member Function Documentation

◆ after_step()

fastreid.engine.hooks.IterationTimer.after_step ( self)
Called after each iteration.

Reimplemented from fastreid.engine.train_loop.HookBase.

Definition at line 133 of file hooks.py.

133 def after_step(self):
134 # +1 because we're in after_step
135 iter_done = self.trainer.iter - self.trainer.start_iter + 1
136 if iter_done >= self._warmup_iter:
137 sec = self._step_timer.seconds()
138 self.trainer.storage.put_scalars(time=sec)
139 else:
140 self._start_time = time.perf_counter()
141 self._total_timer.reset()
142
143 self._total_timer.pause()
144
145

◆ after_train()

fastreid.engine.hooks.IterationTimer.after_train ( self)
Called after the last iteration.

Reimplemented from fastreid.engine.train_loop.HookBase.

Definition at line 103 of file hooks.py.

103 def after_train(self):
104 logger = logging.getLogger(__name__)
105 total_time = time.perf_counter() - self._start_time
106 total_time_minus_hooks = self._total_timer.seconds()
107 hook_time = total_time - total_time_minus_hooks
108
109 num_iter = self.trainer.iter + 1 - self.trainer.start_iter - self._warmup_iter
110
111 if num_iter > 0 and total_time_minus_hooks > 0:
112 # Speed is meaningful only after warmup
113 # NOTE this format is parsed by grep in some scripts
114 logger.info(
115 "Overall training speed: {} iterations in {} ({:.4f} s / it)".format(
116 num_iter,
117 str(datetime.timedelta(seconds=int(total_time_minus_hooks))),
118 total_time_minus_hooks / num_iter,
119 )
120 )
121
122 logger.info(
123 "Total training time: {} ({} on hooks)".format(
124 str(datetime.timedelta(seconds=int(total_time))),
125 str(datetime.timedelta(seconds=int(hook_time))),
126 )
127 )
128

◆ before_step()

fastreid.engine.hooks.IterationTimer.before_step ( self)
Called before each iteration.

Reimplemented from fastreid.engine.train_loop.HookBase.

Definition at line 129 of file hooks.py.

129 def before_step(self):
130 self._step_timer.reset()
131 self._total_timer.resume()
132

◆ before_train()

fastreid.engine.hooks.IterationTimer.before_train ( self)
Called before the first iteration.

Reimplemented from fastreid.engine.train_loop.HookBase.

Definition at line 98 of file hooks.py.

98 def before_train(self):
99 self._start_time = time.perf_counter()
100 self._total_timer = Timer()
101 self._total_timer.pause()
102

Member Data Documentation

◆ _start_time

fastreid.engine.hooks.IterationTimer._start_time
protected

Definition at line 99 of file hooks.py.

◆ _step_timer

fastreid.engine.hooks.IterationTimer._step_timer
protected

Definition at line 96 of file hooks.py.

◆ _total_timer

fastreid.engine.hooks.IterationTimer._total_timer
protected

Definition at line 100 of file hooks.py.

◆ _warmup_iter

fastreid.engine.hooks.IterationTimer._warmup_iter
protected

Definition at line 95 of file hooks.py.


The documentation for this class was generated from the following file: