提交 599bdad6 authored 作者: Olivier Delalleau's avatar Olivier Delalleau

Merge pull request #172 from nouiz/fix_assert_import

Fix assert import
......@@ -100,9 +100,30 @@ def register_optimizer(name, opt):
raise ValueError('Optimizer name already taken: %s' % name)
predefined_optimizers[name] = opt
def register_OutputGuard_c_code(type):
OutputGuard.c_code_types.append(type)
class OutputGuard(gof.Op):
"""
This op is used only internally by Theano.
Only the AddDestroyHandler optimizer tries to insert them in the graph.
This Op is declared as destructive while it is not destroying
anything. It returns a view. This is used to prevent destruction of
the output variables of a Theano function.
There is a mechanism in Theano that should prevent this, but the use
of OutputGuard adds a safeguard: it may be possible for some optimization
run before the add_destroy_handler phase to bypass this mechanism, by
making in-place optimizations.
TODO: find a current full explanation.
"""
destroy_map = {0:[0]}
view_map = {0:[0]}
c_code_types = []
def make_node(self, x):
return gof.Apply(self, [x], [x.type()])
def __eq__(self, other):
......@@ -124,12 +145,7 @@ class OutputGuard(gof.Op):
return """
%(z)s = %(x)s;
""" % locals()
elif (isinstance(node.inputs[0].type,
(theano.tensor.TensorType,
theano.sandbox.cuda.CudaNdarrayType,
theano.tensor.raw_random.RandomStateType)) or
node.inputs[0].type.__class__.__name__ == 'SparseType'
):
elif (isinstance(node.inputs[0].type, tuple(self.c_code_types))):
# These are Python object types
return """
Py_XDECREF(%(z)s);
......
......@@ -351,6 +351,12 @@ class CudaNdarrayType(Type):
ret.append('-use_fast_math')
return ret
# Register CudaNdarrayType to the OutputGuard list of known types
# to have OutputGuard generate C code for this type.
theano.compile.mode.register_OutputGuard_c_code(CudaNdarrayType)
# THIS WORKS
# But CudaNdarray instances don't compare equal to one another, and what about __hash__ ?
# So the unpickled version doesn't equal the pickled version, and the cmodule cache is not
......
......@@ -326,6 +326,11 @@ class SparseType(gof.Type):
def is_valid_value(self, a):
return scipy.sparse.issparse(a) and (a.format == self.format)
# Register CudaNdarrayType to the OutputGuard list of known types
# to have OutputGuard generate C code for this type.
theano.compile.mode.register_OutputGuard_c_code(SparseType)
# for more dtypes, call SparseType(format, dtype)
def matrix(format, name=None, dtype=None):
if dtype is None:
......
......@@ -910,6 +910,10 @@ class TensorType(Type):
else:
return ()
# Register CudaNdarrayType to the OutputGuard list of known types
# to have OutputGuard generate C code for this type.
theano.compile.mode.register_OutputGuard_c_code(TensorType)
# Easy constructors
def tensor(*args, **kwargs):
......@@ -3307,8 +3311,25 @@ class Subtensor(Op):
{
%(fail)s;
}
assert (xview->dimensions != %(x)s->dimensions);
assert (xview->strides != %(x)s->strides);
if ((xview->dimensions == %(x)s->dimensions)
&& (%(x)s->dimensions != NULL))
{
PyErr_Format(PyExc_ValueError, "x and xview"
"(with %%d dims) have the same dimensions"
" pointers: %%p and %%p",
%(x)s->nd, xview->dimensions, %(x)s->dimensions);
%(fail)s;
}
if (xview->strides == %(x)s->strides
&& (%(x)s->dimensions != NULL))
{
PyErr_Format(PyExc_ValueError, "x and xview"
"(with %%d dims) have the same strides"
" pointers: %%p and %%p",
%(x)s->nd, xview->strides, %(x)s->strides);
%(fail)s;
}
for (; outer_ii < %(len_is_slice)s; ++outer_ii)
{
......@@ -3425,7 +3446,7 @@ class Subtensor(Op):
@staticmethod
def helper_c_code_cache_version():
return (2,)
return (3,)
def c_code(self, node, name, inputs, outputs, sub): #DEBUG
part0 = self.helper_c_code(node, name, inputs, outputs, sub,
......@@ -3446,6 +3467,10 @@ class Subtensor(Op):
def c_code_cache_version(self):
hv = self.helper_c_code_cache_version()
# If `helper_c_code_cache_version` is not versioned we do not want to
# have a versioned version of this op's C code.
if len(hv) == 0:
return ()
return (1, hv)
def R_op(self, inputs, eval_points):
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -53,6 +53,10 @@ class RandomStateType(gof.Type):
return False
return True
# Register CudaNdarrayType to the OutputGuard list of known types
# to have OutputGuard generate C code for this type.
theano.compile.mode.register_OutputGuard_c_code(RandomStateType)
random_state_type = RandomStateType()
......
......@@ -1957,6 +1957,17 @@ class T_subtensor(unittest.TestCase):
self.assertTrue(tval.shape == (2,))
self.assertTrue(numpy.allclose(tval, n.get_value()[idx]))
def test1_0_dims(self):
n = self.shared(numpy.ones((), dtype=self.dtype))
t = theano.tensor.Subtensor([])(n)
self.assertTrue(isinstance(t.owner.op, Subtensor))
mode = self.mode
self.mode = mode.excluding("local_useless_subtensor")
try:
self.eval_output_and_check(t)
finally:
self.mode = mode
def test1_err_invalid(self):
n = self.shared(numpy.ones(1, dtype=self.dtype))
try:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论