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

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. */

15
#include "MKLDNNFcLayer.h"
T
tensor-tang 已提交
16
#include "paddle/utils/Logging.h"
T
tensor-tang 已提交
17

T
tensor-tang 已提交
18 19 20
using namespace mkldnn;  // NOLINT
typedef memory::format format;

T
tensor-tang 已提交
21 22
namespace paddle {

23
REGISTER_LAYER(mkldnn_fc, MKLDNNFcLayer);
T
tensor-tang 已提交
24

25
bool MKLDNNFcLayer::init(const LayerMap& layerMap,
T
tensor-tang 已提交
26
                         const ParameterMap& parameterMap) {
27
  if (!MKLDNNLayer::init(layerMap, parameterMap)) {
T
tensor-tang 已提交
28 29 30
    return false;
  }

31
  CHECK_EQ(inputLayers_.size(), 1UL) << "Only support one input layer yet";
T
tensor-tang 已提交
32 33 34 35 36 37 38
  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;
39 40
  ih_ = 1;
  iw_ = 1;
T
tensor-tang 已提交
41 42 43 44 45 46 47 48 49 50 51

  // 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) {
T
tensor-tang 已提交
52
    biases_ = std::unique_ptr<Weight>(new Weight(1, oc_, biasParameter_, 0));
T
tensor-tang 已提交
53 54 55 56
  }
  return true;
}

57
void MKLDNNFcLayer::convertWeightsFromPaddle() {
T
tensor-tang 已提交
58
  if (hasInitedWgt_) {
T
tensor-tang 已提交
59 60 61
    return;
  }

T
tensor-tang 已提交
62 63
  CHECK(wgtVal_) << "should have been initialized";
  auto targetDim = wgtVal_->getDims();
T
tensor-tang 已提交
64
  auto srcFmt = targetDim.size() == 2 ? format::io : format::ihwo;
T
tensor-tang 已提交
65
  wgtVal_->reorderDataFrom(wgtVal_, srcFmt, targetDim);
T
tensor-tang 已提交
66 67 68
  hasInitedWgt_ = true;
}

69
void MKLDNNFcLayer::convertWeightsToPaddle() {
T
tensor-tang 已提交
70 71
  CHECK(wgtVal_) << "should have been initialized";
  auto targetDim = wgtVal_->getDims();
T
tensor-tang 已提交
72
  auto dstFmt = targetDim.size() == 2 ? format::io : format::ihwo;
T
tensor-tang 已提交
73
  wgtVal_->reorderDataTo(wgtVal_, dstFmt, targetDim);
T
tensor-tang 已提交
74 75
}

76
void MKLDNNFcLayer::reshape(
77
    int& bs, int& ic, int& ih, int& iw, int& oc, int& oh, int& ow) {
78
  reshapeInput(bs, ih, iw);
79

T
tensor-tang 已提交
80
  CHECK_EQ(iLayerSize_, inputLayers_[0]->getSize());
81 82 83
  ic = iLayerSize_ / (ih * iw);
  CHECK_EQ(size_t(ic * ih * iw), iLayerSize_) << "not divisible";
  CHECK_EQ(size_t(oc), getSize());
T
tensor-tang 已提交
84

85 86
  reshapeOutput(oh, ow);
  resizeOutput(bs, oc);
T
tensor-tang 已提交
87 88
}

89
void MKLDNNFcLayer::resetFwd(std::vector<primitive>& pipeline,
90
                             std::vector<MKLDNNMatrixPtr>& inputs,
91
                             MKLDNNMatrixPtr& out) {
92
  resetFwdBuffers(inputs[0], wgtVal_, biasVal_, out);
93

94
  resetFwdPD(fwdPD_, inputs[0], wgtVal_, biasVal_, out);
95

96
  resetFwdPipeline(pipeline, fwdPD_, inputs[0], wgtVal_, biasVal_, out);
97 98 99
}

