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
H
update  
hjchen2 已提交
18 19

#include <cmath>
W
wangliu 已提交
20 21 22
#ifdef _OPENMP
#include <omp.h>
#endif
E
eclipsess 已提交
23
#ifdef __ARM_NEON
H
update  
hjchen2 已提交
24 25
#include <arm_neon.h>
#include "operators/math/math.h"
E
eclipsess 已提交
26
#endif
H
update  
hjchen2 已提交
27 28
#include "framework/operator.h"
#include "operators/op_param.h"
E
eclipsess 已提交
29

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

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

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

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

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

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

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

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

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

#endif