elementwise_add_arm_func.h 3.0 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

namespace paddle_mobile {
namespace operators {

template <typename T>
29 30 31 32
inline void ElementwiseAddCompute(const ElementwiseAddParam<CPU> &param) {
  const framework::Tensor *input_x = param.InputX();
  const framework::Tensor *input_y = param.InputY();
  framework::Tensor *Out = param.Out();
E
eclipsess 已提交
33
  int axis = param.Axis();
34

H
hjchen2 已提交
35 36 37 38
  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);
39
  size_t batch = 1;
H
hjchen2 已提交
40
  size_t channels = 1;
41 42
  size_t elementwise_num = 1;
  for (int i = 0; i < axis; ++i) {
H
hjchen2 已提交
43
    batch *= x_dims[i];
44
  }
H
hjchen2 已提交
45 46
  for (int i = 0; i < y_dims.size(); ++i) {
    channels *= y_dims[i];
47
  }
H
hjchen2 已提交
48 49 50 51 52 53
  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>();
54 55

  #pragma omp parallel for collapse(2)
56
  for (int i = 0; i < batch; ++i) {
H
hjchen2 已提交
57 58 59
    for (int j = 0; j < channels; ++j) {
      size_t offset = (i * channels + j) * elementwise_num;
      const float *input = input_data + offset;
60
      const float bias = bias_data[j];
H
hjchen2 已提交
61
      float *output = output_data + offset;
62 63
      int remain = elementwise_num;
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
64
      int loop = elementwise_num >> 0x4;
65
      remain = elementwise_num & 0xF;
66
      for (int k = 0; k < loop; ++k) {
67
        float32x4_t rb = vdupq_n_f32(bias);
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        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;
      }
83
#endif
84
      for (int k = 0; k < remain; ++k) {
85
        output[k] = input[k] + bias;
86 87 88
      }
    }
  }
E
eclipsess 已提交
89 90 91 92 93 94 95 96
}

template class ElementwiseAddKernel<CPU, float>;

}  // namespace operators
}  // namespace paddle_mobile

#endif