You need to sign in or sign up before continuing.
MkldnnFcLayer.cpp 4.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 "MkldnnFcLayer.h"
T
tensor-tang 已提交
16
#include "paddle/utils/Logging.h"
T
tensor-tang 已提交
17
#include "paddle/utils/Stat.h"
T
tensor-tang 已提交
18 19 20 21 22 23 24

namespace paddle {

REGISTER_LAYER(mkldnn_fc, MkldnnFcLayer);

bool MkldnnFcLayer::init(const LayerMap& layerMap,
                         const ParameterMap& parameterMap) {
T
tensor-tang 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  if (!MkldnnLayer::init(layerMap, parameterMap)) {
    return false;
  }

  CHECK_EQ(inputLayers_.size(), 1) << "Only support one input layer yet!";
  CHECK_EQ(inputLayers_.size(), parameters_.size());
  CHECK(!parameters_[0]->isSparse()) << "Do not support sparse yet";

  // output size, cat not be changed
  oc_ = getSize();
  oh_ = 1;
  ow_ = 1;

  // input size can not change in FC
  iLayerSize_ = inputLayers_[0]->getSize();
  CHECK_EQ(parameters_[0]->getSize(), iLayerSize_ * oc_);

  // create weight
  weight_ =
      std::unique_ptr<Weight>(new Weight(oc_, iLayerSize_, parameters_[0], 0));
T
tensor-tang 已提交
45
  initWgt();
T
tensor-tang 已提交
46 47 48 49 50 51 52 53

  // create biases
  if (biasParameter_.get() != NULL) {
    biases_ = std::unique_ptr<Weight>(new Weight(1, oc_, biasParameter_));
  }
  return true;
}

T
tensor-tang 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
void MkldnnFcLayer::initWgt() {
  // The weight_ is transposed from initial paddle weight
  MatrixPtr paddleWgt = Matrix::create(
      weight_->getW()->getData(), iLayerSize_, oc_, false, false);

  std::ostringstream ostr;
  paddleWgt->print(ostr);
  VLOG(DNN_BASE) << ostr.str();

  // Firstly in mkldnn, the matrix is transposed from initial paddle weight
  MatrixPtr paddleWgtT;
  paddleWgt->transpose(paddleWgtT, true);

  weight_->getW()->copyFrom(*paddleWgtT);
}

T
tensor-tang 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
void MkldnnFcLayer::reshape() {
  const Argument& input = getInput(0);
  int batchSize = input.getBatchSize();
  if (bs_ == batchSize) {
    return;
  }
  bs_ = batchSize;
  ih_ = input.getFrameHeight();
  iw_ = input.getFrameWidth();
  if (ih_ == 0) {
    ih_ = 1;
  }
  if (iw_ == 0) {
    iw_ = 1;
  }
  CHECK_EQ(iLayerSize_, inputLayers_[0]->getSize());
  ic_ = iLayerSize_ / (ih_ * iw_);
  CHECK_EQ(size_t(ic_ * ih_ * iw_), iLayerSize_) << "not divisible";
  CHECK_EQ(size_t(oc_), getSize());

  // reset output
  output_.setFrameHeight(oh_);
  output_.setFrameWidth(ow_);
  resetOutput(bs_, oc_);
T
tensor-tang 已提交
94 95
}

T
tensor-tang 已提交
96 97 98
void MkldnnFcLayer::forward(PassType passType) {
  Layer::forward(passType);
  reshape();
T
tensor-tang 已提交
99

T
tensor-tang 已提交
100 101 102 103 104 105 106 107 108
  {
    REGISTER_TIMER_INFO("mkldnn_FwdTimer", getName().c_str());
    real* input = getInputValue(0)->getData();
    real* output = getOutputValue()->getData();
    real* wgt = weight_->getW()->getData();
    bool hasBias = biases_ && biases_->getW();
    real* bias = hasBias ? biases_->getW()->getData() : NULL;
    mkldnnForwardFC(bs_, ic_, ih_, iw_, input, oc_, output, wgt, bias);
  }
T
tensor-tang 已提交
109

T
tensor-tang 已提交
110 111 112 113 114 115 116
  /* activation */ {
    REGISTER_TIMER_INFO("FwActTimer", getName().c_str());
    forwardActivation();
  }
}

void MkldnnFcLayer::backward(const UpdateCallback& callback) {
T
tensor-tang 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
  /* Do derivation */ {
    REGISTER_TIMER_INFO("BpActTimer", getName().c_str());
    backwardActivation();
  }

  bool hasBias = biases_ && biases_->getWGrad();
  {
    REGISTER_TIMER_INFO("mkldnn_bwdTimer", getName().c_str());
    real* inVal = getInputValue(0)->getData();
    real* inGrad =
        getInputGrad(0) != nullptr ? getInputGrad(0)->getData() : NULL;
    real* outGrad = getOutputGrad()->getData();
    real* wgtGrad = weight_->getWGrad()->getData();
    real* wgtVal = weight_->getW()->getData();
    real* biasGrad = hasBias ? biases_->getWGrad()->getData() : NULL;
    mkldnnBackwardFC(bs_,
                     ic_,
                     ih_,
                     iw_,
                     inGrad,
                     inVal,
                     oc_,
                     outGrad,
                     wgtGrad,
                     wgtVal,
                     biasGrad);
  }

  {
    REGISTER_TIMER_INFO("WeightUpdate", getName().c_str());
    weight_->getParameterPtr()->incUpdate(callback);
    if (hasBias) {
      biases_->getParameterPtr()->incUpdate(callback);
    }
  }
T
tensor-tang 已提交
152
}
T
tensor-tang 已提交
153
}  // namespace paddle