MCPcopy Create free account
hub / github.com/hpcaitech/ColossalAI / Timer

Class Timer

colossalai/utils/timer.py:9–88  ·  view source on GitHub ↗

A timer object which helps to log the execution times, and provides different tools to assess the times.

Source from the content-addressed store, hash-verified

7
8
9class Timer:
10 """A timer object which helps to log the execution times, and provides different tools to assess the times."""
11
12 def __init__(self):
13 self._started = False
14 self._start_time = time.time()
15 self._elapsed = 0
16 self._history = []
17
18 @property
19 def has_history(self):
20 return len(self._history) != 0
21
22 @property
23 def current_time(self) -> float:
24 get_accelerator().synchronize()
25 return time.time()
26
27 def start(self):
28 """Firstly synchronize cuda, reset the clock and then start the timer."""
29 self._elapsed = 0
30 get_accelerator().synchronize()
31 self._start_time = time.time()
32 self._started = True
33
34 def lap(self):
35 """lap time and return elapsed time"""
36 return self.current_time - self._start_time
37
38 def stop(self, keep_in_history: bool = False):
39 """Stop the timer and record the start-stop time interval.
40
41 Args:
42 keep_in_history (bool, optional): Whether does it record into history
43 each start-stop interval, defaults to False.
44 Returns:
45 int: Start-stop interval.
46 """
47 get_accelerator().synchronize()
48 end_time = time.time()
49 elapsed = end_time - self._start_time
50 if keep_in_history:
51 self._history.append(elapsed)
52 self._elapsed = elapsed
53 self._started = False
54 return elapsed
55
56 def get_history_mean(self):
57 """Mean of all history start-stop time intervals.
58
59 Returns:
60 int: Mean of time intervals
61 """
62 return sum(self._history) / len(self._history)
63
64 def get_history_sum(self):
65 """Add up all the start-stop time intervals.
66

Callers 1

startMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…