MCPcopy Create free account
hub / github.com/zai-org/CodeGeeX / _Timer

Class _Timer

codegeex/megatron/global_vars.py:192–234  ·  view source on GitHub ↗

Timer.

Source from the content-addressed store, hash-verified

190
191
192class _Timer:
193 """Timer."""
194
195 def __init__(self, name):
196 self.name_ = name
197 self.elapsed_ = 0.0
198 self.started_ = False
199 self.start_time = time.time()
200
201 def start(self):
202 """Start the timer."""
203 assert not self.started_, "timer has already been started"
204 torch.cuda.synchronize()
205 self.start_time = time.time()
206 self.started_ = True
207
208 def stop(self):
209 """Stop the timer."""
210 assert self.started_, "timer is not started"
211 torch.cuda.synchronize()
212 self.elapsed_ += time.time() - self.start_time
213 self.started_ = False
214
215 def reset(self):
216 """Reset timer."""
217 self.elapsed_ = 0.0
218 self.started_ = False
219
220 def elapsed(self, reset=True):
221 """Calculate the elapsed time."""
222 started_ = self.started_
223 # If the timing in progress, end it first.
224 if self.started_:
225 self.stop()
226 # Get the elapsed time.
227 elapsed_ = self.elapsed_
228 # Reset the elapsed time
229 if reset:
230 self.reset()
231 # If timing was in progress, set it back.
232 if started_:
233 self.start()
234 return elapsed_
235
236
237class Timers:

Callers 1

__call__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected