dequant_bn_relu_kernel.cpp 5.3 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
#include "operators/kernel/dequant_bn_relu_kernel.h"
H
hjchen2 已提交
16 17 18 19 20 21 22 23
#include <cmath>
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
#include <arm_neon.h>
#endif

namespace paddle_mobile {
namespace operators {

24 25 26 27 28
#if defined(FUSION_DEQUANT_BN_RELU_OP) || defined(FUSION_DEQUANT_ADD_BN_RELU_OP)
void DequantBNReluCompute(const FusionDequantBNParam<CPU> *param) {
  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>();
H
hjchen2 已提交
29
  // dequantize params
30 31
  const float activation_scale = param->activation_scale_->data<float>()[0];
  const float weight_scale = param->weight_scale_;
H
hjchen2 已提交
32 33
  const float dequant_scale = activation_scale / weight_scale;

34 35 36 37
  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];
H
hjchen2 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

  #pragma omp parallel for collapse(2)
  for (int batch = 0; batch < batch_size; ++batch) {
    for (int c = 0; c < channels; ++c) {
      float scale = bn_scale[c] * dequant_scale;
      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;
      float32x4_t __scale = vdupq_n_f32(scale);
      float32x4_t __bias = vdupq_n_f32(bias);
      float32x4_t __zero = vdupq_n_f32(0.f);

      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);
        f0 = vmlaq_f32(__bias, __scale, f0);
        f1 = vmlaq_f32(__bias, __scale, f1);
        f2 = vmlaq_f32(__bias, __scale, f2);
        f3 = vmlaq_f32(__bias, __scale, f3);
        f0 = vmaxq_f32(__zero, f0);
        f1 = vmaxq_f32(__zero, f1);
        f2 = vmaxq_f32(__zero, f2);
        f3 = vmaxq_f32(__zero, 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) {
        y[k] = std::max(scale * x[k] + bias, 0.f);
      }
    }
  }
}
84
#endif
H
hjchen2 已提交
85

86 87 88 89 90 91 92 93 94 95
#ifdef FUSION_DEQUANT_BN_RELU_OP
template <>
bool FusionDequantBNReluKernel<CPU, float>::Init(
    FusionDequantBNReluParam<CPU> *param) {
  // 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_;
H
hjchen2 已提交
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
  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] = bn_bias_ptr[c] - inv_scale * mean_ptr[c];
  }
  return true;
}

template <>
void FusionDequantBNReluKernel<CPU, float>::Compute(
    const FusionDequantBNReluParam<CPU> &param) {
  DequantBNReluCompute(&param);
}
#endif  // FUSION_DEQUANT_BN_RELU_OP

#ifdef FUSION_DEQUANT_ADD_BN_RELU_OP
template <>
bool FusionDequantAddBNReluKernel<CPU, float>::Init(
    FusionDequantAddBNReluParam<CPU> *param) {
  // 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 <>
void FusionDequantAddBNReluKernel<CPU, float>::Compute(
    const FusionDequantAddBNReluParam<CPU> &param) {
  DequantBNReluCompute(&param);
}
H
hjchen2 已提交
147
#endif  // FUSION_DEQUANT_ADD_BN_RELU_OP
148 149 150

}  // namespace operators
}  // namespace paddle_mobile