gru_cpu_kernel.h 6.7 KB
Newer Older
xiebaiyuan's avatar
xiebaiyuan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2016 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. */
14

xiebaiyuan's avatar
xiebaiyuan 已提交
15
#ifdef GRU_OP
16

xiebaiyuan's avatar
xiebaiyuan 已提交
17
#pragma once
18

xiebaiyuan's avatar
xiebaiyuan 已提交
19
#include <type_traits>
20
#include "operators/math/activation.h"
xiebaiyuan's avatar
xiebaiyuan 已提交
21 22 23 24 25 26
#include "operators/math/gru_compute.h"

namespace paddle_mobile {
namespace operators {
namespace math {

27 28 29
template <typename T, ActivationType Act>
void hl_naive_gru_forward_reset_output(T *gate_value, T *reset_output_value,
                                       T *prev_output_value, int frame_size) {
xiebaiyuan's avatar
xiebaiyuan 已提交
30 31 32 33 34 35 36
  T r_value_update_gate;
  T r_value_reset_gate;
  T r_value_reset_output;
  T r_prev_out = 0;
  T *update_gate = gate_value;
  T *reset_gate = gate_value + frame_size;

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 64 65 66 67 68 69 70
  int remain = frame_size;
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
  int loop = remain >> 3;
  remain = remain & 0x7;
  float32x4_t prev0 = vdupq_n_f32(0.f);
  float32x4_t prev1 = vdupq_n_f32(0.f);
  for (int i = 0; i < loop; ++i) {
    float32x4_t update0 = vld1q_f32(update_gate);
    float32x4_t update1 = vld1q_f32(update_gate + 4);
    float32x4_t reset0 = vld1q_f32(reset_gate);
    float32x4_t reset1 = vld1q_f32(reset_gate + 4);
    if (prev_output_value) {
      prev0 = vld1q_f32(prev_output_value);
      prev1 = vld1q_f32(prev_output_value + 4);
      prev_output_value += 8;
    }
    update0 = vActiveq_f32<Act>(update0);
    update1 = vActiveq_f32<Act>(update1);
    reset0 = vActiveq_f32<Act>(reset0);
    reset1 = vActiveq_f32<Act>(reset1);
    float32x4_t output0 = vmulq_f32(prev0, reset0);
    float32x4_t output1 = vmulq_f32(prev1, reset1);
    vst1q_f32(update_gate, update0);
    vst1q_f32(update_gate + 4, update1);
    vst1q_f32(reset_gate, reset0);
    vst1q_f32(reset_gate + 4, reset1);
    vst1q_f32(reset_output_value, output0);
    vst1q_f32(reset_output_value + 4, output1);
    update_gate += 8;
    reset_gate += 8;
    reset_output_value += 8;
  }
#endif  // __ARM_NEON__
  for (int i = 0; i < remain; i++) {
xiebaiyuan's avatar
xiebaiyuan 已提交
71 72 73 74 75
    r_value_update_gate = update_gate[i];
    r_value_reset_gate = reset_gate[i];
    if (prev_output_value) {
      r_prev_out = prev_output_value[i];
    }
76 77 78
    r_value_update_gate = Active<Act>(r_value_update_gate);
    r_value_reset_gate = Active<Act>(r_value_reset_gate);
    r_value_reset_output = r_prev_out * r_value_reset_gate;
xiebaiyuan's avatar
xiebaiyuan 已提交
79 80 81 82 83 84
    update_gate[i] = r_value_update_gate;
    reset_gate[i] = r_value_reset_gate;
    reset_output_value[i] = r_value_reset_output;
  }
}

85 86 87
template <typename T, ActivationType Act>
void hl_naive_gru_forward_final_output(T *gate_value, T *prev_output_value,
                                       T *output_value, int frame_size) {
xiebaiyuan's avatar
xiebaiyuan 已提交
88 89 90 91 92 93 94
  T r_value_update_gate;
  T r_value_frame_state;
  T r_prev_out = 0;
  T r_output;
  T *update_gate = gate_value;
  T *frame_state = gate_value + frame_size * 2;

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  int remain = frame_size;
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
  int loop = remain >> 3;
  remain = remain & 0x7;
  float32x4_t prev0 = vdupq_n_f32(0.f);
  float32x4_t prev1 = vdupq_n_f32(0.f);
  for (int i = 0; i < loop; ++i) {
    float32x4_t update0 = vld1q_f32(update_gate);
    float32x4_t update1 = vld1q_f32(update_gate + 4);
    float32x4_t state0 = vld1q_f32(frame_state);
    float32x4_t state1 = vld1q_f32(frame_state + 4);
    if (prev_output_value) {
      prev0 = vld1q_f32(prev_output_value);
      prev1 = vld1q_f32(prev_output_value + 4);
      prev_output_value += 8;
    }
    state0 = vActiveq_f32<Act>(state0);
    state1 = vActiveq_f32<Act>(state1);
    float32x4_t output0 = vmlsq_f32(prev0, update0, prev0);
    float32x4_t output1 = vmlsq_f32(prev1, update1, prev1);
    output0 = vmlaq_f32(output0, update0, state0);
    output1 = vmlaq_f32(output1, update1, state1);
    vst1q_f32(frame_state, state0);
    vst1q_f32(frame_state + 4, state1);
    vst1q_f32(output_value, output0);
    vst1q_f32(output_value + 4, output1);
    update_gate += 8;
    frame_state += 8;
    output_value += 8;
  }
#endif  // __ARM_NEON__
  for (int i = 0; i < remain; i++) {
xiebaiyuan's avatar
xiebaiyuan 已提交
127 128 129 130 131
    r_value_update_gate = update_gate[i];
    r_value_frame_state = frame_state[i];
    if (prev_output_value) {
      r_prev_out = prev_output_value[i];
    }
132 133 134
    r_value_frame_state = Active<Act>(r_value_frame_state);
    r_output = r_prev_out - r_value_update_gate * r_prev_out +
               r_value_update_gate * r_value_frame_state;
xiebaiyuan's avatar
xiebaiyuan 已提交
135 136 137 138 139
    frame_state[i] = r_value_frame_state;
    output_value[i] = r_output;
  }
}

140 141 142 143
#define FORWARD_RESET_OUTPUT(active_type, value, frame_size)            \
  hl_naive_gru_forward_reset_output<float, active_type>(                \
      value.gate_value, value.reset_output_value, value.prev_out_value, \
      frame_size);
xiebaiyuan's avatar
xiebaiyuan 已提交
144

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
template <typename T>
inline void forward_reset_output(GRUMetaValue<T> value, int frame_size,
                                 int batch_size, ActivationType active_node) {
  for (int b = 0; b < batch_size; ++b) {
    switch (active_node) {
      case RELU:
        FORWARD_RESET_OUTPUT(RELU, value, frame_size);
        break;
      case SIGMOID:
        FORWARD_RESET_OUTPUT(SIGMOID, value, frame_size);
        break;
      case TANH:
        FORWARD_RESET_OUTPUT(TANH, value, frame_size);
        break;
      default:
        FORWARD_RESET_OUTPUT(IDENTITY, value, frame_size);
    }
xiebaiyuan's avatar
xiebaiyuan 已提交
162 163 164 165 166 167 168 169
    value.gate_value += frame_size * 3;
    value.reset_output_value += frame_size;
    if (value.prev_out_value) {
      value.prev_out_value += frame_size;
    }
  }
}

170 171 172
#define FORWARD_FINAL_OUTPUT(active_type, value, frame_size) \
  hl_naive_gru_forward_final_output<float, active_type>(     \
      value.gate_value, value.prev_out_value, value.output_value, frame_size)
xiebaiyuan's avatar
xiebaiyuan 已提交
173

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
template <typename T>
inline void forward_final_output(GRUMetaValue<T> value, int frame_size,
                                 int batch_size, ActivationType active_node) {
  for (int b = 0; b < batch_size; ++b) {
    switch (active_node) {
      case RELU:
        FORWARD_FINAL_OUTPUT(RELU, value, frame_size);
        break;
      case SIGMOID:
        FORWARD_FINAL_OUTPUT(SIGMOID, value, frame_size);
        break;
      case TANH:
        FORWARD_FINAL_OUTPUT(TANH, value, frame_size);
        break;
      default:
        FORWARD_FINAL_OUTPUT(IDENTITY, value, frame_size);
    }
xiebaiyuan's avatar
xiebaiyuan 已提交
191 192 193 194 195 196 197 198 199 200 201
    value.gate_value += frame_size * 3;
    value.output_value += frame_size;
    if (value.prev_out_value) {
      value.prev_out_value += frame_size;
    }
  }
}

}  // namespace math
}  // namespace operators
}  // namespace paddle_mobile
202

xiebaiyuan's avatar
xiebaiyuan 已提交
203
#endif