Skip to content

Commit becfda0

Browse files
shunting314pytorchmergebot
authored andcommitted
tiny improvement to the cprofile wrapper (#120100)
1. right now we double increment the profile counter. The PR avoid that so we don't end up with profile_0, profile_2, profile_4 ... 2. log the latency to run the passed in function with profiling on so we can easily skip those _compile call which returns quickly. Pull Request resolved: #120100 Approved by: https://github.com/eellison
1 parent 36e118b commit becfda0

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

torch/_dynamo/utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,17 @@ def cprofile_wrapper(func):
131131
@wraps(func)
132132
def profile_wrapper(*args, **kwargs):
133133
global timer_counter
134-
profile_path = Path(func.__name__ + f"{next(timer_counter)}.profile")
134+
profile_cnt = next(timer_counter)
135+
profile_path = Path(func.__name__ + f"{profile_cnt}.profile")
135136
prof = cProfile.Profile()
136137
prof.enable()
138+
start_ts = time.time()
137139
retval = prof.runcall(func, *args, **kwargs)
140+
profile_latency = time.time() - start_ts
138141
prof.disable()
139-
print(f"### Cprofile for {func.__name__} iter {next(timer_counter)} ###")
142+
print(
143+
f"### Cprofile for {func.__name__} iter {profile_cnt} took {profile_latency:.3f} seconds ###"
144+
)
140145
ps = pstats.Stats(prof)
141146
prof.dump_stats(profile_path)
142147
svg_path = profile_path.with_suffix(".svg")

0 commit comments

Comments
 (0)