sigmoid_kernel.cpp 2.7 KB
Newer Older
W
wangliu 已提交
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 31 32 33 34 35 36 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
/* 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. */

#include "../sigmoid_kernel.h"
#if __ARM_NEON
#include "../../math/math_func_neon.h"
#endif

namespace paddle_mobile {
namespace operators {

using framework::DDim;
using framework::Tensor;

void sigmoid(const Tensor *X, Tensor *Y) {
#if __ARM_NEON
  DLOG << "step1";
  const float *input = X->data<float>();
  DLOG << "step11";

  float *output = Y->mutable_data<float>();
  DLOG << "step2";

  const DDim &dDim = X->dims();
  DLOG << "step3";

  int axis_index = 1;
  if (dDim.size() < 4) {
    axis_index = 0;
  }
  DLOG << "step4";

  DDim outer_ddim =
      paddle_mobile::framework::slice_ddim(dDim, 0, axis_index + 1);
  DDim inner_ddim =
      paddle_mobile::framework::slice_ddim(dDim, axis_index + 1, dDim.size());
  DLOG << "step5";

  int out_size = paddle_mobile::framework::product(outer_ddim);
  int inner_size = paddle_mobile::framework::product(inner_ddim);
  DLOG << "step6";

#pragma omp parallel for
  DLOG << "outsize=" << out_size;
  DLOG << "innersize=" << inner_size;
  for (int i = 0; i < out_size; ++i) {
    const float *input_outer_ptr = input + i * inner_size;
    float *output_outer_ptr = output + i * inner_size;
    int nn = inner_size >> 2;
    int remain = inner_size - (nn << 2);
    float32x4_t _one = vdupq_n_f32(1.f);
    for (; nn > 0; nn--) {
      float32x4_t data = vld1q_f32(input_outer_ptr);
      data = vnegq_f32(data);
      data = exp_ps(data);
      data = vaddq_f32(data, _one);
      float32x4_t out_data = vrecpeq_f32(data);
      out_data = vmulq_f32(vrecpsq_f32(data, out_data), out_data);
      vst1q_f32(output_outer_ptr, out_data);

      input_outer_ptr += 4;
      output_outer_ptr += 4;
    }
    for (; remain > 0; remain--) {
      *output_outer_ptr = 1.f / (1.f + exp(-*input_outer_ptr));
      output_outer_ptr++;
      input_outer_ptr++;
    }
  }
#endif
}

template <>
void SigmoidKernel<CPU, float>::Compute(const SigmoidParam &param) const {
  const Tensor *in_x = param.InputX();
  Tensor *out = param.Out();
  auto x_dims = in_x->dims();
  out->Resize(x_dims);
  sigmoid(in_x, out);
}

template class SigmoidKernel<CPU, float>;
}  // namespace operators
}  // namespace paddle_mobile