Caffe2 - C++ API
A deep learning, cross platform ML framework
conv_op.h
1 #ifndef CAFFE2_OPERATORS_CONV_OP_H_
2 #define CAFFE2_OPERATORS_CONV_OP_H_
3 
4 #include "caffe2/core/context.h"
5 #include "caffe2/core/operator.h"
6 #include "caffe2/operators/conv_op_shared.h"
7 #include "caffe2/operators/conv_pool_op_base.h"
8 
9 CAFFE2_DECLARE_bool(caffe2_force_shared_col_buffer);
10 
11 namespace caffe2 {
12 
13 template <typename T, class Context>
14 class ConvOp final : public ConvPoolOpBase<Context> {
15  public:
16  USE_CONV_POOL_BASE_FUNCTIONS(Context);
17  ConvOp(const OperatorDef& operator_def, Workspace* ws)
18  : ConvPoolOpBase<Context>(operator_def, ws) {
19  // Since this is the default convolution implementation, we will
20  // use CAFFE_ENFORCE instead of OPERATOR_NEEDS_FEATURE.
21  CAFFE_ENFORCE(
22  group_ == 1 || order_ == StorageOrder::NCHW,
23  "Group convolution only supports NCHW order right now.");
24 
25  // Create shared buffer mutex in the constructor
26  // to avoid race-condition in DAGNet.
27  if (FLAGS_caffe2_force_shared_col_buffer || shared_buffer_) {
28  createSharedBuffer<Context>(ws_);
29  }
30  }
31  ~ConvOp() {}
32 
33  bool RunOnDeviceWithOrderNCHW() override;
34  bool RunOnDeviceWithOrderNHWC() override;
35 
36  private:
37  Tensor<Context> col_buffer_;
38  Tensor<Context> bias_multiplier_;
39  Tensor<Context> img_shape_device_;
40  Tensor<Context> col_buffer_shape_device_;
41  // Input: X, W, b
42  // Output: Y
43  INPUT_TAGS(INPUT, FILTER, BIAS);
44 };
45 
46 template <typename T, class Context>
47 class ConvGradientOp final : public ConvPoolOpBase<Context> {
48  public:
49  USE_CONV_POOL_BASE_FUNCTIONS(Context);
50  ConvGradientOp(const OperatorDef& operator_def, Workspace* ws)
51  : ConvPoolOpBase<Context>(operator_def, ws),
52  no_bias_(OperatorBase::GetSingleArgument<int>("no_bias", 0)) {
53  CAFFE_ENFORCE(
54  !(no_bias_ && OutputSize() == 3),
55  "If bias is not present, you should not have 3 grad output.");
56  CAFFE_ENFORCE(
57  group_ == 1 || order_ == StorageOrder::NCHW,
58  "Group convolution only supports NCHW order right now.");
59  }
60  ~ConvGradientOp() {}
61 
62  bool RunOnDeviceWithOrderNCHW() override;
63  bool RunOnDeviceWithOrderNHWC() override;
64 
65  private:
66  Tensor<Context> col_buffer_;
67  Tensor<Context> bias_multiplier_;
68  Tensor<Context> img_shape_device_;
69  Tensor<Context> col_buffer_shape_device_;
70  bool no_bias_;
71  // input: X, W, dY
72  // output: dW, db, and optionally dX
73  INPUT_TAGS(INPUT, FILTER, OUTPUT_GRAD);
74  OUTPUT_TAGS(FILTER_GRAD, BIAS_OR_INPUT_GRAD, INPUT_GRAD);
75 };
76 
77 } // namespace caffe2
78 
79 #endif // CAFFE2_OPERATORS_CONV_OP_H_
Tensor is the basic class in Caffe2 that stores a contiguous memory with its shape information...
Definition: tensor.h:93
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
Definition: workspace.h:47
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...