LstmStepLayer.cpp 6.4 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Z
zhangjinchao01 已提交
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

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 "Layer.h"
#include "LstmCompute.h"
#include "paddle/utils/Stat.h"

namespace paddle {

/*
 * LstmStepLayer used in recurrent layer group.
 */
class LstmStepLayer : public Layer, public LstmCompute {
protected:
  Argument state_;
  Argument gate_;
  Argument stateActive_;
  MatrixPtr checkIg_, checkFg_, checkOg_;
  MatrixPtr checkIgGrad_, checkFgGrad_, checkOgGrad_;
  std::unique_ptr<Weight> weight_;

public:
  explicit LstmStepLayer(const LayerConfig& config) : Layer(config) {}

  ~LstmStepLayer() {}

  bool init(const LayerMap& layerMap, const ParameterMap& parameterMap);

  void forward(PassType passType);
  void backward(const UpdateCallback& callback = nullptr);
};

REGISTER_LAYER(lstm_step, LstmStepLayer);

bool LstmStepLayer::init(const LayerMap& layerMap,
                         const ParameterMap& parameterMap) {
  if (!Layer::init(layerMap, parameterMap)) return false;
  CHECK_EQ(2U, inputLayers_.size());

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 76 77 78 79 80
  checkIg_ = Matrix::create(nullptr,
                            /* height= */ 1,
                            getSize(),
                            /* trans= */ false,
                            useGpu_);
  checkFg_ = Matrix::create(nullptr,
                            /* height= */ 1,
                            getSize(),
                            /* trans= */ false,
                            useGpu_);
  checkOg_ = Matrix::create(nullptr,
                            /* height= */ 1,
                            getSize(),
                            /* trans= */ false,
                            useGpu_);
  checkIgGrad_ = Matrix::create(nullptr,
                                /* height= */ 1,
                                getSize(),
                                /* trans= */ false,
                                useGpu_);
  checkFgGrad_ = Matrix::create(nullptr,
                                /* height= */ 1,
                                getSize(),
                                /* trans= */ false,
                                useGpu_);
  checkOgGrad_ = Matrix::create(nullptr,
                                /* height= */ 1,
                                getSize(),
                                /* trans= */ false,
                                useGpu_);
Z
zhangjinchao01 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

  if (biasParameter_.get() != NULL) {
    CHECK_EQ(getSize() * 3, biasParameter_->getSize());
    weight_.reset(new Weight(1, getSize() * 3, biasParameter_));
    if (weight_->getW()) {
      real* data = weight_->getW()->getData();
      checkIg_->setData(data);
      checkFg_->setData(data + getSize());
      checkOg_->setData(data + getSize() * 2);
    }

    if (weight_->getWGrad()) {
      real* data = weight_->getWGrad()->getData();
      checkIgGrad_->setData(data);
      checkFgGrad_->setData(data + getSize());
      checkOgGrad_->setData(data + getSize() * 2);
    }
  }

  setOutput("state", &state_);
  LstmCompute::init(config_);
  return true;
}

void LstmStepLayer::forward(PassType passType) {
  REGISTER_TIMER_INFO("LstmRecurrentFwTime", getName().c_str());
  Layer::forward(passType);

  const Argument& input = getInput(0);
  const Argument& prevState = getInput(1);
  CHECK_EQ(getSize() * 4, input.value->getWidth());
  CHECK_EQ(getSize(), prevState.value->getWidth());
  int batchSize = input.getBatchSize();
  reserveOutput(batchSize, getSize());
115 116 117 118
  resetSpecifyOutput(state_,
                     batchSize,
                     getSize(),
                     /*  isValueClean */ false,
Z
zhangjinchao01 已提交
119
                     /* isGradClean */ true);
120 121 122 123 124 125 126 127 128 129
  resetSpecifyOutput(gate_,
                     batchSize,
                     getSize() * 4,
                     /* isValueClean */ false,
                     /* isGradClean */ false);
  resetSpecifyOutput(stateActive_,
                     batchSize,
                     getSize(),
                     /*  isValueClean */ false,
                     /* isGradClean */ false);
Z
zhangjinchao01 已提交
130 131 132 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 168 169 170 171 172 173 174 175 176 177 178
  gate_.value->assign(*input.value);

  hl_lstm_value lstmValue;
  lstmValue.checkIg = checkIg_->getData();
  lstmValue.checkFg = checkFg_->getData();
  lstmValue.checkOg = checkOg_->getData();
  lstmValue.gateValue = gate_.value->getData();
  lstmValue.stateValue = state_.value->getData();
  lstmValue.prevStateValue = prevState.value->getData();
  lstmValue.stateActiveValue = stateActive_.value->getData();
  lstmValue.outputValue = output_.value->getData();

  if (useGpu_) {
    LstmCompute::forwardBatch<1>(lstmValue, getSize(), batchSize);
  } else {
    LstmCompute::forwardBatch<0>(lstmValue, getSize(), batchSize);
  }
}

void LstmStepLayer::backward(const UpdateCallback& callback) {
  REGISTER_TIMER_INFO("LstmRecurrentBwTime", getName().c_str());
  const Argument& input = getInput(0);
  const Argument& prevState = getInput(1);
  int batchSize = input.getBatchSize();

  hl_lstm_value lstmValue;
  hl_lstm_grad lstmGrad;
  lstmValue.checkIg = checkIg_->getData();
  lstmValue.checkFg = checkFg_->getData();
  lstmValue.checkOg = checkOg_->getData();
  lstmValue.gateValue = gate_.value->getData();
  lstmValue.prevStateValue = prevState.value->getData();
  lstmValue.stateValue = state_.value->getData();
  lstmValue.stateActiveValue = stateActive_.value->getData();

  lstmGrad.gateGrad = gate_.grad->getData();
  if (prevState.grad) {
    lstmGrad.prevStateGrad = prevState.grad->getData();
  } else {
    lstmGrad.prevStateGrad = nullptr;
  }
  lstmGrad.stateGrad = state_.grad->getData();
  lstmGrad.stateActiveGrad = stateActive_.grad->getData();
  lstmGrad.outputGrad = output_.grad->getData();
  lstmGrad.checkIgGrad = checkIgGrad_->getData();
  lstmGrad.checkFgGrad = checkFgGrad_->getData();
  lstmGrad.checkOgGrad = checkOgGrad_->getData();

  if (useGpu_) {
179
    LstmCompute::backwardBatch<1>(lstmValue, lstmGrad, getSize(), batchSize);
Z
zhangjinchao01 已提交
180
  } else {
181
    LstmCompute::backwardBatch<0>(lstmValue, lstmGrad, getSize(), batchSize);
Z
zhangjinchao01 已提交
182 183 184 185 186 187 188 189 190 191 192 193
  }

  if (input.grad) {
    input.grad->add(*gate_.grad);
  }

  if (weight_) {
    weight_->getParameterPtr()->incUpdate(callback);
  }
}

}  // namespace paddle