MKLDNNAddtoLayer.cpp 7.4 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
/* 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 "MKLDNNAddtoLayer.h"

using namespace mkldnn;  // NOLINT

namespace paddle {

REGISTER_LAYER(mkldnn_addto, MKLDNNAddtoLayer);

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

  layerSize_ = getSize();
  for (size_t i = 0; i < inputLayers_.size(); i++) {
    CHECK_EQ(layerSize_, inputLayers_[i]->getSize()) << "input size must equal";
  }
  if (biasParameter_.get() != NULL) {
    biases_ =
        std::unique_ptr<Weight>(new Weight(1, layerSize_, biasParameter_, 0));
  }
  return true;
}

void MKLDNNAddtoLayer::reshape(
    int& bs, int& ic, int& ih, int& iw, int oc, int& oh, int& ow) {
  CHECK_EQ(layerSize_, getSize()) << "this layer size can not be changed";
  reshapeInput(bs, ih, iw);
  ic = inputLayers_[0]->getSize() / ih / iw;
  CHECK_EQ((size_t)ic * ih * iw, inputLayers_[0]->getSize());
  CHECK_EQ(inputElemenCnt_, (size_t)bs * ic * ih * iw);
  for (size_t i = 0; i < inputLayers_.size(); i++) {
    CHECK_EQ(int64_t(bs), inputLayers_[i]->getOutput().getBatchSize());
    CHECK_EQ(layerSize_, inputLayers_[i]->getSize());
  }

  oc = ic;
  oh = ih;
  ow = iw;
  reshapeOutput(oh, ow);
  resizeOutput(bs, oc * oh * ow);
}

void MKLDNNAddtoLayer::resetFwd(std::vector<primitive>& pipeline,
                                MKLDNNMatrixPtr& in,
                                MKLDNNMatrixPtr& wgt,
                                MKLDNNMatrixPtr& bias,
                                MKLDNNMatrixPtr& out) {
T
tensor-tang 已提交
64
  resetFwdBuffers(inVals_, bias, out);
T
tensor-tang 已提交
65 66 67
  in = inVals_[0];

  std::shared_ptr<sum::primitive_desc> fwdPD;
T
tensor-tang 已提交
68 69
  std::shared_ptr<sum::primitive_desc> biasPD;
  resetFwdPD(fwdPD, biasPD, inVals_, bias, out);
T
tensor-tang 已提交
70

T
tensor-tang 已提交
71
  resetFwdPipeline(pipeline, fwdPD, biasPD, inVals_, bias, out);
T
tensor-tang 已提交
72 73 74 75 76 77 78
}

void MKLDNNAddtoLayer::resetBwd(std::vector<primitive>& pipeline,
                                MKLDNNMatrixPtr& in,
                                MKLDNNMatrixPtr& wgt,
                                MKLDNNMatrixPtr& bias,
                                MKLDNNMatrixPtr& out) {
T
tensor-tang 已提交
79
  resetBwdBuffers(inGrads_, bias, out);
T
tensor-tang 已提交
80 81 82 83 84 85 86 87 88
  in = inGrads_[0];

  // backward only need share output grad to input grad
  for (size_t i = 0; i < inGrads_.size(); i++) {
    if (inGrads_[i] != nullptr) {
      inGrads_[i] = out;
      inputLayers_[i]->getOutputGrad()->setData(inGrads_[i]->getData());
    }
  }
T
tensor-tang 已提交
89 90 91 92

  // backward bias
  bwdBias_ = nullptr;
  if (bias) {
T
tensor-tang 已提交
93
    std::vector<float> scales(bs_, 1.0);
T
tensor-tang 已提交
94 95 96 97 98 99 100 101 102
    std::vector<memory::primitive_desc> srcPDs(bs_, bias->getPrimitiveDesc());
    auto biasPD = sum::primitive_desc(bias->getMemoryDesc(), scales, srcPDs);
    std::vector<primitive::at> srcs;
    for (size_t i = 0; i < grads_.size(); ++i) {
      srcs.push_back(*(grads_[i]));
    }
    bwdBias_.reset(new sum(biasPD, srcs, *bias));
    pipeline.push_back(*bwdBias_);
  }
T
tensor-tang 已提交
103 104 105 106 107 108 109 110
}

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

T
tensor-tang 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
void MKLDNNAddtoLayer::prepareBias(MKLDNNMatrixPtr& bias,
                                   const MatrixPtr& biasMat,
                                   const MKLDNNMatrixPtr& out,
                                   std::vector<MKLDNNMatrixPtr>& outs) {
  auto pd = MKLDNNMatrix::createPrimitiveDesc(
      {(int)layerSize_}, memory::format::x, engine_);
  bias = MKLDNNMatrix::create(pd, biasMat);
  outs.clear();
  real* data = out->getData();
  CHECK_EQ(bs_ * layerSize_, out->getElementCnt());
  for (int i = 0; i < bs_; ++i) {
    MatrixPtr tmp =
        Matrix::create(data + i * layerSize_, 1, layerSize_, false, false);
    outs.push_back(MKLDNNMatrix::create(bias->getPrimitiveDesc(), tmp));
  }
}

T
tensor-tang 已提交
128
void MKLDNNAddtoLayer::resetFwdBuffers(std::vector<MKLDNNMatrixPtr>& inputs,
T
tensor-tang 已提交
129
                                       MKLDNNMatrixPtr& bias,
T
tensor-tang 已提交
130 131 132 133 134 135 136 137 138 139 140 141
                                       MKLDNNMatrixPtr& out) {
  inputs.resize(inputLayers_.size());
  for (size_t i = 0; i < inputs.size(); i++) {
    resetInValue(inputs[i], nullptr, i);
    CHECK(inputs[i]);
    inputs[i]->downSpatial();
  }
  for (size_t i = 1; i < inputs.size(); i++) {
    CHECK_PRIMITIVE_DESC_EQ(inputs[i], inputs[0]->getPrimitiveDesc());
  }

  resetOutValue(out, inputs[0]->getPrimitiveDesc());
T
tensor-tang 已提交
142 143 144 145 146 147

  if (biases_ && biases_->getW()) {
    prepareBias(bias, biases_->getW(), out, vals_);
  } else {
    bias = nullptr;
  }
T
tensor-tang 已提交
148 149 150
}

void MKLDNNAddtoLayer::resetFwdPD(std::shared_ptr<sum::primitive_desc>& pd,
T
tensor-tang 已提交
151
                                  std::shared_ptr<sum::primitive_desc>& biasPD,
T
tensor-tang 已提交
152
                                  std::vector<MKLDNNMatrixPtr>& inputs,
T
tensor-tang 已提交
153
                                  MKLDNNMatrixPtr bias,
T
tensor-tang 已提交
154
                                  MKLDNNMatrixPtr out) {
T
tensor-tang 已提交
155
  std::vector<float> scales(inputs.size(), 1.0);
T
tensor-tang 已提交
156 157 158 159 160 161 162
  std::vector<memory::primitive_desc> srcPDs;
  for (size_t i = 0; i < inputs.size(); i++) {
    srcPDs.push_back(inputs[i]->getPrimitiveDesc());
  }
  CHECK(out);
  pd.reset(new sum::primitive_desc(out->getMemoryDesc(), scales, srcPDs));
  CHECK_PRIMITIVE_DESC_EQ(out, pd->dst_primitive_desc());
T
tensor-tang 已提交
163 164 165

  biasPD = nullptr;
  if (bias) {
T
tensor-tang 已提交
166
    std::vector<float> scales(2, 1.0);
T
tensor-tang 已提交
167 168 169 170 171
    std::vector<memory::primitive_desc> srcPDs(2, bias->getPrimitiveDesc());
    biasPD.reset(
        new sum::primitive_desc(bias->getMemoryDesc(), scales, srcPDs));
    CHECK_PRIMITIVE_DESC_EQ(bias, biasPD->dst_primitive_desc());
  }
T
tensor-tang 已提交
172 173 174 175 176
}

void MKLDNNAddtoLayer::resetFwdPipeline(
    std::vector<primitive>& pipeline,
    std::shared_ptr<sum::primitive_desc>& pd,
T
tensor-tang 已提交
177
    std::shared_ptr<sum::primitive_desc>& biasPD,
T
tensor-tang 已提交
178
    std::vector<MKLDNNMatrixPtr>& inputs,
T
tensor-tang 已提交
179
    MKLDNNMatrixPtr& bias,
T
tensor-tang 已提交
180 181 182 183 184 185 186
    MKLDNNMatrixPtr& out) {
  std::vector<primitive::at> srcs;
  for (size_t i = 0; i < inputs.size(); i++) {
    srcs.push_back(*(inputs[i]));
  }
  fwd_.reset(new sum(*pd, srcs, *out));
  pipeline.push_back(*fwd_);
T
tensor-tang 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199

  fwdBias_.clear();
  if (biasPD == nullptr || bias == nullptr) {
    return;
  }
  fwdBias_.resize(vals_.size());
  for (size_t i = 0; i < vals_.size(); ++i) {
    std::vector<primitive::at> srcs;
    srcs.push_back(*(vals_[i]));
    srcs.push_back(*bias);
    fwdBias_[i].reset(new sum(*biasPD, srcs, *vals_[i]));
    pipeline.push_back(*fwdBias_[i]);
  }
T
tensor-tang 已提交
200 201 202
}

void MKLDNNAddtoLayer::resetBwdBuffers(std::vector<MKLDNNMatrixPtr>& inputs,
T
tensor-tang 已提交
203
                                       MKLDNNMatrixPtr& bias,
T
tensor-tang 已提交
204 205 206 207 208 209 210 211 212 213
                                       MKLDNNMatrixPtr& out) {
  CHECK(outVal_);
  resetOutGrad(out, outVal_->getPrimitiveDesc());
  CHECK(out);

  inputs.resize(inputLayers_.size());
  for (size_t i = 0; i < inputs.size(); i++) {
    resetInGrad(inputs[i], inVal_->getPrimitiveDesc(), i);
    CHECK_PRIMITIVE_DESC_EQ(inputs[i], out->getPrimitiveDesc());
  }
T
tensor-tang 已提交
214 215 216 217 218 219

  if (biases_ && biases_->getWGrad()) {
    prepareBias(bias, biases_->getWGrad(), out, grads_);
  } else {
    bias = nullptr;
  }
T
tensor-tang 已提交
220 221 222
}

}  // namespace paddle