relu_kernel.cpp 2.7 KB
Newer Older
E
eclipsess 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

L
liuruilong 已提交
15 16
#ifdef RELU_OP

E
eclipsess 已提交
17
#include "operators/kernel/relu_kernel.h"
18 19
#include "common/types.h"
#include "operators/math/activation.h"
20 21 22
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
#include <arm_neon.h>
#endif
E
eclipsess 已提交
23 24 25 26

namespace paddle_mobile {
namespace operators {

27
template <typename Dtype, ActivationType Act>
28 29 30 31
struct ReluCompute {
  void operator()(const Tensor *input, Tensor *output) {}
};

32 33
template <ActivationType Act>
struct ReluCompute<float, Act> {
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  void operator()(const Tensor *input, Tensor *output) {
    const float *x = input->data<float>();
    float *y = output->mutable_data<float>();
    size_t remain = input->numel();
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
    size_t loop = remain >> 4;
    remain = remain & 0xF;

    #pragma omp parallel for
    for (size_t i = 0; i < loop; ++i) {
      const float *local_x = x + (i << 4);
      float *local_y = y + (i << 4);
      float32x4_t r0 = vld1q_f32(local_x);
      float32x4_t r1 = vld1q_f32(local_x + 4);
      float32x4_t r2 = vld1q_f32(local_x + 8);
      float32x4_t r3 = vld1q_f32(local_x + 12);
50 51 52 53
      r0 = math::vActiveq_f32<Act>(r0);
      r1 = math::vActiveq_f32<Act>(r1);
      r2 = math::vActiveq_f32<Act>(r2);
      r3 = math::vActiveq_f32<Act>(r3);
54 55 56 57 58 59 60 61 62
      vst1q_f32(local_y, r0);
      vst1q_f32(local_y + 4, r1);
      vst1q_f32(local_y + 8, r2);
      vst1q_f32(local_y + 12, r3);
    }
    x += (loop << 4);
    y += (loop << 4);
#endif
    for (size_t i = 0; i < remain; ++i) {
63
      y[i] = math::Active<Act>(x[i]);
64 65 66 67
    }
  }
};

L
liuruilong 已提交
68
template <>
N
nhzlx 已提交
69
bool ReluKernel<CPU, float>::Init(ReluParam<CPU> *param) {
L
liuruilong 已提交
70 71 72
  return true;
}

E
eclipsess 已提交
73
template <>
L
liuruilong 已提交
74
void ReluKernel<CPU, float>::Compute(const ReluParam<CPU> &param) {
75 76
  const Tensor *input = param.InputX();
  Tensor *output = param.Out();
77
  ReluCompute<float, RELU>()(input, output);
78 79 80 81 82 83 84 85 86 87 88
}

template <>
bool Relu6Kernel<CPU, float>::Init(ReluParam<CPU> *param) {
  return true;
}

template <>
void Relu6Kernel<CPU, float>::Compute(const ReluParam<CPU> &param) {
  const Tensor *input = param.InputX();
  Tensor *output = param.Out();
89
  ReluCompute<float, RELU6>()(input, output);
E
eclipsess 已提交
90
}
E
eclipsess 已提交
91

E
eclipsess 已提交
92
}  // namespace operators
E
eclipsess 已提交
93
}  // namespace paddle_mobile
L
liuruilong 已提交
94 95

#endif