Skip to content

Commit 55bd4c0

Browse files
committed
TVM debugresult dump to Chrome Tracing
1 parent 4ac64fc commit 55bd4c0

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

python/tvm/contrib/debugger/debug_result.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
"""Graph debug results dumping class."""
2-
import os
2+
import collections
33
import json
4+
import os
5+
import numpy as np
46
import tvm
57

68
GRAPH_DUMP_FILE_NAME = '_tvmdbg_graph_dump.json'
9+
CHROME_TRACE_FILE_NAME = "_tvmdbg_execution_trace.json"
710

811
class DebugResult(object):
912
"""Graph debug data module.
@@ -127,6 +130,52 @@ def dump_output_tensor(self):
127130
with open(os.path.join(self._dump_path, "output_tensors.params"), "wb") as param_f:
128131
param_f.write(save_tensors(output_tensors))
129132

133+
def dump_chrome_trace(self):
134+
"""Dump the trace to the Chrome trace.json format.
135+
"""
136+
eid = 0
137+
order = 0
138+
139+
def s_to_us(t):
140+
return t * 10 ** 6
141+
142+
TraceEvent = collections.namedtuple(
143+
'TraceEvent',
144+
['ts', 'tid', 'pid', 'name', 'ph']
145+
)
146+
147+
starting_times = np.zeros(len(self._time_list) + 1)
148+
starting_times[1:] = np.cumsum([times[0] for times in self._time_list])
149+
150+
def node_to_events(node, times, starting_time):
151+
return [
152+
TraceEvent(
153+
ts=s_to_us(starting_time),
154+
tid=1,
155+
pid=1,
156+
ph='B',
157+
name=node['name'],
158+
),
159+
TraceEvent(
160+
# Use start + duration instead of end to ensure precise timings.
161+
ts=s_to_us(times[0] + starting_time),
162+
tid=1,
163+
pid=1,
164+
ph='E',
165+
name=node['name'],
166+
),
167+
]
168+
events = [e
169+
for (node, times, starting_time) in zip(self._nodes_list, self._time_list, starting_times)
170+
for e in node_to_events(node, times, starting_time)]
171+
result = dict(
172+
displayTimeUnit='ns',
173+
traceEvents=[e._asdict() for e in events]
174+
)
175+
176+
with open(os.path.join(self._dump_path, CHROME_TRACE_FILE_NAME), "w") as trace_f:
177+
json.dump(result, trace_f)
178+
130179
def dump_graph_json(self, graph):
131180
"""Dump json formatted graph.
132181

python/tvm/contrib/debugger/debug_runtime.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,9 @@ def run(self, **input_dict):
220220
self._run_debug()
221221
# Step 2. Dump the output tensors to the dump folder
222222
self.debug_datum.dump_output_tensor()
223-
# Step 3. Display the collected information
223+
# Step 3. Dump the Chrome trace to the dump folder
224+
self.debug_datum.dump_chrome_trace()
225+
# Step 4. Display the collected information
224226
self.debug_datum.display_debug_result()
225227

226228
def run_individual(self, number, repeat=1, min_repeat_ms=0):

tests/python/unittest/test_runtime_graph_debug.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from tvm.contrib import util
77
from tvm.contrib.debugger import debug_runtime as graph_runtime
88

9+
910
def test_graph_simple():
1011
n = 4
1112
A = tvm.placeholder((n,), name='A')
@@ -57,13 +58,31 @@ def check_verify():
5758
GRAPH_DUMP_FILE_NAME = '_tvmdbg_graph_dump.json'
5859
assert(len(os.listdir(directory)) == 1)
5960

61+
assert(len(os.listdir(directory)) == 1)
62+
6063
#verify the file name is proper
6164
assert(os.path.exists(os.path.join(directory, GRAPH_DUMP_FILE_NAME)))
6265

6366
mod.run()
6467
#Verify the tensors are dumped
6568
assert(len(os.listdir(directory)) > 1)
6669

70+
print(os.listdir(directory))
71+
CHROME_TRACE_FILE_NAME = '_tvmdbg_execution_trace.json'
72+
assert(os.path.exists(os.path.join(directory, CHROME_TRACE_FILE_NAME)))
73+
74+
with open(os.path.join(directory, CHROME_TRACE_FILE_NAME)) as f:
75+
trace = json.load(f)
76+
assert trace["displayTimeUnit"] == "ns"
77+
events = trace["traceEvents"]
78+
assert len(events) == 4
79+
assert all(event["ph"] in ('B', 'E') for event in events)
80+
assert all(event["pid"] == 1 for event in events)
81+
assert all(event["tid"] == 1 for event in events)
82+
assert all(event["name"] == 'x' for event in events[:2])
83+
assert all(event["name"] == 'add' for event in events[2:])
84+
assert events[0]["ts"] == 0
85+
assert events[0]["ph"] == 'B'
6786
#verify the output is correct
6887
out = mod.get_output(0, tvm.nd.empty((n,)))
6988
np.testing.assert_equal(out.asnumpy(), a + 1)

0 commit comments

Comments
 (0)