Skip to content

Commit 62388fd

Browse files
diego-urgellfacebook-github-bot
authored andcommitted
Add report generation methods to TimerProtocol interface (#1012)
Summary: Pull Request resolved: #1012 Reviewed By: JKSenthil Differential Revision: D77345642 fbshipit-source-id: f1f9c24c6876c61154d2fd8794a7f8a793fc14d3
1 parent e63ec2b commit 62388fd

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

torchtnt/utils/timer.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ def reset(self) -> None:
114114
"""
115115
...
116116

117+
def _make_report(self) -> TimerReport:
118+
"""
119+
Creates a report of timing data.
120+
"""
121+
...
122+
117123

118124
class Timer(TimerProtocol):
119125
def __init__(
@@ -176,6 +182,31 @@ def reset(self) -> None:
176182
"""
177183
self.recorded_durations = defaultdict(list)
178184

185+
def _make_report(self: TimerProtocol) -> TimerReport:
186+
total_time = 0.0
187+
for _, durations in self.recorded_durations.items():
188+
array_value = np.array(durations)
189+
array_sum = np.sum(array_value)
190+
total_time += array_sum
191+
192+
action_stats = [
193+
TimedActionStats(
194+
action_name=a,
195+
mean_duration=np.mean(d),
196+
num_calls=len(d),
197+
total_duration=np.sum(d),
198+
percentage_of_total_time=100.0 * np.sum(d) / total_time,
199+
)
200+
for a, d in self.recorded_durations.items()
201+
]
202+
action_stats.sort(reverse=True)
203+
total_calls = sum(x.num_calls for x in action_stats)
204+
return TimerReport(
205+
timed_action_stats=action_stats,
206+
total_calls=total_calls,
207+
total_duration=total_time,
208+
)
209+
179210

180211
class BoundedTimer(Timer):
181212
"""
@@ -215,32 +246,6 @@ def _apply_bounds(self, action_name: str) -> None:
215246
)
216247

217248

218-
def _make_report(self: TimerProtocol) -> TimerReport:
219-
total_time = 0.0
220-
for _, durations in self.recorded_durations.items():
221-
array_value = np.array(durations)
222-
array_sum = np.sum(array_value)
223-
total_time += array_sum
224-
225-
action_stats = [
226-
TimedActionStats(
227-
action_name=a,
228-
mean_duration=np.mean(d),
229-
num_calls=len(d),
230-
total_duration=np.sum(d),
231-
percentage_of_total_time=100.0 * np.sum(d) / total_time,
232-
)
233-
for a, d in self.recorded_durations.items()
234-
]
235-
action_stats.sort(reverse=True)
236-
total_calls = sum(x.num_calls for x in action_stats)
237-
return TimerReport(
238-
timed_action_stats=action_stats,
239-
total_calls=total_calls,
240-
total_duration=total_time,
241-
)
242-
243-
244249
def get_timer_summary(timer: TimerProtocol) -> str:
245250
"""Given a timer, generate a summary of all the recorded actions.
246251
@@ -251,7 +256,7 @@ def get_timer_summary(timer: TimerProtocol) -> str:
251256
ValueError
252257
If the input Timer has no recorded actions
253258
"""
254-
report: TimerReport = _make_report(timer)
259+
report: TimerReport = timer._make_report()
255260

256261
sep: str = os.linesep
257262
output_string = f"Timer Report{sep}"
@@ -279,7 +284,6 @@ def log_row(action: str, mean: str, num_calls: str, total: str, per: str) -> str
279284
output_string_len = len(header_string.expandtabs()) - 1
280285
sep_lines = f"{sep}{'-' * output_string_len}"
281286
output_string += sep_lines + header_string + sep_lines
282-
283287
output_string += log_row(
284288
"Total", "-", f"{report.total_calls:}", f"{report.total_duration:.5}", "100 %"
285289
)

0 commit comments

Comments
 (0)