Skip to content

Commit 49b45cc

Browse files
diego-urgellfacebook-github-bot
authored andcommitted
Add report generation methods to TimerProtocol interface
Differential Revision: D77345642
1 parent 3092d14 commit 49b45cc

1 file changed

Lines changed: 33 additions & 29 deletions

File tree

torchtnt/utils/timer.py

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ def reset(self) -> None:
112112
"""
113113
...
114114

115+
def _make_report(self) -> TimerReport:
116+
"""
117+
Creates a report of timing data.
118+
"""
119+
...
120+
115121

116122
class Timer(TimerProtocol):
117123
def __init__(
@@ -174,6 +180,31 @@ def reset(self) -> None:
174180
"""
175181
self.recorded_durations = defaultdict(list)
176182

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

178209
class BoundedTimer(Timer):
179210
"""
@@ -213,32 +244,6 @@ def _apply_bounds(self, action_name: str) -> None:
213244
)
214245

215246

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

254259
sep: str = os.linesep
255260
output_string = f"Timer Report{sep}"
@@ -258,7 +263,7 @@ def get_timer_summary(timer: TimerProtocol) -> str:
258263
if not report.timed_action_stats:
259264
return output_string
260265

261-
max_key = max(len(a.action_name) for a in report.timed_action_stats)
266+
max_key = max(len(k) for k in timer.recorded_durations.keys())
262267

263268
# pyre-fixme[53]: Captured variable `max_key` is not annotated.
264269
def log_row(action: str, mean: str, num_calls: str, total: str, per: str) -> str:
@@ -277,7 +282,6 @@ def log_row(action: str, mean: str, num_calls: str, total: str, per: str) -> str
277282
output_string_len = len(header_string.expandtabs()) - 1
278283
sep_lines = f"{sep}{'-' * output_string_len}"
279284
output_string += sep_lines + header_string + sep_lines
280-
281285
output_string += log_row(
282286
"Total", "-", f"{report.total_calls:}", f"{report.total_duration:.5}", "100 %"
283287
)

0 commit comments

Comments
 (0)