Skip to content

Commit 88d9bde

Browse files
ajtullochwweic
authored andcommitted
TVM debugresult dump to Chrome Tracing (apache#2922)
1 parent 44056d7 commit 88d9bde

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

python/tvm/contrib/debugger/debug_result.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
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"
10+
11+
ChromeTraceEvent = collections.namedtuple(
12+
'ChromeTraceEvent',
13+
['ts', 'tid', 'pid', 'name', 'ph']
14+
)
15+
716

817
class DebugResult(object):
918
"""Graph debug data module.
@@ -127,6 +136,45 @@ def dump_output_tensor(self):
127136
with open(os.path.join(self._dump_path, "output_tensors.params"), "wb") as param_f:
128137
param_f.write(save_tensors(output_tensors))
129138

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

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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ def check_verify():
6464
#Verify the tensors are dumped
6565
assert(len(os.listdir(directory)) > 1)
6666

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

0 commit comments

Comments
 (0)