提交 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): ...@@ -100,9 +100,30 @@ def register_optimizer(name, opt):
raise ValueError('Optimizer name already taken: %s' % name) raise ValueError('Optimizer name already taken: %s' % name)
predefined_optimizers[name] = opt predefined_optimizers[name] = opt
def register_OutputGuard_c_code(type):
OutputGuard.c_code_types.append(type)
class OutputGuard(gof.Op): 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]} destroy_map = {0:[0]}
view_map = {0:[0]} view_map = {0:[0]}
c_code_types = []
def make_node(self, x): def make_node(self, x):
return gof.Apply(self, [x], [x.type()]) return gof.Apply(self, [x], [x.type()])
def __eq__(self, other): def __eq__(self, other):
...@@ -124,12 +145,7 @@ class OutputGuard(gof.Op): ...@@ -124,12 +145,7 @@ class OutputGuard(gof.Op):
return """ return """
%(z)s = %(x)s; %(z)s = %(x)s;
""" % locals() """ % locals()
elif (isinstance(node.inputs[0].type, elif (isinstance(node.inputs[0].type, tuple(self.c_code_types))):
(theano.tensor.TensorType,
theano.sandbox.cuda.CudaNdarrayType,
theano.tensor.raw_random.RandomStateType)) or
node.inputs[0].type.__class__.__name__ == 'SparseType'
):
# These are Python object types # These are Python object types
return """ return """
Py_XDECREF(%(z)s); Py_XDECREF(%(z)s);
......
...@@ -351,6 +351,12 @@ class CudaNdarrayType(Type): ...@@ -351,6 +351,12 @@ class CudaNdarrayType(Type):
ret.append('-use_fast_math') ret.append('-use_fast_math')
return ret 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 # THIS WORKS
# But CudaNdarray instances don't compare equal to one another, and what about __hash__ ? # 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 # So the unpickled version doesn't equal the pickled version, and the cmodule cache is not
......
...@@ -326,6 +326,11 @@ class SparseType(gof.Type): ...@@ -326,6 +326,11 @@ class SparseType(gof.Type):
def is_valid_value(self, a): def is_valid_value(self, a):
return scipy.sparse.issparse(a) and (a.format == self.format) 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) # for more dtypes, call SparseType(format, dtype)
def matrix(format, name=None, dtype=None): def matrix(format, name=None, dtype=None):
if dtype is None: if dtype is None:
......
...@@ -910,6 +910,10 @@ class TensorType(Type): ...@@ -910,6 +910,10 @@ class TensorType(Type):
else: else:
return () 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 # Easy constructors
def tensor(*args, **kwargs): def tensor(*args, **kwargs):
...@@ -3307,8 +3311,25 @@ class Subtensor(Op): ...@@ -3307,8 +3311,25 @@ class Subtensor(Op):
{ {
%(fail)s; %(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) for (; outer_ii < %(len_is_slice)s; ++outer_ii)
{ {
...@@ -3425,7 +3446,7 @@ class Subtensor(Op): ...@@ -3425,7 +3446,7 @@ class Subtensor(Op):
@staticmethod @staticmethod
def helper_c_code_cache_version(): def helper_c_code_cache_version():
return (2,) return (3,)
def c_code(self, node, name, inputs, outputs, sub): #DEBUG def c_code(self, node, name, inputs, outputs, sub): #DEBUG
part0 = self.helper_c_code(node, name, inputs, outputs, sub, part0 = self.helper_c_code(node, name, inputs, outputs, sub,
...@@ -3446,6 +3467,10 @@ class Subtensor(Op): ...@@ -3446,6 +3467,10 @@ class Subtensor(Op):
def c_code_cache_version(self): def c_code_cache_version(self):
hv = self.helper_c_code_cache_version() 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) return (1, hv)
def R_op(self, inputs, eval_points): 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): ...@@ -53,6 +53,10 @@ class RandomStateType(gof.Type):
return False return False
return True 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() random_state_type = RandomStateType()
......
...@@ -1957,6 +1957,17 @@ class T_subtensor(unittest.TestCase): ...@@ -1957,6 +1957,17 @@ class T_subtensor(unittest.TestCase):
self.assertTrue(tval.shape == (2,)) self.assertTrue(tval.shape == (2,))
self.assertTrue(numpy.allclose(tval, n.get_value()[idx])) 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): def test1_err_invalid(self):
n = self.shared(numpy.ones(1, dtype=self.dtype)) n = self.shared(numpy.ones(1, dtype=self.dtype))
try: try:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论