MkldnnFcLayer.cpp 4.8 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 45 46 47 48 49 50 51 52
  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));

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

T
tensor-tang 已提交
53 54 55 56 57
void MkldnnFcLayer::cvtWgtFromPaddle() {
  if (hasInitedWgt_) {
    return;
  }

T
tensor-tang 已提交
58 59 60 61 62 63
  // 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);
T
tensor-tang 已提交
64
  VLOG(DNN_ALL) << "Initial Weight from paddle: " << std::endl << ostr.str();
T
tensor-tang 已提交
65

T
tensor-tang 已提交
66
  // The mkldnn weight is transposed from initial paddle matrix
T
tensor-tang 已提交
67 68 69 70
  MatrixPtr paddleWgtT;
  paddleWgt->transpose(paddleWgtT, true);

  weight_->getW()->copyFrom(*paddleWgtT);
T
tensor-tang 已提交
71 72 73 74 75 76 77 78 79 80 81 82
  hasInitedWgt_ = true;
}

void MkldnnFcLayer::cvtWgtToPaddle() {
  MatrixPtr dnnWgt = weight_->getW();
  MatrixPtr paddleWgt;
  dnnWgt->transpose(paddleWgt, true);

  // copy paddle weight and override on weight_
  MatrixPtr dnnWgtT = Matrix::create(
      dnnWgt->getData(), dnnWgt->getWidth(), dnnWgt->getHeight(), false, false);
  dnnWgtT->copyFrom(*paddleWgt);
T
tensor-tang 已提交
83 84
}

T
tensor-tang 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
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());
T
tensor-tang 已提交
104
  printSizeInfo();
T
tensor-tang 已提交
105 106 107 108 109

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

T
tensor-tang 已提交
112 113 114
void MkldnnFcLayer::forward(PassType passType) {
  Layer::forward(passType);
  reshape();
T
tensor-tang 已提交
115

T
tensor-tang 已提交
116 117 118 119 120 121 122 123 124
  {
    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 已提交
125

T
tensor-tang 已提交
126 127 128 129 130 131 132
  /* activation */ {
    REGISTER_TIMER_INFO("FwActTimer", getName().c_str());
    forwardActivation();
  }
}

void MkldnnFcLayer::backward(const UpdateCallback& callback) {
T
tensor-tang 已提交
133 134 135 136 137 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 167
  /* 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 已提交
168
}
T
tensor-tang 已提交
169
}  // namespace paddle