提交 875711f4 authored 作者: Ian Goodfellow's avatar Ian Goodfellow

fix way that gpu view is created

上级 9070d3c3
...@@ -2448,7 +2448,7 @@ class GpuIncSubtensor(tensor.IncSubtensor, GpuOp): ...@@ -2448,7 +2448,7 @@ class GpuIncSubtensor(tensor.IncSubtensor, GpuOp):
""" """
return """(CudaNdarray*) CudaNdarray_Copy(%(x)s)""" % locals() return """(CudaNdarray*) CudaNdarray_Copy(%(x)s)""" % locals()
def make_view_buffer(self, x, view_ndim): def make_view_array(self, x, view_ndim):
""" """
x: a string identifying an array to be viewed x: a string identifying an array to be viewed
view_ndim: a string specifying the number of dimensions view_ndim: a string specifying the number of dimensions
...@@ -2484,6 +2484,33 @@ class GpuIncSubtensor(tensor.IncSubtensor, GpuOp): ...@@ -2484,6 +2484,33 @@ class GpuIncSubtensor(tensor.IncSubtensor, GpuOp):
def define_set_data(self): def define_set_data(self):
return _define_set_data return _define_set_data
def link_view_array(self, x, fail):
return """
if (CudaNdarray_set_device_data(xview, CudaNdarray_DEV_DATA(%(x)s),
(PyObject*) NULL))
{
PyErr_Format(PyExc_RuntimeError,
"GpuSubtensor is not able to set the"
" devdata field of the view");
Py_XDECREF(xview);
%(fail)s;
cnda_mark_dev_structure_dirty(xview);
}""" % locals()
def set_view_base(self, x, fail):
return """
//Set the base only now
if(CudaNdarray_set_device_data(xview, CudaNdarray_DEV_DATA(xview),
%(x)s)){
PyErr_Format(PyExc_RuntimeError,
"GpuSubtensor is not able to set"
" the base of the view array");
Py_XDECREF(xview);
%(fail)s;
}""" % locals()
def c_code_cache_version(self): def c_code_cache_version(self):
# TODO: cooperate with parent class' C code # TODO: cooperate with parent class' C code
return () return ()
......
...@@ -437,7 +437,7 @@ DllExport int CudaNdarray_dimshuffle(CudaNdarray * self, unsigned int len, const ...@@ -437,7 +437,7 @@ DllExport int CudaNdarray_dimshuffle(CudaNdarray * self, unsigned int len, const
DllExport PyObject* DllExport PyObject*
CudaNdarray_TakeFrom(CudaNdarray * self, PyObject *args); CudaNdarray_TakeFrom(CudaNdarray * self, PyObject *args);
static int fprint_CudaNdarray(FILE * fd, const CudaNdarray *self); int fprint_CudaNdarray(FILE * fd, const CudaNdarray *self);
PyObject * CudaNdarray_View(const CudaNdarray * self); PyObject * CudaNdarray_View(const CudaNdarray * self);
......
...@@ -4616,7 +4616,14 @@ class IncSubtensor(Op): ...@@ -4616,7 +4616,14 @@ class IncSubtensor(Op):
} }
""" % locals() """ % locals()
alloc_view_of_z = self.make_view_buffer(z, view_ndim) # IG: Note: this makes a variable called "xview"
# even though it is a view of z.
# I assume this is because IncSubtensor was written
# by copy-pasting Subtensor and in Subtensor you make
# a view of x.
alloc_view_of_z = self.make_view_array(z, view_ndim)
# On GPU, it takes two steps to make a view
link_view_of_z = self.link_view_array(z, fail);
#Make a first view on the output, as we will write into it. #Make a first view on the output, as we will write into it.
build_view = """ build_view = """
...@@ -4627,6 +4634,7 @@ class IncSubtensor(Op): ...@@ -4627,6 +4634,7 @@ class IncSubtensor(Op):
{ {
%(fail)s; %(fail)s;
} }
%(link_view_of_z)s;
""" % locals() """ % locals()
# make xview actually a view of %(z)s # make xview actually a view of %(z)s
get_xview = self.define_set_data() + \ get_xview = self.define_set_data() + \
...@@ -4713,7 +4721,7 @@ class IncSubtensor(Op): ...@@ -4713,7 +4721,7 @@ class IncSubtensor(Op):
return """(PyArrayObject*)PyArray_FromAny(py_%(x)s, NULL, 0, 0, return """(PyArrayObject*)PyArray_FromAny(py_%(x)s, NULL, 0, 0,
NPY_ARRAY_ENSURECOPY, NULL)""" % locals() NPY_ARRAY_ENSURECOPY, NULL)""" % locals()
def make_view_buffer(x, view_ndim): def make_view_array(x, view_ndim):
""" """
x: a string identifying an array to be viewed x: a string identifying an array to be viewed
view_ndim: a string specifying the number of dimensions view_ndim: a string specifying the number of dimensions
...@@ -4754,6 +4762,19 @@ class IncSubtensor(Op): ...@@ -4754,6 +4762,19 @@ class IncSubtensor(Op):
set data argument to the helper C code. """ set data argument to the helper C code. """
return "" return ""
def link_view_array(self, x, fail):
""" Returns code to complete making xview a view of x"""
# On CPU there is nothing to do, make_view_array already did this
return ""
def set_view_base(self, x, fail):
""" Returns code to make xview be a correct view of x,
after helper_c_code is done messing with x"""
# On CPU there is nothing to do
return ""
def infer_shape(self, node, shapes): def infer_shape(self, node, shapes):
return [shapes[0]] return [shapes[0]]
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论