def activation_backward(x, y, gy, mode): x = cupy.ascontiguousarray(x) gy = cupy.ascontiguousarray(gy) gx = cupy.empty_like(x) dtype = 'd' if x.dtype == 'd' else 'f' one = numpy.array(1, dtype=dtype).ctypes zero = numpy.array(0, dtype=dtype).ctypes handle = get_handle() y_mat = _as4darray(y) desc = create_tensor_descriptor(y_mat) if _cudnn_version >= 4000: act_desc = Descriptor(cudnn.createActivationDescriptor(), cudnn.destroyActivationDescriptor) cudnn.setActivationDescriptor( act_desc.value, mode, cudnn.CUDNN_NOT_PROPAGATE_NAN, 0.0) cudnn.activationBackward_v4( handle, act_desc.value, one.data, desc.value, y.data.ptr, desc.value, gy.data.ptr, desc.value, x.data.ptr, zero.data, desc.value, gx.data.ptr) else: cudnn.activationBackward_v3( handle, mode, one.data, desc.value, y.data.ptr, desc.value, gy.data.ptr, desc.value, x.data.ptr, zero.data, desc.value, gx.data.ptr) return gx
def activation_forward(x, mode): x = cupy.ascontiguousarray(x) y = cupy.empty_like(x) dtype = 'd' if x.dtype == 'd' else 'f' one = numpy.array(1, dtype=dtype).ctypes zero = numpy.array(0, dtype=dtype).ctypes handle = get_handle() x_mat = _as4darray(x) desc = create_tensor_descriptor(x_mat) act_desc = Descriptor(cudnn.createActivationDescriptor(), cudnn.destroyActivationDescriptor) cudnn.setActivationDescriptor(act_desc.value, mode, cudnn.CUDNN_NOT_PROPAGATE_NAN, 0.0) cudnn.activationForward_v4(handle, act_desc.value, one.data, desc.value, x_mat.data.ptr, zero.data, desc.value, y.data.ptr) return y