elementwise_add_arm_func.h 3.2 KB
Newer Older
E
eclipsess 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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 ELEMENTWISEADD_OP

#pragma once
18

Z
zhaojiaying01 已提交
19 20
#include "operators/math/elementwise_op_function.h"
#include "operators/op_param.h"
21 22 23
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
#include <arm_neon.h>
#endif
E
eclipsess 已提交
24 25 26 27 28 29 30 31 32 33

namespace paddle_mobile {
namespace operators {

template <typename T>
struct AddFunctor {
  inline T operator()(T a, T b) const { return a + b; }
};

template <typename P>
N
nhzlx 已提交
34
void ElementwiseAddCompute(const ElementwiseAddParam<CPU> &param) {
E
eclipsess 已提交
35 36 37 38 39
  const Tensor *input_x = param.InputX();
  const Tensor *input_y = param.InputY();
  Tensor *Out = param.Out();
  Out->mutable_data<float>();
  int axis = param.Axis();
40
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
H
hjchen2 已提交
41 42 43 44
  const auto &x_dims = input_x->dims();
  const auto &y_dims = input_y->dims();
  /// axis = -1 represent the last dimensions.
  axis = (axis == -1 ? x_dims.size() - y_dims.size() : axis);
45
  size_t batch = 1;
H
hjchen2 已提交
46
  size_t channels = 1;
47 48
  size_t elementwise_num = 1;
  for (int i = 0; i < axis; ++i) {
H
hjchen2 已提交
49
    batch *= x_dims[i];
50
  }
H
hjchen2 已提交
51 52
  for (int i = 0; i < y_dims.size(); ++i) {
    channels *= y_dims[i];
53
  }
H
hjchen2 已提交
54 55 56 57 58 59
  for (int i = y_dims.size() + axis; i < x_dims.size(); ++i) {
    elementwise_num *= x_dims[i];
  }
  const float *bias_data = input_y->data<float>();
  const float *input_data = input_x->data<float>();
  float *output_data = Out->mutable_data<float>();
60
  for (int i = 0; i < batch; ++i) {
H
hjchen2 已提交
61
    #pragma omp parallel for
H
hjchen2 已提交
62 63 64 65 66
    for (int j = 0; j < channels; ++j) {
      size_t offset = (i * channels + j) * elementwise_num;
      const float *input = input_data + offset;
      const float *bias = bias_data + j;
      float *output = output_data + offset;
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

      int loop = elementwise_num >> 0x4;
      int remain = elementwise_num & 0xF;
      for (int k = 0; k < loop; ++k) {
        float32x4_t rb = vdupq_n_f32(*bias);
        float32x4_t r0 = vld1q_f32(input);
        float32x4_t r1 = vld1q_f32(input + 4);
        float32x4_t r2 = vld1q_f32(input + 8);
        float32x4_t r3 = vld1q_f32(input + 12);
        r0 = vaddq_f32(r0, rb);
        r1 = vaddq_f32(r1, rb);
        r2 = vaddq_f32(r2, rb);
        r3 = vaddq_f32(r3, rb);
        vst1q_f32(output, r0);
        vst1q_f32(output + 4, r1);
        vst1q_f32(output + 8, r2);
        vst1q_f32(output + 12, r3);
        input += 16;
        output += 16;
      }
      for (int k = 0; k < remain; ++k) {
        output[k] = input[k] + *bias;
      }
    }
  }
#else
E
eclipsess 已提交
93 94
  ElementwiseComputeEx<AddFunctor<float>, float>(input_x, input_y, axis,
                                                 AddFunctor<float>(), Out);
95
#endif
E
eclipsess 已提交
96 97 98 99 100 101 102 103
}

template class ElementwiseAddKernel<CPU, float>;

}  // namespace operators
}  // namespace paddle_mobile

#endif