提交 64922a8b authored 作者: Hengjean's avatar Hengjean

Added check inut for cuda and tensor

上级 799e97dd
...@@ -303,51 +303,58 @@ class CudaNdarrayType(Type): ...@@ -303,51 +303,58 @@ class CudaNdarrayType(Type):
} }
//std::cerr << "c_extract " << %(name)s << " nd check passed\\n"; //std::cerr << "c_extract " << %(name)s << " nd check passed\\n";
""" % locals() """ % locals()
for i, b in enumerate(self.broadcastable): if(theano.compile.ops.Shape.check_input):
if b: for i, b in enumerate(self.broadcastable):
print >> sio, """ if b:
if (CudaNdarray_HOST_DIMS(%(name)s)[%(i)s] != 1) print >> sio, """
if (CudaNdarray_HOST_DIMS(%(name)s)[%(i)s] != 1)
{
PyErr_Format(PyExc_RuntimeError,
"c_extract: Some CudaNdarray has dim %%i on broadcastable dimension %%i",
CudaNdarray_HOST_DIMS(%(name)s)[%(i)s], %(i)s);
%(name)s = NULL;
%(fail)s;
}
//std::cerr << "c_extract " << %(name)s << "dim check %(i)s passed\\n";
//std::cerr << "c_extract " << %(name)s << "checking bcast %(i)s <" << %(name)s->str<< ">\\n";
//std::cerr << "c_extract " << %(name)s->str[%(i)s] << "\\n";
if (CudaNdarray_HOST_STRIDES(%(name)s)[%(i)s])
{
//std::cerr << "c_extract bad stride detected...\\n";
PyErr_Format(PyExc_RuntimeError,
"c_extract: Some CudaNdarray has a nonzero stride %%i on a broadcastable dimension %%i",
CudaNdarray_HOST_STRIDES(%(name)s)[%(i)s], %(i)s);
%(name)s = NULL;
%(fail)s;
}
//std::cerr << "c_extract " << %(name)s << "bcast check %(i)s passed\\n";
""" % locals()
print >> sio, """
assert(%(name)s);
Py_INCREF(py_%(name)s);
}
else if (py_%(name)s == Py_None)
{ {
PyErr_Format(PyExc_RuntimeError, PyErr_SetString(PyExc_TypeError,
"c_extract: Some CudaNdarray has dim %%i on broadcastable dimension %%i", "expected a CudaNdarray, not None");
CudaNdarray_HOST_DIMS(%(name)s)[%(i)s], %(i)s);
%(name)s = NULL; %(name)s = NULL;
%(fail)s; %(fail)s;
} }
//std::cerr << "c_extract " << %(name)s << "dim check %(i)s passed\\n"; else
//std::cerr << "c_extract " << %(name)s << "checking bcast %(i)s <" << %(name)s->str<< ">\\n";
//std::cerr << "c_extract " << %(name)s->str[%(i)s] << "\\n";
if (CudaNdarray_HOST_STRIDES(%(name)s)[%(i)s])
{ {
//std::cerr << "c_extract bad stride detected...\\n"; //fprintf(stderr, "FAILING c_extract CNDA object w refcnt %%p %%i\\n", py_%(name)s, (py_%(name)s->ob_refcnt));
PyErr_Format(PyExc_RuntimeError, PyErr_SetString(PyExc_TypeError, "Argument not a CudaNdarray");
"c_extract: Some CudaNdarray has a nonzero stride %%i on a broadcastable dimension %%i",
CudaNdarray_HOST_STRIDES(%(name)s)[%(i)s], %(i)s);
%(name)s = NULL; %(name)s = NULL;
%(fail)s; %(fail)s;
} }
//std::cerr << "c_extract " << %(name)s << "bcast check %(i)s passed\\n"; //std::cerr << "c_extract done " << %(name)s << '\\n';
""" % locals() """ % locals()
print >> sio, """ else:
assert(%(name)s); print >> sio, """
Py_INCREF(py_%(name)s); assert(%(name)s);
} Py_INCREF(py_%(name)s);
else if (py_%(name)s == Py_None) }
{ """ % locals()
PyErr_SetString(PyExc_TypeError,
"expected a CudaNdarray, not None");
%(name)s = NULL;
%(fail)s;
}
else
{
//fprintf(stderr, "FAILING c_extract CNDA object w refcnt %%p %%i\\n", py_%(name)s, (py_%(name)s->ob_refcnt));
PyErr_SetString(PyExc_TypeError, "Argument not a CudaNdarray");
%(name)s = NULL;
%(fail)s;
}
//std::cerr << "c_extract done " << %(name)s << '\\n';
""" % locals()
#print sio.getvalue() #print sio.getvalue()
return sio.getvalue() return sio.getvalue()
......
...@@ -433,55 +433,60 @@ class TensorType(Type): ...@@ -433,55 +433,60 @@ class TensorType(Type):
def c_extract(self, name, sub): def c_extract(self, name, sub):
"""Override `CLinkerType.c_extract` """ """Override `CLinkerType.c_extract` """
return """ if(theano.compile.ops.Shape.check_input):
%(name)s = NULL; check = """
if (py_%(name)s == Py_None) { %(name)s = NULL;
// We can either fail here or set %(name)s to NULL and rely on Ops if (py_%(name)s == Py_None) {
// using tensors to handle the NULL case, but if they fail to do so // We can either fail here or set %(name)s to NULL and rely on Ops
// they'll end up with nasty segfaults, so this is public service. // using tensors to handle the NULL case, but if they fail to do so
PyErr_SetString(PyExc_ValueError, "expected an ndarray, not None"); // they'll end up with nasty segfaults, so this is public service.
%(fail)s PyErr_SetString(PyExc_ValueError, "expected an ndarray, not None");
} %(fail)s
if (!PyArray_Check(py_%(name)s)) { }
PyErr_SetString(PyExc_ValueError, "expected an ndarray"); if (!PyArray_Check(py_%(name)s)) {
%(fail)s PyErr_SetString(PyExc_ValueError, "expected an ndarray");
} %(fail)s
// We expect %(type_num)s }
type_num_%(name)s = PyArray_TYPE((PyArrayObject*) py_%(name)s); // We expect %(type_num)s
if (!PyArray_ISALIGNED((PyArrayObject*) py_%(name)s)) { type_num_%(name)s = PyArray_TYPE((PyArrayObject*) py_%(name)s);
PyArrayObject * tmp = (PyArrayObject*) py_%(name)s; if (!PyArray_ISALIGNED((PyArrayObject*) py_%(name)s)) {
PyErr_Format(PyExc_NotImplementedError, PyArrayObject * tmp = (PyArrayObject*) py_%(name)s;
"expected an aligned array of type %%ld " PyErr_Format(PyExc_NotImplementedError,
"(%(type_num)s), got non-aligned array of type %%ld" "expected an aligned array of type %%ld "
" with %%ld dimensions, with 3 last dims " "(%(type_num)s), got non-aligned array of type %%ld"
"%%ld, %%ld, %%ld" " with %%ld dimensions, with 3 last dims "
" and 3 last strides %%ld %%ld, %%ld.", "%%ld, %%ld, %%ld"
(long int) %(type_num)s, " and 3 last strides %%ld %%ld, %%ld.",
(long int) type_num_%(name)s, (long int) %(type_num)s,
(long int) PyArray_NDIM(tmp), (long int) type_num_%(name)s,
(long int) PyArray_NDIM(tmp) >= 3 ? (long int) PyArray_NDIM(tmp),
PyArray_DIMS(tmp)[PyArray_NDIM(tmp)-3] : -1, (long int) PyArray_NDIM(tmp) >= 3 ?
(long int) PyArray_NDIM(tmp) >= 2 ? PyArray_DIMS(tmp)[PyArray_NDIM(tmp)-3] : -1,
PyArray_DIMS(tmp)[PyArray_NDIM(tmp)-2] : -1, (long int) PyArray_NDIM(tmp) >= 2 ?
(long int) PyArray_NDIM(tmp) >= 1 ? PyArray_DIMS(tmp)[PyArray_NDIM(tmp)-2] : -1,
PyArray_DIMS(tmp)[PyArray_NDIM(tmp)-1] : -1, (long int) PyArray_NDIM(tmp) >= 1 ?
(long int) PyArray_NDIM(tmp) >= 3 ? PyArray_DIMS(tmp)[PyArray_NDIM(tmp)-1] : -1,
PyArray_STRIDES(tmp)[PyArray_NDIM(tmp)-3] : -1, (long int) PyArray_NDIM(tmp) >= 3 ?
(long int) PyArray_NDIM(tmp) >= 2 ? PyArray_STRIDES(tmp)[PyArray_NDIM(tmp)-3] : -1,
PyArray_STRIDES(tmp)[PyArray_NDIM(tmp)-2] : -1, (long int) PyArray_NDIM(tmp) >= 2 ?
(long int) PyArray_NDIM(tmp) >= 1 ? PyArray_STRIDES(tmp)[PyArray_NDIM(tmp)-2] : -1,
PyArray_STRIDES(tmp)[PyArray_NDIM(tmp)-1] : -1 (long int) PyArray_NDIM(tmp) >= 1 ?
); PyArray_STRIDES(tmp)[PyArray_NDIM(tmp)-1] : -1
%(fail)s );
} %(fail)s
// This is a TypeError to be consistent with DEBUG_MODE }
// Note: DEBUG_MODE also tells the name of the container // This is a TypeError to be consistent with DEBUG_MODE
if (type_num_%(name)s != %(type_num)s) { // Note: DEBUG_MODE also tells the name of the container
PyErr_Format(PyExc_TypeError, if (type_num_%(name)s != %(type_num)s) {
"expected type_num %%d (%(type_num)s) got %%d", PyErr_Format(PyExc_TypeError,
%(type_num)s, type_num_%(name)s); "expected type_num %%d (%(type_num)s) got %%d",
%(fail)s %(type_num)s, type_num_%(name)s);
} %(fail)s
}
""" % dict(sub, name=name, type_num=self.dtype_specs()[2])
else:
check = ""
return check + """
%(name)s = (PyArrayObject*)(py_%(name)s); %(name)s = (PyArrayObject*)(py_%(name)s);
Py_XINCREF(%(name)s); Py_XINCREF(%(name)s);
""" % dict(sub, name=name, type_num=self.dtype_specs()[2]) """ % dict(sub, name=name, type_num=self.dtype_specs()[2])
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论