Add checks for grid dimensions and remove dimensions from gradient of inputs op

上级 b79eb604
...@@ -6,10 +6,10 @@ int APPLY_SPECIFIC(dnn_sptf_desc)(PyArrayObject * dims, ...@@ -6,10 +6,10 @@ int APPLY_SPECIFIC(dnn_sptf_desc)(PyArrayObject * dims,
{ {
cudnnStatus_t err; cudnnStatus_t err;
const int nimages = *((int *) PyArray_GETPTR1(dims, 0)); const int nimages = (int) *((npy_int64 *) PyArray_GETPTR1(dims, 0));
const int nchannels = *((int *) PyArray_GETPTR1(dims, 1)); const int nchannels = (int) *((npy_int64 *) PyArray_GETPTR1(dims, 1));
const int height = *((int *) PyArray_GETPTR1(dims, 2)); const int height = (int) *((npy_int64 *) PyArray_GETPTR1(dims, 2));
const int width = *((int *) PyArray_GETPTR1(dims, 3)); const int width = (int) *((npy_int64 *) PyArray_GETPTR1(dims, 3));
if ( nimages == 0 || nchannels == 0 || height == 0 || width == 0 ) if ( nimages == 0 || nchannels == 0 || height == 0 || width == 0 )
{ {
......
...@@ -57,7 +57,6 @@ int ...@@ -57,7 +57,6 @@ int
APPLY_SPECIFIC(dnn_sptf_gi)(PyGpuArrayObject * input, APPLY_SPECIFIC(dnn_sptf_gi)(PyGpuArrayObject * input,
PyGpuArrayObject * theta, PyGpuArrayObject * theta,
PyGpuArrayObject * grid, PyGpuArrayObject * grid,
PyArrayObject * grid_dims,
PyGpuArrayObject * dy, PyGpuArrayObject * dy,
cudnnSpatialTransformerDescriptor_t desc, cudnnSpatialTransformerDescriptor_t desc,
PyGpuArrayObject ** input_grad, PyGpuArrayObject ** input_grad,
......
...@@ -2918,6 +2918,10 @@ class GpuDnnTransformer(DnnBase): ...@@ -2918,6 +2918,10 @@ class GpuDnnTransformer(DnnBase):
# Setup grid dimensions using input from descriptor # Setup grid dimensions using input from descriptor
grid_dims = as_tensor_variable(desc.owner.inputs[0]) grid_dims = as_tensor_variable(desc.owner.inputs[0])
assert grid_dims.dtype in theano.tensor.basic.int_dtypes
assert grid_dims.ndim == 1
# Ensure 64-bit ints are passed to the C code
grid_dims = theano.tensor.basic.cast(grid_dims, 'int64')
output = GpuArrayType(dtype=img.dtype, output = GpuArrayType(dtype=img.dtype,
broadcastable=img.type.ndim * (False,), broadcastable=img.type.ndim * (False,),
...@@ -2952,7 +2956,7 @@ class GpuDnnTransformerGradI(DnnBase): ...@@ -2952,7 +2956,7 @@ class GpuDnnTransformerGradI(DnnBase):
Gradients of inputs of the spatial transformer Gradients of inputs of the spatial transformer
""" """
__props__ = () __props__ = ()
_cop_num_inputs = 6 _cop_num_inputs = 5
_cop_num_outputs = 2 _cop_num_outputs = 2
_f16_ok = True _f16_ok = True
...@@ -2978,9 +2982,6 @@ class GpuDnnTransformerGradI(DnnBase): ...@@ -2978,9 +2982,6 @@ class GpuDnnTransformerGradI(DnnBase):
if img.ndim != grid.ndim: if img.ndim != grid.ndim:
raise TypeError('grid should have the same number of dimensions as img') raise TypeError('grid should have the same number of dimensions as img')
# Setup grid dimensions from descriptor's input
grid_dims = as_tensor_variable(desc.owner.inputs[0])
dy = as_gpuarray_variable(dy, context_name) dy = as_gpuarray_variable(dy, context_name)
if dy.ndim != 4: if dy.ndim != 4:
raise TypeError('dy must have 4 dimensions.') raise TypeError('dy must have 4 dimensions.')
...@@ -2988,26 +2989,25 @@ class GpuDnnTransformerGradI(DnnBase): ...@@ -2988,26 +2989,25 @@ class GpuDnnTransformerGradI(DnnBase):
dimg = img.type() dimg = img.type()
dgrid = grid.type() dgrid = grid.type()
inputs = [img, theta, grid, grid_dims, dy, desc] inputs = [img, theta, grid, dy, desc]
outputs = [dimg, dgrid] outputs = [dimg, dgrid]
return Apply(self, inputs, outputs) return Apply(self, inputs, outputs)
def L_op(self, inputs, outputs, grads): def L_op(self, inputs, outputs, grads):
img, theta, grid, grid_dims, dy, desc = inputs img, theta, grid, dy, desc = inputs
dimg_out, dgrid = outputs dimg_out, dgrid = outputs
grad_cost = grads[0] grad_cost = grads[0]
dimg = dimg_out * grad_cost dimg = dimg_out * grad_cost
dtheta = GpuDnnTransformerGradT()(dgrid, desc) dtheta = GpuDnnTransformerGradT()(dgrid, desc)
dgrid_dims = grad_not_implemented(self, grid_dims, 3) d_dy = grad_not_implemented(self, dy, 3)
d_dy = grad_not_implemented(self, dy, 4)
return [dimg, dtheta, dgrid, dgrid_dims, d_dy, DisconnectedType()()] return [dimg, dtheta, dgrid, d_dy, DisconnectedType()()]
def connection_pattern(self, node): def connection_pattern(self, node):
# not connected to desc # not connected to desc
return [[1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [0, 0]] return [[1, 1], [1, 1], [1, 1], [1, 1], [0, 0]]
class GpuDnnTransformerGradT(DnnBase): class GpuDnnTransformerGradT(DnnBase):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论