void MKLDNNFcLayer::resetBwd(std::vector<primitive>& pipeline,
100
                             std::vector<MKLDNNMatrixPtr>& inputs,
101 102 103 104
                             MKLDNNMatrixPtr& out) {
  std::shared_ptr<fc_bwdWgt::primitive_desc> bwdWgtPD;
  std::shared_ptr<fc_bwdData::primitive_desc> bwdDataPD;

105
  resetBwdBuffers(inputs[0], wgtGrad_, biasGrad_, out);
106

107
  resetBwdWgtPD(bwdWgtPD, wgtGrad_, biasGrad_, out);
108

109
  resetBwdDataPD(bwdDataPD, inputs[0], out);
110

111 112
  resetBwdPipeline(
      pipeline, bwdWgtPD, bwdDataPD, inputs[0], wgtGrad_, biasGrad_, out);
113
}
T
tensor-tang 已提交
114

115 116 117 118 119 120 121 122 123 124 125 126
void MKLDNNFcLayer::updateWeights(const UpdateCallback& callback) {
  weight_->getParameterPtr()->incUpdate(callback);
  if (biases_ && biases_->getWGrad()) {
    biases_->getParameterPtr()->incUpdate(callback);
  }
}

void MKLDNNFcLayer::resetFwdBuffers(MKLDNNMatrixPtr& in,
                                    MKLDNNMatrixPtr& wgt,
                                    MKLDNNMatrixPtr& bias,
                                    MKLDNNMatrixPtr& out) {
  resetInValue(in);
127 128
  CHECK(in);
  in->downSpatial();
129

130 131 132
  auto outPD =
      MKLDNNMatrix::createPrimitiveDesc({bs_, oc_}, format::nc, engine_);
  resetOutValue(out, outPD);
133

T
tensor-tang 已提交
134
  format wgtFmt = format::oihw;
135
  if (in->getFormat() == format::nChw8c) {
T
tensor-tang 已提交
136
    wgtFmt = format::oIhw8i;
137
  } else if (in->getFormat() == format::nChw16c) {
T
tensor-tang 已提交
138 139
    wgtFmt = format::oIhw16i;
  }
140 141 142
  auto wgtPD =
      MKLDNNMatrix::createPrimitiveDesc({oc_, ic_, ih_, iw_}, wgtFmt, engine_);
  resetWithMatrix(wgt, weight_->getW(), wgtPD);
143
  wgt->downSpatial();
144

T
tensor-tang 已提交
145 146 147 148 149
  if (biases_ && biases_->getW()) {
    auto biasPD = MKLDNNMatrix::createPrimitiveDesc({oc_}, format::x, engine_);
    resetWithMatrix(bias, biases_->getW(), biasPD);
  } else {
    bias = nullptr;
150
  }
151
}
T
tensor-tang 已提交
152

153 154 155 156 157 158 159 160
void MKLDNNFcLayer::resetFwdPD(std::shared_ptr<fc_fwd::primitive_desc>& pd,
                               MKLDNNMatrixPtr in,
                               MKLDNNMatrixPtr wgt,
                               MKLDNNMatrixPtr bias,
                               MKLDNNMatrixPtr out) {
  CHECK(in);
  CHECK(wgt);
  CHECK(out);
T
tensor-tang 已提交
161
  prop_kind pk = prop_kind::forward;
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
  fc_fwd::desc fwdDesc = bias != nullptr ? fc_fwd::desc(pk,
                                                        in->getMemoryDesc(),
                                                        wgt->getMemoryDesc(),
                                                        bias->getMemoryDesc(),
                                                        out->getMemoryDesc())
                                         : fc_fwd::desc(pk,
                                                        in->getMemoryDesc(),
                                                        wgt->getMemoryDesc(),
                                                        out->getMemoryDesc());
  pd.reset(new fc_fwd::primitive_desc(fwdDesc, engine_));
}

void MKLDNNFcLayer::resetFwdPipeline(
    std::vector<primitive>& pipeline,
    std::shared_ptr<fc_fwd::primitive_desc>& pd,
    MKLDNNMatrixPtr& in,
    MKLDNNMatrixPtr& wgt,
    MKLDNNMatrixPtr& bias,
    MKLDNNMatrixPtr& out) {
  if (bias) {
    fwd_.reset(new fc_fwd(*pd, *in, *wgt, *bias, *out));
T
tensor-tang 已提交
183
  } else {
184
    fwd_.reset(new fc_fwd(*pd, *in, *wgt, *out));
T
tensor-tang 已提交
185
  }
186
  pipeline.push_back(*fwd_);
T
tensor-tang 已提交
187 188
}

