batchnorm_kernel.cpp 3.2 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 15 16 17 18 19 20 21 22 23

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

#pragma once

#include "operators/kernel/batchnorm_kernel.h"

namespace paddle_mobile {
namespace operators {

template <>
void BatchNormKernel<CPU, float>::Compute(const BatchNormParam &param) const {
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  /// todo: test.
  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 已提交
46

47 48 49 50 51 52
  Tensor inv_std;
  auto inv_std_ptr = inv_std.mutable_data<float>(make_ddim({C}));
  if (C != variance->numel()) {
    std::cout << "C must equal to variance.numel()" << std::endl;
  }
  assert(C == variance->numel());
E
eclipsess 已提交
53

54 55 56 57 58 59
  /// std = (var + epsilon).sqrt();
  /// inv_std = 1 / std;
  for (int i = 0; i < C; i++) {
    inv_std_ptr[i] =
        1 / static_cast<float>(pow((variance_ptr[i] + epsilon), 0.5));
  }
E
eclipsess 已提交
60

61 62 63 64
  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}));
E
eclipsess 已提交
65

66 67
  /// ((x - est_mean) * (inv_var) * scale + bias equal to
  /// (x * inv_var * scale) + (bias - est_mean * inv_var * scale)
E
eclipsess 已提交
68 69 70 71 72 73 74 75 76 77 78
  for (int i = 0; i < C; i++) {
    new_scale_ptr[i] = inv_std_ptr[i] * scale_ptr[i];
    new_bias_ptr[i] = bias_ptr[i] - mean_ptr[i] * inv_std_ptr[i] * scale_ptr[i];
    {
      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] =
                input_x_ptr[index] * new_scale_ptr[i] + new_bias_ptr[i];
79
          }
E
eclipsess 已提交
80
        }
81
      }
E
eclipsess 已提交
82
    }
83 84 85 86 87 88 89 90
  }
  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];
E
eclipsess 已提交
91
}
朔-望's avatar
朔-望 已提交
92 93
}  // namespace operators
}  // namespace paddle_mobile