Dragon - C++ API
A Computation Graph Virtual Machine Based Deep Learning Framework
conv_op.h
Go to the documentation of this file.
1 
13 #ifndef DRAGON_OPERATORS_VISION_CONV_OP_H_
14 #define DRAGON_OPERATORS_VISION_CONV_OP_H_
15 
17 
18 namespace dragon {
19 
20 template <class Context>
21 class Conv2dOp : public ConvOpBase<Context> {
22  public:
23  Conv2dOp(const OperatorDef& def, Workspace* ws)
24  : ConvOpBase<Context>(def, ws) { Setup(2); }
27 
28  bool Transposed() override { return false; }
29  bool HasBias() override { return XSize() > 2; }
30 
31  void RunOnDevice() override;
32  template <typename T> void RunImpl();
33 };
34 
35 template <class Context>
37  : public Conv2dOp<Context> {
38  public:
40  const OperatorDef& def,
41  Workspace* ws)
42  : Conv2dOp<Context>(def, ws) {}
45 
46  bool HasBias() override {
47  return Y(2)->name() != "NULL";
48  }
49 
50  void RunOnDevice() override;
51  template <typename T> void RunImpl();
52 };
53 
54 #ifdef WITH_CUDNN
55 
56 template <class Context>
57 class CuDNNConv2dOp final
58  : public Conv2dOp<Context> {
59  public:
61  const OperatorDef& def,
62  Workspace* ws)
63  : Conv2dOp<Context>(def, ws),
65  TENSOR_CORE_AVAILABLE() ? 1 : 0
66  ) {
67 #if CUDNN_VERSION_MIN(7, 0, 0)
68  cudnn_group_ = 1;
69 #else
71 #endif
76  CUDNN_CHECK(cudnnCreateFilterDescriptor(&filter_desc_));
77  CUDNN_CHECK(cudnnCreateConvolutionDescriptor(&conv_desc_));
78  if (data_format() == "NCHW") format_ = CUDNN_TENSOR_NCHW;
79  else if (data_format() == "NHWC") format_ = CUDNN_TENSOR_NHWC;
80  }
83 
89  CUDNN_CHECK(cudnnDestroyFilterDescriptor(filter_desc_));
90  CUDNN_CHECK(cudnnDestroyConvolutionDescriptor(conv_desc_));
91  }
92 
93  void RunOnDevice() override;
94  void SetConvDescFromInputs();
95  template <typename T> void ResetDesc();
96  template <typename T> void RunImpl();
97 
98  protected:
99  cudnnDataType_t compute_type_;
100  cudnnTensorFormat_t format_;
101  cudnnConvolutionFwdAlgo_t fwd_algo_;
102  cudnnTensorDescriptor_t input_desc_, output_desc_;
103  cudnnTensorDescriptor_t bias_desc_, output2b_desc_;
104  cudnnConvolutionDescriptor_t conv_desc_;
105  cudnnFilterDescriptor_t filter_desc_;
109 };
110 
111 template <class Context>
113  : public Conv2dGradientOp<Context> {
114  public:
116  const OperatorDef& def,
117  Workspace* ws)
118  : Conv2dGradientOp<Context>(def, ws),
120  TENSOR_CORE_AVAILABLE() ? 1 : 0
121  ) {
122 #if CUDNN_VERSION_MIN(7, 0, 0)
123  cudnn_group_ = 1;
124 #else
126 #endif
131  CUDNN_CHECK(cudnnCreateFilterDescriptor(&filter_desc_));
132  CUDNN_CHECK(cudnnCreateConvolutionDescriptor(&conv_desc_));
133  if (data_format() == "NCHW") format_ = CUDNN_TENSOR_NCHW;
134  else if (data_format() == "NHWC") format_ = CUDNN_TENSOR_NHWC;
135  }
138 
144  CUDNN_CHECK(cudnnDestroyFilterDescriptor(filter_desc_));
145  CUDNN_CHECK(cudnnDestroyConvolutionDescriptor(conv_desc_));
146  }
147 
148  void SetConvDescFromInputs();
149 
150  void RunOnDevice() override;
151  template <typename T> void ResetDesc();
152  template <typename T> void RunImpl();
153 
154  protected:
155  cudnnDataType_t compute_type_;
156  cudnnTensorFormat_t format_;
157  cudnnConvolutionBwdFilterAlgo_t bwd_filter_algo_;
158  cudnnConvolutionBwdDataAlgo_t bwd_data_algo_;
159  cudnnTensorDescriptor_t input_desc_, output_desc_;
160  cudnnTensorDescriptor_t bias_desc_, input2b_desc_;
161  cudnnConvolutionDescriptor_t conv_desc_;
162  cudnnFilterDescriptor_t filter_desc_;
166 };
167 
168 #endif // WITH_CUDNN
169 
170 } // namespace dragon
171 
172 #endif // DRAGON_OPERATORS_VISION_CONV_OP_H_
USE_CONVOLUTION_FUNCTIONS
Definition: conv_op.h:137
cudnnTensorDescriptor_t bias_desc_
Definition: conv_op.h:103
cudnnTensorDescriptor_t input_desc_
Definition: conv_op.h:159
size_t bwd_filter_size_
Definition: conv_op.h:163
void RunImpl()
Definition: cudnn_conv2d_op.cc:171
void RunOnDevice() override
Implement the detailed execution.
Definition: conv2d_op.cc:23
vec64_t input_dims_
Definition: conv_op.h:165
Definition: workspace.h:20
cudnnConvolutionBwdDataAlgo_t bwd_data_algo_
Definition: conv_op.h:158
void RunImpl()
Definition: conv2d_op.cc:35
cudnnFilterDescriptor_t filter_desc_
Definition: conv_op.h:105
Definition: conv_op.h:112
cudnnConvolutionBwdFilterAlgo_t bwd_filter_algo_
Definition: conv_op.h:157
bool TENSOR_CORE_AVAILABLE()
Definition: cuda_device.h:154
int64_t group_
Definition: conv_op_base.h:47
#define CUDNN_CHECK(condition)
Definition: cudnn_device.h:34
Definition: conv_op.h:21
void RunImpl()
Definition: conv2d_op.cc:8
~CuDNNConv2dGradientOp()
Definition: conv_op.h:139
void RunOnDevice() override
Implement the detailed execution.
Definition: conv2d_op.cc:57
const string & data_format() const
Return the data format.
Definition: operator.h:79
bool HasBias() override
Definition: conv_op.h:46
cudnnTensorDescriptor_t input_desc_
Definition: conv_op.h:102
const OperatorDef & def() const
Return the stored def.
Definition: operator.h:114
void RunOnDevice() override
Implement the detailed execution.
Definition: cudnn_conv2d_op.cc:211
Workspace * ws() const
Return the parent workspace.
Definition: operator.h:87
Definition: conv_op.h:57
Conv2dOp(const OperatorDef &def, Workspace *ws)
Definition: conv_op.h:23
void ResetDesc()
Definition: cudnn_conv2d_op.cc:71
USE_OPERATOR_FUNCTIONS
Definition: conv_op.h:43
int64_t enable_tensor_core_
Definition: conv_op.h:107
USE_OPERATOR_FUNCTIONS
Definition: conv_op.h:136
cudnnDataType_t compute_type_
Definition: conv_op.h:99
cudnnTensorDescriptor_t output_desc_
Definition: conv_op.h:159
CuDNNConv2dOp(const OperatorDef &def, Workspace *ws)
Definition: conv_op.h:60
~CuDNNConv2dOp()
Definition: conv_op.h:84
Conv2dGradientOp(const OperatorDef &def, Workspace *ws)
Definition: conv_op.h:39
void CuDNNCreateTensorDesc(cudnnTensorDescriptor_t *desc)
Definition: cudnn_device.h:67
cudnnConvolutionDescriptor_t conv_desc_
Definition: conv_op.h:161
size_t fwd_data_size_
Definition: conv_op.h:106
cudnnDataType_t compute_type_
Definition: conv_op.h:155
bool Transposed() override
Definition: conv_op.h:28
vec64_t filter_dims_
Definition: conv_op.h:165
vec64_t filter_dims_
Definition: conv_op.h:108
int XSize()
Return the number of inputs.
Definition: operator.h:46
void RunImpl()
Definition: cudnn_conv2d_op.cc:404
void Setup(int num_axes)
Definition: conv_op_base.cc:248
std::vector< int64_t > vec64_t
Definition: types.h:25
void SetConvDescFromInputs()
Definition: cudnn_conv2d_op.cc:13
cudnnTensorDescriptor_t input2b_desc_
Definition: conv_op.h:160
USE_OPERATOR_FUNCTIONS
Definition: conv_op.h:81
cudnnConvolutionFwdAlgo_t fwd_algo_
Definition: conv_op.h:101
CuDNNConv2dGradientOp(const OperatorDef &def, Workspace *ws)
Definition: conv_op.h:115
int64_t cudnn_group_
Definition: conv_op.h:164
cudnnConvolutionDescriptor_t conv_desc_
Definition: conv_op.h:104
size_t bwd_data_size_
Definition: conv_op.h:163
void CuDNNDestroyTensorDesc(cudnnTensorDescriptor_t *desc)
Definition: cudnn_device.h:72
int64_t enable_tensor_core_
Definition: conv_op.h:164
void ResetDesc()
Definition: cudnn_conv2d_op.cc:282
cudnnTensorDescriptor_t output2b_desc_
Definition: conv_op.h:103
int64_t cudnn_group_
Definition: conv_op.h:107
cudnnTensorFormat_t format_
Definition: conv_op.h:156
cudnnTensorDescriptor_t output_desc_
Definition: conv_op.h:102
vec64_t input_dims_
Definition: conv_op.h:108
cudnnTensorDescriptor_t bias_desc_
Definition: conv_op.h:160
USE_CONVOLUTION_FUNCTIONS
Definition: conv_op.h:44
USE_OPERATOR_FUNCTIONS
Definition: conv_op.h:25
cudnnTensorFormat_t format_
Definition: conv_op.h:100
USE_CONVOLUTION_FUNCTIONS
Definition: conv_op.h:26
Tensor * Y(int i)
Return the specified output tensor.
Definition: operator.cc:55
const string & name() const
Return the tensor name.
Definition: tensor.h:69
USE_CONVOLUTION_FUNCTIONS
Definition: conv_op.h:82
void RunOnDevice() override
Implement the detailed execution.
Definition: cudnn_conv2d_op.cc:462
Definition: common.h:41
bool HasBias() override
Definition: conv_op.h:29
cudnnFilterDescriptor_t filter_desc_
Definition: conv_op.h:162
Definition: conv_op_base.h:23
void SetConvDescFromInputs()
Definition: cudnn_conv2d_op.cc:224
Definition: conv_op.h:36