189 190 191 192
void MKLDNNFcLayer::resetBwdBuffers(MKLDNNMatrixPtr& in,
                                    MKLDNNMatrixPtr& wgt,
                                    MKLDNNMatrixPtr& bias,
                                    MKLDNNMatrixPtr& out) {
193
  CHECK(inVals_[0] && outVal_);
194
  resetOutGrad(out, outVal_->getPrimitiveDesc());
195
  resetInGrad(in, inVals_[0]->getPrimitiveDesc());
196 197

  CHECK(wgtVal_);
198
  resetWithMatrix(wgt, weight_->getWGrad(), wgtVal_->getPrimitiveDesc());
199

T
tensor-tang 已提交
200 201 202 203
  if (biasVal_) {
    resetWithMatrix(bias, biases_->getWGrad(), biasVal_->getPrimitiveDesc());
  } else {
    bias = nullptr;
T
tensor-tang 已提交
204
  }
205
}
T
tensor-tang 已提交
206

207 208 209 210 211
void MKLDNNFcLayer::resetBwdWgtPD(
    std::shared_ptr<fc_bwdWgt::primitive_desc>& pd,
    MKLDNNMatrixPtr& wgt,
    MKLDNNMatrixPtr& bias,
    MKLDNNMatrixPtr& out) {
212 213 214 215 216 217 218 219 220
  CHECK(inVals_[0]);
  fc_bwdWgt::desc bwdWgtDesc =
      bias ? fc_bwdWgt::desc(inVals_[0]->getMemoryDesc(),
                             wgt->getMemoryDesc(),
                             bias->getMemoryDesc(),
                             out->getMemoryDesc())
           : fc_bwdWgt::desc(inVals_[0]->getMemoryDesc(),
                             wgt->getMemoryDesc(),
                             out->getMemoryDesc());
221
  pd.reset(new fc_bwdWgt::primitive_desc(bwdWgtDesc, engine_, *fwdPD_));
T
tensor-tang 已提交
222 223
}

224 225 226 227 228 229 230 231 232 233 234 235
void MKLDNNFcLayer::resetBwdDataPD(
    std::shared_ptr<fc_bwdData::primitive_desc>& pd,
    MKLDNNMatrixPtr& in,
    MKLDNNMatrixPtr& out) {
  pd = nullptr;
  if (in == nullptr) {
    return;
  }
  CHECK(wgtVal_);
  fc_bwdData::desc bwdDataDesc = fc_bwdData::desc(
      in->getMemoryDesc(), wgtVal_->getMemoryDesc(), out->getMemoryDesc());
  pd.reset(new fc_bwdData::primitive_desc(bwdDataDesc, engine_, *fwdPD_));
T
tensor-tang 已提交
236 237
}

238 239 240 241 242 243 244 245
void MKLDNNFcLayer::resetBwdPipeline(
    std::vector<primitive>& pipeline,
    std::shared_ptr<fc_bwdWgt::primitive_desc>& bwdWgtPD,
    std::shared_ptr<fc_bwdData::primitive_desc>& bwdDataPD,
    MKLDNNMatrixPtr& in,
    MKLDNNMatrixPtr& wgt,
    MKLDNNMatrixPtr& bias,
    MKLDNNMatrixPtr& out) {
246
  CHECK(inVals_[0]);
247
  if (bias) {
248
    bwdWgt_.reset(new fc_bwdWgt(*bwdWgtPD, *inVals_[0], *out, *wgt, *bias));
249
  } else {
250
    bwdWgt_.reset(new fc_bwdWgt(*bwdWgtPD, *inVals_[0], *out, *wgt));
251 252 253 254 255
  }
  pipeline.push_back(*bwdWgt_);

  if (bwdDataPD == nullptr) {
    return;
T
tensor-tang 已提交
256
  }
257 258 259
  CHECK(wgtVal_) << "Should have weight memory";
  bwdData_.reset(new fc_bwdData(*bwdDataPD, *out, *wgtVal_, *in));
  pipeline.push_back(*bwdData_);
T
tensor-tang 已提交
260
}
261

T
tensor-tang 已提交
262
}  // namespace paddle