-
Notifications
You must be signed in to change notification settings - Fork 7
/
mxProfileParser.py
129 lines (110 loc) · 4.97 KB
/
mxProfileParser.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import json
import argparse
def add_args(parser):
parser.add_argument("--file",
help="profile json file", default="profile.json")
parser.add_argument("--op", help="operator need to anlyze", default="all")
parser.add_argument("--iterations",
help="number of iteration, including warm up",
default=0, type=int)
def parse_all(events, ops, cnt, dur):
assert isinstance(events, list)
for i in range(len(events)):
if events[i]['name'] in ops:
name = events[i]['name']
if events[i]['ph'] == 'B' and \
events[i+1]['name'] == name and \
events[i+1]['ph'] == 'E':
cnt[str(name)] += 1
dur[str(name)] += events[i+1]['ts'] - events[i]['ts']
return ops, cnt, dur
def print_all(cnt, dur, iters=0):
print('Time of each OP:')
oplen = [len(v) for v in cnt.keys()]
maxname = max(oplen)
maxtotal = max([len(str(v / 1000.0)) for v in dur.values()])
maxpercall = max([len(str(dur[v] / 1000.0 / cnt[v])) for v in dur.keys()])
maxcall = max([len(str(v)) for v in cnt.values()])
sorted_dur = sorted(dur.items(), key=lambda kv: kv[1], reverse=True)
total_time = sum(dur.values())
for i in range(len(cnt)):
name = sorted_dur[i][0]
if iters != 0:
assert cnt[name] % iters == 0
str1 = ('%%-%ds' % maxname) % name
str2 = ('%%-%ds ms' % maxtotal) % (dur[name] / 1000.0)
str3 = ('%%-%ds ms/call' % maxpercall) % \
(dur[name] / 1000.0 / cnt[name])
str4 = ('%%-%ds calls' % maxcall) % cnt[name]
str5 = '%-4s calls/iter' % (cnt[name] / iters)
str6 = ('{0:.2f} %'.format(100.0 * dur[name]/total_time))
print('%s %s \t%s \t%s \t%s \t%s' % (str1, str2, str3, str4, str5, str6))
else:
str1 = ('%%-%ds' % maxname) % name
str2 = ('%%-%ds ms' % maxtotal) % (dur[name] / 1000.0)
str3 = ('%%-%ds ms/call' % maxpercall) % \
(dur[name] / 1000.0 / cnt[name])
str4 = ('%%-%ds calls' % maxcall) % cnt[name]
str5 = ('{0:.2f} %'.format(100.0 * dur[name]/total_time))
print('%s %s \t%s \t%s \t%s' % (str1, str2, str3, str4, str5))
print('\nTotal OP Time: %.8f ms' % (total_time / 1000.0))
if iters != 0:
print('Iteration Time: %.8f ms\n' %
(sum(dur.values()) / 1000.0 / iters))
def print_op(op, cnt, dur, iters=0):
print('Time of %s:' % op)
oplen = [len(v) for v in cnt.keys()]
maxname = max(oplen)
maxtotal = max([len(str(v / 1000.0)) for v in dur.values()])
maxpercall = max([len(str(dur[v] / 1000.0 / cnt[v])) for v in dur.keys()])
maxcall = max([len(str(v)) for v in cnt.values()])
total_time = sum(dur.values())
for i in range(len(cnt)):
name = list(cnt.keys())[i]
if op in name:
if iters != 0:
assert cnt[name] % iters == 0
str1 = ('%%-%ds' % maxname) % name
str2 = ('%%-%ds ms' % maxtotal) % (dur[name] / 1000.0)
str3 = ('%%-%ds ms/call' % maxpercall) % \
(dur[name] / 1000.0 / cnt[name])
str4 = ('%%-%ds calls' % maxcall) % cnt[name]
str5 = '%-4s calls/iter' % (cnt[name] / iters)
str6 = ('{0:.2f} %'.format(100.0 * dur[name]/total_time))
print('%s %s \t%s \t%s \t%s \t%s' % (str1, str2, str3, str4, str5, str6))
else:
str1 = ('%%-%ds' % maxname) % name
str2 = ('%%-%ds ms' % maxtotal) % (dur[name] / 1000.0)
str3 = ('%%-%ds ms/call' % maxpercall) % \
(dur[name] / 1000.0 / cnt[name])
str4 = ('%%-%ds calls' % maxcall) % cnt[name]
str5 = ('{0:.2f} %'.format(100.0 * dur[name]/total_time))
print('%s %s \t%s \t%s \t%s' % (str1, str2, str3, str4, str5))
def init_table(events):
ops = []
cnt = {}
dur = {}
for i in range(len(events)):
if events[i]['name'] != 'process_name' and \
events[i]['name'] not in ops and \
events[i]['cat'] == 'operator':
ops.append(str(events[i]['name']))
if events[i]['name'] not in cnt.keys():
cnt.update({events[i]['name']: 0})
if e[i]['name'] not in dur.keys():
dur.update({events[i]['name']: 0})
return ops, cnt, dur
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="mxnet profile file analysis",
formatter_class=argparse.ArgumentDefaultsHelpFormatter) # noqa
add_args(parser)
args = parser.parse_args()
f = open(args.file, 'r')
j = json.load(f)
e = j['traceEvents']
ops, cnt, dur = init_table(e)
parse_all(e, ops, cnt, dur)
if args.op == 'all':
print_all(cnt, dur, args.iterations)
else:
print_op(args.op, cnt, dur, args.iterations)