MKLDNNPoolLayer.cpp 6.4 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "MKLDNNPoolLayer.h"
16
#include "paddle/math/MathUtils.h"
T
tensor-tang 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include "paddle/utils/Logging.h"

using namespace mkldnn;  // NOLINT
typedef memory::format format;

namespace paddle {

REGISTER_LAYER(mkldnn_pool, MKLDNNPoolLayer);

bool MKLDNNPoolLayer::init(const LayerMap& layerMap,
                           const ParameterMap& parameterMap) {
  if (!MKLDNNLayer::init(layerMap, parameterMap)) {
    return false;
  }

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  /* the size of inputs for pool-layer is 1 */
  CHECK_EQ(config_.inputs_size(), 1);
  const PoolConfig& conf = config_.inputs(0).pool_conf();
  ic_ = conf.channels();
  ih_ = conf.img_size_y();
  iw_ = conf.img_size();
  oc_ = ic_;
  oh_ = conf.output_y();
  ow_ = conf.output_x();
  fh_ = conf.size_y();
  fw_ = conf.size_x();
  ph_ = conf.padding_y();
  pw_ = conf.padding();
  sh_ = conf.stride_y();
  sw_ = conf.stride();

  const std::string& type = conf.pool_type();
  if (type == "max-projection") {
    poolAlgo_ = algorithm::pooling_max;
  } else if (type == "avg-projection") {
52 53
    // paddle only use exclude_padding
    poolAlgo_ = algorithm::pooling_avg_exclude_padding;
54 55 56
  } else {
    LOG(FATAL) << "unknow pooling type!";
  }
T
tensor-tang 已提交
57 58 59 60
  return true;
}

void MKLDNNPoolLayer::reshape(
61
    int& bs, int& ic, int& ih, int& iw, int& oc, int& oh, int& ow) {
T
tensor-tang 已提交
62
  reshapeInput(bs, ih, iw);
63
  // ic_ and oc can not be changed
64 65
  CHECK_EQ((size_t)ic,
           inputLayers_[0]->getOutputValue()->getElementCnt() / bs / ih / iw)
66
      << "Input channel can not be changed";
T
tensor-tang 已提交
67 68

  // cal output sizes
69 70 71
  // paddle used false caffeMode for pooling
  oh = outputSize(ih, fh_, ph_, sh_, false);
  ow = outputSize(iw, fw_, pw_, sw_, false);
T
tensor-tang 已提交
72
  reshapeOutput(oh, ow);
73

T
tensor-tang 已提交
74 75 76 77
  resizeOutput(bs, oc * oh * ow);
}

void MKLDNNPoolLayer::resetFwd(std::vector<primitive>& pipeline,
78
                               std::vector<MKLDNNMatrixPtr>& inputs,
T
tensor-tang 已提交
79
                               MKLDNNMatrixPtr& out) {
80
  resetFwdBuffers(inputs[0], out);
T
tensor-tang 已提交
81

82
  resetFwdPD(fwdPD_, inputs[0], out);
T
tensor-tang 已提交
83

84
  resetFwdPipeline(pipeline, fwdPD_, inputs[0], out);
T
tensor-tang 已提交
85 86 87
}

void MKLDNNPoolLayer::resetBwd(std::vector<primitive>& pipeline,
88
                               std::vector<MKLDNNMatrixPtr>& inputs,
T
tensor-tang 已提交
89 90 91
                               MKLDNNMatrixPtr& out) {
  std::shared_ptr<pool_bwd::primitive_desc> pd;

92
  resetBwdBuffers(inputs[0], out);
T
tensor-tang 已提交
93

94
  resetBwdPD(pd, inputs[0], out);
T
tensor-tang 已提交
95

96
  resetBwdPipeline(pipeline, pd, inputs[0], out);
T
tensor-tang 已提交
97 98 99 100 101
}

void MKLDNNPoolLayer::resetFwdBuffers(MKLDNNMatrixPtr& in,
                                      MKLDNNMatrixPtr& out) {
  resetInValue(in);
102 103

  memory::dims outDims = memory::dims{bs_, oc_, oh_, ow_};
104 105 106 107
  CHECK(in);
  auto outPD =
      MKLDNNMatrix::createPrimitiveDesc(outDims, in->getFormat(), engine_);
  resetOutValue(out, outPD);
108
}
T
tensor-tang 已提交
109 110 111

void MKLDNNPoolLayer::resetFwdPD(std::shared_ptr<pool_fwd::primitive_desc>& pd,
                                 MKLDNNMatrixPtr in,
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
                                 MKLDNNMatrixPtr out) {
  memory::dims kernels = memory::dims{fh_, fw_};
  memory::dims strides = memory::dims{sh_, sw_};
  memory::dims padL = memory::dims{ph_, pw_};
  memory::dims padR = getPaddingR();
  padding_kind padKind = padding_kind::zero;
  prop_kind pk = passType_ == PASS_TEST ? prop_kind::forward_scoring
                                        : prop_kind::forward_training;
  auto fwdDesc = pool_fwd::desc(pk,
                                poolAlgo_,
                                in->getMemoryDesc(),
                                out->getMemoryDesc(),
                                strides,
                                kernels,
                                padL,
                                padR,
                                padKind);
  pd.reset(new pool_fwd::primitive_desc(fwdDesc, engine_));

  // prepare workspace if necessary
  workspace_ =
      (passType_ != PASS_TEST && poolAlgo_ == algorithm::pooling_max)
          ? std::make_shared<memory>(memory(pd->workspace_primitive_desc()))
          : nullptr;
}
T
tensor-tang 已提交
137 138

void MKLDNNPoolLayer::resetFwdPipeline(
139
    std::vector<primitive>& pipeline,
T
tensor-tang 已提交
140 141
    std::shared_ptr<pool_fwd::primitive_desc>& pd,
    MKLDNNMatrixPtr& in,
142 143 144 145 146 147
    MKLDNNMatrixPtr& out) {
  fwd_ = workspace_
             ? std::make_shared<pool_fwd>(pool_fwd(*pd, *in, *out, *workspace_))
             : std::make_shared<pool_fwd>(pool_fwd(*pd, *in, *out));
  pipeline.push_back(*fwd_);
}
T
tensor-tang 已提交
148 149 150

void MKLDNNPoolLayer::resetBwdBuffers(MKLDNNMatrixPtr& in,
                                      MKLDNNMatrixPtr& out) {
151
  CHECK(inVals_[0] && outVal_);
152
  resetOutGrad(out, outVal_->getPrimitiveDesc());
153
  resetInGrad(in, inVals_[0]->getPrimitiveDesc());
154
}
T
tensor-tang 已提交
155 156 157

void MKLDNNPoolLayer::resetBwdPD(std::shared_ptr<pool_bwd::primitive_desc>& pd,
                                 MKLDNNMatrixPtr& in,
158
                                 MKLDNNMatrixPtr& out) {
159 160 161 162
  pd = nullptr;
  if (in == nullptr) {
    return;
  }
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
  memory::dims kernels = memory::dims{fh_, fw_};
  memory::dims strides = memory::dims{sh_, sw_};
  memory::dims padL = memory::dims{ph_, pw_};
  memory::dims padR = getPaddingR();
  CHECK(out);
  auto bwdDesc = pool_bwd::desc(poolAlgo_,
                                in->getMemoryDesc(),
                                out->getMemoryDesc(),
                                strides,
                                kernels,
                                padL,
                                padR,
                                padding_kind::zero);
  pd.reset(new pool_bwd::primitive_desc(bwdDesc, engine_, *fwdPD_));
}
T
tensor-tang 已提交
178 179

void MKLDNNPoolLayer::resetBwdPipeline(
180
    std::vector<primitive>& pipeline,
T
tensor-tang 已提交
181 182
    std::shared_ptr<pool_bwd::primitive_desc>& pd,
    MKLDNNMatrixPtr& in,
183
    MKLDNNMatrixPtr& out) {
184 185
  if (pd == nullptr) {
    return;
186 187 188 189 190 191 192 193
  }

  bwdData_ =
      workspace_
          ? std::make_shared<pool_bwd>(pool_bwd(*pd, *out, *workspace_, *in))
          : std::make_shared<pool_bwd>(pool_bwd(*pd, *out, *in));
  pipeline.push_back(*bwdData_);
}
T
tensor-tang 已提交
194 195

}  // namespace paddle