Package: cudnn-python-wrappers
Python wrappers for the NVIDIA cuDNN libraries.This is a set of minimal Python wrappers for theNVIDIA
cuDNNlibrary of convolutional
neural network primitives. NVIDIA cuDNN is available free of charge, but
requires an NVIDIA developer account to download. Users should follow
the cuDNN API documentation to use these wrappers, as they faithfully
replicate the cuDNN C API.These wrappers expose the full cuDNN API as Python functions, but are
minimalistic in that they don’t implement any higher order
functionality, such as operating directly on data structures like
PyCUDAGPUArrayor cudamatCUDAMatrix. Since the interface
faithfully replicates the C API, the user is responsible for
allocating and deallocating handles to all cuDNN data structures and
passing references to arrays as pointers. However, cuDNN status codes
are translated to Python exceptions. The most common application for
these wrappers will be to be used alongPyCUDA, but they will work
equally well with other frameworks such asCUDAMat.Users need to make sure that they pass all arguments as the correct data
type, that isctypes.c_void_pfor all handles and array pointers andctypes.c_intfor all integer arguments and enums. Here is an example
on how to perform forward convolution on a PyCUDAGPUArray:importpycuda.autoinitfrompycudaimportgpuarrayimportlibcudnn,ctypesimportnumpyasnp# Create a cuDNN contextcudnn_context=libcudnn.cudnnCreate()# Set some options and tensor dimensionsaccumulate=libcudnn.cudnnAccumulateResults['CUDNN_RESULT_NO_ACCUMULATE']tensor_format=libcudnn.cudnnTensorFormat['CUDNN_TENSOR_NCHW']data_type=libcudnn.cudnnDataType['CUDNN_DATA_FLOAT']convolution_mode=libcudnn.cudnnConvolutionMode['CUDNN_CROSS_CORRELATION']convolution_path=libcudnn.cudnnConvolutionPath['CUDNN_CONVOLUTION_FORWARD']n_input=100filters_in=10filters_out=8height_in=20width_in=20height_filter=5width_filter=5pad_h=4pad_w=4vertical_stride=1horizontal_stride=1upscalex=1upscaley=1# Input tensorX=gpuarray.to_gpu(np.random.rand(n_input,filters_in,height_in,width_in).astype(np.float32))# Filter tensorfilters=gpuarray.to_gpu(np.random.rand(filters_out,filters_in,height_filter,width_filter).astype(np.float32))#Descriptor for inputX_desc=libcudnn.cudnnCreateTensor4dDescriptor()libcudnn.cudnnSetTensor4dDescriptor(X_desc,tensor_format,data_type,n_input,filters_in,height_in,width_in)# Filter descriptorfilters_desc=libcudnn.cudnnCreateFilterDescriptor()libcudnn.cudnnSetFilterDescriptor(filters_desc,data_type,filters_out,filters_in,height_filter,width_filter)# Convolution descriptorconv_desc=libcudnn.cudnnCreateConvolutionDescriptor()libcudnn.cudnnSetConvolutionDescriptor(conv_desc,X_desc,filters_desc,pad_h,pad_w,vertical_stride,horizontal_stride,upscalex,upscaley,convolution_mode)# Get output dimensions (first two values are n_input and filters_out)_,_,height_output,width_output=libcudnn.cudnnGetOutputTensor4dDim(conv_desc,convolution_path)# Output tensorY=gpuarray.empty((n_input,filters_out,height_output,width_output),np.float32)Y_desc=libcudnn.cudnnCreateTensor4dDescriptor()libcudnn.cudnnSetTensor4dDescriptor(Y_desc,tensor_format,data_type,n_input,filters_out,height_output,width_output)# Get pointers to GPU memoryX_data=ctypes.c_void_p(int(X.gpudata))filters_data=ctypes.c_void_p(int(filters.gpudata))Y_data=ctypes.c_void_p(int(Y.gpudata))# Perform convolutionlibcudnn.cudnnConvolutionForward(cudnn_context,X_desc,X_data,filters_desc,filters_data,conv_desc,Y_desc,Y_data,accumulate)# Clean uplibcudnn.cudnnDestroyTensor4dDescriptor(X_desc)libcudnn.cudnnDestroyTensor4dDescriptor(Y_desc)libcudnn.cudnnDestroyFilterDescriptor(filters_desc)libcudnn.cudnnDestroyConvolutionDescriptor(conv_desc)libcudnn.cudnnDestroy(cudnn_context)InstallationInstall from PyPi withpip install cudnn-python-wrappers