From 60280229c45494feb39cf4c021f47358a2c9a39c Mon Sep 17 00:00:00 2001 From: Christian Payer Date: Tue, 3 May 2016 11:27:40 +0200 Subject: [PATCH] compatibility to cudnn-v5 --- include/caffe/layers/cudnn_relu_layer.hpp | 3 ++ include/caffe/layers/cudnn_sigmoid_layer.hpp | 3 ++ include/caffe/layers/cudnn_tanh_layer.hpp | 3 ++ include/caffe/util/cudnn.hpp | 32 +++++++++++++++++--- src/caffe/layers/cudnn_conv_layer.cu | 4 +-- src/caffe/layers/cudnn_relu_layer.cpp | 8 +++++ src/caffe/layers/cudnn_relu_layer.cu | 19 ++++++++++++ src/caffe/layers/cudnn_sigmoid_layer.cpp | 8 +++++ src/caffe/layers/cudnn_sigmoid_layer.cu | 19 ++++++++++++ src/caffe/layers/cudnn_tanh_layer.cpp | 8 +++++ src/caffe/layers/cudnn_tanh_layer.cu | 19 ++++++++++++ 11 files changed, 119 insertions(+), 7 deletions(-) diff --git a/include/caffe/layers/cudnn_relu_layer.hpp b/include/caffe/layers/cudnn_relu_layer.hpp index e01f568abc9..9dc49ca1570 100644 --- a/include/caffe/layers/cudnn_relu_layer.hpp +++ b/include/caffe/layers/cudnn_relu_layer.hpp @@ -37,6 +37,9 @@ class CuDNNReLULayer : public ReLULayer { cudnnHandle_t handle_; cudnnTensorDescriptor_t bottom_desc_; cudnnTensorDescriptor_t top_desc_; +#if CUDNN_VERSION_MIN(5, 0, 0) + cudnnActivationDescriptor_t activation_desc_; +#endif }; #endif diff --git a/include/caffe/layers/cudnn_sigmoid_layer.hpp b/include/caffe/layers/cudnn_sigmoid_layer.hpp index 9c597958b0b..b4934f0e887 100644 --- a/include/caffe/layers/cudnn_sigmoid_layer.hpp +++ b/include/caffe/layers/cudnn_sigmoid_layer.hpp @@ -37,6 +37,9 @@ class CuDNNSigmoidLayer : public SigmoidLayer { cudnnHandle_t handle_; cudnnTensorDescriptor_t bottom_desc_; cudnnTensorDescriptor_t top_desc_; +#if CUDNN_VERSION_MIN(5, 0, 0) + cudnnActivationDescriptor_t activation_desc_; +#endif }; #endif diff --git a/include/caffe/layers/cudnn_tanh_layer.hpp b/include/caffe/layers/cudnn_tanh_layer.hpp index c0f0053f71e..1f0e4ed3dbe 100644 --- a/include/caffe/layers/cudnn_tanh_layer.hpp +++ b/include/caffe/layers/cudnn_tanh_layer.hpp @@ -37,6 +37,9 @@ class CuDNNTanHLayer : public TanHLayer { cudnnHandle_t handle_; cudnnTensorDescriptor_t bottom_desc_; cudnnTensorDescriptor_t top_desc_; +#if CUDNN_VERSION_MIN(5, 0, 0) + cudnnActivationDescriptor_t activation_desc_; +#endif }; #endif diff --git a/include/caffe/util/cudnn.hpp b/include/caffe/util/cudnn.hpp index 18fc3930121..999c77da8c6 100644 --- a/include/caffe/util/cudnn.hpp +++ b/include/caffe/util/cudnn.hpp @@ -128,15 +128,20 @@ inline void createFilterDesc(cudnnFilterDescriptor_t* desc, int n, int c, int h, int w) { CUDNN_CHECK(cudnnCreateFilterDescriptor(desc)); CUDNN_CHECK(cudnnSetFilter4dDescriptor(*desc, dataType::type, - n, c, h, w)); + n, c, h, w)); } template inline void createNdFilterDesc(cudnnFilterDescriptor_t* desc, std::vector shape) { CUDNN_CHECK(cudnnCreateFilterDescriptor(desc)); +#if CUDNN_VERSION_MIN(5, 0, 0) CUDNN_CHECK(cudnnSetFilterNdDescriptor(*desc, dataType::type, - shape.size(), shape.data())); + CUDNN_TENSOR_NCHW, shape.size(), shape.data())); +#else + CUDNN_CHECK(cudnnSetFilterNdDescriptor(*desc, dataType::type, + shape.size(), shape.data())); +#endif } template @@ -149,7 +154,7 @@ inline void setConvolutionDesc(cudnnConvolutionDescriptor_t* conv, cudnnTensorDescriptor_t bottom, cudnnFilterDescriptor_t filter, int pad_h, int pad_w, int stride_h, int stride_w) { CUDNN_CHECK(cudnnSetConvolution2dDescriptor(*conv, - pad_h, pad_w, stride_h, stride_w, 1, 1, CUDNN_CROSS_CORRELATION)); + pad_h, pad_w, stride_h, stride_w, 1, 1, CUDNN_CROSS_CORRELATION)); } template @@ -159,16 +164,22 @@ inline void setNdConvolutionDesc(cudnnConvolutionDescriptor_t* conv, int nbDims; std::vector shape(pad.size() + 2); cudnnDataType_t cudnn_type; +#if CUDNN_VERSION_MIN(5, 0, 0) + cudnnTensorFormat_t tensor_format; + cudnnGetFilterNdDescriptor(filter, + shape.size(), &cudnn_type, &tensor_format, &nbDims, shape.data()); +#else cudnnGetFilterNdDescriptor(filter, shape.size(), &cudnn_type, &nbDims, shape.data()); +#endif CHECK_EQ(nbDims, pad.size() + 2) << "Dimensions of filters and pad don't match !"; CHECK_EQ(nbDims, stride.size() + 2) << "Dimensions of filters and stride don't match !"; std::vector upscale(pad.size(), 1); CUDNN_CHECK(cudnnSetConvolutionNdDescriptor(*conv, - pad.size(), pad.data(), stride.data(), upscale.data(), - CUDNN_CROSS_CORRELATION, cudnn_type)); + pad.size(), pad.data(), stride.data(), upscale.data(), + CUDNN_CROSS_CORRELATION, cudnn_type)); } template @@ -186,8 +197,13 @@ inline void createPoolingDesc(cudnnPoolingDescriptor_t* pool_desc, LOG(FATAL) << "Unknown pooling method."; } CUDNN_CHECK(cudnnCreatePoolingDescriptor(pool_desc)); +#if CUDNN_VERSION_MIN(5, 0, 0) + CUDNN_CHECK(cudnnSetPooling2dDescriptor(*pool_desc, *mode, + CUDNN_PROPAGATE_NAN, h, w, pad_h, pad_w, stride_h, stride_w)); +#else CUDNN_CHECK(cudnnSetPooling2dDescriptor(*pool_desc, *mode, h, w, pad_h, pad_w, stride_h, stride_w)); +#endif } template @@ -210,8 +226,14 @@ inline void createNdPoolingDesc(cudnnPoolingDescriptor_t* pool_desc, LOG(FATAL) << "Unknown pooling method."; } CUDNN_CHECK(cudnnCreatePoolingDescriptor(pool_desc)); +#if CUDNN_VERSION_MIN(5, 0, 0) + CUDNN_CHECK(cudnnSetPoolingNdDescriptor(*pool_desc, *mode, + CUDNN_PROPAGATE_NAN, shape.size(), shape.data(), pad.data(), + stride.data())); +#else CUDNN_CHECK(cudnnSetPoolingNdDescriptor(*pool_desc, *mode, shape.size(), shape.data(), pad.data(), stride.data())); +#endif } } // namespace cudnn diff --git a/src/caffe/layers/cudnn_conv_layer.cu b/src/caffe/layers/cudnn_conv_layer.cu index 42c4fd0260c..481e079c93f 100644 --- a/src/caffe/layers/cudnn_conv_layer.cu +++ b/src/caffe/layers/cudnn_conv_layer.cu @@ -82,7 +82,7 @@ void CuDNNConvolutionLayer::Backward_gpu(const vector*>& top, // Gradient w.r.t. weights. if (this->param_propagate_down_[0]) { const Dtype* bottom_data = bottom[i]->gpu_data(); - CUDNN_CHECK(cudnnConvolutionBackwardFilter_v3( + CUDNN_CHECK(cudnnConvolutionBackwardFilter( handle_[1*this->group_ + g], cudnn::dataType::one, bottom_descs_[i], bottom_data + bottom_offset_ * g, @@ -100,7 +100,7 @@ void CuDNNConvolutionLayer::Backward_gpu(const vector*>& top, weight = this->blobs_[0]->gpu_data(); } Dtype* bottom_diff = bottom[i]->mutable_gpu_diff(); - CUDNN_CHECK(cudnnConvolutionBackwardData_v3( + CUDNN_CHECK(cudnnConvolutionBackwardData( handle_[2*this->group_ + g], cudnn::dataType::one, filter_desc_, weight + this->weight_offset_ * g, diff --git a/src/caffe/layers/cudnn_relu_layer.cpp b/src/caffe/layers/cudnn_relu_layer.cpp index b48ad4e6371..c795d34f10c 100644 --- a/src/caffe/layers/cudnn_relu_layer.cpp +++ b/src/caffe/layers/cudnn_relu_layer.cpp @@ -13,6 +13,11 @@ void CuDNNReLULayer::LayerSetUp(const vector*>& bottom, CUDNN_CHECK(cudnnCreate(&handle_)); cudnn::createTensorDesc(&bottom_desc_); cudnn::createTensorDesc(&top_desc_); +#if CUDNN_VERSION_MIN(5, 0, 0) + cudnnCreateActivationDescriptor(&activation_desc_); + cudnnSetActivationDescriptor(activation_desc_, + CUDNN_ACTIVATION_RELU, CUDNN_PROPAGATE_NAN, 0); +#endif handles_setup_ = true; } @@ -31,6 +36,9 @@ CuDNNReLULayer::~CuDNNReLULayer() { cudnnDestroyTensorDescriptor(this->bottom_desc_); cudnnDestroyTensorDescriptor(this->top_desc_); +#if CUDNN_VERSION_MIN(5, 0, 0) + cudnnDestroyActivationDescriptor(this->activation_desc_); +#endif cudnnDestroy(this->handle_); } diff --git a/src/caffe/layers/cudnn_relu_layer.cu b/src/caffe/layers/cudnn_relu_layer.cu index 9f617183baa..834831876ca 100644 --- a/src/caffe/layers/cudnn_relu_layer.cu +++ b/src/caffe/layers/cudnn_relu_layer.cu @@ -15,12 +15,21 @@ void CuDNNReLULayer::Forward_gpu(const vector*>& bottom, const Dtype* bottom_data = bottom[0]->gpu_data(); Dtype* top_data = top[0]->mutable_gpu_data(); +#if CUDNN_VERSION_MIN(5, 0, 0) + CUDNN_CHECK(cudnnActivationForward(this->handle_, + this->activation_desc_, + cudnn::dataType::one, + this->bottom_desc_, bottom_data, + cudnn::dataType::zero, + this->top_desc_, top_data)); +#else CUDNN_CHECK(cudnnActivationForward(this->handle_, CUDNN_ACTIVATION_RELU, cudnn::dataType::one, this->bottom_desc_, bottom_data, cudnn::dataType::zero, this->top_desc_, top_data)); +#endif } template @@ -40,6 +49,15 @@ void CuDNNReLULayer::Backward_gpu(const vector*>& top, const Dtype* top_diff = top[0]->gpu_diff(); const Dtype* bottom_data = bottom[0]->gpu_data(); Dtype* bottom_diff = bottom[0]->mutable_gpu_diff(); +#if CUDNN_VERSION_MIN(5, 0, 0) + CUDNN_CHECK(cudnnActivationBackward(this->handle_, + this->activation_desc_, + cudnn::dataType::one, + this->top_desc_, top_data, this->top_desc_, top_diff, + this->bottom_desc_, bottom_data, + cudnn::dataType::zero, + this->bottom_desc_, bottom_diff)); +#else CUDNN_CHECK(cudnnActivationBackward(this->handle_, CUDNN_ACTIVATION_RELU, cudnn::dataType::one, @@ -47,6 +65,7 @@ void CuDNNReLULayer::Backward_gpu(const vector*>& top, this->bottom_desc_, bottom_data, cudnn::dataType::zero, this->bottom_desc_, bottom_diff)); +#endif } INSTANTIATE_LAYER_GPU_FUNCS(CuDNNReLULayer); diff --git a/src/caffe/layers/cudnn_sigmoid_layer.cpp b/src/caffe/layers/cudnn_sigmoid_layer.cpp index ccb955cdaff..e7c2507aa22 100644 --- a/src/caffe/layers/cudnn_sigmoid_layer.cpp +++ b/src/caffe/layers/cudnn_sigmoid_layer.cpp @@ -13,6 +13,11 @@ void CuDNNSigmoidLayer::LayerSetUp(const vector*>& bottom, CUDNN_CHECK(cudnnCreate(&handle_)); cudnn::createTensor4dDesc(&bottom_desc_); cudnn::createTensor4dDesc(&top_desc_); +#if CUDNN_VERSION_MIN(5, 0, 0) + cudnnCreateActivationDescriptor(&activation_desc_); + cudnnSetActivationDescriptor(activation_desc_, + CUDNN_ACTIVATION_SIGMOID, CUDNN_PROPAGATE_NAN, 0); +#endif handles_setup_ = true; } @@ -35,6 +40,9 @@ CuDNNSigmoidLayer::~CuDNNSigmoidLayer() { cudnnDestroyTensorDescriptor(this->bottom_desc_); cudnnDestroyTensorDescriptor(this->top_desc_); +#if CUDNN_VERSION_MIN(5, 0, 0) + cudnnDestroyActivationDescriptor(this->activation_desc_); +#endif cudnnDestroy(this->handle_); } diff --git a/src/caffe/layers/cudnn_sigmoid_layer.cu b/src/caffe/layers/cudnn_sigmoid_layer.cu index e2a4b460c6c..e45e996b41a 100644 --- a/src/caffe/layers/cudnn_sigmoid_layer.cu +++ b/src/caffe/layers/cudnn_sigmoid_layer.cu @@ -10,12 +10,21 @@ void CuDNNSigmoidLayer::Forward_gpu(const vector*>& bottom, const vector*>& top) { const Dtype* bottom_data = bottom[0]->gpu_data(); Dtype* top_data = top[0]->mutable_gpu_data(); +#if CUDNN_VERSION_MIN(5, 0, 0) + CUDNN_CHECK(cudnnActivationForward(this->handle_, + this->activation_desc_, + cudnn::dataType::one, + this->bottom_desc_, bottom_data, + cudnn::dataType::zero, + this->top_desc_, top_data)); +#else CUDNN_CHECK(cudnnActivationForward(this->handle_, CUDNN_ACTIVATION_SIGMOID, cudnn::dataType::one, this->bottom_desc_, bottom_data, cudnn::dataType::zero, this->top_desc_, top_data)); +#endif } template @@ -30,6 +39,15 @@ void CuDNNSigmoidLayer::Backward_gpu(const vector*>& top, const Dtype* top_diff = top[0]->gpu_diff(); const Dtype* bottom_data = bottom[0]->gpu_data(); Dtype* bottom_diff = bottom[0]->mutable_gpu_diff(); +#if CUDNN_VERSION_MIN(5, 0, 0) + CUDNN_CHECK(cudnnActivationBackward(this->handle_, + this->activation_desc_, + cudnn::dataType::one, + this->top_desc_, top_data, this->top_desc_, top_diff, + this->bottom_desc_, bottom_data, + cudnn::dataType::zero, + this->bottom_desc_, bottom_diff)); +#else CUDNN_CHECK(cudnnActivationBackward(this->handle_, CUDNN_ACTIVATION_SIGMOID, cudnn::dataType::one, @@ -37,6 +55,7 @@ void CuDNNSigmoidLayer::Backward_gpu(const vector*>& top, this->bottom_desc_, bottom_data, cudnn::dataType::zero, this->bottom_desc_, bottom_diff)); +#endif } INSTANTIATE_LAYER_GPU_FUNCS(CuDNNSigmoidLayer); diff --git a/src/caffe/layers/cudnn_tanh_layer.cpp b/src/caffe/layers/cudnn_tanh_layer.cpp index 1a56418227c..a30323bf0bd 100644 --- a/src/caffe/layers/cudnn_tanh_layer.cpp +++ b/src/caffe/layers/cudnn_tanh_layer.cpp @@ -13,6 +13,11 @@ void CuDNNTanHLayer::LayerSetUp(const vector*>& bottom, CUDNN_CHECK(cudnnCreate(&handle_)); cudnn::createTensor4dDesc(&bottom_desc_); cudnn::createTensor4dDesc(&top_desc_); +#if CUDNN_VERSION_MIN(5, 0, 0) + cudnnCreateActivationDescriptor(&activation_desc_); + cudnnSetActivationDescriptor(activation_desc_, + CUDNN_ACTIVATION_TANH, CUDNN_PROPAGATE_NAN, 0); +#endif handles_setup_ = true; } @@ -35,6 +40,9 @@ CuDNNTanHLayer::~CuDNNTanHLayer() { cudnnDestroyTensorDescriptor(this->bottom_desc_); cudnnDestroyTensorDescriptor(this->top_desc_); +#if CUDNN_VERSION_MIN(5, 0, 0) + cudnnDestroyActivationDescriptor(this->activation_desc_); +#endif cudnnDestroy(this->handle_); } diff --git a/src/caffe/layers/cudnn_tanh_layer.cu b/src/caffe/layers/cudnn_tanh_layer.cu index 89df28a3e8b..de3ddc19eab 100644 --- a/src/caffe/layers/cudnn_tanh_layer.cu +++ b/src/caffe/layers/cudnn_tanh_layer.cu @@ -10,12 +10,21 @@ void CuDNNTanHLayer::Forward_gpu(const vector*>& bottom, const vector*>& top) { const Dtype* bottom_data = bottom[0]->gpu_data(); Dtype* top_data = top[0]->mutable_gpu_data(); +#if CUDNN_VERSION_MIN(5, 0, 0) + CUDNN_CHECK(cudnnActivationForward(this->handle_, + this->activation_desc_, + cudnn::dataType::one, + this->bottom_desc_, bottom_data, + cudnn::dataType::zero, + this->top_desc_, top_data)); +#else CUDNN_CHECK(cudnnActivationForward(this->handle_, CUDNN_ACTIVATION_TANH, cudnn::dataType::one, this->bottom_desc_, bottom_data, cudnn::dataType::zero, this->top_desc_, top_data)); +#endif } template @@ -31,6 +40,15 @@ void CuDNNTanHLayer::Backward_gpu(const vector*>& top, const Dtype* bottom_data = bottom[0]->gpu_data(); Dtype* bottom_diff = bottom[0]->mutable_gpu_diff(); +#if CUDNN_VERSION_MIN(5, 0, 0) + CUDNN_CHECK(cudnnActivationBackward(this->handle_, + this->activation_desc_, + cudnn::dataType::one, + this->top_desc_, top_data, this->top_desc_, top_diff, + this->bottom_desc_, bottom_data, + cudnn::dataType::zero, + this->bottom_desc_, bottom_diff)); +#else CUDNN_CHECK(cudnnActivationBackward(this->handle_, CUDNN_ACTIVATION_TANH, cudnn::dataType::one, @@ -38,6 +56,7 @@ void CuDNNTanHLayer::Backward_gpu(const vector*>& top, this->bottom_desc_, bottom_data, cudnn::dataType::zero, this->bottom_desc_, bottom_diff)); +#endif } INSTANTIATE_LAYER_GPU_FUNCS(CuDNNTanHLayer);