def forward_gpu(self, x): if (cuda.cudnn_enabled and self.use_cudnn and pooling_nd._check_cudnn_acceptable_type(x[0].dtype)): # With cuDNN v3 or greater, use cuDNN implementation for inputs # with spatial dimensions of two or more. if _cudnn_version >= 3000 and self.ndim >= 2: return super(MaxPoolingND, self).forward_gpu(x) # With cuDNN v2, use cuDNN implementation only for inputs with # spatial dimensions of two. elif self.ndim == 2: return super(MaxPoolingND, self).forward_gpu(x) n, c = x[0].shape[:2] dims = x[0].shape[2:] ys = tuple( conv_nd.get_conv_outsize(d, k, s, p, self.cover_all) for (d, k, s, p) in six.moves.zip(dims, self.ksize, self.stride, self.pad)) # (n, c, y_1, y_2, ..., y_N) y_shape = (n, c) + ys y = cuda.cupy.empty(y_shape, dtype=x[0].dtype) self.indexes = cuda.cupy.empty(y_shape, dtype=numpy.int32) in_params, out_params, operation, name = \ max_pooling_nd_kernel.MaxPoolingNDKernelForward.generate(self.ndim) cuda.elementwise(in_params, out_params, operation, name)(x[0].reduced_view(), *(dims + ys + self.ksize + self.stride + self.pad + (y, self.indexes))) return y,
def forward_gpu(self, x): if (chainer.should_use_cudnn('>=auto') and pooling_nd._check_cudnn_acceptable_type(x[0].dtype)): # With cuDNN v3 or greater, use cuDNN implementation for inputs # with spatial dimensions of two or more. if _cudnn_version >= 3000 and self.ndim >= 2: return super(AveragePoolingND, self).forward_gpu(x) # With cuDNN v2, use cuDNN implementation only for inputs with # spatial dimensions of two. elif self.ndim == 2: return super(AveragePoolingND, self).forward_gpu(x) self.retain_inputs(()) self._in_shape = x[0].shape self._in_dtype = x[0].dtype n, c = x[0].shape[:2] dims = x[0].shape[2:] ys = tuple(conv_nd.get_conv_outsize(d, k, s, p, cover_all=self.cover_all) for (d, k, s, p) in six.moves.zip( dims, self.ksize, self.stride, self.pad)) # (n, c, y_1, y_2, ..., y_N) y_shape = (n, c) + ys y = cuda.cupy.empty(y_shape, dtype=x[0].dtype) coeff = 1. / functools.reduce(operator.mul, self.ksize) in_params, out_params, operation, name = \ average_pooling_nd_kernel.AveragePoolingNDKernelForward.generate( self.ndim) cuda.elementwise(in_params, out_params, operation, name)( x[0].reduced_view(), *(dims + ys + self.ksize + self.stride + self.pad + (coeff, y))) return y,
def forward_gpu(self, x): if (cuda.cudnn_enabled and self.use_cudnn and pooling_nd._check_cudnn_acceptable_type(x[0].dtype)): # With cuDNN v3 or greater, use cuDNN implementation for inputs # with spatial dimensions of two or more. if _cudnn_version >= 3000 and self.ndim >= 2: return super(MaxPoolingND, self).forward_gpu(x) # With cuDNN v2, use cuDNN implementation only for inputs with # spatial dimensions of two. elif self.ndim == 2: return super(MaxPoolingND, self).forward_gpu(x) n, c = x[0].shape[:2] dims = x[0].shape[2:] ys = tuple(conv_nd.get_conv_outsize(d, k, s, p, self.cover_all) for (d, k, s, p) in six.moves.zip( dims, self.ksize, self.stride, self.pad)) # (n, c, y_1, y_2, ..., y_N) y_shape = (n, c) + ys y = cuda.cupy.empty(y_shape, dtype=x[0].dtype) self.indexes = cuda.cupy.empty(y_shape, dtype=numpy.int32) in_params, out_params, operation, name = \ max_pooling_nd_kernel.MaxPoolingNDKernelForward.generate(self.ndim) cuda.elementwise(in_params, out_params, operation, name)( x[0].reduced_view(), *(dims + ys + self.ksize + self.stride + self.pad + (y, self.indexes))) return y,
def backward_gpu(self, x, gy): if (cuda.cudnn_enabled and self.use_cudnn and pooling_nd._check_cudnn_acceptable_type(x[0].dtype)): # With cuDNN v3 or greater, use cuDNN implementation for inputs # with spatial dimensions of two or more. if _cudnn_version >= 3000 and self.ndim >= 2: return super(AveragePoolingND, self).backward_gpu(x, gy) # With cuDNN v2, use cuDNN implementation only for inputs with # spatial dimensions of two. elif self.ndim == 2: return super(AveragePoolingND, self).backward_gpu(x, gy) n, c = x[0].shape[:2] dims = x[0].shape[2:] ys = gy[0].shape[2:] gx = cuda.cupy.empty_like(x[0]) coeff = 1. / functools.reduce(operator.mul, self.ksize) in_params, out_params, operation, name = \ average_pooling_nd_kernel.AveragePoolingNDKernelBackward.generate( self.ndim) cuda.elementwise(in_params, out_params, operation, name)( gy[0].reduced_view(), *(dims + ys + self.ksize + self.stride + self.pad + (coeff, gx))) return gx,
def backward_gpu(self, x, gy): if (cuda.cudnn_enabled and self.use_cudnn and pooling_nd._check_cudnn_acceptable_type(x[0].dtype)): # With cuDNN v3 or greater, use cuDNN implementation for inputs # with spatial dimensions of two or more. if _cudnn_version >= 3000 and self.ndim >= 2: return super(AveragePoolingND, self).backward_gpu(x, gy) # With cuDNN v2, use cuDNN implementation only for inputs with # spatial dimensions of two. elif self.ndim == 2: return super(AveragePoolingND, self).backward_gpu(x, gy) n, c = x[0].shape[:2] dims = x[0].shape[2:] ys = gy[0].shape[2:] gx = cuda.cupy.empty_like(x[0]) coeff = 1. / functools.reduce(operator.mul, self.ksize) in_params, out_params, operation, name = \ average_pooling_nd_kernel.AveragePoolingNDKernelBackward.generate( self.ndim) cuda.elementwise(in_params, out_params, operation, name)( gy[0].reduced_view(), *(dims + ys + self.ksize + self.stride + self.pad + (coeff, gx))) return gx,