MKLDNNBatchNormLayer.cpp 10.7 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
/* 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 "MKLDNNBatchNormLayer.h"

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

namespace paddle {

REGISTER_LAYER(mkldnn_batch_norm, MKLDNNBatchNormLayer);

const real MKLDNNBatchNormLayer::EPS = 1E-5;

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

  // first one is input layer
  // the other two are created in config_parser.py saving moving mean and var
  CHECK_EQ(inputLayers_.size(), 3U);
  CHECK_EQ(inputLayers_.size(), parameters_.size());
  CHECK_EQ(inputLayers_.size(), size_t(config_.inputs_size()));

  const ImageConfig& conf = config_.inputs(0).image_conf();
  ic_ = conf.channels();
  ih_ = inputLayers_[0]->getOutput().getFrameHeight();
  iw_ = inputLayers_[0]->getOutput().getFrameWidth();
  if (iw_ == 0 && ih_ == 0) {
    iw_ = conf.img_size();
    ih_ = conf.has_img_size_y() ? conf.img_size_y() : conf.img_size();
  }
  oc_ = ic_;
  oh_ = ih_;
  ow_ = iw_;
  if (config_.has_use_global_stats()) {
    useGlobalStats_ = config_.use_global_stats();
  }
  movingAvgFraction_ = config_.moving_average_fraction();
  VLOG(MKLDNN_BASE) << "--- " << (useGlobalStats_ ? "use" : "do not use")
                    << " --- global stats";
  VLOG(MKLDNN_BASE) << "Moving average fraction: " << movingAvgFraction_;

  initWeight();
  movingMean_.reset(new Weight(oc_, 1, parameters_[1], 0));
  movingVar_.reset(new Weight(oc_, 1, parameters_[2], 0));
  return true;
}

void MKLDNNBatchNormLayer::initWeight() {
  weight_.reset(new Weight(1, oc_, parameters_[0]));
  if (biasParameter_.get() != NULL) {
    biases_ = std::unique_ptr<Weight>(new Weight(1, oc_, biasParameter_));
  }
  CHECK_EQ(weight_ != nullptr, biases_ != nullptr)
      << "only support have both weight and bias, or neither";
  if (weight_ && weight_->getW()) {
    CHECK(biases_ && biases_->getW());
    valueScaleShift_ = Matrix::create(2, oc_, false, false);
    valueScaleShift_->zeroMem();
    VectorPtr scale(new CpuVector(oc_, valueScaleShift_->getMemoryHandle(), 0));
    VectorPtr shift(
        new CpuVector(oc_, valueScaleShift_->getMemoryHandle(), oc_));
    const VectorPtr& wgt = parameters_[0]->getBuf(PARAMETER_VALUE);
    const VectorPtr& bias = biasParameter_->getBuf(PARAMETER_VALUE);
    scale->copyFrom(*wgt);
    shift->copyFrom(*bias);
    wgt->setData(valueScaleShift_->getData());
    bias->setData(valueScaleShift_->getData() + oc_);
  }
  if (weight_ && weight_->getWGrad()) {
    CHECK(biases_ && biases_->getWGrad());
    gradScaleShift_ = Matrix::create(2, oc_, false, false);
    gradScaleShift_->zeroMem();
    const VectorPtr& wgt = parameters_[0]->getBuf(PARAMETER_GRADIENT);
    const VectorPtr& bias = biasParameter_->getBuf(PARAMETER_GRADIENT);
    wgt->setData(gradScaleShift_->getData());
    bias->setData(gradScaleShift_->getData() + oc_);
  }
}

void MKLDNNBatchNormLayer::convertWeightsFromPaddle() {
  if (hasInitedWgt_) {
    return;
  }
  // prepare mean and var if necessary
  if (useGlobalStats_) {
    CHECK(mean_);
    CHECK(var_);
    mean_->copyFrom(*(movingMean_->getW()));
    var_->copyFrom(*(movingVar_->getW()));
  }
  hasInitedWgt_ = true;
}

void MKLDNNBatchNormLayer::calMovingMeanAndVar() {
  // calculating and saving moving mean and variance
  CHECK_EQ(useGlobalStats_, false);
T
tensor-tang 已提交
112 113 114 115
  movingMean_->getW()->add(
      *mean_, movingAvgFraction_, 1.0 - movingAvgFraction_);
  // here var is v^2
  movingVar_->getW()->add(*var_, movingAvgFraction_, 1.0 - movingAvgFraction_);
T
tensor-tang 已提交
116 117 118 119 120 121
}

void MKLDNNBatchNormLayer::reshape(
    int& bs, int& ic, int& ih, int& iw, int oc, int& oh, int& ow) {
  reshapeInput(bs, ih, iw);
  oh = ih;
T
tensor-tang 已提交
122
  ow = iw;
T
tensor-tang 已提交
123 124 125 126 127 128 129 130 131 132 133 134
  // ic_ and oc can not be changed
  CHECK_EQ(inputElemenCnt_ / bs / ih / iw, (size_t)ic)
      << "Input channel can not be changed";
  reshapeOutput(oh, ow);
  resizeOutput(bs, oc * oh * ow);
}

void MKLDNNBatchNormLayer::resetFwd(std::vector<primitive>& pipeline,
                                    MKLDNNMatrixPtr& in,
                                    MKLDNNMatrixPtr& wgt,
                                    MKLDNNMatrixPtr& bias,
                                    MKLDNNMatrixPtr& out) {
T
tensor-tang 已提交
135 136 137
  // In training phase, it will always calculate mean and var,
  // so useGlobalStats must be false.
  // In scoring phase, it depends on useGlobalStats choice.
T
tensor-tang 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  if (passType_ != PASS_TEST && useGlobalStats_ == true) {
    LOG(WARNING) << "use_global_stats is invalid setting in training phase";
    useGlobalStats_ = false;
  }

  resetFwdBuffers(in, wgt, out);

  resetFwdPD(fwdPD_, in, wgt, out);

  resetFwdPipeline(pipeline, fwdPD_, in, wgt, out);
}

void MKLDNNBatchNormLayer::resetBwd(std::vector<primitive>& pipeline,
                                    MKLDNNMatrixPtr& in,
                                    MKLDNNMatrixPtr& wgt,
                                    MKLDNNMatrixPtr& bias,
                                    MKLDNNMatrixPtr& out) {
  std::shared_ptr<bn_bwd::primitive_desc> pd;

  resetBwdBuffers(in, wgt, out);

  resetBwdPD(pd, in, wgt, out);

  resetBwdPipeline(pipeline, pd, in, wgt, out);
}

void MKLDNNBatchNormLayer::forward(PassType passType) {
  MKLDNNLayer::forward(passType);

T
tensor-tang 已提交
167
  // calculate and save moving mean and variance
T
tensor-tang 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  if (passType_ != PASS_TEST) {
    calMovingMeanAndVar();
  }
}

void MKLDNNBatchNormLayer::updateWeights(const UpdateCallback& callback) {
  weight_->getParameterPtr()->incUpdate(callback);
  if (biases_ && biases_->getWGrad()) {
    biases_->getParameterPtr()->incUpdate(callback);
  }
}

void MKLDNNBatchNormLayer::resetFwdBuffers(MKLDNNMatrixPtr& in,
                                           MKLDNNMatrixPtr& wgt,
                                           MKLDNNMatrixPtr& out) {
  resetInValue(in);

  memory::dims outDims = memory::dims{bs_, oc_, oh_, ow_};
  CHECK(in);
  auto outPD =
      MKLDNNMatrix::createPrimitiveDesc(outDims, in->getFormat(), engine_);
  resetOutValue(out, outPD);

  if (valueScaleShift_) {
    auto pd = MKLDNNMatrix::createPrimitiveDesc({2, oc_}, format::nc, engine_);
    resetWithMatrix(wgt, valueScaleShift_, pd);
  }
  if (passType_ != PASS_TEST || useGlobalStats_) {
    auto pd = MKLDNNMatrix::createPrimitiveDesc({oc_}, format::x, engine_);
    mean_ = MKLDNNMatrix::create(pd);
    var_ = MKLDNNMatrix::create(pd);
  }
}

void MKLDNNBatchNormLayer::resetFwdPD(
    std::shared_ptr<bn_fwd::primitive_desc>& pd,
    MKLDNNMatrixPtr in,
    MKLDNNMatrixPtr wgt,
    MKLDNNMatrixPtr out) {
  flags_ = 0u;
  prop_kind pk = passType_ == PASS_TEST ? prop_kind::forward_scoring
                                        : prop_kind::forward_training;
  if (useGlobalStats_) {
    flags_ = (flags_ | batch_normalization_flag::use_global_stats);
  }
  if (wgt) {
    flags_ = (flags_ | batch_normalization_flag::use_scale_shift);
  }
  auto fwdDesc = bn_fwd::desc(pk, in->getMemoryDesc(), EPS, flags_);
  pd.reset(new bn_fwd::primitive_desc(fwdDesc, engine_));
T
tensor-tang 已提交
218
  CHECK_PRIMITIVE_DESC_EQ(out, pd->dst_primitive_desc());
T
tensor-tang 已提交
219
  if (wgt) {
T
tensor-tang 已提交
220
    CHECK_PRIMITIVE_DESC_EQ(wgt, pd->weights_primitive_desc());
T
tensor-tang 已提交
221 222
  }
  if (passType_ != PASS_TEST || useGlobalStats_) {
T
tensor-tang 已提交
223 224
    CHECK_PRIMITIVE_DESC_EQ(mean_, pd->mean_primitive_desc());
    CHECK_PRIMITIVE_DESC_EQ(var_, pd->variance_primitive_desc());
T
tensor-tang 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
  }
}

void MKLDNNBatchNormLayer::resetFwdPipeline(
    std::vector<primitive>& pipeline,
    std::shared_ptr<bn_fwd::primitive_desc>& pd,
    MKLDNNMatrixPtr& in,
    MKLDNNMatrixPtr& wgt,
    MKLDNNMatrixPtr& out) {
  if (passType_ == PASS_TEST) {
    if (useGlobalStats_) {
      fwd_.reset(wgt != nullptr ? new bn_fwd(*pd,
                                             *in,
                                             (const primitive::at)(*mean_),
                                             (const primitive::at)(*var_),
                                             *wgt,
                                             *out)
                                : new bn_fwd(*pd,
                                             *in,
                                             (const primitive::at)(*mean_),
                                             (const primitive::at)(*var_),
                                             *out));
    } else {
      fwd_.reset(wgt != nullptr ? new bn_fwd(*pd, *in, *wgt, *out)
                                : new bn_fwd(*pd, *in, *out));
    }
  } else {
    CHECK_EQ(useGlobalStats_, false)
        << "useGlobalStats should be false in training";
    fwd_.reset(wgt != nullptr ? new bn_fwd(*pd, *in, *wgt, *out, *mean_, *var_)
                              : new bn_fwd(*pd, *in, *out, *mean_, *var_));
  }
  pipeline.push_back(*fwd_);
}

void MKLDNNBatchNormLayer::resetBwdBuffers(MKLDNNMatrixPtr& in,
                                           MKLDNNMatrixPtr& wgt,
                                           MKLDNNMatrixPtr& out) {
  CHECK(inVal_ && outVal_);
  resetOutGrad(out, outVal_->getPrimitiveDesc());
  resetInGrad(in, inVal_->getPrimitiveDesc());
  if (gradScaleShift_) {
    CHECK(wgtVal_);
    resetWithMatrix(wgt, gradScaleShift_, wgtVal_->getPrimitiveDesc());
  }
}

void MKLDNNBatchNormLayer::resetBwdPD(
    std::shared_ptr<bn_bwd::primitive_desc>& pd,
    MKLDNNMatrixPtr& in,
    MKLDNNMatrixPtr& wgt,
    MKLDNNMatrixPtr& out) {
  pd = nullptr;
  if (in == nullptr) {
    return;
  }
T
tensor-tang 已提交
281
  CHECK_PRIMITIVE_DESC_EQ(out, in->getPrimitiveDesc());
T
tensor-tang 已提交
282 283 284 285
  auto md = in->getMemoryDesc();
  auto bwdDesc = bn_bwd::desc(prop_kind::backward, md, md, EPS, flags_);
  pd.reset(new bn_bwd::primitive_desc(bwdDesc, engine_, *fwdPD_));
  CHECK(pd->weights_primitive_desc() == fwdPD_->weights_primitive_desc());
T
tensor-tang 已提交
286 287 288
  CHECK_PRIMITIVE_DESC_EQ(wgt, pd->diff_weights_primitive_desc());
  CHECK_PRIMITIVE_DESC_EQ(mean_, pd->mean_primitive_desc());
  CHECK_PRIMITIVE_DESC_EQ(var_, pd->variance_primitive_desc());
T
tensor-tang 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
}

void MKLDNNBatchNormLayer::resetBwdPipeline(
    std::vector<primitive>& pipeline,
    std::shared_ptr<bn_bwd::primitive_desc>& pd,
    MKLDNNMatrixPtr& in,
    MKLDNNMatrixPtr& wgt,
    MKLDNNMatrixPtr& out) {
  if (pd == nullptr) {
    return;
  }
  CHECK(inVal_);
  bwdData_.reset(
      wgt && wgtVal_
          ? new bn_bwd(*pd, *inVal_, *mean_, *var_, *out, *wgtVal_, *in, *wgt)
          : new bn_bwd(*pd, *inVal_, *mean_, *var_, *out, *in));
  pipeline.push_back(*bwdData_);
}

}  // namespace paddle