Skip to content

Commit 853e9f3

Browse files
author
Christian Payer
committed
compatibility to cudnn-v5
1 parent 46261f7 commit 853e9f3

File tree

11 files changed

+116
-7
lines changed

11 files changed

+116
-7
lines changed

include/caffe/layers/cudnn_relu_layer.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class CuDNNReLULayer : public ReLULayer<Dtype> {
3737
cudnnHandle_t handle_;
3838
cudnnTensorDescriptor_t bottom_desc_;
3939
cudnnTensorDescriptor_t top_desc_;
40+
#if CUDNN_VERSION_MIN(5, 0, 0)
41+
cudnnActivationDescriptor_t activation_desc_;
42+
#endif
4043
};
4144
#endif
4245

include/caffe/layers/cudnn_sigmoid_layer.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class CuDNNSigmoidLayer : public SigmoidLayer<Dtype> {
3737
cudnnHandle_t handle_;
3838
cudnnTensorDescriptor_t bottom_desc_;
3939
cudnnTensorDescriptor_t top_desc_;
40+
#if CUDNN_VERSION_MIN(5, 0, 0)
41+
cudnnActivationDescriptor_t activation_desc_;
42+
#endif
4043
};
4144
#endif
4245

include/caffe/layers/cudnn_tanh_layer.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class CuDNNTanHLayer : public TanHLayer<Dtype> {
3737
cudnnHandle_t handle_;
3838
cudnnTensorDescriptor_t bottom_desc_;
3939
cudnnTensorDescriptor_t top_desc_;
40+
#if CUDNN_VERSION_MIN(5, 0, 0)
41+
cudnnActivationDescriptor_t activation_desc_;
42+
#endif
4043
};
4144
#endif
4245

include/caffe/util/cudnn.hpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,20 @@ inline void createFilterDesc(cudnnFilterDescriptor_t* desc,
128128
int n, int c, int h, int w) {
129129
CUDNN_CHECK(cudnnCreateFilterDescriptor(desc));
130130
CUDNN_CHECK(cudnnSetFilter4dDescriptor(*desc, dataType<Dtype>::type,
131-
n, c, h, w));
131+
n, c, h, w));
132132
}
133133

134134
template <typename Dtype>
135135
inline void createNdFilterDesc(cudnnFilterDescriptor_t* desc,
136136
std::vector<int> shape) {
137137
CUDNN_CHECK(cudnnCreateFilterDescriptor(desc));
138+
#if CUDNN_VERSION_MIN(5, 0, 0)
138139
CUDNN_CHECK(cudnnSetFilterNdDescriptor(*desc, dataType<Dtype>::type,
139-
shape.size(), shape.data()));
140+
CUDNN_TENSOR_NCHW, shape.size(), shape.data()));
141+
#else
142+
CUDNN_CHECK(cudnnSetFilterNdDescriptor(*desc, dataType<Dtype>::type,
143+
shape.size(), shape.data()));
144+
#endif
140145
}
141146

142147
template <typename Dtype>
@@ -149,7 +154,7 @@ inline void setConvolutionDesc(cudnnConvolutionDescriptor_t* conv,
149154
cudnnTensorDescriptor_t bottom, cudnnFilterDescriptor_t filter,
150155
int pad_h, int pad_w, int stride_h, int stride_w) {
151156
CUDNN_CHECK(cudnnSetConvolution2dDescriptor(*conv,
152-
pad_h, pad_w, stride_h, stride_w, 1, 1, CUDNN_CROSS_CORRELATION));
157+
pad_h, pad_w, stride_h, stride_w, 1, 1, CUDNN_CROSS_CORRELATION));
153158
}
154159

