LstmCompute.cpp 2.9 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

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 "LstmCompute.h"
Y
Yu Yang 已提交
16 17
#include "hl_recurrent_apply.cuh"
#include "paddle/utils/Util.h"
Z
zhangjinchao01 已提交
18 19 20 21 22 23 24 25 26 27 28

namespace paddle {

void LstmCompute::init(LayerConfig &config) {
  activeNode_ = hlActiveType(config.active_type());
  activeGate_ = hlActiveType(config.active_gate_type());
  activeState_ = hlActiveType(config.active_state_type());
}

template <>
void LstmCompute::forwardOneSequence<0>(hl_lstm_value value, int frameSize) {
29 30 31 32 33
  hl_cpu_lstm_forward(hppl::forward::lstm(),
                      value,
                      frameSize,
                      activeNode_,
                      activeGate_,
Z
zhangjinchao01 已提交
34 35 36 37
                      activeState_);
}

template <>
38 39 40 41 42 43 44 45 46
void LstmCompute::backwardOneSequence<0>(hl_lstm_value value,
                                         hl_lstm_grad grad,
                                         int frameSize) {
  hl_cpu_lstm_backward(hppl::backward::lstm(),
                       value,
                       grad,
                       frameSize,
                       activeNode_,
                       activeGate_,
Z
zhangjinchao01 已提交
47 48 49 50
                       activeState_);
}

template <>
51 52 53
void LstmCompute::forwardBatch<0>(hl_lstm_value value,
                                  int frameSize,
                                  int batchSize) {
Z
zhangjinchao01 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67
  for (int b = 0; b < batchSize; b++) {
    forwardOneSequence<0>(value, frameSize);

    value.gateValue += frameSize * 4;
    value.stateValue += frameSize;
    value.stateActiveValue += frameSize;
    value.outputValue += frameSize;
    if (value.prevStateValue) {
      value.prevStateValue += frameSize;
    }
  }
}

template <>
68 69 70 71
void LstmCompute::backwardBatch<0>(hl_lstm_value value,
                                   hl_lstm_grad grad,
                                   int frameSize,
                                   int batchSize) {
Z
zhangjinchao01 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  for (int b = 0; b < batchSize; b++) {
    backwardOneSequence<0>(value, grad, frameSize);

    value.gateValue += frameSize * 4;
    value.stateValue += frameSize;
    value.stateActiveValue += frameSize;
    value.outputValue += frameSize;
    if (value.prevStateValue) {
      value.prevStateValue += frameSize;
    }

    grad.gateGrad += frameSize * 4;
    grad.stateGrad += frameSize;
    grad.stateActiveGrad += frameSize;
    grad.outputGrad += frameSize;
    if (grad.prevStateGrad) {
      grad.prevStateGrad += frameSize;
    }
  }
}

}  // namespace paddle