gru_unit_arm_func.h 2.2 KB
Newer Older
Z
zhaojiaying01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

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

#ifdef GRU_UNIT_OP

#pragma once

#include <operators/math/gru_compute.h>
#include "operators/kernel/activation_kernel.h"
#include "operators/math/gemm.h"
Z
zhaojiaying01 已提交
22
#include "operators/math/math_function.h"
Z
zhaojiaying01 已提交
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
#include "operators/op_param.h"
namespace paddle_mobile {
namespace operators {

template <typename P>
void GruUnitCompute(const GruUnitParam<CPU>& param) {
  auto* input = param.InputInput();
  auto* hidden_prev = param.InputHiddenPrev();
  auto* weight = param.InputWeight();
  auto* bias = param.InputBias();
  auto* gate = param.OutGate();
  auto* reset_hidden_prev = param.OutResetHiddenPrev();
  auto* hidden = param.OutHidden();

  if (bias) {
    math::RowwiseAdd<CPU, float> add_bias;
    add_bias(*gate, *bias, gate);
  }

  int batch_size = input->dims()[0];
  int frame_size = hidden_prev->dims()[1];
  const P* weight_data = weight->data<P>();
  math::GRUMetaValue<P> gru_value;
  gru_value.gate_weight = const_cast<P*>(weight_data);
  gru_value.state_weight =
      const_cast<P*>(weight_data + 2 * frame_size * frame_size);
  gru_value.output_value = hidden->data<P>();
Z
zhaojiaying01 已提交
50
  gru_value.prev_out_value = const_cast<P*>(hidden_prev->data<P>());
Z
zhaojiaying01 已提交
51 52 53 54 55 56 57 58 59 60 61
  gru_value.gate_value = gate->data<P>();
  gru_value.reset_output_value = reset_hidden_prev->data<P>();
  auto active_node = math::GetActivationType(param.Activation());
  auto active_gate = math::GetActivationType(param.GateActivation());
  math::GRUUnitFunctor<CPU, float>::compute(gru_value, frame_size, batch_size,
                                            active_node, active_gate);
}

}  // namespace operators
}  // namespace paddle_mobile

Z
zhaojiaying01 已提交
62
#endif