提交 388805f9 authored 作者: Pascal Lamblin's avatar Pascal Lamblin 提交者: GitHub

Merge pull request #5716 from nouiz/err

Better error message when an opt make an invalide replace.
...@@ -153,165 +153,8 @@ class BadThunkOutput(DebugModeError): ...@@ -153,165 +153,8 @@ class BadThunkOutput(DebugModeError):
return ret return ret
class BadOptimization(DebugModeError): class BadOptimization(DebugModeError, theano.gof.toolbox.BadOptimization):
""" pass
Exception: some variable and its substitute take different runtime values.
"""
new_r = None
"""
A `Variable` instance that took a different value from `old_r`,
but which replaced `old_r`.
"""
old_r = None
"""
A `Variable` instance that was replaced by `new_r`.
"""
old_r_val = None
"""
The value computed for `old_r`.
"""
new_r_val = None
"""
The value computed for `new_r`.
"""
reason = None
"""
An object that indicates why old_r was turned into new_r.
Convention is that this is the name of the optimization that
requested the replacement.
"""
old_graph = ""
"""
A multiline string representation of the graph leading to
old_r, at the time of the replacement.
"""
new_graph = ""
"""
A multiline string representation of the graph leading to
new_r, at the time of the replacement.
"""
def __init__(self, old_r, new_r, old_r_val, new_r_val, reason,
old_graph, new_graph):
super(BadOptimization, self).__init__()
self.old_r = old_r
self.new_r = new_r
self.old_r_val = old_r_val
self.new_r_val = new_r_val
self.reason = reason
self.old_graph = old_graph
self.new_graph = new_graph
def __str__(self):
return self.str_diagnostic()
def str_diagnostic(self):
"""
Return a pretty multiline string representating the cause
of the exception.
"""
sio = StringIO()
val_str_len_limit = 800
print("BadOptimization Error", super(BadOptimization,
self).__str__(), file=sio)
print(" Variable: id", id(self.new_r), self.new_r, file=sio)
print(" Op", self.new_r.owner, file=sio)
print(" Value Type:", type(self.new_r_val), file=sio)
try:
ssio = StringIO()
print(" Old Value shape, dtype, strides:", end=' ', file=ssio)
print(self.old_r_val.shape, end=' ', file=ssio)
print(self.old_r_val.dtype, end=' ', file=ssio)
print(self.old_r_val.strides, file=ssio)
# only if all succeeds to we add anything to sio
print(ssio.getvalue(), file=sio)
except Exception:
pass
str_old_r_val = str(self.old_r_val)
if len(str_old_r_val) > val_str_len_limit:
print(" Old Value: ", str(self.old_r_val)[
:val_str_len_limit], '...', file=sio)
else:
print(" Old Value: ", str(self.old_r_val), file=sio)
try:
ssio = StringIO()
print(" New Value shape, dtype, strides:", end=' ', file=ssio)
print(self.new_r_val.shape, end=' ', file=ssio)
print(self.new_r_val.dtype, end=' ', file=ssio)
print(self.new_r_val.strides, file=ssio)
# only if all succeeds to we add anything to sio
print(ssio.getvalue(), file=sio)
except Exception:
pass
str_new_r_val = str(self.new_r_val)
if len(str_new_r_val) > val_str_len_limit:
print(" New Value: ", str(self.new_r_val)[
:val_str_len_limit], '...', file=sio)
else:
print(" New Value: ", str(self.new_r_val), file=sio)
try:
ov = np.asarray(self.old_r_val)
nv = np.asarray(self.new_r_val)
ssio = StringIO()
abs_diff = np.absolute(nv - ov)
print(" Max Abs Diff: ", np.max(abs_diff), file=ssio)
print(" Mean Abs Diff: ", np.mean(abs_diff), file=ssio)
print(" Median Abs Diff: ", np.median(abs_diff), file=ssio)
print(" Std Abs Diff: ", np.std(abs_diff), file=ssio)
arg_max_val = np.argmax(abs_diff)
values_at_max = (nv.flatten()[arg_max_val],
ov.flatten()[arg_max_val])
print(" Value at Max Diff: ", values_at_max, file=ssio)
# N.B. the maximum(..., 1e-8) protects against div by 0 when
# nv == ov == 0
reldiff = (abs_diff /
np.maximum(np.absolute(nv) + np.absolute(ov),
1e-8))
print(" Max Rel Diff: ", np.max(reldiff), file=ssio)
print(" Mean Rel Diff: ", np.mean(reldiff), file=ssio)
print(" Median Rel Diff: ", np.median(reldiff), file=ssio)
print(" Std Rel Diff: ", np.std(reldiff), file=ssio)
arg_max_val = np.argmax(reldiff)
values_at_max = (nv.flatten()[arg_max_val],
ov.flatten()[arg_max_val])
print(" Value at Max Diff: ", values_at_max, file=ssio)
# only if all succeeds to we add anything to sio
print(ssio.getvalue(), file=sio)
except Exception:
pass
print(" Reason: ", str(self.reason), file=sio)
print(" Old Graph:", file=sio)
print(self.old_graph, file=sio)
print(" New Graph:", file=sio)
print(self.new_graph, file=sio)
print("", file=sio)
print("Hint: relax the tolerance by setting tensor.cmp_sloppy=1",
file=sio)
print(" or even tensor.cmp_sloppy=2 for less-strict comparison",
file=sio)
return sio.getvalue()
class BadDestroyMap(DebugModeError): class BadDestroyMap(DebugModeError):
...@@ -1697,9 +1540,11 @@ class _VariableEquivalenceTracker(object): ...@@ -1697,9 +1540,11 @@ class _VariableEquivalenceTracker(object):
r, r,
debugprint(r, prefix=' ', depth=6, debugprint(r, prefix=' ', depth=6,
file=StringIO(), done=done, file=StringIO(), done=done,
print_type=True,
used_ids=used_ids).getvalue(), used_ids=used_ids).getvalue(),
debugprint(new_r, prefix=' ', depth=6, debugprint(new_r, prefix=' ', depth=6,
file=StringIO(), done=done, file=StringIO(), done=done,
print_type=True,
used_ids=used_ids).getvalue())) used_ids=used_ids).getvalue()))
self.replaced_by[r].append((reason, new_r)) self.replaced_by[r].append((reason, new_r))
......
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
from nose.plugins.skip import SkipTest import sys
import unittest import unittest
from nose.plugins.skip import SkipTest
import numpy as np import numpy as np
from six import reraise
from theano import config from theano import config
from theano import gof from theano import gof
import theano import theano
import theano.tensor
from theano.compat import exc_message from theano.compat import exc_message
from theano.compile import debugmode from theano.compile import debugmode
import theano.compile import theano.tensor
from theano.tests import unittest_tools as utt from theano.tests import unittest_tools as utt
...@@ -255,23 +256,55 @@ def test_badoptimization_opt_err(): ...@@ -255,23 +256,55 @@ def test_badoptimization_opt_err():
inputs[-1])) inputs[-1]))
return [node.op(*inputs)] return [node.op(*inputs)]
return False return False
@gof.local_optimizer([theano.tensor.add])
def insert_bad_dtype(node):
if node.op == theano.tensor.add:
inputs = list(node.inputs)
if inputs[-1].owner is None:
return [node.outputs[0].astype('float32')]
return False
edb = gof.EquilibriumDB() edb = gof.EquilibriumDB()
edb.register('insert_bigger_b_add', insert_bigger_b_add, 'all') edb.register('insert_bigger_b_add', insert_bigger_b_add, 'all')
opt = edb.query('+all') opt = edb.query('+all')
edb2 = gof.EquilibriumDB()
edb2.register('insert_bad_dtype', insert_bad_dtype, 'all')
opt2 = edb2.query('+all')
a = theano.tensor.dvector() a = theano.tensor.dvector()
b = theano.tensor.dvector() b = theano.tensor.dvector()
f = theano.function([a, b], a + b, f = theano.function([a, b], a + b,
mode=debugmode.DebugMode(optimizer=opt)) mode=debugmode.DebugMode(optimizer=opt))
try: try:
f([1.0, 2.0, 3.0], [2, 3, 4],) f([1.0, 2.0, 3.0], [2, 3, 4],)
except Exception as e: except ValueError as e:
assert 'insert_bigger_b_add' in exc_message(e) assert 'insert_bigger_b_add' in exc_message(e)
return # TEST PASS else:
assert False
assert False # Test that opt that do an illegal change still get the error from gof.
try:
with theano.configparser.change_flags(on_opt_error='raise'):
f2 = theano.function([a, b], a + b,
mode=debugmode.DebugMode(optimizer=opt2,
stability_patience=1))
f2([1.0, 2.0, 3.0], [2, 3, 4],)
except theano.gof.toolbox.BadOptimization as e:
assert 'insert_bad_dtype' in str(e)
# Test that we can reraise the error with an extended message
try:
new_e = e.__class__("TTT" + str(e))
exc_type, exc_value, exc_trace = sys.exc_info()
exc_value = new_e
reraise(e.__class__, exc_value, exc_trace)
except theano.gof.toolbox.BadOptimization as e:
pass
else:
assert False
else:
assert False
def test_stochasticoptimization(): def test_stochasticoptimization():
......
...@@ -105,7 +105,7 @@ class change_flags(object): ...@@ -105,7 +105,7 @@ class change_flags(object):
for k in args: for k in args:
l = [v for v in theano.configparser._config_var_list l = [v for v in theano.configparser._config_var_list
if v.fullname == k] if v.fullname == k]
assert len(l) == 1 assert len(l) == 1, l
confs[k] = l[0] confs[k] = l[0]
self.confs = confs self.confs = confs
self.new_vals = args self.new_vals = args
......
...@@ -15,6 +15,7 @@ from theano.gof import toolbox ...@@ -15,6 +15,7 @@ from theano.gof import toolbox
from theano import config from theano import config
from six import iteritems, itervalues from six import iteritems, itervalues
from six.moves import StringIO
from theano.gof.utils import get_variable_trace_string from theano.gof.utils import get_variable_trace_string
from theano.misc.ordered_set import OrderedSet from theano.misc.ordered_set import OrderedSet
NullType = None NullType = None
...@@ -468,10 +469,21 @@ class FunctionGraph(utils.object2): ...@@ -468,10 +469,21 @@ class FunctionGraph(utils.object2):
new_r2 = r.type.convert_variable(new_r) new_r2 = r.type.convert_variable(new_r)
# We still make sure that the type converts correctly # We still make sure that the type converts correctly
if new_r2 is None or new_r2.type != r.type: if new_r2 is None or new_r2.type != r.type:
raise TypeError("The type of the replacement must be " done = dict()
"compatible with the type of the original " used_ids = dict()
"Variable.", r, new_r, r.type, new_r.type, old = theano.compile.debugmode.debugprint(
str(reason)) r, prefix=' ', depth=6,
file=StringIO(), done=done,
print_type=True,
used_ids=used_ids).getvalue()
new = theano.compile.debugmode.debugprint(
new_r, prefix=' ', depth=6,
file=StringIO(), done=done,
print_type=True,
used_ids=used_ids).getvalue()
raise toolbox.BadOptimization(
r, new_r, None, None, str(reason) +
". The type of the replacement must be the same.", old, new)
new_r = new_r2 new_r = new_r2
if r not in self.variables: if r not in self.variables:
# this variable isn't in the graph... don't raise an # this variable isn't in the graph... don't raise an
......
...@@ -6,6 +6,9 @@ import sys ...@@ -6,6 +6,9 @@ import sys
import time import time
import inspect import inspect
import numpy as np
from six.moves import StringIO
import theano import theano
from theano import config from theano import config
from theano.gof import graph from theano.gof import graph
...@@ -33,6 +36,181 @@ class ReplacementDidntRemovedError(Exception): ...@@ -33,6 +36,181 @@ class ReplacementDidntRemovedError(Exception):
pass pass
class BadOptimization(Exception):
"""
Exception: some variable and its substitute take different runtime values.
Note: If there is only 1 parameter and it is a string, we will use
it as the error message. This is needed when we catch, extend and
reraise an error.
"""
new_r = None
"""
A `Variable` instance that took a different value from `old_r`,
but which replaced `old_r`.
"""
old_r = None
"""
A `Variable` instance that was replaced by `new_r`.
"""
old_r_val = None
"""
The value computed for `old_r`.
"""
new_r_val = None
"""
The value computed for `new_r`.
"""
reason = None
"""
An object that indicates why old_r was turned into new_r.
Convention is that this is the name of the optimization that
requested the replacement.
"""
old_graph = ""
"""
A multiline string representation of the graph leading to
old_r, at the time of the replacement.
"""
new_graph = ""
"""
A multiline string representation of the graph leading to
new_r, at the time of the replacement.
"""
def __init__(self, old_r, new_r=None, old_r_val=None, new_r_val=None, reason=None,
old_graph=None, new_graph=None):
super(BadOptimization, self).__init__()
self.old_r = old_r
self.new_r = new_r
self.old_r_val = old_r_val
self.new_r_val = new_r_val
self.reason = reason
self.old_graph = old_graph
self.new_graph = new_graph
# To allow extending the error message of an existing error.
self.full_err = None
if isinstance(old_r, str):
assert (new_r is None and old_r_val is None and new_r_val is None and
reason is None and old_graph is None and new_graph is None)
self.full_err = old_r
def __str__(self):
return self.str_diagnostic()
def str_diagnostic(self):
"""
Return a pretty multiline string representating the cause
of the exception.
"""
# We have a pre-made message
if getattr(self, 'full_err', None) is not None:
return self.full_err
sio = StringIO()
val_str_len_limit = 800
print("BadOptimization Error", super(BadOptimization,
self).__str__(), file=sio)
print(" Variable: id", id(self.new_r), self.new_r, file=sio)
print(" Op", self.new_r.owner, file=sio)
print(" Value Type:", type(self.new_r_val), file=sio)
try:
ssio = StringIO()
print(" Old Value shape, dtype, strides:", end=' ', file=ssio)
print(self.old_r_val.shape, end=' ', file=ssio)
print(self.old_r_val.dtype, end=' ', file=ssio)
print(self.old_r_val.strides, file=ssio)
# only if all succeeds to we add anything to sio
print(ssio.getvalue(), file=sio)
except Exception:
pass
str_old_r_val = str(self.old_r_val)
if len(str_old_r_val) > val_str_len_limit:
print(" Old Value: ", str(self.old_r_val)[
:val_str_len_limit], '...', file=sio)
else:
print(" Old Value: ", str(self.old_r_val), file=sio)
try:
ssio = StringIO()
print(" New Value shape, dtype, strides:", end=' ', file=ssio)
print(self.new_r_val.shape, end=' ', file=ssio)
print(self.new_r_val.dtype, end=' ', file=ssio)
print(self.new_r_val.strides, file=ssio)
# only if all succeeds to we add anything to sio
print(ssio.getvalue(), file=sio)
except Exception:
pass
str_new_r_val = str(self.new_r_val)
if len(str_new_r_val) > val_str_len_limit:
print(" New Value: ", str(self.new_r_val)[
:val_str_len_limit], '...', file=sio)
else:
print(" New Value: ", str(self.new_r_val), file=sio)
try:
ov = np.asarray(self.old_r_val)
nv = np.asarray(self.new_r_val)
ssio = StringIO()
abs_diff = np.absolute(nv - ov)
print(" Max Abs Diff: ", np.max(abs_diff), file=ssio)
print(" Mean Abs Diff: ", np.mean(abs_diff), file=ssio)
print(" Median Abs Diff: ", np.median(abs_diff), file=ssio)
print(" Std Abs Diff: ", np.std(abs_diff), file=ssio)
arg_max_val = np.argmax(abs_diff)
values_at_max = (nv.flatten()[arg_max_val],
ov.flatten()[arg_max_val])
print(" Value at Max Diff: ", values_at_max, file=ssio)
# N.B. the maximum(..., 1e-8) protects against div by 0 when
# nv == ov == 0
reldiff = (abs_diff /
np.maximum(np.absolute(nv) + np.absolute(ov),
1e-8))
print(" Max Rel Diff: ", np.max(reldiff), file=ssio)
print(" Mean Rel Diff: ", np.mean(reldiff), file=ssio)
print(" Median Rel Diff: ", np.median(reldiff), file=ssio)
print(" Std Rel Diff: ", np.std(reldiff), file=ssio)
arg_max_val = np.argmax(reldiff)
values_at_max = (nv.flatten()[arg_max_val],
ov.flatten()[arg_max_val])
print(" Value at Max Diff: ", values_at_max, file=ssio)
# only if all succeeds to we add anything to sio
print(ssio.getvalue(), file=sio)
except Exception:
pass
print(" Reason: ", str(self.reason), file=sio)
print(" Old Graph:", file=sio)
print(self.old_graph, file=sio)
print(" New Graph:", file=sio)
print(self.new_graph, file=sio)
print("", file=sio)
print("Hint: relax the tolerance by setting tensor.cmp_sloppy=1",
file=sio)
print(" or even tensor.cmp_sloppy=2 for less-strict comparison",
file=sio)
return sio.getvalue()
class Feature(object): class Feature(object):
""" """
Base class for FunctionGraph extensions. Base class for FunctionGraph extensions.
......
...@@ -176,6 +176,7 @@ class Solve(Op): ...@@ -176,6 +176,7 @@ class Solve(Op):
""" """
Solve a system of linear equations. Solve a system of linear equations.
For on CPU and GPU.
""" """
__props__ = ('A_structure', 'lower', 'overwrite_A', 'overwrite_b') __props__ = ('A_structure', 'lower', 'overwrite_A', 'overwrite_b')
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论