Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Debug Graph Runtime] Export TVM DebugResult trace to Chrome Tracing format #2922

Merged
merged 1 commit into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion python/tvm/contrib/debugger/debug_result.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
"""Graph debug results dumping class."""
import os
import collections
import json
import os
import numpy as np
import tvm

GRAPH_DUMP_FILE_NAME = '_tvmdbg_graph_dump.json'
CHROME_TRACE_FILE_NAME = "_tvmdbg_execution_trace.json"

ChromeTraceEvent = collections.namedtuple(
'ChromeTraceEvent',
['ts', 'tid', 'pid', 'name', 'ph']
)


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

def dump_chrome_trace(self):
"""Dump the trace to the Chrome trace.json format.
"""
def s_to_us(t):
return t * 10 ** 6

starting_times = np.zeros(len(self._time_list) + 1)
starting_times[1:] = np.cumsum([times[0] for times in self._time_list])

def node_to_events(node, times, starting_time):
return [
ChromeTraceEvent(
ts=s_to_us(starting_time),
tid=1,
pid=1,
ph='B',
name=node['name'],
),
ChromeTraceEvent(
# Use start + duration instead of end to ensure precise timings.
ts=s_to_us(times[0] + starting_time),
tid=1,
pid=1,
ph='E',
name=node['name'],
),
]
events = [
e for (node, times, starting_time) in zip(
self._nodes_list, self._time_list, starting_times)
for e in node_to_events(node, times, starting_time)]
result = dict(
displayTimeUnit='ns',
traceEvents=[e._asdict() for e in events]
)

with open(os.path.join(self._dump_path, CHROME_TRACE_FILE_NAME), "w") as trace_f:
json.dump(result, trace_f)

def dump_graph_json(self, graph):
"""Dump json formatted graph.

Expand Down
4 changes: 3 additions & 1 deletion python/tvm/contrib/debugger/debug_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ def run(self, **input_dict):
self._run_debug()
# Step 2. Dump the output tensors to the dump folder
self.debug_datum.dump_output_tensor()
# Step 3. Display the collected information
# Step 3. Dump the Chrome trace to the dump folder
self.debug_datum.dump_chrome_trace()
# Step 4. Display the collected information
self.debug_datum.display_debug_result()

def run_individual(self, number, repeat=1, min_repeat_ms=0):
Expand Down
16 changes: 16 additions & 0 deletions tests/python/unittest/test_runtime_graph_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ def check_verify():
#Verify the tensors are dumped
assert(len(os.listdir(directory)) > 1)

CHROME_TRACE_FILE_NAME = '_tvmdbg_execution_trace.json'
assert(os.path.exists(os.path.join(directory, CHROME_TRACE_FILE_NAME)))

with open(os.path.join(directory, CHROME_TRACE_FILE_NAME)) as f:
trace = json.load(f)
assert trace["displayTimeUnit"] == "ns"
events = trace["traceEvents"]
assert len(events) == 4
assert all(event["ph"] in ('B', 'E') for event in events)
assert all(event["pid"] == 1 for event in events)
assert all(event["tid"] == 1 for event in events)
assert all(event["name"] == 'x' for event in events[:2])
assert all(event["name"] == 'add' for event in events[2:])
assert events[0]["ts"] == 0
assert events[0]["ph"] == 'B'

#verify the output is correct
out = mod.get_output(0, tvm.nd.empty((n,)))
np.testing.assert_equal(out.asnumpy(), a + 1)
Expand Down