forked from pytorch/benchmark
-
Notifications
You must be signed in to change notification settings - Fork 1
/
collect_graph_ir.py
executable file
·76 lines (64 loc) · 2.73 KB
/
collect_graph_ir.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
import argparse
import gc
import logging
import os
import re
import warnings
from torchbenchmark import list_models
import torch
NO_JIT = {"demucs", "dlrm", "maml", "yolov3", "moco", "pytorch_CycleGAN_and_pix2pix", "tacotron2"}
NO_GET_MODULE = {"Background_Matting"}
def get_dump_filename(name, device, args):
if args.no_profiling:
return f"{name}.{device}.last_executed_graph.noprofile.log"
if args.inlined_graph:
return f"{name}.{device}.inlined_graph.log"
return f"{name}.{device}.last_executed_graph.log"
def iter_models(args):
device = "cpu"
for benchmark_cls in list_models():
bench_name = benchmark_cls.name
if args.benchmark and args.benchmark != bench_name:
continue
if bench_name in NO_GET_MODULE:
print(f"{bench_name} has no get_module, skipped")
continue
if bench_name in NO_JIT:
print(f"{bench_name} has no scripted module, skipped")
continue
try:
# disable profiling mode so that the collected graph does not contain
# profiling node
if args.no_profiling:
torch._C._jit_set_profiling_mode(False)
benchmark = benchmark_cls(device=device, jit=True)
model, example_inputs = benchmark.get_module()
# extract ScriptedModule object for BERT model
if bench_name == "BERT_pytorch":
model = model.bert
fname = get_dump_filename(bench_name, device, args)
print(f"Dump Graph IR for {bench_name} to {fname}")
# default mode need to warm up ProfileExecutor
if not (args.no_profiling or args.inlined_graph):
model.graph_for(*example_inputs)
with open(fname, 'w') as dump_file:
if args.inlined_graph:
print(model.inlined_graph, file=dump_file)
else:
print(model.graph_for(*example_inputs), file=dump_file)
except NotImplementedError:
print(f"Cannot collect graph IR dump for {bench_name}")
pass
def main(args=None):
parser = argparse.ArgumentParser(description="dump last_executed graph for all benchmarks with JIT implementation")
parser.add_argument("--benchmark", "-b",
help="dump graph for <BENCHMARK>")
parser.add_argument("--no_profiling", action="store_true",
help="dump last_executed graphs w/o profiling executor")
parser.add_argument("--inlined_graph", action="store_true",
help="dump graphs dumped by module.inlined_graph")
args = parser.parse_args(args)
iter_models(args)
if __name__ == '__main__':
main()