提交 69354dd0 authored 作者: Frederic's avatar Frederic

in pydotprint don't have dot merge Theano node with too long name.

上级 aacaa693
...@@ -479,10 +479,10 @@ def pydotprint(fct, outfile=None, ...@@ -479,10 +479,10 @@ def pydotprint(fct, outfile=None,
high_contrast=True, cond_highlight=None, colorCodes=None, high_contrast=True, cond_highlight=None, colorCodes=None,
max_label_size=50, scan_graphs=False, max_label_size=50, scan_graphs=False,
var_with_name_simple=False, var_with_name_simple=False,
print_output_file=True print_output_file=True,
assert_nb_all_strings=-1
): ):
""" """print to a file in png format the graph of op of a compile theano fct.
print to a file in png format the graph of op of a compile theano fct.
:param fct: the theano fct returned by theano.function. :param fct: the theano fct returned by theano.function.
:param outfile: the output file where to put the graph. :param outfile: the output file where to put the graph.
...@@ -509,6 +509,10 @@ def pydotprint(fct, outfile=None, ...@@ -509,6 +509,10 @@ def pydotprint(fct, outfile=None,
:param var_with_name_simple: If true and a variable have a name, :param var_with_name_simple: If true and a variable have a name,
we will print only the variable name. we will print only the variable name.
Otherwise, we concatenate the type to the var name. Otherwise, we concatenate the type to the var name.
:param assert_nb_all_strings: Used for tests. This assert the
number of uniq string node in the dot graph. This is
used in tests to verify that dot won't merge Theano
node.
In the graph, ellipses are Apply Nodes (the execution of an op) In the graph, ellipses are Apply Nodes (the execution of an op)
and boxes are variables. If variables have names they are used as and boxes are variables. If variables have names they are used as
...@@ -526,6 +530,7 @@ def pydotprint(fct, outfile=None, ...@@ -526,6 +530,7 @@ def pydotprint(fct, outfile=None,
grey boxes are variables that are not outputs and are not used grey boxes are variables that are not outputs and are not used
red ellipses are transfers from/to the gpu (ops with names GpuFromHost, red ellipses are transfers from/to the gpu (ops with names GpuFromHost,
HostFromGpu) HostFromGpu)
""" """
if colorCodes is None: if colorCodes is None:
colorCodes = default_colorCodes colorCodes = default_colorCodes
...@@ -622,6 +627,13 @@ def pydotprint(fct, outfile=None, ...@@ -622,6 +627,13 @@ def pydotprint(fct, outfile=None,
varstr = varstr + idx varstr = varstr + idx
elif len(varstr) > max_label_size: elif len(varstr) > max_label_size:
varstr = varstr[:max_label_size - 3] + '...' varstr = varstr[:max_label_size - 3] + '...'
idx = 1
while varstr in all_strings:
idx += 1
suffix = ' id=' + str(idx)
varstr = (varstr[:max_label_size - 3 - len(suffix)] +
'...' +
suffix)
var_str[var] = varstr var_str[var] = varstr
all_strings.add(varstr) all_strings.add(varstr)
...@@ -656,6 +668,13 @@ def pydotprint(fct, outfile=None, ...@@ -656,6 +668,13 @@ def pydotprint(fct, outfile=None,
applystr = applystr + idx applystr = applystr + idx
elif len(applystr) > max_label_size: elif len(applystr) > max_label_size:
applystr = applystr[:max_label_size - 3] + '...' applystr = applystr[:max_label_size - 3] + '...'
idx = 1
while applystr in all_strings:
idx += 1
suffix = ' id=' + str(idx)
applystr = (applystr[:max_label_size - 3 - len(suffix)] +
'...' +
suffix)
all_strings.add(applystr) all_strings.add(applystr)
apply_name_cache[node] = applystr apply_name_cache[node] = applystr
...@@ -698,10 +717,10 @@ def pydotprint(fct, outfile=None, ...@@ -698,10 +717,10 @@ def pydotprint(fct, outfile=None,
for id, var in enumerate(node.inputs): for id, var in enumerate(node.inputs):
varstr = var_name(var) varstr = var_name(var)
label = str(var.type) label = str(var.type)
if len(label) > max_label_size:
label = label[:max_label_size - 3] + '...'
if len(node.inputs) > 1: if len(node.inputs) > 1:
label = str(id) + ' ' + label label = str(id) + ' ' + label
if len(label) > max_label_size:
label = label[:max_label_size - 3] + '...'
if var.owner is None: if var.owner is None:
if high_contrast: if high_contrast:
g.add_node(pd.Node(varstr, g.add_node(pd.Node(varstr,
...@@ -751,10 +770,14 @@ def pydotprint(fct, outfile=None, ...@@ -751,10 +770,14 @@ def pydotprint(fct, outfile=None,
if not outfile.endswith('.' + format): if not outfile.endswith('.' + format):
outfile += '.' + format outfile += '.' + format
g.write(outfile, prog='dot', format=format)
g.write(outfile, prog='dot', format=format)
if print_output_file: if print_output_file:
print 'The output file is available at', outfile print 'The output file is available at', outfile
if assert_nb_all_strings != -1:
assert len(all_strings) == assert_nb_all_strings
if scan_graphs: if scan_graphs:
scan_ops = [(idx, x) for idx, x in enumerate(fct_env.toposort()) scan_ops = [(idx, x) for idx, x in enumerate(fct_env.toposort())
if isinstance(x.op, theano.scan_module.scan_op.Scan)] if isinstance(x.op, theano.scan_module.scan_op.Scan)]
......
...@@ -47,6 +47,34 @@ def test_pydotprint_cond_highlight(): ...@@ -47,6 +47,34 @@ def test_pydotprint_cond_highlight():
' is no IfElse node in the graph\n') ' is no IfElse node in the graph\n')
def test_pydotprint_long_name():
"""This is a REALLY PARTIAL TEST.
It print a graph where there is variable and apply node that
there too long name is different, but not the shortened name.
We should not merge those node in the dot graph.
"""
# Skip test if pydot is not available.
if not theano.printing.pydot_imported:
raise SkipTest('pydot not available')
x = tensor.dvector()
mode = theano.compile.mode.get_default_mode().excluding("fusion")
f = theano.function([x], [x * 2, x + x], mode=mode)
f([1, 2, 3, 4])
s = StringIO.StringIO()
new_handler = logging.StreamHandler(s)
new_handler.setLevel(logging.DEBUG)
orig_handler = theano.logging_default_handler
theano.printing.pydotprint(f, max_label_size=5,
print_output_file=False,
assert_nb_all_strings=6)
def test_pydotprint_profile(): def test_pydotprint_profile():
"""Just check that pydotprint does not crash with ProfileMode.""" """Just check that pydotprint does not crash with ProfileMode."""
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论