155160
template <typename Dtype>
@@ -159,16 +164,22 @@ inline void setNdConvolutionDesc(cudnnConvolutionDescriptor_t* conv,
159164
int nbDims;
160165
std::vector<int> shape(pad.size() + 2);
161166
cudnnDataType_t cudnn_type;
167+
#if CUDNN_VERSION_MIN(5, 0, 0)
168+
cudnnTensorFormat_t tensor_format;
169+
cudnnGetFilterNdDescriptor(filter,
170+
shape.size(), &cudnn_type, &tensor_format, &nbDims, shape.data());
171+
#else
162172
cudnnGetFilterNdDescriptor(filter,
163173
shape.size(), &cudnn_type, &nbDims, shape.data());
174+
#endif
164175
CHECK_EQ(nbDims, pad.size() + 2)
165176
<< "Dimensions of filters and pad don't match !";
166177
CHECK_EQ(nbDims, stride.size() + 2)
167178
<< "Dimensions of filters and stride don't match !";
168179
std::vector<int> upscale(pad.size(), 1);
169180
CUDNN_CHECK(cudnnSetConvolutionNdDescriptor(*conv,
170-
pad.size(), pad.data(), stride.data(), upscale.data(),
171-
CUDNN_CROSS_CORRELATION, cudnn_type));
181+
pad.size(), pad.data(), stride.data(), upscale.data(),
182+
CUDNN_CROSS_CORRELATION, cudnn_type));
172183
}
173184

174185
template <typename Dtype>
@@ -186,8 +197,13 @@ inline void createPoolingDesc(cudnnPoolingDescriptor_t* pool_desc,
186197
LOG(FATAL) << "Unknown pooling method.";
187198
}
188199
CUDNN_CHECK(cudnnCreatePoolingDescriptor(pool_desc));
200+
#if CUDNN_VERSION_MIN(5, 0, 0)
201+
CUDNN_CHECK(cudnnSetPooling2dDescriptor(*pool_desc, *mode,
202+
CUDNN_PROPAGATE_NAN, h, w, pad_h, pad_w, stride_h, stride_w));
203+
#else
189204
CUDNN_CHECK(cudnnSetPooling2dDescriptor(*pool_desc, *mode, h, w,
190205
pad_h, pad_w, stride_h, stride_w));
206+
#endif
191207
}
192208

193209
template <typename Dtype>
@@ -210,8 +226,14 @@ inline void createNdPoolingDesc(cudnnPoolingDescriptor_t* pool_desc,
210226
LOG(FATAL) << "Unknown pooling method.";
211227
}
212228
CUDNN_CHECK(cudnnCreatePoolingDescriptor(pool_desc));
229+
#if CUDNN_VERSION_MIN(5, 0, 0)
230+
CUDNN_CHECK(cudnnSetPoolingNdDescriptor(*pool_desc, *mode,
231+
CUDNN_PROPAGATE_NAN, shape.size(), shape.data(), pad.data(),
232+
stride.data()));
233+
#else
213234
CUDNN_CHECK(cudnnSetPoolingNdDescriptor(*pool_desc, *mode, shape.size(),
214235
shape.data(), pad.data(), stride.data()));
236+
#endif
215237
}
216238

217239
} // namespace cudnn

