提交 d0b42eff authored 作者: Frederic Bastien's avatar Frederic Bastien

GpuElemwise now raise an error when we could broadcast input to make the code…

GpuElemwise now raise an error when we could broadcast input to make the code run but did not set the broadcast flags to True. Added test that we raise this error.
上级 88e9b1c3
...@@ -37,7 +37,7 @@ def get_str_list_logical_scalar(node, value_str='ii_i%i_value', data_str='ii_i%i ...@@ -37,7 +37,7 @@ def get_str_list_logical_scalar(node, value_str='ii_i%i_value', data_str='ii_i%i
class NaiveAlgo(object): class NaiveAlgo(object):
verbose = 0 # 1, 2 or 3 for more verbose output. verbose = 0 # 1, 2 or 3 for more verbose output.
cache_version = () cache_version = ()
cache_version = ('debug', 10, verbose) cache_version = ('debug', 11, verbose)
def __init__(self, scalar_op, sync=True): def __init__(self, scalar_op, sync=True):
""" """
...@@ -834,7 +834,14 @@ nd_collapse_[i]=0; ...@@ -834,7 +834,14 @@ nd_collapse_[i]=0;
""" %locals() """ %locals()
#check that all inputs have valid dimensions #check that all inputs have valid dimensions
for iname in inputs: for id,iname in enumerate(inputs):
broadcasts = ', '.join(map(str,map(int,node.inputs[id].broadcastable)))
nd = node.inputs[id].ndim
print >> sio, """
int broadcasts_%(iname)s[%(nd)s] = {%(broadcasts)s};
""" %locals()
#check that all inputs have valid dimensions
for id,iname in enumerate(inputs):
print >> sio, """ print >> sio, """
//std::cerr << "C_CODE %(opname)s checking input %(iname)s\\n"; //std::cerr << "C_CODE %(opname)s checking input %(iname)s\\n";
if (%(nd)s != %(iname)s->nd) if (%(nd)s != %(iname)s->nd)
...@@ -845,7 +852,7 @@ nd_collapse_[i]=0; ...@@ -845,7 +852,7 @@ nd_collapse_[i]=0;
for (int i = 0; i< %(nd)s; ++i) for (int i = 0; i< %(nd)s; ++i)
{ {
dims[i] = (dims[i] == 1) ? CudaNdarray_HOST_DIMS(%(iname)s)[i] : dims[i]; dims[i] = (dims[i] == 1) ? CudaNdarray_HOST_DIMS(%(iname)s)[i] : dims[i];
if ((CudaNdarray_HOST_DIMS(%(iname)s)[i] != 1) && (dims[i] != CudaNdarray_HOST_DIMS(%(iname)s)[i])) if ((!(broadcasts_%(iname)s[i] && CudaNdarray_HOST_DIMS(%(iname)s)[i] == 1))&& (dims[i] != CudaNdarray_HOST_DIMS(%(iname)s)[i]))
{ {
//std::cerr << "C_CODE %(opname)s checking input %(iname)s failed\\n"; //std::cerr << "C_CODE %(opname)s checking input %(iname)s failed\\n";
PyErr_Format(PyExc_TypeError, "GpuElemwise input has incompatible dim[%%i] == %%i, where output has size %%i", PyErr_Format(PyExc_TypeError, "GpuElemwise input has incompatible dim[%%i] == %%i, where output has size %%i",
......
...@@ -27,6 +27,9 @@ else: ...@@ -27,6 +27,9 @@ else:
mode_with_gpu = theano.compile.mode.get_default_mode().including('gpu') mode_with_gpu = theano.compile.mode.get_default_mode().including('gpu')
mode_without_gpu = theano.compile.mode.get_default_mode().excluding('gpu') mode_without_gpu = theano.compile.mode.get_default_mode().excluding('gpu')
def rand_cuda_ndarray(shape):
return cuda_ndarray.CudaNdarray(theano._asarray(numpy.random.rand(*shape),dtype='float32'))
def tes_use(): def tes_use():
tcn.use() tcn.use()
...@@ -206,6 +209,19 @@ def test_elemwise0(): ...@@ -206,6 +209,19 @@ def test_elemwise0():
assert numpy.all(a0 + 1.0 == a.value) assert numpy.all(a0 + 1.0 == a.value)
def test_elemwise_bad_broadcast():
x = cuda.fmatrix('x')
y = cuda.fmatrix('y')
f = theano.function([x, y], x * y)
import unittest
try:
f(rand_cuda_ndarray((10, 3)), rand_cuda_ndarray((10, 1)))
except ValueError:
pass
else:
raise Exception("Theano should have raised an error")
def test_elemwise1(): def test_elemwise1():
""" Several kinds of elemwise expressions with no broadcasting, non power-of-two shape """ """ Several kinds of elemwise expressions with no broadcasting, non power-of-two shape """
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论