提交 641b8143 authored 作者: Frederic Bastien's avatar Frederic Bastien

Make a parameter to debugprint() to print the clients.

上级 6104d3f9
......@@ -513,7 +513,7 @@ def debugprint(r, prefix='', depth=-1, done=None, print_type=False,
stop_on_name=False, prefix_child=None,
scan_ops=None, profile=None,
scan_inner_to_outer_inputs=None, smap=None,
used_ids=None):
used_ids=None, print_clients=False):
"""
Print the graph leading to `r` to given depth.
......@@ -526,7 +526,8 @@ def debugprint(r, prefix='', depth=-1, done=None, print_type=False,
depth
Maximum recursion depth (Default -1 for unlimited).
done
dict of Apply instances that have already been printed and their
Internal. Used to pass information when recursing.
Dict of Apply instances that have already been printed and their
associated printed ids.
print_type
Whether to print the Variable type after the other infos.
......@@ -555,6 +556,12 @@ def debugprint(r, prefix='', depth=-1, done=None, print_type=False,
inputs (outer inputs) for printing purposes.
smap
None or the storage_map when printing an Theano function.
used_ids
Internal. Used to pass information when recursing.
It is a dict from obj to the id used for it.
It wasn't always printed, but at least a reference to it was printed.
print_clients
If True, we will print the clients of nodes when they have more then one clients.
"""
if depth == 0:
return
......@@ -637,7 +644,7 @@ def debugprint(r, prefix='', depth=-1, done=None, print_type=False,
if smap:
data = " " + str(smap.get(a.outputs[0], ''))
clients = ''
if len(getattr(r, 'clients', [])) > 1:
if print_clients and len(getattr(r, 'clients', [])) > 1:
def get_index(c):
try:
return order.index(c)
......@@ -700,7 +707,7 @@ def debugprint(r, prefix='', depth=-1, done=None, print_type=False,
prefix_child=new_prefix_child, scan_ops=scan_ops,
profile=profile,
scan_inner_to_outer_inputs=scan_inner_to_outer_inputs,
smap=smap, used_ids=used_ids)
smap=smap, used_ids=used_ids, print_clients=print_clients)
else:
if scan_inner_to_outer_inputs is not None and\
r in scan_inner_to_outer_inputs:
......@@ -1688,13 +1695,16 @@ class _VariableEquivalenceTracker(object):
# N.B. compute the debugprint now, because future
# optimizations will change the graph
done = dict()
used_ids = dict()
self.reasons[new_r].append(
(reason,
r,
debugprint(r, prefix=' ', depth=6,
file=StringIO(), done=done).getvalue(),
file=StringIO(), done=done,
used_ids=used_ids).getvalue(),
debugprint(new_r, prefix=' ', depth=6,
file=StringIO(), done=done).getvalue()))
file=StringIO(), done=done,
used_ids=used_ids).getvalue()))
self.replaced_by[r].append((reason, new_r))
if r in self.equiv:
......
......@@ -50,7 +50,8 @@ VALID_ASSOC = set(['left', 'right', 'either'])
def debugprint(obj, depth=-1, print_type=False,
file=None, ids='CHAR', stop_on_name=False,
done=None, print_storage=False):
done=None, print_storage=False, print_clients=False,
used_ids=None):
"""Print a computation graph as text to stdout or a file.
:type obj: Variable, Apply, or Function instance
......@@ -76,6 +77,13 @@ def debugprint(obj, depth=-1, print_type=False,
:param print_storage: If True, this will print the storage map
for Theano functions. Combined with allow_gc=False, after the
execution of a Theano function, we see the intermediate result.
:type print_clients: bool
:param print_clients: If True, this will print for Apply node that
have more then 1 clients its clients. This help find who use
an Apply node.
:type used_ids: dict or None
:param used_ids: the id to use for some object, but maybe we only
refered to it yet.
:returns: string if `file` == 'str', else file arg
......@@ -105,6 +113,8 @@ def debugprint(obj, depth=-1, print_type=False,
_file = file
if done is None:
done = dict()
if used_ids is None:
used_ids = dict()
used_ids = dict()
results_to_print = []
profile_list = []
......@@ -186,7 +196,8 @@ N.B.:
debugmode.debugprint(r, depth=depth, done=done, print_type=print_type,
file=_file, order=o, ids=ids,
scan_ops=scan_ops, stop_on_name=stop_on_name,
profile=p, smap=s, used_ids=used_ids)
profile=p, smap=s, used_ids=used_ids,
print_clients=print_clients)
if len(scan_ops) > 0:
print("", file=_file)
......@@ -216,7 +227,8 @@ N.B.:
file=_file, ids=ids,
scan_ops=scan_ops,
stop_on_name=stop_on_name,
scan_inner_to_outer_inputs=inner_to_outer_inputs)
scan_inner_to_outer_inputs=inner_to_outer_inputs,
print_clients=print_clients, used_ids=used_ids)
if hasattr(s.owner.op, 'fn'):
# If the op was compiled, print the optimized version.
outputs = s.owner.op.fn.maker.fgraph.outputs
......@@ -235,7 +247,8 @@ N.B.:
ids=ids, stop_on_name=stop_on_name,
prefix_child=new_prefix_child,
scan_ops=scan_ops,
scan_inner_to_outer_inputs=inner_to_outer_inputs)
scan_inner_to_outer_inputs=inner_to_outer_inputs,
print_clients=print_clients, used_ids=used_ids)
if file is _file:
return file
......
......@@ -274,7 +274,7 @@ def test_debugprint():
# test clients
s = StringIO()
f = theano.function([A, B, D], [A + B, A + B - D])
debugprint(f, file=s)
debugprint(f, file=s, print_clients=True)
s = s.getvalue()
# The additional white space are needed!
reference = '\n'.join([
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论