Skip to content

Commit

Permalink
Made pattern-matcher diagnostics lazily reported + added TORCH_COMPIL…
Browse files Browse the repository at this point in the history
…E_CPROFILE (#110504)

Summary:
X-link: pytorch/pytorch#110504
Approved by: https://github.com/mlazos, https://github.com/eellison
ghstack dependencies: #110501

Reviewed By: PaliC

Differential Revision: D49957337

Pulled By: Chillee

fbshipit-source-id: c5e77ab44fcdc74eaecd327d4e77c5fce9d92beb
  • Loading branch information
Chillee authored and facebook-github-bot committed Oct 6, 2023
1 parent 0f37477 commit fe46634
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions userbenchmark/dynamo/dynamobench/_dynamo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import operator
import os
import pstats
import subprocess
import sys
import textwrap
import threading
Expand All @@ -26,6 +27,7 @@
import weakref
from contextlib import contextmanager
from functools import lru_cache, wraps
from pathlib import Path
from typing import Any, Dict, Optional, Tuple, Union

try:
Expand Down Expand Up @@ -96,26 +98,43 @@ def tabulate(rows, headers):
)


CPROFILE_ENABLED = False


def cprofile_wrapper(func):
@wraps(func)
def profile_wrapper(*args, **kwargs):
global timer_counter, CPROFILE_ENABLED
CPROFILE_ENABLED = True
datafn = (
func.__name__ + f"{next(timer_counter)}.profile"
) # Name the data file sensibly
global timer_counter
profile_path = Path(func.__name__ + f"{next(timer_counter)}.profile")
prof = cProfile.Profile()
prof.enable()
retval = prof.runcall(func, *args, **kwargs)
prof.disable()
print(f"### Cprofile for {func.__name__} iter {next(timer_counter)} ###")
ps = pstats.Stats(prof)
ps.sort_stats(pstats.SortKey.TIME).print_stats(20)
ps.sort_stats(pstats.SortKey.CUMULATIVE).print_stats(20)
prof.dump_stats(datafn)
prof.dump_stats(profile_path)
svg_path = profile_path.with_suffix(".svg")
try:
gprof2dot_process = subprocess.Popen(
[
"gprof2dot",
"-f",
"pstats",
"--node-label=total-time-percentage",
"--node-label=self-time-percentage",
"--node-label=total-time",
str(profile_path),
],
stdout=subprocess.PIPE,
)
subprocess.run(
["dot", "-Tsvg", "-o", str(svg_path)], stdin=gprof2dot_process.stdout
)
print(f"Generated SVG from profile at {str(svg_path)}")
except FileNotFoundError:
print(
"Failed to generate SVG from profile -- dumping stats instead."
"Try installing gprof2dot and dot for a better visualization"
)
ps.sort_stats(pstats.SortKey.TIME).print_stats(20)
ps.sort_stats(pstats.SortKey.CUMULATIVE).print_stats(20)
return retval

return profile_wrapper
Expand Down Expand Up @@ -189,7 +208,7 @@ def print_time_report():

def dynamo_timed(original_function=None, phase_name=None):
def dynamo_timed_inner(func):
if CPROFILE_ENABLED:
if config.cprofile:
return func

@wraps(func)
Expand Down

0 comments on commit fe46634

Please sign in to comment.