dequant_add_bn_kernel.cpp 4.2 KB
Newer Older
H
hjchen2 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

15
#ifdef FUSION_DEQUANT_ADD_BN_OP
H
hjchen2 已提交
16

17
#include "operators/kernel/dequant_add_bn_kernel.h"
H
hjchen2 已提交
18 19 20 21 22 23 24 25 26
#include <cmath>
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
#include <arm_neon.h>
#endif

namespace paddle_mobile {
namespace operators {

template <>
27 28
bool FusionDequantAddBNKernel<CPU, float>::Init(
    FusionDequantAddBNParam<CPU> *param) {
H
hjchen2 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  // elementwise add params
  const Tensor *bias = param->bias_;
  // batch norm params
  const Tensor *bn_mean = param->bn_mean_;
  const Tensor *bn_variance = param->bn_variance_;
  Tensor *bn_scale = param->bn_scale_;
  Tensor *bn_bias = param->bn_bias_;
  const float epsilon = param->epsilon_;

  const float *bias_ptr = bias->data<float>();
  const float *mean_ptr = bn_mean->data<float>();
  const float *var_ptr = bn_variance->data<float>();
  float *bn_scale_ptr = bn_scale->mutable_data<float>();
  float *bn_bias_ptr = bn_bias->mutable_data<float>();
  for (int c = 0; c < bn_scale->numel(); ++c) {
    float inv_scale = bn_scale_ptr[c] / (std::sqrt(var_ptr[c] + epsilon));
    bn_scale_ptr[c] = inv_scale;
    bn_bias_ptr[c] = inv_scale * (bias_ptr[c] - mean_ptr[c]) + bn_bias_ptr[c];
  }
  return true;
}

template <>
52 53
void FusionDequantAddBNKernel<CPU, float>::Compute(
    const FusionDequantAddBNParam<CPU> &param) {
H
hjchen2 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  const int32_t *input = param.input_->data<int32_t>();
  const float *bn_scale = param.bn_scale_->data<float>();
  const float *bn_bias = param.bn_bias_->data<float>();
  // dequantize params
  const float activation_scale = param.activation_scale_->data<float>()[0];
  const float weight_scale = param.weight_scale_;
  const float dequant_scale = activation_scale / weight_scale;

  float *output = param.output_->mutable_data<float>();
  int batch_size = param.input_->dims()[0];
  int channels = param.input_->dims()[1];
  size_t spatial_size = param.input_->dims()[2] * param.input_->dims()[3];

  #pragma omp parallel for collapse(2)
  for (int batch = 0; batch < batch_size; ++batch) {
    for (int c = 0; c < channels; ++c) {
70 71 72
      // not fuse bn and dequant scale to minimize precision difference
      // float scale = bn_scale[c] * dequant_scale;
      float scale = bn_scale[c];
H
hjchen2 已提交
73 74 75 76 77 78 79 80
      float bias = bn_bias[c];
      size_t offset = (batch * channels + c) * spatial_size;
      const int32_t *x = input + offset;
      float *y = output + offset;
      size_t remain = spatial_size;
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
      int loop = spatial_size >> 4;
      remain = spatial_size & 0xF;
81
      float32x4_t __dequant_scale = vdupq_n_f32(dequant_scale);
H
hjchen2 已提交
82 83 84 85 86 87 88 89 90 91 92
      float32x4_t __scale = vdupq_n_f32(scale);
      float32x4_t __bias = vdupq_n_f32(bias);
      for (int k = 0; k < loop; ++k, x += 16, y += 16) {
        int32x4_t r0 = vld1q_s32(x);
        int32x4_t r1 = vld1q_s32(x + 4);
        int32x4_t r2 = vld1q_s32(x + 8);
        int32x4_t r3 = vld1q_s32(x + 12);
        float32x4_t f0 = vcvtq_f32_s32(r0);
        float32x4_t f1 = vcvtq_f32_s32(r1);
        float32x4_t f2 = vcvtq_f32_s32(r2);
        float32x4_t f3 = vcvtq_f32_s32(r3);
93 94 95 96
        f0 = vmulq_f32(__dequant_scale, f0);
        f1 = vmulq_f32(__dequant_scale, f1);
        f2 = vmulq_f32(__dequant_scale, f2);
        f3 = vmulq_f32(__dequant_scale, f3);
H
hjchen2 已提交
97 98 99 100 101 102 103 104 105 106 107
        f0 = vmlaq_f32(__bias, __scale, f0);
        f1 = vmlaq_f32(__bias, __scale, f1);
        f2 = vmlaq_f32(__bias, __scale, f2);
        f3 = vmlaq_f32(__bias, __scale, f3);
        vst1q_f32(y, f0);
        vst1q_f32(y + 4, f1);
        vst1q_f32(y + 8, f2);
        vst1q_f32(y + 12, f3);
      }
#endif  // __ARM_NEON__
      for (int k = 0; k < remain; ++k) {
108
        y[k] = scale * (dequant_scale * x[k]) + bias;
H
hjchen2 已提交
109 110 111 112 113 114 115 116
      }
    }
  }
}

}  // namespace operators
}  // namespace paddle_mobile

117
#endif  // FUSION_DEQUANT_ADD_BN_OP