lrn_kernel.h 5.0 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
#pragma once
E
eclipsess 已提交
16

D
dolphin8 已提交
17
#include <cmath>
Z
zhaojiaying01 已提交
18 19
#include "framework/operator.h"
#include "operators/op_param.h"
D
dolphin8 已提交
20

E
eclipsess 已提交
21
#ifdef __ARM_NEON
Z
zhaojiaying01 已提交
22
#include <arm_neon.h>
E
eclipsess 已提交
23 24 25
#include "operators/math/math_func_neon.h"
#endif

E
eclipsess 已提交
26 27 28
namespace paddle_mobile {
namespace operators {

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

39 40 41 42
    const int stride0 = C * H * W;
    const int stride1 = H * W;
    const int stride2 = W;
    framework::Tensor sqr_buffer;
E
eclipsess 已提交
43 44 45
    auto sqr_buffer_ptr = sqr_buffer.mutable_data<float>(input.dims());
    std::fill(sqr_buffer_ptr, sqr_buffer_ptr + sqr_buffer.numel(), 0.0);

46 47 48 49 50
    for (int a = 0; a < N; a++) {
      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 已提交
51 52
            int tmp_s = a * stride0 + b * stride1;
            int tmp_c = a * stride0 + channel * stride1;
E
eclipsess 已提交
53
#ifdef __ARM_NEON
E
eclipsess 已提交
54 55 56 57 58 59 60 61
            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 已提交
62 63

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

E
eclipsess 已提交
66 67
              tmp_s += 4;
              tmp_c += 4;
E
eclipsess 已提交
68 69
            }

E
eclipsess 已提交
70
            for (int i = 0; i < m4; i++) {
E
eclipsess 已提交
71 72 73 74 75 76 77 78 79 80
              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 已提交
81
            }
E
eclipsess 已提交
82
#endif
83
          }
E
eclipsess 已提交
84
        }
85 86
      }
    }
E
eclipsess 已提交
87 88 89 90 91 92 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

#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
159
    for (int i = 0; i < input.numel(); i++) {
E
eclipsess 已提交
160
      out_ptr[i] = input_ptr[i] / pow(k + alpha * sqr_buffer_ptr[i], beta);
E
eclipsess 已提交
161
    }
E
eclipsess 已提交
162
#endif
163
  }
E
eclipsess 已提交
164 165 166 167
};

template <typename DeviceType, typename T>
class LrnKernel : public framework::OpKernelBase<DeviceType, LrnParam> {
朔-望's avatar
朔-望 已提交
168
 public:
169
  void Compute(const LrnParam &param) const;
E
eclipsess 已提交
170
  bool Init(LrnParam *param);
E
eclipsess 已提交
171
};
朔-望's avatar
朔-望 已提交
172 173
}  // namespace operators
}  // namespace paddle_mobile
L
liuruilong 已提交
174 175

#endif