Skip to content

Commit

Permalink
Merge pull request BVLC#122 from thatguymike/v5support
Browse files Browse the repository at this point in the history
Add basic v5 support
  • Loading branch information
lukeyeager committed Mar 12, 2016
2 parents c0c2109 + 2a4fb59 commit 192d349
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 18 deletions.
3 changes: 3 additions & 0 deletions include/caffe/neuron_layers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ class CuDNNReLULayer : public ReLULayer<Dtype> {
bool handles_setup_;
cudnnTensorDescriptor_t bottom_desc_;
cudnnTensorDescriptor_t top_desc_;
cudnnActivationDescriptor_t activ_desc_;
};
#endif

Expand Down Expand Up @@ -581,6 +582,7 @@ class CuDNNSigmoidLayer : public SigmoidLayer<Dtype> {
bool handles_setup_;
cudnnTensorDescriptor_t bottom_desc_;
cudnnTensorDescriptor_t top_desc_;
cudnnActivationDescriptor_t activ_desc_;
};
#endif

Expand Down Expand Up @@ -665,6 +667,7 @@ class CuDNNTanHLayer : public TanHLayer<Dtype> {
bool handles_setup_;
cudnnTensorDescriptor_t bottom_desc_;
cudnnTensorDescriptor_t top_desc_;
cudnnActivationDescriptor_t activ_desc_;
};
#endif

Expand Down
21 changes: 18 additions & 3 deletions include/caffe/util/cudnn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include "caffe/common.hpp"
#include "caffe/proto/caffe.pb.h"

#define CUDNN_VERSION_MIN(major, minor, patch) \
(CUDNN_VERSION >= (major * 1000 + minor * 100 + patch))

#define CUDNN_CHECK(condition) \
do { \
cudnnStatus_t status = condition; \
Expand Down Expand Up @@ -88,8 +91,13 @@ template <typename Dtype>
inline void createFilterDesc(cudnnFilterDescriptor_t* desc,
int n, int c, int h, int w) {
CUDNN_CHECK(cudnnCreateFilterDescriptor(desc));
#if CUDNN_VERSION_MIN(5, 0, 0)
CUDNN_CHECK(cudnnSetFilter4dDescriptor(*desc, dataType<Dtype>::type,
n, c, h, w));
CUDNN_TENSOR_NCHW, n, c, h, w));
#else
CUDNN_CHECK(cudnnSetFilter4dDescriptor_v4(*desc, dataType<Dtype>::type,
CUDNN_TENSOR_NCHW, n, c, h, w));
#endif
}

template <typename Dtype>
Expand Down Expand Up @@ -120,8 +128,15 @@ inline void createPoolingDesc(cudnnPoolingDescriptor_t* pool_desc,
LOG(FATAL) << "Unknown pooling method.";
}
CUDNN_CHECK(cudnnCreatePoolingDescriptor(pool_desc));
CUDNN_CHECK(cudnnSetPooling2dDescriptor(*pool_desc, *mode, h, w,
pad_h, pad_w, stride_h, stride_w));
#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_v4(*pool_desc, *mode,
CUDNN_PROPAGATE_NAN, h, w,
pad_h, pad_w, stride_h, stride_w));
#endif
}

} // namespace cudnn
Expand Down
18 changes: 9 additions & 9 deletions src/caffe/layers/cudnn_conv_layer.cu
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// in presence of <80 characters rule
#define cudnnConvFwd cudnnConvolutionForward
#define cudnnConvBwdBias cudnnConvolutionBackwardBias
#define cudnnConvBwdFilter cudnnConvolutionBackwardFilter_v3
#define cudnnConvBwdData cudnnConvolutionBackwardData_v3
#define cudnnConvBwdFilter cudnnConvolutionBackwardFilter
#define cudnnConvBwdData cudnnConvolutionBackwardData

