Skip to content

Commit 991a4b5

Browse files
xuzhao9pytorchmergebot
authored andcommitted
[dynamo] Add --profile-details and --export-perfdoctor option (pytorch#144751)
Summary: Add `--profile-details` option to add shapes and other details to the Kineto profile. Add `--export-perfdoctor` to directly dump trace to perfdoctor for webview. Test Plan: ``` $ buck2 run mode/opt //caffe2/benchmarks/dynamo:torchbench_internal -- --only mrs_video_watch_over --performance --training --amp --export-profiler-trace --backend=inductor --profile-details --export-perfdoctor ``` https://interncache-all.fbcdn.net/manifold/perfetto-artifacts/tree/ui/index.html#!/?url=https://interncache-all.fbcdn.net/manifold/pyper_traces/tree/traces/test/inductor_mrs_video_watch_over_rank_0_20250113_173817_6535183793.json.gz Differential Revision: D68134547 Pull Request resolved: pytorch#144751 Approved by: https://github.com/drisspg
1 parent 5b37249 commit 991a4b5

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

benchmarks/dynamo/common.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ class CI(NamedTuple):
137137
except ImportError:
138138
INTERNAL_CI_SKIP_DYNAMIC_BATCH_ONLY = set()
139139

140+
try:
141+
from pytorch.benchmark.fb.run_utils import trace_handler
142+
except ImportError:
143+
trace_handler = None
144+
145+
140146
CI_SKIP_DYNAMIC_BATCH_ONLY = {
141147
"sam",
142148
# See https://github.com/mindee/doctr/blob/f2114758d529ed8d3d0030581638f0520b6b98d8/doctr/models/detection/core.py#L89
@@ -905,7 +911,7 @@ def maybe_mark_profile(*args, **kwargs):
905911

906912
times = args.iterations_per_run
907913

908-
with maybe_profile(args.export_profiler_trace) as p:
914+
with maybe_profile(args.export_profiler_trace, **args.profile_details) as p:
909915
for rep in trange(args.repeat, desc="running benchmark"):
910916
inputs = (
911917
randomize_input(copy.deepcopy(example_inputs))
@@ -1060,7 +1066,7 @@ def maybe_mark_profile(*args, **kwargs):
10601066
tolerance = args.xla_tolerance if args.trace_on_xla else 1e-4
10611067
torch._dynamo.config.repro_tolerance = tolerance
10621068

1063-
with maybe_profile(args.export_profiler_trace) as p:
1069+
with maybe_profile(args.export_profiler_trace, **args.profile_details) as p:
10641070
if args.export_aot_inductor:
10651071
frozen_model_iter_fn = export_aot_inductor(model, example_inputs)
10661072
else:
@@ -1109,9 +1115,13 @@ def maybe_mark_profile(*args, **kwargs):
11091115
name = args.profiler_trace_name + "_" + model.name
11101116
if hasattr(args, "rank"):
11111117
name += f"_rank_{args.rank}"
1112-
name += ".json"
1113-
name = os.path.join(torch._dynamo.config.base_dir, name)
1114-
p.export_chrome_trace(name)
1118+
if args.export_perfdoctor and trace_handler:
1119+
trace_handler(name, p)
1120+
else:
1121+
name += ".json"
1122+
name = os.path.join(torch._dynamo.config.base_dir, name)
1123+
p.export_chrome_trace(name)
1124+
11151125
median = np.median(timings, axis=0)
11161126
speedup = median[0] / median[1]
11171127
if args.dump_raw_metrics:
@@ -3917,6 +3927,14 @@ def get_example_inputs(self):
39173927
"--profiler_trace_name",
39183928
help="Overwrites exported trace name",
39193929
)
3930+
parser.add_argument(
3931+
"--profile-details", action="store_true", help="More detailed profiler trace."
3932+
)
3933+
parser.add_argument(
3934+
"--export-perfdoctor",
3935+
action="store_true",
3936+
help="Export Chrome trace to perf doctor. (internal only)",
3937+
)
39203938
parser.add_argument(
39213939
"--diff-branch",
39223940
default=diff_branch_default,
@@ -4753,7 +4771,16 @@ def model_iter_fn_and_mark_step(*args, **kwargs):
47534771
write_outputs(output_filename, [], [args.only, batch_size])
47544772
return
47554773

4774+
args.profile_details = {}
47564775
if args.export_profiler_trace:
4776+
if args.profile_details:
4777+
args.profile_details = {
4778+
"record_shapes": True,
4779+
"profile_memory": True,
4780+
"with_stack": True,
4781+
"with_modules": True,
4782+
}
4783+
47574784
if args.profiler_trace_name is None:
47584785
if args.backend:
47594786
args.profiler_trace_name = args.backend

0 commit comments

Comments
 (0)