batchnorm_kernel.cpp 8.5 KB
Newer Older
E
eclipsess 已提交
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
E
eclipsess 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

L
liuruilong 已提交
15 16
#ifdef BATCHNORM_OP

E
eclipsess 已提交
17
#include "operators/kernel/batchnorm_kernel.h"
D
dolphin8 已提交
18
#include <cmath>
E
eclipsess 已提交
19 20 21 22

namespace paddle_mobile {
namespace operators {

L
liuruilong 已提交
23 24 25 26 27
template <>
bool BatchNormKernel<CPU, float>::Init(const BatchNormParam &para) const {
  return true;
}

E
eclipsess 已提交
28 29
template <>
void BatchNormKernel<CPU, float>::Compute(const BatchNormParam &param) const {
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  const Tensor *input_x = param.InputX();
  auto input_x_ptr = input_x->data<float>();
  const auto &x_dims = input_x->dims();
  const int N = x_dims[0];
  const int C = x_dims[1];
  const int H = x_dims[2];
  const int W = x_dims[3];
  const int stride0 = C * H * W;
  const int stride1 = H * W;
  const int stride2 = W;
  Tensor *out = param.OutputY();
  auto out_ptr = out->mutable_data<float>();
  const float epsilon = param.Epsilon();
  const Tensor *mean = param.InputMean();
  const Tensor *variance = param.InputVariance();
  const Tensor *scale = param.InputScale();
  const Tensor *bias = param.InputBias();
  auto mean_ptr = mean->data<float>();
  auto variance_ptr = variance->data<float>();
  auto scale_ptr = scale->data<float>();
  auto bias_ptr = bias->data<float>();
E
eclipsess 已提交
51

L
liuruilong 已提交
52 53 54
  //  Tensor inv_std;
  //  auto inv_std_ptr = inv_std.mutable_data<float>(make_ddim({C}));

L
liuruilong 已提交
55 56
  PADDLE_MOBILE_ENFORCE(C == variance->numel(),
                        "C must equal to variance.numel()");
L
liuruilong 已提交
57 58 59 60 61

  int HXW = H * W;
  if (HXW > 32) {
    int NXC = N * C;
    float *inv_std_ptr = new float[NXC * 4];
L
liuruilong 已提交
62 63
    float *volatile new_scale_ptr = new float[NXC * 4];
    float *volatile new_bias_ptr = new float[NXC * 4];
L
liuruilong 已提交
64 65 66 67

    /// std = (var + epsilon).sqrt();
    /// inv_std = 1 / std;
    for (int i = 0; i < C * 4; i += 4) {
L
liuruilong 已提交
68
      int index = i / 4;
L
liuruilong 已提交
69
      inv_std_ptr[i] =
L
liuruilong 已提交
70
          1 / static_cast<float>(pow((variance_ptr[index] + epsilon), 0.5));
L
liuruilong 已提交
71 72 73 74
      inv_std_ptr[i + 1] = inv_std_ptr[i];
      inv_std_ptr[i + 2] = inv_std_ptr[i];
      inv_std_ptr[i + 3] = inv_std_ptr[i];

L
liuruilong 已提交
75
      new_scale_ptr[i] = inv_std_ptr[i] * scale_ptr[index];
L
liuruilong 已提交
76 77 78 79
      new_scale_ptr[i + 1] = new_scale_ptr[i];
      new_scale_ptr[i + 2] = new_scale_ptr[i];
      new_scale_ptr[i + 3] = new_scale_ptr[i];

L
liuruilong 已提交
80
      new_bias_ptr[i] =
L
liuruilong 已提交
81
          bias_ptr[index] - mean_ptr[index] * inv_std_ptr[i] * scale_ptr[index];
L
liuruilong 已提交
82 83 84 85 86 87 88 89 90 91 92 93

      new_bias_ptr[i + 1] = new_bias_ptr[i];
      new_bias_ptr[i + 2] = new_bias_ptr[i];
      new_bias_ptr[i + 3] = new_bias_ptr[i];
    }

    for (int j = C * 4; j < NXC * 4; ++j) {
      new_scale_ptr[j] = new_scale_ptr[j - C * 4];
      new_bias_ptr[j] = new_bias_ptr[j - C * 4];
    }

    asm volatile(
L
liuruilong 已提交
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 165 166 167 168 169 170 171
        "subs %[N], %[N], #1                  \n\t"
        "blt        end_n_%=                  \n\t"
        "loop_n_%=:                           \n\t"

        "subs %[C], %[C], #1                   \n\t"
        "blt        end_c_%=                  \n\t"
        "loop_c_%=:                           \n\t"

        "vld1.32 {q9}, [%[new_scale_ptr]]!    \n\t"
        "vld1.32 {q10}, [%[new_bias_ptr]]!    \n\t"

        "mov r6, %[HXW]       \n\t"

        "subs r6, r6, #32                       \n\t"
        "blt        end_hw_%=                   \n\t"
        "loop_hw_%=:                            \n\t"

        "vld1.32 {q1, q2}, [%[input_x_ptr]]!    \n\t"
        "vld1.32 {q3, q4}, [%[input_x_ptr]]!    \n\t"
        "vld1.32 {q5, q6}, [%[input_x_ptr]]!    \n\t"
        "vld1.32 {q7, q8}, [%[input_x_ptr]]!    \n\t"

        "vmul.f32   q1, q1,   q9  \n\t"
        "vmul.f32   q2, q2,   q9  \n\t"
        "vmul.f32   q3, q3,   q9  \n\t"
        "vmul.f32   q4, q4,   q9  \n\t"

        "vmul.f32   q5, q5,   q9  \n\t"
        "vmul.f32   q6, q6,   q9  \n\t"
        "vmul.f32   q7, q7,   q9  \n\t"
        "vmul.f32   q8, q8,   q9  \n\t"

        "vadd.f32   q1,  q1,  q10 \n\t"
        "vadd.f32   q2, q2,   q10  \n\t"
        "vadd.f32   q3, q3,   q10  \n\t"
        "vadd.f32   q4,  q4,  q10 \n\t"
        "vadd.f32   q5,  q5,  q10 \n\t"
        "vadd.f32   q6,  q6,  q10 \n\t"
        "vadd.f32   q7,  q7,  q10 \n\t"
        "vadd.f32   q8,  q8,  q10 \n\t"

        "vst1.32 {q1, q2}, [%[out_ptr]]!        \n\t"
        "vst1.32 {q3, q4}, [%[out_ptr]]!       \n\t"
        "vst1.32 {q5, q6}, [%[out_ptr]]!       \n\t"
        "vst1.32 {q7, q8}, [%[out_ptr]]!       \n\t"

        "subs r6, r6, #32                    \n\t"
        "bge        loop_hw_%=                \n\t"
        "end_hw_%=:                           \n\t"

        "cmp  r6, #0                                \n\t"
        "bge  end_remainder_%=                      \n\t"
        "mov r5, #4                             \n\t"
        "mul  r6, r6, r5                            \n\t"
        "add %[input_x_ptr], %[input_x_ptr], r6     \n\t"

        "vld1.32 {q1, q2}, [%[input_x_ptr]]!    \n\t"
        "vld1.32 {q3, q4}, [%[input_x_ptr]]!    \n\t"
        "vld1.32 {q5, q6}, [%[input_x_ptr]]!    \n\t"
        "vld1.32 {q7, q8}, [%[input_x_ptr]]!    \n\t"

        "vmul.f32   q1, q1,   q9  \n\t"
        "vmul.f32   q2, q2,   q9  \n\t"
        "vmul.f32   q3, q3,   q9  \n\t"
        "vmul.f32   q4, q4,   q9  \n\t"
        "vmul.f32   q5, q5,   q9  \n\t"
        "vmul.f32   q6, q6,   q9  \n\t"
        "vmul.f32   q7, q7,   q9  \n\t"
        "vmul.f32   q8, q8,   q9  \n\t"
        "vadd.f32   q1,  q1,  q10 \n\t"
        "vadd.f32   q2, q2,   q10  \n\t"
        "vadd.f32   q3, q3,   q10  \n\t"
        "vadd.f32   q4,  q4,  q10 \n\t"
        "vadd.f32   q5,  q5,  q10 \n\t"
        "vadd.f32   q6,  q6,  q10 \n\t"
        "vadd.f32   q7,  q7,  q10 \n\t"
        "vadd.f32   q8,  q8,  q10 \n\t"

L
liuruilong 已提交
172
        "add %[out_ptr], %[out_ptr], r6         \n\t"
L
liuruilong 已提交
173
        "vst1.32 {q1, q2}, [%[out_ptr]]!        \n\t"
L
liuruilong 已提交
174 175 176
        "vst1.32 {q3, q4}, [%[out_ptr]]!        \n\t"
        "vst1.32 {q5, q6}, [%[out_ptr]]!        \n\t"
        "vst1.32 {q7, q8}, [%[out_ptr]]!        \n\t"
L
liuruilong 已提交
177

L
liuruilong 已提交
178
        "end_remainder_%=:                      \n\t"
L
liuruilong 已提交
179 180 181 182 183

        "subs %[C], %[C], #1                    \n\t"
        "bge        loop_c_%=                   \n\t"
        "end_c_%=:                              \n\t"

L
liuruilong 已提交
184 185 186
        "subs %[N], %[N], #1                    \n\t"
        "bge        loop_n_%=                   \n\t"
        "end_n_%=:                              \n\t"
L
liuruilong 已提交
187 188 189 190 191 192 193 194 195 196
        :
        : [input_x_ptr] "r"(input_x_ptr), [out_ptr] "r"(out_ptr),
          [new_scale_ptr] "r"(new_scale_ptr), [new_bias_ptr] "r"(new_bias_ptr),
          [N] "r"(N), [C] "r"(C), [HXW] "r"(HXW)
        : "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9",
          "q10", "r5", "r6");

    delete[] inv_std_ptr;
    delete[] new_scale_ptr;
    delete[] new_bias_ptr;
L
liuruilong 已提交
197 198 199 200 201

  } else {
    float *inv_std_ptr = new float[C];
    for (int i = 0; i < C; i++) {
      inv_std_ptr[i] =
L
liuruilong 已提交
202
          1 / static_cast<float>(pow((variance_ptr[i] + epsilon), 0.5));
L
liuruilong 已提交
203 204 205 206 207 208 209 210 211 212 213
    }

    Tensor new_scale;
    auto new_scale_ptr = new_scale.mutable_data<float>(make_ddim({C}));
    Tensor new_bias;
    auto new_bias_ptr = new_bias.mutable_data<float>(make_ddim({C}));

    /// ((x - est_mean) * (inv_var) * scale + bias equal to
    /// (x * inv_var * scale) + (bias - est_mean * inv_var * scale)
    for (int i = 0; i < C; i++) {
      new_scale_ptr[i] = inv_std_ptr[i] * scale_ptr[i];
L
liuruilong 已提交
214 215
      new_bias_ptr[i] =
          bias_ptr[i] - mean_ptr[i] * inv_std_ptr[i] * scale_ptr[i];
L
liuruilong 已提交
216 217 218 219 220 221 222
      {
        for (int n = 0; n < N; n++) {
          for (int h = 0; h < H; h++) {
            int tmp_index = n * stride0 + i * stride1 + h * stride2;
            for (int w = 0; w < W; w++) {
              int index = tmp_index + w;
              out_ptr[index] =
L
liuruilong 已提交
223
                  input_x_ptr[index] * new_scale_ptr[i] + new_bias_ptr[i];
L
liuruilong 已提交
224
            }
225
          }
E
eclipsess 已提交
226
        }
227
      }
E
eclipsess 已提交
228
    }
L
liuruilong 已提交
229

L
liuruilong 已提交
230 231 232 233 234 235 236 237
    delete[] inv_std_ptr;
    //    DLOG << "input[2,5,1,0](input[102]) ,channel 5 :";
    //    DLOG << "input_x_ptr : " << input_x_ptr[102];
    //    DLOG << "variance : " << variance_ptr[5];
    //    DLOG << "inv_std_ptr : " << inv_std_ptr[5];
    //    DLOG << "new_scale_ptr : " << new_scale_ptr[5];
    //    DLOG << "new_bias_ptr : " << new_bias_ptr[5];
    //    DLOG << "out_ptr : " << out_ptr[102];
238
  }
E
eclipsess 已提交
239
}
L
liuruilong 已提交
240

朔-望's avatar
朔-望 已提交
241 242
}  // namespace operators
}  // namespace paddle_mobile
L
liuruilong 已提交
243 244

#endif