提交 82d4e9a7 authored 作者: carriepl's avatar carriepl

Merge pull request #2909 from aalmah/ticket_2347

improving debugprint for scan ops
...@@ -25,8 +25,7 @@ from theano.configparser import (config, AddConfigVar, BoolParam, IntParam, ...@@ -25,8 +25,7 @@ from theano.configparser import (config, AddConfigVar, BoolParam, IntParam,
StrParam) StrParam)
from theano.compile.function_module import ( from theano.compile.function_module import (
FunctionMaker, Function, infer_reuse_pattern, FunctionMaker, Function, infer_reuse_pattern,
SymbolicInputKit, SymbolicOutput, Supervisor, std_fgraph SymbolicInputKit, SymbolicOutput, Supervisor, std_fgraph)
)
from theano.compile.mode import Mode, register_mode from theano.compile.mode import Mode, register_mode
from theano.compile.ops import OutputGuard from theano.compile.ops import OutputGuard
...@@ -521,7 +520,8 @@ def debugprint(r, prefix='', depth=-1, done=None, print_type=False, ...@@ -521,7 +520,8 @@ def debugprint(r, prefix='', depth=-1, done=None, print_type=False,
file=sys.stdout, print_destroy_map=False, file=sys.stdout, print_destroy_map=False,
print_view_map=False, order=None, ids='CHAR', print_view_map=False, order=None, ids='CHAR',
stop_on_name=False, prefix_child=None, stop_on_name=False, prefix_child=None,
scan_ops=None, profile=None): scan_ops=None, profile=None,
scan_inner_to_outer_inputs=None):
"""Print the graph leading to `r` to given depth. """Print the graph leading to `r` to given depth.
:param r: Variable instance :param r: Variable instance
...@@ -544,6 +544,9 @@ def debugprint(r, prefix='', depth=-1, done=None, print_type=False, ...@@ -544,6 +544,9 @@ def debugprint(r, prefix='', depth=-1, done=None, print_type=False,
we don't print anything below it. we don't print anything below it.
:param scan_ops: Scan ops in the graph will be added inside this list :param scan_ops: Scan ops in the graph will be added inside this list
for later printing purposes. for later printing purposes.
:param scan_inner_to_outer_inputs: a dictionary mapping a scan ops
inner function inputs to the scan op inputs (outer inputs) for
printing purposes.
""" """
if depth == 0: if depth == 0:
...@@ -578,6 +581,7 @@ def debugprint(r, prefix='', depth=-1, done=None, print_type=False, ...@@ -578,6 +581,7 @@ def debugprint(r, prefix='', depth=-1, done=None, print_type=False,
elif ids == "": elif ids == "":
id_str = "" id_str = ""
done[obj] = id_str done[obj] = id_str
return id_str return id_str
if hasattr(r.owner, 'op'): if hasattr(r.owner, 'op'):
...@@ -678,16 +682,30 @@ def debugprint(r, prefix='', depth=-1, done=None, print_type=False, ...@@ -678,16 +682,30 @@ def debugprint(r, prefix='', depth=-1, done=None, print_type=False,
theano.scan_module.scan_op.Scan): theano.scan_module.scan_op.Scan):
scan_ops.append(i) scan_ops.append(i)
debugprint(i, new_prefix, depth=depth - 1, done=done, debugprint(
print_type=print_type, file=file, order=order, i, new_prefix, depth=depth - 1, done=done,
ids=ids, stop_on_name=stop_on_name, print_type=print_type, file=file, order=order,
prefix_child=new_prefix_child, ids=ids, stop_on_name=stop_on_name,
scan_ops=scan_ops, profile=profile) prefix_child=new_prefix_child, scan_ops=scan_ops,
profile=profile,
scan_inner_to_outer_inputs=scan_inner_to_outer_inputs)
else: else:
# this is an input variable if scan_inner_to_outer_inputs is not None and\
id_str = get_id_str(r) r in scan_inner_to_outer_inputs:
print('%s%s %s%s' % (prefix, r, id_str, type_str), file=file)
id_str = get_id_str(r)
outer_r = scan_inner_to_outer_inputs[r]
if hasattr(outer_r.owner, 'op'):
outer_id_str = get_id_str(outer_r.owner)
else:
outer_id_str = get_id_str(outer_r)
print('%s%s %s%s -> %s' % (prefix, r, id_str, type_str,
outer_id_str), file=file)
else:
# this is an input variable
id_str = get_id_str(r)
print('%s%s %s%s' % (prefix, r, id_str, type_str), file=file)
return file return file
...@@ -1601,7 +1619,7 @@ class _VariableEquivalenceTracker(object): ...@@ -1601,7 +1619,7 @@ class _VariableEquivalenceTracker(object):
r, r,
debugprint(r, prefix=' ', depth=6, debugprint(r, prefix=' ', depth=6,
file=StringIO(), done=done).getvalue(), file=StringIO(), done=done).getvalue(),
debugprint(new_r, prefix=' ', depth=6, debugprint(new_r, prefix=' ', depth=6,
file=StringIO(), done=done).getvalue())) file=StringIO(), done=done).getvalue()))
self.replaced_by[r].append((reason, new_r)) self.replaced_by[r].append((reason, new_r))
......
...@@ -149,6 +149,7 @@ N.B.: ...@@ -149,6 +149,7 @@ N.B.:
file=_file, order=order, ids=ids, file=_file, order=order, ids=ids,
scan_ops=scan_ops, stop_on_name=stop_on_name, scan_ops=scan_ops, stop_on_name=stop_on_name,
profile=p) profile=p)
if len(scan_ops) > 0: if len(scan_ops) > 0:
print("", file=_file) print("", file=_file)
new_prefix = ' >' new_prefix = ' >'
...@@ -156,27 +157,47 @@ N.B.: ...@@ -156,27 +157,47 @@ N.B.:
print("Inner graphs of the scan ops:", file=_file) print("Inner graphs of the scan ops:", file=_file)
for s in scan_ops: for s in scan_ops:
# prepare a dict which maps the scan op's inner inputs
# to its outer inputs.
if hasattr(s.owner.op, 'fn'):
# If the op was compiled, print the optimized version.
inner_inputs = s.owner.op.fn.maker.fgraph.inputs
else:
inner_inputs = s.owner.op.inputs
outer_inputs = s.owner.inputs
inner_to_outer_inputs = \
dict([(inner_inputs[i], outer_inputs[o])
for i, o in
s.owner.op.var_mappings['outer_inp_from_inner_inp']
.items()])
print("", file=_file) print("", file=_file)
debugmode.debugprint(s, depth=depth, done=done, debugmode.debugprint(
print_type=print_type, s, depth=depth, done=done,
file=_file, ids=ids, print_type=print_type,
scan_ops=scan_ops, stop_on_name=stop_on_name) file=_file, ids=ids,
scan_ops=scan_ops,
stop_on_name=stop_on_name,
scan_inner_to_outer_inputs=inner_to_outer_inputs)
if hasattr(s.owner.op, 'fn'): if hasattr(s.owner.op, 'fn'):
# If the op was compiled, print the optimized version. # If the op was compiled, print the optimized version.
outputs = s.owner.op.fn.maker.fgraph.outputs outputs = s.owner.op.fn.maker.fgraph.outputs
else: else:
outputs = s.owner.op.outputs outputs = s.owner.op.outputs
for idx, i in enumerate(outputs): for idx, i in enumerate(outputs):
if hasattr(i, 'owner') and hasattr(i.owner, 'op'): if hasattr(i, 'owner') and hasattr(i.owner, 'op'):
if isinstance(i.owner.op, theano.scan_module.scan_op.Scan): if isinstance(i.owner.op, theano.scan_module.scan_op.Scan):
scan_ops.append(i) scan_ops.append(i)
debugmode.debugprint(r=i, prefix=new_prefix, debugmode.debugprint(
depth=depth, done=done, r=i, prefix=new_prefix,
print_type=print_type, file=_file, depth=depth, done=done,
ids=ids, stop_on_name=stop_on_name, print_type=print_type, file=_file,
prefix_child=new_prefix_child, ids=ids, stop_on_name=stop_on_name,
scan_ops=scan_ops) prefix_child=new_prefix_child,
scan_ops=scan_ops,
scan_inner_to_outer_inputs=inner_to_outer_inputs)
if file is _file: if file is _file:
return file return file
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论