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

L
liuruilong 已提交
15 16
#pragma once

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

Z
zhaojiaying01 已提交
24 25
#include <cmath>

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

E
eclipsess 已提交
31 32 33
namespace paddle_mobile {
namespace operators {

Z
zhaojiaying01 已提交
34 35
using namespace framework;

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

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

53
    for (int a = 0; a < N; a++) {
W
wangliu 已提交
54
#pragma parallel for
55 56 57 58
      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 已提交
59 60
            int tmp_s = a * stride0 + b * stride1;
            int tmp_c = a * stride0 + channel * stride1;
E
eclipsess 已提交
61
#ifdef __ARM_NEON
E
eclipsess 已提交
62 63 64 65 66 67 68 69
            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 已提交
70 71

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

E
eclipsess 已提交
74 75
              tmp_s += 4;
              tmp_c += 4;
E
eclipsess 已提交
76 77
            }

E
eclipsess 已提交
78
            for (int i = 0; i < m4; i++) {
E
eclipsess 已提交
79 80 81 82 83 84 85 86 87 88
              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 已提交
89
            }
E
eclipsess 已提交
90
#endif
91
          }
E
eclipsess 已提交
92
        }
93 94
      }
    }
E
eclipsess 已提交
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 165 166

#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
167
    for (int i = 0; i < input.numel(); i++) {
E
eclipsess 已提交
168
      out_ptr[i] = input_ptr[i] / pow(k + alpha * sqr_buffer_ptr[i], beta);
E
eclipsess 已提交
169
    }
E
eclipsess 已提交
170
#endif
171
  }
E
eclipsess 已提交
172 173 174
};

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

#endif