src/caffe/layers/cudnn_conv_layer.cu

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void CuDNNConvolutionLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
8282
// Gradient w.r.t. weights.
8383
if (this->param_propagate_down_[0]) {
8484
const Dtype* bottom_data = bottom[i]->gpu_data();
85-
CUDNN_CHECK(cudnnConvolutionBackwardFilter_v3(
85+
CUDNN_CHECK(cudnnConvolutionBackwardFilter(
8686
handle_[1*this->group_ + g],
8787
cudnn::dataType<Dtype>::one,
8888
bottom_descs_[i], bottom_data + bottom_offset_ * g,
@@ -100,7 +100,7 @@ void CuDNNConvolutionLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
100100
weight = this->blobs_[0]->gpu_data();
101101
}
102102
Dtype* bottom_diff = bottom[i]->mutable_gpu_diff();
103-
CUDNN_CHECK(cudnnConvolutionBackwardData_v3(
103+
CUDNN_CHECK(cudnnConvolutionBackwardData(
104104
handle_[2*this->group_ + g],
105105
cudnn::dataType<Dtype>::one,
106106
filter_desc_, weight + this->weight_offset_ * g,

src/caffe/layers/cudnn_relu_layer.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ void CuDNNReLULayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
1313
CUDNN_CHECK(cudnnCreate(&handle_));
1414
cudnn::createTensorDesc<Dtype>(&bottom_desc_);
1515
cudnn::createTensorDesc<Dtype>(&top_desc_);
16+
#if CUDNN_VERSION_MIN(5, 0, 0)
17+
cudnnCreateActivationDescriptor(&activation_desc_);
18+
cudnnSetActivationDescriptor(activation_desc_, CUDNN_ACTIVATION_RELU, CUDNN_PROPAGATE_NAN, 0);
19+
#endif
1620
handles_setup_ = true;
1721
}
1822

@@ -31,6 +35,9 @@ CuDNNReLULayer<Dtype>::~CuDNNReLULayer() {
3135

3236
cudnnDestroyTensorDescriptor(this->bottom_desc_);
3337
cudnnDestroyTensorDescriptor(this->top_desc_);
38+
#if CUDNN_VERSION_MIN(5, 0, 0)
39+
cudnnDestroyActivationDescriptor(this->activation_desc_);
40+
#endif
3441
cudnnDestroy(this->handle_);
3542
}
3643

src/caffe/layers/cudnn_relu_layer.cu

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,21 @@ void CuDNNReLULayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
1515

1616
const Dtype* bottom_data = bottom[0]->gpu_data();
1717
Dtype* top_data = top[0]->mutable_gpu_data();
18+
#if CUDNN_VERSION_MIN(5, 0, 0)
19+
CUDNN_CHECK(cudnnActivationForward(this->handle_,
20+
this->activation_desc_,
21+
cudnn::dataType<Dtype>::one,
22+
this->bottom_desc_, bottom_data,
23+
cudnn::dataType<Dtype>::zero,
24+
this->top_desc_, top_data));
25+
#else
1826
CUDNN_CHECK(cudnnActivationForward(this->handle_,
1927
CUDNN_ACTIVATION_RELU,
2028
cudnn::dataType<Dtype>::one,
2129
this->bottom_desc_, bottom_data,
2230
cudnn::dataType<Dtype>::zero,
2331
this->top_desc_, top_data));
32+
#endif
2433
}
2534

2635
template <typename Dtype>
@@ -40,13 +49,23 @@ void CuDNNReLULayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
4049
const Dtype* top_diff = top[0]->gpu_diff();
4150
const Dtype* bottom_data = bottom[0]->gpu_data();
4251
Dtype* bottom_diff = bottom[0]->mutable_gpu_diff();
52+
#if CUDNN_VERSION_MIN(5, 0, 0)
53+
CUDNN_CHECK(cudnnActivationBackward(this->handle_,
54+
this->activation_desc_,
55+
cudnn::dataType<Dtype>::one,
56+
this->top_desc_, top_data, this->top_desc_, top_diff,
57+
this->bottom_desc_, bottom_data,
58+
cudnn::dataType<Dtype>::zero,
59+
this->bottom_desc_, bottom_diff));
60+
#else
4361
CUDNN_CHECK(cudnnActivationBackward(this->handle_,
4462
CUDNN_ACTIVATION_RELU,
4563
cudnn::dataType<Dtype>::one,
4664
this->top_desc_, top_data, this->top_desc_, top_diff,
4765
this->bottom_desc_, bottom_data,
4866
cudnn::dataType<Dtype>::zero,
4967
this->bottom_desc_, bottom_diff));
68+
#endif
5069
}
5170

5271
INSTANTIATE_LAYER_GPU_FUNCS(CuDNNReLULayer);

src/caffe/layers/cudnn_sigmoid_layer.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ void CuDNNSigmoidLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
1313
CUDNN_CHECK(cudnnCreate(&handle_));
1414
cudnn::createTensor4dDesc<Dtype>(&bottom_desc_);
1515
cudnn::createTensor4dDesc<Dtype>(&top_desc_);
16+
#if CUDNN_VERSION_MIN(5, 0, 0)
17+
cudnnCreateActivationDescriptor(&activation_desc_);
18+
cudnnSetActivationDescriptor(activation_desc_, CUDNN_ACTIVATION_SIGMOID, CUDNN_PROPAGATE_NAN, 0);
19+
#endif
1620
handles_setup_ = true;
1721
}
1822

@@ -35,6 +39,9 @@ CuDNNSigmoidLayer<Dtype>::~CuDNNSigmoidLayer() {
3539

3640
cudnnDestroyTensorDescriptor(this->bottom_desc_);
3741
cudnnDestroyTensorDescriptor(this->top_desc_);
42+
#if CUDNN_VERSION_MIN(5, 0, 0)
43+
cudnnDestroyActivationDescriptor(this->activation_desc_);
44+
#endif
3845
cudnnDestroy(this->handle_);
3946
}
4047

src/caffe/layers/cudnn_sigmoid_layer.cu

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@ void CuDNNSigmoidLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
1010
const vector<Blob<Dtype>*>& top) {
1111
const Dtype* bottom_data = bottom[0]->gpu_data();
1212
Dtype* top_data = top[0]->mutable_gpu_data();
13+
#if CUDNN_VERSION_MIN(5, 0, 0)
14+
CUDNN_CHECK(cudnnActivationForward(this->handle_,
15+
this->activation_desc_,
16+
cudnn::dataType<Dtype>::one,
17+
this->bottom_desc_, bottom_data,
18+
cudnn::dataType<Dtype>::zero,
19+
this->top_desc_, top_data));
20+
#else
1321
CUDNN_CHECK(cudnnActivationForward(this->handle_,
1422
CUDNN_ACTIVATION_SIGMOID,
1523
cudnn::dataType<Dtype>::one,
1624
this->bottom_desc_, bottom_data,
1725
cudnn::dataType<Dtype>::zero,
1826
this->top_desc_, top_data));
27+
#endif
1928
}
2029

2130
template <typename Dtype>
@@ -30,13 +39,23 @@ void CuDNNSigmoidLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
3039
const Dtype* top_diff = top[0]->gpu_diff();
3140
const Dtype* bottom_data = bottom[0]->gpu_data();
3241
Dtype* bottom_diff = bottom[0]->mutable_gpu_diff();
42+
#if CUDNN_VERSION_MIN(5, 0, 0)
43+
CUDNN_CHECK(cudnnActivationBackward(this->handle_,
44+
this->activation_desc_,
45+
cudnn::dataType<Dtype>::one,
46+
this->top_desc_, top_data, this->top_desc_, top_diff,
47+
this->bottom_desc_, bottom_data,
48+
cudnn::dataType<Dtype>::zero,
49+
this->bottom_desc_, bottom_diff));
50+
#else
3351
CUDNN_CHECK(cudnnActivationBackward(this->handle_,
3452
CUDNN_ACTIVATION_SIGMOID,
3553
cudnn::dataType<Dtype>::one,
3654
this->top_desc_, top_data, this->top_desc_, top_diff,
3755
this->bottom_desc_, bottom_data,
3856
cudnn::dataType<Dtype>::zero,
3957
this->bottom_desc_, bottom_diff));
58+
#endif
4059
}
4160

4261
INSTANTIATE_LAYER_GPU_FUNCS(CuDNNSigmoidLayer);

src/caffe/layers/cudnn_tanh_layer.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ void CuDNNTanHLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
1313
CUDNN_CHECK(cudnnCreate(&handle_));
1414
cudnn::createTensor4dDesc<Dtype>(&bottom_desc_);
1515
cudnn::createTensor4dDesc<Dtype>(&top_desc_);
16+
#if CUDNN_VERSION_MIN(5, 0, 0)
17+
cudnnCreateActivationDescriptor(&activation_desc_);
18+
cudnnSetActivationDescriptor(activation_desc_, CUDNN_ACTIVATION_TANH, CUDNN_PROPAGATE_NAN, 0);
19+
#endif
1620
handles_setup_ = true;
1721
}
1822

@@ -35,6 +39,9 @@ CuDNNTanHLayer<Dtype>::~CuDNNTanHLayer() {
3539

3640
cudnnDestroyTensorDescriptor(this->bottom_desc_);
3741
cudnnDestroyTensorDescriptor(this->top_desc_);
42+
#if CUDNN_VERSION_MIN(5, 0, 0)
43+
cudnnDestroyActivationDescriptor(this->activation_desc_);
44+
#endif
3845
cudnnDestroy(this->handle_);
3946
}
4047

0 commit comments

Comments
 (0)