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

Z
zhaojiaying01 已提交
15
#ifdef LRN_OP
W
wangliu 已提交
16 17 18
#ifdef _OPENMP
#include <omp.h>
#endif
Z
zhaojiaying01 已提交
19 20
#include "framework/operator.h"
#include "operators/op_param.h"
D
dolphin8 已提交
21

Z
zhaojiaying01 已提交
22 23
#include <cmath>

E
eclipsess 已提交
24
#ifdef __ARM_NEON
Z
zhaojiaying01 已提交
25
#include "arm_neon.h"
E
eclipsess 已提交
26 27 28
#include "operators/math/math_func_neon.h"
#endif

E
eclipsess 已提交
29 30 31
namespace paddle_mobile {
namespace operators {

Z
zhaojiaying01 已提交
32 33
using namespace framework;

朔-望's avatar
朔-望 已提交
34 35
template <typename T>
struct LRNFunctor {
36
  void operator()(const framework::Tensor &input, framework::Tensor *out, int N,
E
eclipsess 已提交
37 38 39
                  int C, int H, int W, int n, float k, float alpha,
                  float beta) {
    const float *input_ptr = input.data<float>();
40 41
    const int start = -(n - 1) / 2;
    const int end = start + n;
E
eclipsess 已提交
42
    auto out_ptr = out->data<T>();
E
eclipsess 已提交
43

44 45 46 47
    const int stride0 = C * H * W;
    const int stride1 = H * W;
    const int stride2 = W;
    framework::Tensor sqr_buffer;
E
eclipsess 已提交
48 49 50
    auto sqr_buffer_ptr = sqr_buffer.mutable_data<float>(input.dims());
    std::fill(sqr_buffer_ptr, sqr_buffer_ptr + sqr_buffer.numel(), 0.0);

51
    for (int a = 0; a < N; a++) {
W
wangliu 已提交
52
#pragma parallel for
53 54 55 56
      for (int b = 0; b < C; b++) {
        for (int index = start; index < end; index++) {
          int channel = b + index;
          if (channel >= 0 && channel < C) {
E
eclipsess 已提交
57 58
            int tmp_s = a * stride0 + b * stride1;
            int tmp_c = a * stride0 + channel * stride1;
E
eclipsess 已提交
59
#ifdef __ARM_NEON
E
eclipsess 已提交
60 61 62 63 64 65 66 67
            int n4 = stride1 / 4;
            int m4 = stride1 % 4;
            float32x4_t sqr0;
            float32x4_t in0;
            float32x4_t res0;
            for (int i = 0; i < n4; i++) {
              sqr0 = vld1q_f32(sqr_buffer_ptr + tmp_s);
              in0 = vld1q_f32(input_ptr + tmp_c);
E
eclipsess 已提交
68 69

              res0 = vmlaq_f32(sqr0, in0, in0);
E
eclipsess 已提交
70
              vst1q_f32(sqr_buffer_ptr + tmp_s, res0);
E
eclipsess 已提交
71

E
eclipsess 已提交
72 73
              tmp_s += 4;
              tmp_c += 4;
E
eclipsess 已提交
74 75
            }

E
eclipsess 已提交
76
            for (int i = 0; i < m4; i++) {
E
eclipsess 已提交
77 78 79 80 81 82 83 84 85 86
              int s_i = tmp_s + i;
              int c_i = tmp_c + i;
              sqr_buffer_ptr[s_i] += input_ptr[c_i] * input_ptr[c_i];
            }

#else
            for (int tmp = 0; tmp < stride1; tmp++) {
              int s_i = tmp_s + tmp;
              int c_i = tmp_c + tmp;
              sqr_buffer_ptr[s_i] += input_ptr[c_i] * input_ptr[c_i];
E
eclipsess 已提交
87
            }
E
eclipsess 已提交
88
#endif
89
          }
E
eclipsess 已提交
90
        }
91 92
      }
    }
E
eclipsess 已提交
93 94 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

#ifdef __ARM_NEON

    float32x4_t sqr1, sqr2, sqr3, sqr4;
    float32x4_t alpha4;
    float32x4_t k4;
    float32x4_t beta4;
    float32x4_t res1, res2, res3, res4;
    float32x4_t in1, in2, in3, in4;

    beta4 = vdupq_n_f32(beta);
    alpha4 = vdupq_n_f32(alpha);
    k4 = vdupq_n_f32(k);
    auto out_tmp_ptr = out_ptr;

    int n16 = input.numel() / 16;
    int m16 = input.numel() % 16;
    int m16n4 = m16 / 4;
    int m16m4 = m16 % 4;

    for (int i = 0; i < n16; i++) {
      sqr1 = vld1q_f32(sqr_buffer_ptr);
      sqr2 = vld1q_f32(sqr_buffer_ptr + 4);
      sqr3 = vld1q_f32(sqr_buffer_ptr + 8);
      sqr4 = vld1q_f32(sqr_buffer_ptr + 12);

      in1 = vld1q_f32(input_ptr);
      in2 = vld1q_f32(input_ptr + 4);
      in3 = vld1q_f32(input_ptr + 8);
      in4 = vld1q_f32(input_ptr + 12);

      sqr1 = vmlaq_f32(k4, sqr1, alpha4);
      sqr2 = vmlaq_f32(k4, sqr2, alpha4);
      sqr3 = vmlaq_f32(k4, sqr3, alpha4);
      sqr4 = vmlaq_f32(k4, sqr4, alpha4);

      sqr1 = pow_ps(sqr1, -beta4);
      sqr2 = pow_ps(sqr2, -beta4);
      sqr3 = pow_ps(sqr3, -beta4);
      sqr4 = pow_ps(sqr4, -beta4);

      sqr1 = vmulq_f32(sqr1, in1);
      sqr2 = vmulq_f32(sqr2, in2);
      sqr3 = vmulq_f32(sqr3, in3);
      sqr4 = vmulq_f32(sqr4, in4);

      vst1q_f32(out_tmp_ptr, sqr1);
      vst1q_f32(out_tmp_ptr + 4, sqr2);
      vst1q_f32(out_tmp_ptr + 8, sqr3);
      vst1q_f32(out_tmp_ptr + 12, sqr4);

      sqr_buffer_ptr += 4 * 4;
      input_ptr += 4 * 4;
      out_tmp_ptr += 4 * 4;
    }
    for (int i = 0; i < m16n4; i++) {
      sqr4 = vld1q_f32(sqr_buffer_ptr);
      in4 = vld1q_f32(input_ptr);
      sqr4 = vmlaq_f32(k4, sqr4, alpha4);
      sqr4 = pow_ps(sqr4, -beta4);
      sqr4 = vmulq_f32(sqr4, in4);
      vst1q_f32(out_tmp_ptr, sqr4);
      sqr_buffer_ptr += 4;
      input_ptr += 4;
      out_tmp_ptr += 4;
    }

    for (int i = 0; i < m16m4; i++) {
      out_tmp_ptr[i] = input_ptr[i] / pow(k + alpha * sqr_buffer_ptr[i], beta);
    }

#else
165
    for (int i = 0; i < input.numel(); i++) {
E
eclipsess 已提交
166
      out_ptr[i] = input_ptr[i] / pow(k + alpha * sqr_buffer_ptr[i], beta);
E
eclipsess 已提交
167
    }
E
eclipsess 已提交
168
#endif
169
  }
E
eclipsess 已提交
170 171 172 173
};

template <typename DeviceType, typename T>
class LrnKernel : public framework::OpKernelBase<DeviceType, LrnParam> {
朔-望's avatar
朔-望 已提交
174
 public:
175
  void Compute(const LrnParam &param) const;
E
eclipsess 已提交
176
  bool Init(LrnParam *param);
E
eclipsess 已提交
177
};
朔-望's avatar
朔-望 已提交
178 179
}  // namespace operators
}  // namespace paddle_mobile
L
liuruilong 已提交
180 181

#endif