Caffe2 - C++ API
A deep learning, cross platform ML framework
pool_op.cc
1 #include <caffe2/ideep/operators/conv_pool_base_op.h>
2 
3 namespace caffe2 {
4 
5 class IDEEPPoolOp final : public IDEEPConvPoolOpBase {
6  public:
7  USE_IDEEP_DEF_ALIASES();
8  USE_IDEEP_CONV_POOL_BASE_FUNCTIONS();
9 
10  IDEEPPoolOp(const OperatorDef& operator_def, Workspace* ws)
11  : IDEEPConvPoolOpBase(operator_def, ws),
12  training_mode_(
13  OperatorBase::GetSingleArgument<int>("training_mode", 1)) {
14  CAFFE_ENFORCE(
15  (dilation_h() == 1) && (dilation_w() == 1),
16  "Pooling op does not support dilation right now.");
17  if (!global_pooling_) {
18  CAFFE_ENFORCE(
19  pad_t() < kernel_h() && pad_b() < kernel_h() &&
20  pad_l() < kernel_w() && pad_r() < kernel_w(),
21  "Pad should be smaller than kernel.");
22  }
23  // Figure out the pooling descriptor.
24  if (operator_def.type().substr(0, 7) == "MaxPool") {
25  algo_ = ialgo::pooling_max;
26  } else if (operator_def.type().substr(0, 11) == "AveragePool") {
27  algo_ = ialgo::pooling_avg;
28  } else {
29  LOG(FATAL) << "Unsupported pooling method: " << operator_def.type();
30  }
31  }
32  ~IDEEPPoolOp() override {}
33 
34  bool RunOnDeviceWithOrderNCHW() override {
35  auto& X = Input(INPUT);
36  auto* Y = Output(OUTPUT);
37  auto Y_dims = CalcOutputDims(X, X.get_dim(1));
38  mkldnn::prop_kind pk = training_mode_ ?
39  mkldnn::prop_kind::forward_training : mkldnn::prop_kind::forward_inference;
40 
41  ideep::pooling_forward::compute(X, Y_dims, *Y,
42  stride_, kernel_, pad_tl(), pad_br(), algo_, pk);
43 
44  return true;
45  }
46 
47  private:
48  ialgo algo_;
49  bool training_mode_;
50 
51  INPUT_TAGS(INPUT);
52  OUTPUT_TAGS(OUTPUT);
53 };
54 
56  public:
57  USE_IDEEP_DEF_ALIASES();
58  USE_IDEEP_CONV_POOL_BASE_FUNCTIONS();
59 
60  IDEEPPoolGradientOp(const OperatorDef& operator_def, Workspace* ws)
61  : IDEEPConvPoolOpBase(operator_def, ws) {
62  CAFFE_ENFORCE(
63  (dilation_h() == 1) && (dilation_w() == 1),
64  "Pooling op does not support dilation right now.");
65  if (!global_pooling_) {
66  CAFFE_ENFORCE(
67  pad_t() < kernel_h() && pad_b() < kernel_h() &&
68  pad_l() < kernel_w() && pad_r() < kernel_w(),
69  "Pad should be smaller than kernel.");
70  }
71  // Figure out the pooling descriptor.
72  if (operator_def.type().substr(0, 15) == "MaxPoolGradient") {
73  algo_ = ialgo::pooling_max;
74  } else if (operator_def.type().substr(0, 19) == "AveragePoolGradient") {
75  algo_ = ialgo::pooling_avg;
76  } else {
77  LOG(FATAL) << "Unsupported pooling method: " << operator_def.type();
78  }
79  }
80  ~IDEEPPoolGradientOp() override {}
81 
82  bool RunOnDeviceWithOrderNCHW() override {
83  const auto& X = Input(INPUT);
84  const auto& Y = Input(OUTPUT);
85  const auto& dY = Input(OUTPUT_GRAD);
86  auto* dX = Output(INPUT_GRAD);
87 
88  ideep::pooling_backward::compute(dY, Y, X, *dX,
89  stride_, kernel_, pad_tl(), pad_br(), algo_);
90 
91  return true;
92  }
93 
94  private:
95  ialgo algo_;
96 
97  INPUT_TAGS(INPUT, OUTPUT, OUTPUT_GRAD);
98  OUTPUT_TAGS(INPUT_GRAD);
99 };
100 
101 REGISTER_IDEEP_OPERATOR(MaxPool, IDEEPPoolOp);
102 REGISTER_IDEEP_OPERATOR(MaxPoolGradient, IDEEPPoolGradientOp);
103 
104 REGISTER_IDEEP_OPERATOR(AveragePool, IDEEPPoolOp);
105 REGISTER_IDEEP_OPERATOR(AveragePoolGradient, IDEEPPoolGradientOp);
106 
107 } // namespace caffe2
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 ...
Definition: blob.h:13