MkldnnFcLayer.cpp 2.9 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/Stat.h"
T
tensor-tang 已提交
17 18 19 20 21 22 23

namespace paddle {

REGISTER_LAYER(mkldnn_fc, MkldnnFcLayer);

bool MkldnnFcLayer::init(const LayerMap& layerMap,
                         const ParameterMap& parameterMap) {
T
tensor-tang 已提交
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
  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;
}

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 已提交
76 77
}

T
tensor-tang 已提交
78 79 80 81
void MkldnnFcLayer::forward(PassType passType) {
  Layer::forward(passType);

  reshape();
T
tensor-tang 已提交
82

T
tensor-tang 已提交
83 84 85 86 87 88 89 90 91
  {
    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 已提交
92

T
tensor-tang 已提交
93 94 95 96 97 98 99 100 101
  /* activation */ {
    REGISTER_TIMER_INFO("FwActTimer", getName().c_str());
    forwardActivation();
  }
}

void MkldnnFcLayer::backward(const UpdateCallback& callback) {
  ;  // bool hasBias = biases_ && biases_->getWGrad();
}
T
tensor-tang 已提交
102
}  // namespace paddle