提交 65ead2f5 authored 作者: abergeron's avatar abergeron

Merge pull request #1736 from nouiz/err_msg

[MRG]Better error message when a thunk fail: print user code line when node was created
...@@ -259,7 +259,9 @@ AddConfigVar('gpuelemwise.sync', ...@@ -259,7 +259,9 @@ AddConfigVar('gpuelemwise.sync',
AddConfigVar('traceback.limit', AddConfigVar('traceback.limit',
"The number of stack to trace. -1 mean all.", "The number of stack to trace. -1 mean all.",
IntParam(5), # We default to 6 to be able to know where v1 + v2 is created in the
# user script. The bigger this number is, the more run time it takes.
IntParam(6),
in_c_key=False) in_c_key=False)
AddConfigVar('experimental.mrg', AddConfigVar('experimental.mrg',
......
...@@ -56,14 +56,14 @@ sys.excepthook = thunk_hook ...@@ -56,14 +56,14 @@ sys.excepthook = thunk_hook
# TODO: Make this work with linker defined schedule # TODO: Make this work with linker defined schedule
def raise_with_op(op, thunk=None, exc_info=None): def raise_with_op(node, thunk=None, exc_info=None):
""" """
Re-raise an exception while annotating the exception object with Re-raise an exception while annotating the exception object with
debug info. debug info.
Parameters Parameters
---------- ----------
op : Apply node node : Apply node
The Apply node object that resulted in the raised exception. The Apply node object that resulted in the raised exception.
exc_info : tuple, optional exc_info : tuple, optional
A tuple containing the exception type, exception object and A tuple containing the exception type, exception object and
...@@ -94,16 +94,16 @@ def raise_with_op(op, thunk=None, exc_info=None): ...@@ -94,16 +94,16 @@ def raise_with_op(op, thunk=None, exc_info=None):
# print a simple traceback from KeyboardInterrupt # print a simple traceback from KeyboardInterrupt
raise exc_type, exc_value, exc_trace raise exc_type, exc_value, exc_trace
try: try:
trace = op.tag.trace trace = node.tag.trace
except AttributeError: except AttributeError:
try: try:
trace = op.op.tag.trace trace = node.op.tag.trace
except AttributeError: except AttributeError:
trace = () trace = ()
exc_value.__thunk_trace__ = trace exc_value.__thunk_trace__ = trace
exc_value.__op_instance__ = op exc_value.__op_instance__ = node
if op in op.fgraph.toposort(): if node in node.fgraph.toposort():
exc_value.__applynode_index__ = op.fgraph.toposort().index(op) exc_value.__applynode_index__ = node.fgraph.toposort().index(node)
else: else:
exc_value.__applynode_index__ = None exc_value.__applynode_index__ = None
...@@ -112,7 +112,11 @@ def raise_with_op(op, thunk=None, exc_info=None): ...@@ -112,7 +112,11 @@ def raise_with_op(op, thunk=None, exc_info=None):
if raise_with_op.print_thunk_trace: if raise_with_op.print_thunk_trace:
log_thunk_trace(exc_value) log_thunk_trace(exc_value)
detailed_err_msg = "\nApply node that caused the error: " + str(op) hints = []
detailed_err_msg = "\nApply node that caused the error: " + str(node)
types = [getattr(ipt, 'type', 'No type') for ipt in node.inputs]
detailed_err_msg += "\nInputs types: %s\n" % types
if thunk is not None: if thunk is not None:
if hasattr(thunk, 'inputs'): if hasattr(thunk, 'inputs'):
...@@ -131,26 +135,43 @@ def raise_with_op(op, thunk=None, exc_info=None): ...@@ -131,26 +135,43 @@ def raise_with_op(op, thunk=None, exc_info=None):
strides = "So we can't access the strides of inputs values" strides = "So we can't access the strides of inputs values"
scalar_values = "And can't print its inputs scalar value" scalar_values = "And can't print its inputs scalar value"
types = [getattr(ipt, 'type', 'No type') detailed_err_msg += ("Inputs shapes: %s" % shapes +
for ipt in op.inputs]
detailed_err_msg += ("\nInputs shapes: %s" % shapes +
"\nInputs strides: %s" % strides + "\nInputs strides: %s" % strides +
"\nInputs types: %s" % types + "\nInputs scalar values: %s\n" % scalar_values)
"\nInputs scalar values: %s" % scalar_values)
else: else:
detailed_err_msg += ("\nUse another linker then the c linker to" hints.append(
"HINT: Use another linker then the c linker to"
" have the inputs shapes and strides printed.") " have the inputs shapes and strides printed.")
# Print node backtrace
tr = getattr(node.tag, 'trace', None)
if tr:
sio = StringIO.StringIO()
traceback.print_list(tr, sio)
tr = sio.getvalue()
detailed_err_msg += "\nBacktrace when the node is created:"
detailed_err_msg += str(tr)
else:
hints.append(
"HINT: Re-running with most Theano optimization disabled could"
" give you a back-traces when this node was created. This can"
" be done with by setting the Theano flags"
" optimizer=fast_compile")
if theano.config.exception_verbosity == 'high': if theano.config.exception_verbosity == 'high':
f = StringIO.StringIO() f = StringIO.StringIO()
theano.printing.debugprint(op, file=f, stop_on_name=True, theano.printing.debugprint(node, file=f, stop_on_name=True,
print_type=True) print_type=True)
detailed_err_msg += "\nDebugprint of the apply node: \n" + f.getvalue() detailed_err_msg += "\nDebugprint of the apply node: \n"
detailed_err_msg += f.getvalue()
else: else:
detailed_err_msg += ("\nUse the Theano flag 'exception_verbosity=high'" hints.append(
"HINT: Use the Theano flag 'exception_verbosity=high'"
" for a debugprint of this apply node.") " for a debugprint of this apply node.")
exc_value = exc_type(str(exc_value) + detailed_err_msg) exc_value = exc_type(str(exc_value) + detailed_err_msg +
'\n' + '\n'.join(hints))
raise exc_type, exc_value, exc_trace raise exc_type, exc_value, exc_trace
raise_with_op.print_thunk_trace = False raise_with_op.print_thunk_trace = False
......
...@@ -12,7 +12,13 @@ def add_tag_trace(thing): ...@@ -12,7 +12,13 @@ def add_tag_trace(thing):
limit = config.traceback.limit limit = config.traceback.limit
if limit == -1: if limit == -1:
limit = None limit = None
thing.tag.trace = traceback.extract_stack(limit=limit)[:-1] tr = traceback.extract_stack(limit=limit)[:-1]
# Different python version use different sementic for
# limit. python 2.7 include the call to extrack_stack. The -1 get
# rid of it. We also want to get rid of the add_tag_trace call.
if tr and "add_tag_trace" in tr[-1][-1]:
tr = tr[:-1]
thing.tag.trace = tr
return thing return thing
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论