namespace caffe {

Expand Down Expand Up @@ -58,13 +58,13 @@ namespace caffe {
// Bias.
if (this->bias_term_) {
const Dtype* bias_data = this->blobs_[1]->gpu_data();
CUDNN_CHECK(cudnnAddTensor_v3(Caffe::cudnn_handle(),
cudnn::dataType<Dtype>::one,
bias_desc_,
bias_data + bias_offset_ * g,
cudnn::dataType<Dtype>::one,
top_descs_[i],
top_data + top_offset_ * g));
CUDNN_CHECK(cudnnAddTensor(Caffe::cudnn_handle(),
cudnn::dataType<Dtype>::one,
bias_desc_,
bias_data + bias_offset_ * g,
cudnn::dataType<Dtype>::one,
top_descs_[i],
top_data + top_offset_ * g));
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/caffe/layers/cudnn_relu_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ void CuDNNReLULayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
cudnn::createTensor4dDesc<Dtype>(&bottom_desc_);
cudnn::createTensor4dDesc<Dtype>(&top_desc_);
handles_setup_ = true;
cudnnCreateActivationDescriptor(&activ_desc_);
cudnnSetActivationDescriptor(activ_desc_, CUDNN_ACTIVATION_RELU,
CUDNN_PROPAGATE_NAN, 0.0);
}

template <typename Dtype>
Expand All @@ -34,6 +37,7 @@ CuDNNReLULayer<Dtype>::~CuDNNReLULayer() {
// Check that handles have been setup before destroying.
if (!handles_setup_) { return; }

cudnnDestroyActivationDescriptor(this->activ_desc_);
cudnnDestroyTensorDescriptor(this->bottom_desc_);
cudnnDestroyTensorDescriptor(this->top_desc_);
}
Expand Down
23 changes: 21 additions & 2 deletions src/caffe/layers/cudnn_relu_layer.cu
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@ void CuDNNReLULayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& 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(Caffe::cudnn_handle(),
CUDNN_ACTIVATION_RELU,
activ_desc_,
cudnn::dataType<Dtype>::one,
this->bottom_desc_, bottom_data,
cudnn::dataType<Dtype>::zero,
this->top_desc_, top_data));
#else
CUDNN_CHECK(cudnnActivationForward_v4(Caffe::cudnn_handle(),
activ_desc_,
cudnn::dataType<Dtype>::one,
this->bottom_desc_, bottom_data,
cudnn::dataType<Dtype>::zero,
this->top_desc_, top_data));
#endif
}

template <typename Dtype>
Expand All @@ -42,13 +51,23 @@ void CuDNNReLULayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& 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(Caffe::cudnn_handle(),
CUDNN_ACTIVATION_RELU,
activ_desc_,
cudnn::dataType<Dtype>::one,
this->top_desc_, top_data, this->top_desc_, top_diff,
this->bottom_desc_, bottom_data,
cudnn::dataType<Dtype>::zero,
this->bottom_desc_, bottom_diff));
#else
CUDNN_CHECK(cudnnActivationBackward_v4(Caffe::cudnn_handle(),
activ_desc_,
cudnn::dataType<Dtype>::one,
this->top_desc_, top_data, this->top_desc_, top_diff,
this->bottom_desc_, bottom_data,
cudnn::dataType<Dtype>::zero,
this->bottom_desc_, bottom_diff));
#endif
}

INSTANTIATE_LAYER_GPU_FUNCS(CuDNNReLULayer);
Expand Down
4 changes: 4 additions & 0 deletions src/caffe/layers/cudnn_sigmoid_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ void CuDNNSigmoidLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
// initialize cuDNN
cudnn::createTensor4dDesc<Dtype>(&bottom_desc_);
cudnn::createTensor4dDesc<Dtype>(&top_desc_);
cudnnCreateActivationDescriptor(&activ_desc_);
cudnnSetActivationDescriptor(activ_desc_, CUDNN_ACTIVATION_SIGMOID,
CUDNN_PROPAGATE_NAN, 0.0);
handles_setup_ = true;
}

Expand All @@ -34,6 +37,7 @@ CuDNNSigmoidLayer<Dtype>::~CuDNNSigmoidLayer() {
// Check that handles have been setup before destroying.
if (!handles_setup_) { return; }

cudnnDestroyActivationDescriptor(this->activ_desc_);
cudnnDestroyTensorDescriptor(this->bottom_desc_);
cudnnDestroyTensorDescriptor(this->top_desc_);
}
Expand Down
23 changes: 21 additions & 2 deletions src/caffe/layers/cudnn_sigmoid_layer.cu
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ void CuDNNSigmoidLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& 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(Caffe::cudnn_handle(),
CUDNN_ACTIVATION_SIGMOID,
activ_desc_,
cudnn::dataType<Dtype>::one,
this->bottom_desc_, bottom_data,
cudnn::dataType<Dtype>::zero,
this->top_desc_, top_data));
#else
CUDNN_CHECK(cudnnActivationForward_v4(Caffe::cudnn_handle(),
activ_desc_,
cudnn::dataType<Dtype>::one,
this->bottom_desc_, bottom_data,
cudnn::dataType<Dtype>::zero,
this->top_desc_, top_data));
#endif
}

