提交 872ead68 authored 作者: lamblin's avatar lamblin

Merge pull request #1025 from nouiz/err_msg_unalign

Err msg unalign
...@@ -707,6 +707,17 @@ class TensorType(Type): ...@@ -707,6 +707,17 @@ class TensorType(Type):
raise TypeError("Wrong number of dimensions: expected %s," raise TypeError("Wrong number of dimensions: expected %s,"
" got %s with shape %s." % (self.ndim, data.ndim, " got %s with shape %s." % (self.ndim, data.ndim,
data.shape), data) data.shape), data)
if not data.flags.aligned:
try:
msg = "object buffer" + str(data.data)
except AttributeError:
msg = ""
raise TypeError("The numpy.ndarray object is not aligned."
" Theano c code do not support that.",
msg,
"object shape", data.shape,
"object strides", data.strides)
i = 0 i = 0
for b in self.broadcastable: for b in self.broadcastable:
if b and data.shape[i] != 1: if b and data.shape[i] != 1:
...@@ -988,8 +999,20 @@ class TensorType(Type): ...@@ -988,8 +999,20 @@ class TensorType(Type):
if (!PyArray_ISALIGNED(py_%(name)s)) { if (!PyArray_ISALIGNED(py_%(name)s)) {
PyErr_Format(PyExc_NotImplementedError, PyErr_Format(PyExc_NotImplementedError,
"expected an aligned array of type %%d " "expected an aligned array of type %%d "
"(%(type_num)s), got non-aligned array of type %%d", "(%(type_num)s), got non-aligned array of type %%d"
%(type_num)s, type_num_%(name)s); " with %%d dimensions, with 2 last dims %%d, %%d"
" and 2 last strides %%d, %%d.",
%(type_num)s, type_num_%(name)s,
PyArray_NDIM(py_%(name)s),
PyArray_NDIM(py_%(name)s) >= 2 ?
PyArray_DIMS(py_%(name)s)[PyArray_NDIM(py_%(name)s)-2] : -1,
PyArray_NDIM(py_%(name)s) >= 1 ?
PyArray_DIMS(py_%(name)s)[PyArray_NDIM(py_%(name)s)-1] : -1,
PyArray_NDIM(py_%(name)s) >= 2 ?
PyArray_STRIDES(py_%(name)s)[PyArray_NDIM(py_%(name)s)-2] : -1,
PyArray_NDIM(py_%(name)s) >= 1 ?
PyArray_STRIDES(py_%(name)s)[PyArray_NDIM(py_%(name)s)-1] : -1
);
%(fail)s %(fail)s
} }
// This is a TypeError to be consistent with DEBUG_MODE // This is a TypeError to be consistent with DEBUG_MODE
...@@ -1043,7 +1066,7 @@ class TensorType(Type): ...@@ -1043,7 +1066,7 @@ class TensorType(Type):
def c_code_cache_version(self): def c_code_cache_version(self):
scalar_version = scal.Scalar(self.dtype).c_code_cache_version() scalar_version = scal.Scalar(self.dtype).c_code_cache_version()
if scalar_version: if scalar_version:
return (5,) + scalar_version return (6,) + scalar_version
else: else:
return () return ()
......
...@@ -6062,21 +6062,33 @@ def test_unalign(): ...@@ -6062,21 +6062,33 @@ def test_unalign():
av, bv = tensor.vectors('ab') av, bv = tensor.vectors('ab')
f = theano.function([av, bv], 2 * av + 3 * bv) f = theano.function([av, bv], 2 * av + 3 * bv)
f.maker.fgraph.toposort() f.maker.fgraph.toposort()
# FAST_COMPILE use the python code that support unaligned data
# The DebugMode make a copy of the inputs, so they will be aligned.
should_raise = (theano.config.mode not in ["FAST_COMPILE", "DebugMode",
"DEBUG_MODE"] and
theano.config.linker != 'py')
try: try:
out_theano = f(a, b) out_theano = f(a, b)
assert not a.flags.aligned assert not a.flags.aligned
assert not b.flags.aligned assert not b.flags.aligned
assert numpy.allclose(out_numpy, out_theano) assert numpy.allclose(out_numpy, out_theano)
if should_raise: assert False
raise Exception("Expected an error from Theano!") except TypeError, e:
except NotImplementedError, e: pass
if not should_raise:
raise Exception("Theano raised an unexpected exception") a = numpy.empty((), dtype=dtype)['f1']
b = numpy.empty((), dtype=dtype)['f1']
assert not a.flags.aligned
assert not b.flags.aligned
out_numpy = 2 * a + 3 * b
av, bv = tensor.scalars('ab')
f = theano.function([av, bv], 2 * av + 3 * bv)
f.maker.fgraph.toposort()
try:
out_theano = f(a, b)
assert not a.flags.aligned
assert not b.flags.aligned
assert numpy.allclose(out_numpy, out_theano)
assert False
except TypeError, e:
pass
def test_dimshuffle_duplicate(): def test_dimshuffle_duplicate():
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论