Skip to content

Commit

Permalink
Merge pull request apache#1742 from antinucleon/master
Browse files Browse the repository at this point in the history
basic cudnn v5
  • Loading branch information
antinucleon committed Mar 30, 2016
2 parents a72e39a + 81988b4 commit 68890c2
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/operator/activation-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class ActivationProp : public OperatorProperty {
CHECK_NE(dtype, -1) << "First input must have specified type";
for (index_t i = 0; i < in_type->size(); ++i) {
if ((*in_type)[i] == -1) {
(*in_type)[i] = dtype;
(*in_type)[i] = dtype;
} else {
CHECK_EQ((*in_type)[i], dtype) << "This layer requires uniform type. "
<< "Expected " << dtype << " v.s. given "
Expand Down
27 changes: 26 additions & 1 deletion src/operator/cudnn_activation-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,21 @@ class CuDNNActivationOp : public Operator {
LOG(FATAL) << "Not implmented";
break;
}
#if CUDNN_MAJOR == 5
nan_prop_ = CUDNN_NOT_PROPAGATE_NAN;
CHECK_EQ(cudnnCreateActivationDescriptor(&desc_),
CUDNN_STATUS_SUCCESS);
CHECK_EQ(cudnnSetActivationDescriptor(desc_, mode_, nan_prop_, relu_ceil_),
CUDNN_STATUS_SUCCESS);
#endif
}

~CuDNNActivationOp() {
if (init_cudnn_) {
CHECK_EQ(cudnnDestroyTensorDescriptor(shape_desc_), CUDNN_STATUS_SUCCESS);
#if CUDNN_MAJOR == 5
CHECK_EQ(cudnnDestroyActivationDescriptor(desc_), CUDNN_STATUS_SUCCESS);
#endif
}
}

Expand Down Expand Up @@ -89,7 +99,12 @@ class CuDNNActivationOp : public Operator {
data.shape_[3]), CUDNN_STATUS_SUCCESS);
}
CHECK_EQ(cudnnActivationForward(s->dnn_handle_,
mode_,
#if CUDNN_MAJOR <= 4
mode_,
#endif
#if CUDNN_MAJOR == 5
desc_,
#endif
&alpha,
shape_desc_,
data.dptr_,
Expand Down Expand Up @@ -145,7 +160,12 @@ class CuDNNActivationOp : public Operator {
}
CHECK_EQ(s->dnn_handle_ownership_, mshadow::Stream<gpu>::OwnHandle);
CHECK_EQ(cudnnActivationBackward(s->dnn_handle_,
#if CUDNN_MAJOR <= 4
mode_,
#endif
#if CUDNN_MAJOR == 5
desc_,
#endif
&alpha,
shape_desc_,
output_data.dptr_,
Expand All @@ -164,6 +184,11 @@ class CuDNNActivationOp : public Operator {
cudnnActivationMode_t mode_;
cudnnTensorDescriptor_t shape_desc_;
ActivationParam param_;
#if CUDNN_MAJOR == 5
cudnnActivationDescriptor_t desc_;
cudnnNanPropagation_t nan_prop_;
double relu_ceil_;
#endif
}; // class CuDNNActivationOp
} // namespace op
} // namespace mxnet
Expand Down
30 changes: 24 additions & 6 deletions src/operator/cudnn_convolution-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ class CuDNNConvolutionOp : public Operator {
if (!init_cudnn_) {
Init(s, in_data, out_data);
}
Tensor<gpu, 1, DType> workspace =
ctx.requested[conv::kTempSpace].get_space_typed<gpu, 1, DType>(
mshadow::Shape1(forward_workspace_), s);
Tensor<gpu, 1> workspace = ctx.requested[conv::kTempSpace].get_space<gpu>(
mshadow::Shape1(forward_workspace_), s);
for (uint32_t g = 0; g < param_.num_group; ++g) {
typename DataType<DType>::ScaleType alpha = 1.0f;
typename DataType<DType>::ScaleType beta = 0.0f;
Expand All @@ -76,7 +75,7 @@ class CuDNNConvolutionOp : public Operator {
if (!param_.no_bias) {
beta = 1.0f;
Tensor<gpu, 1, DType> bias = in_data[conv::kBias].get<gpu, 1, DType>(s);
#if CUDNN_MAJOR == 4
#if CUDNN_MAJOR >= 4
CHECK_EQ(cudnnAddTensor(s->dnn_handle_,
&alpha,
bias_desc_,
Expand Down Expand Up @@ -120,8 +119,8 @@ class CuDNNConvolutionOp : public Operator {
Tensor<gpu, 4, DType> data = in_data[conv::kData].get<gpu, 4, DType>(s);
Tensor<gpu, 4, DType> gdata = in_grad[conv::kData].get<gpu, 4, DType>(s);
Tensor<gpu, 1, DType> workspace =
ctx.requested[conv::kTempSpace].get_space_typed<gpu, 1, DType>(
mshadow::Shape1(backward_workspace_), s);
ctx.requested[conv::kTempSpace].get_space_typed<gpu, 1, DType>(
mshadow::Shape1(backward_workspace_), s);
for (uint32_t g = 0; g < param_.num_group; ++g) {
typename DataType<DType>::ScaleType alpha = 1.0f;
typename DataType<DType>::ScaleType beta = 0.0f;
Expand All @@ -136,7 +135,12 @@ class CuDNNConvolutionOp : public Operator {
gbias.dptr_ + bias_offset_ * g),
CUDNN_STATUS_SUCCESS);
}
#if CUDNN_MAJOR <= 4
CHECK_EQ(cudnnConvolutionBackwardFilter_v3(s->dnn_handle_,
#endif
#if CUDNN_MAJOR == 5
CHECK_EQ(cudnnConvolutionBackwardFilter(s->dnn_handle_,
#endif
&alpha,
in_desc_,
data.dptr_ + data_offset_ * g,
Expand All @@ -149,7 +153,12 @@ class CuDNNConvolutionOp : public Operator {
&beta,
filter_desc_,
gwmat.dptr_ + weight_offset_ * g), CUDNN_STATUS_SUCCESS);
#if CUDNN_MAJOR <= 4
CHECK_EQ(cudnnConvolutionBackwardData_v3(s->dnn_handle_,
#endif
#if CUDNN_MAJOR == 5
CHECK_EQ(cudnnConvolutionBackwardData(s->dnn_handle_,
#endif
&alpha,
filter_desc_,
wmat.dptr_ + weight_offset_ * g,
Expand All @@ -171,6 +180,9 @@ class CuDNNConvolutionOp : public Operator {
const std::vector<TBlob> &out_data) {
using namespace mshadow;
size_t expected = param_.no_bias ? 2 : 3;
#if CUDNN_MAJOR == 5
format_ = CUDNN_TENSOR_NCHW;
#endif
CHECK_EQ(in_data.size(), expected);
CHECK_EQ(out_data.size(), 1);
if (!init_cudnn_) {
Expand All @@ -191,6 +203,9 @@ class CuDNNConvolutionOp : public Operator {
CHECK_EQ(cudnnCreateConvolutionDescriptor(&conv_desc_), CUDNN_STATUS_SUCCESS);
CHECK_EQ(cudnnSetFilter4dDescriptor(filter_desc_,
dtype_,
#if CUDNN_MAJOR == 5
format_,
#endif
param_.num_filter / param_.num_group,
data.shape_[1] / param_.num_group,
param_.kernel[0],
Expand Down Expand Up @@ -304,6 +319,9 @@ class CuDNNConvolutionOp : public Operator {
cudnnConvolutionFwdAlgo_t algo_;
cudnnConvolutionBwdDataAlgo_t back_algo_;
cudnnConvolutionBwdFilterAlgo_t back_algo_w_;
#if CUDNN_MAJOR == 5
cudnnTensorFormat_t format_;
#endif
ConvolutionParam param_;
};
#endif // __CUDACC__ && CUDNN
Expand Down
21 changes: 20 additions & 1 deletion src/operator/cudnn_deconvolution-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ class CuDNNDeconvolutionOp : public Operator {
for (uint32_t g = 0; g < param_.num_group; ++g) {
float alpha = 1.0f;
float beta = 0.0f;
#if CUDNN_MAJOR <= 4
CHECK_EQ(cudnnConvolutionBackwardData_v3(s->dnn_handle_,
#endif
#if CUDNN_MAJOR == 5
CHECK_EQ(cudnnConvolutionBackwardData(s->dnn_handle_,
#endif
&alpha,
filter_desc_,
wmat.dptr_ + weight_offset_ * g,
Expand All @@ -75,7 +80,7 @@ class CuDNNDeconvolutionOp : public Operator {
if (!param_.no_bias) {
beta = 1.0f;
Tensor<gpu, 1> bias = in_data[deconv::kBias].get<gpu, 1, real_t>(s);
#if CUDNN_MAJOR == 4
#if CUDNN_MAJOR >= 4
CHECK_EQ(cudnnAddTensor(s->dnn_handle_,
&alpha,
bias_desc_,
Expand Down Expand Up @@ -134,7 +139,12 @@ class CuDNNDeconvolutionOp : public Operator {
gbias.dptr_ + bias_offset_ * g),
CUDNN_STATUS_SUCCESS);
}
#if CUDNN_MAJOR <= 4
CHECK_EQ(cudnnConvolutionBackwardFilter_v3(s->dnn_handle_,
#endif
#if CUDNN_MAJOR == 5
CHECK_EQ(cudnnConvolutionBackwardFilter(s->dnn_handle_,
#endif
&alpha,
out_desc_,
grad.dptr_ + out_offset_ * g,
Expand Down Expand Up @@ -168,6 +178,9 @@ class CuDNNDeconvolutionOp : public Operator {
const std::vector<TBlob> &in_data,
const std::vector<TBlob> &out_data) {
using namespace mshadow;
#if CUDNN_MAJOR == 5
format_ = CUDNN_TENSOR_NCHW;
#endif
size_t expected = param_.no_bias ? 2 : 3;
CHECK_EQ(in_data.size(), expected);
CHECK_EQ(out_data.size(), 1);
Expand All @@ -189,6 +202,9 @@ class CuDNNDeconvolutionOp : public Operator {
CHECK_EQ(cudnnCreateConvolutionDescriptor(&conv_desc_), CUDNN_STATUS_SUCCESS);
CHECK_EQ(cudnnSetFilter4dDescriptor(filter_desc_,
dtype_,
#if CUDNN_MAJOR == 5
format_,
#endif
data.shape_[1] / param_.num_group,
param_.num_filter / param_.num_group,
param_.kernel[0],
Expand Down Expand Up @@ -302,6 +318,9 @@ class CuDNNDeconvolutionOp : public Operator {
cudnnConvolutionFwdAlgo_t algo_;
cudnnConvolutionBwdDataAlgo_t back_algo_;
cudnnConvolutionBwdFilterAlgo_t back_algo_w_;
#if CUDNN_MAJOR == 5
cudnnTensorFormat_t format_;
#endif
DeconvolutionParam param_;
};
#endif // __CUDACC__ && CUDNN
Expand Down
9 changes: 9 additions & 0 deletions src/operator/cudnn_pooling-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ class CuDNNPoolingOp : public Operator {
const std::vector<TBlob> &in_data,
const std::vector<TBlob> &out_data) {
using namespace mshadow;
#if CUDNN_MAJOR == 5
nan_prop_ = CUDNN_NOT_PROPAGATE_NAN;
#endif
CHECK_EQ(in_data.size(), 1);
CHECK_EQ(out_data.size(), 1);
if (!init_cudnn_) {
Expand All @@ -138,6 +141,9 @@ class CuDNNPoolingOp : public Operator {
out.shape_[3]), CUDNN_STATUS_SUCCESS);
CHECK_EQ(cudnnSetPooling2dDescriptor(pooling_desc_,
mode_,
#if CUDNN_MAJOR == 5
nan_prop_,
#endif
param_.kernel[0],
param_.kernel[1],
param_.pad[0],
Expand All @@ -153,6 +159,9 @@ class CuDNNPoolingOp : public Operator {
cudnnTensorDescriptor_t in_desc_;
cudnnTensorDescriptor_t out_desc_;
cudnnPoolingDescriptor_t pooling_desc_;
#if CUDNN_MAJOR == 5
cudnnNanPropagation_t nan_prop_;
#endif
PoolingParam param_;
}; // class CuDNNPoolingOp
} // namespace op
Expand Down

0 comments on commit 68890c2

Please sign in to comment.