template <typename Dtype>
Expand All @@ -32,13 +41,23 @@ void CuDNNSigmoidLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& 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(Caffe::cudnn_handle(),
CUDNN_ACTIVATION_SIGMOID,
activ_desc_,
cudnn::dataType<Dtype>::one,
this->top_desc_, top_data, this->top_desc_, top_diff,
this->bottom_desc_, bottom_data,
cudnn::dataType<Dtype>::zero,
this->bottom_desc_, bottom_diff));
#else
CUDNN_CHECK(cudnnActivationBackward_v4(Caffe::cudnn_handle(),
activ_desc_,
cudnn::dataType<Dtype>::one,
this->top_desc_, top_data, this->top_desc_, top_diff,
this->bottom_desc_, bottom_data,
cudnn::dataType<Dtype>::zero,
this->bottom_desc_, bottom_diff));
#endif
}

INSTANTIATE_LAYER_GPU_FUNCS(CuDNNSigmoidLayer);
Expand Down
4 changes: 4 additions & 0 deletions src/caffe/layers/cudnn_tanh_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ void CuDNNTanHLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
// initialize cuDNN
cudnn::createTensor4dDesc<Dtype>(&bottom_desc_);
cudnn::createTensor4dDesc<Dtype>(&top_desc_);
cudnnCreateActivationDescriptor(&activ_desc_);
cudnnSetActivationDescriptor(activ_desc_, CUDNN_ACTIVATION_TANH,
CUDNN_PROPAGATE_NAN, 0.0);
handles_setup_ = true;
}

Expand All @@ -34,6 +37,7 @@ CuDNNTanHLayer<Dtype>::~CuDNNTanHLayer() {
// Check that handles have been setup before destroying.
if (!handles_setup_) { return; }

cudnnDestroyActivationDescriptor(this->activ_desc_);
cudnnDestroyTensorDescriptor(this->bottom_desc_);
cudnnDestroyTensorDescriptor(this->top_desc_);
}
Expand Down
23 changes: 21 additions & 2 deletions src/caffe/layers/cudnn_tanh_layer.cu
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ void CuDNNTanHLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& 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(Caffe::cudnn_handle(),
CUDNN_ACTIVATION_TANH,
activ_desc_,
cudnn::dataType<Dtype>::one,
this->bottom_desc_, bottom_data,
cudnn::dataType<Dtype>::zero,
this->top_desc_, top_data));
#else
CUDNN_CHECK(cudnnActivationForward_v4(Caffe::cudnn_handle(),
activ_desc_,
cudnn::dataType<Dtype>::one,
this->bottom_desc_, bottom_data,
cudnn::dataType<Dtype>::zero,
this->top_desc_, top_data));
#endif
}

template <typename Dtype>
Expand All @@ -33,13 +42,23 @@ void CuDNNTanHLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& 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(Caffe::cudnn_handle(),
CUDNN_ACTIVATION_TANH,
activ_desc_,
cudnn::dataType<Dtype>::one,
this->top_desc_, top_data, this->top_desc_, top_diff,
this->bottom_desc_, bottom_data,
cudnn::dataType<Dtype>::zero,
this->bottom_desc_, bottom_diff));
#else
CUDNN_CHECK(cudnnActivationBackward_v4(Caffe::cudnn_handle(),
activ_desc_,
cudnn::dataType<Dtype>::one,
this->top_desc_, top_data, this->top_desc_, top_diff,
this->bottom_desc_, bottom_data,
cudnn::dataType<Dtype>::zero,
this->bottom_desc_, bottom_diff));
#endif
}

INSTANTIATE_LAYER_GPU_FUNCS(CuDNNTanHLayer);
Expand Down

0 comments on commit 192d349

Please sign in to comment.