gru_compute.cpp 2.7 KB
Newer Older
xiebaiyuan's avatar
xiebaiyuan 已提交
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
/* 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_OP
#include "operators/math/gru_compute.h"
#include "common/types.h"
#include "operators/math/activation_functions.h"
#include "operators/math/gemm.h"
#include "operators/math/gru_cpu_kernel.h"
#include "operators/math/gru_kernel.h"

namespace paddle_mobile {
namespace operators {
namespace math {

template <typename T>
struct GRUUnitFunctor<CPU, T> {
  static void compute(GRUMetaValue<T> value, int frame_size, int batch_size,
                      const ActivationType active_node,
                      const ActivationType active_gate) {
31
    Gemm gemm;
xiebaiyuan's avatar
xiebaiyuan 已提交
32
    if (value.prev_out_value) {
E
eclipsess 已提交
33 34 35 36
#ifdef _OPENMP
      gemm.Sgemm_omp(batch_size, frame_size * 2, frame_size, 1,
                     value.prev_out_value, frame_size, value.gate_weight,
                     frame_size * 2, 1, value.gate_value, frame_size * 3, false,
Z
ZhenWang 已提交
37
                     static_cast<float *>(nullptr));
E
eclipsess 已提交
38
#else
39 40 41
      gemm.Sgemm(batch_size, frame_size * 2, frame_size, 1,
                 value.prev_out_value, frame_size, value.gate_weight,
                 frame_size * 2, 1, value.gate_value, frame_size * 3, false,
Z
ZhenWang 已提交
42
                 static_cast<float *>(nullptr));
E
eclipsess 已提交
43
#endif
xiebaiyuan's avatar
xiebaiyuan 已提交
44 45 46 47 48 49
    }

    forward_reset_output(forward::gru_resetOutput<T>(), value, frame_size,
                         batch_size, active_gate);

    if (value.prev_out_value) {
E
eclipsess 已提交
50 51 52 53
#ifdef _OPENMP
      gemm.Sgemm_omp(batch_size, frame_size, frame_size, 1,
                     value.reset_output_value, frame_size, value.state_weight,
                     frame_size, 1, value.gate_value + frame_size * 2,
Z
ZhenWang 已提交
54
                     frame_size * 3, false, static_cast<float *>(nullptr));
E
eclipsess 已提交
55
#else
56 57 58
      gemm.Sgemm(batch_size, frame_size, frame_size, 1,
                 value.reset_output_value, frame_size, value.state_weight,
                 frame_size, 1, value.gate_value + frame_size * 2,
Z
ZhenWang 已提交
59
                 frame_size * 3, false, static_cast<float *>(nullptr));
E
eclipsess 已提交
60
#endif
xiebaiyuan's avatar
xiebaiyuan 已提交
61 62 63 64 65 66 67 68 69 70 71 72
    }

    forward_final_output(forward::gru_finalOutput<T>(), value, frame_size,
                         batch_size, active_node);
  }
};

template struct GRUUnitFunctor<CPU, float>;
}  // namespace math
}  // namespace operators
}  // namespace paddle_mobile
#endif