quantize_kernel.cpp 6.3 KB
Newer Older
T
Tian 已提交
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 QUANT_OP
16

17
#include "operators/kernel/quantize_kernel.h"
18
#include <cmath>
T
Tian 已提交
19

20 21
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
#include <arm_neon.h>
H
hjchen2 已提交
22
#endif
H
Refine  
hjchen2 已提交
23

24 25 26
namespace paddle_mobile {
namespace operators {

H
hjchen2 已提交
27
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
28
#ifndef __aarch64__
H
hjchen2 已提交
29
inline float32_t vmaxvq_f32(float32x4_t r) {
30 31 32 33 34
  float32x2_t v = vmax_f32(vget_high_f32(r), vget_low_f32(r));
  return vget_lane_f32(vpmax_f32(v, v), 0);
}
#endif

35 36 37 38
template <RoundType R = ROUND_NEAREST_TOWARDS_ZERO>
inline int32x4_t vround_f32(float32x4_t r) {
  return vcvtq_s32_f32(r);
}
39

40 41
template <>
inline int32x4_t vround_f32<ROUND_NEAREST_AWAY_ZERO>(float32x4_t r) {
42
  float32x4_t plus = vdupq_n_f32(0.5);
43
  float32x4_t minus = vdupq_n_f32(-0.5);
44
  float32x4_t zero = vdupq_n_f32(0);
H
Refine  
hjchen2 已提交
45
  uint32x4_t more_than_zero = vcgtq_f32(r, zero);
46
  float32x4_t temp = vbslq_f32(more_than_zero, plus, minus);
H
Refine  
hjchen2 已提交
47
  temp = vaddq_f32(r, temp);
48 49 50 51
  int32x4_t ret = vcvtq_s32_f32(temp);
  return ret;
}

52 53
template <>
inline int32x4_t vround_f32<ROUND_NEAREST_TO_EVEN>(float32x4_t r) {
H
Refine  
hjchen2 已提交
54 55 56 57
  float32x4_t point5 = vdupq_n_f32(0.5);
  int32x4_t one = vdupq_n_s32(1);
  int32x4_t zero = vdupq_n_s32(0);

58
  int32x4_t rnd = vround_f32<ROUND_NEAREST_AWAY_ZERO>(r);
H
Refine  
hjchen2 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  float32x4_t frnd = vcvtq_f32_s32(rnd);
  frnd = vsubq_f32(frnd, r);
  frnd = vabsq_f32(frnd);
  uint32x4_t equal_point5 = vceqq_f32(frnd, point5);
  int32x4_t abs_rnd = vabsq_s32(rnd);
  abs_rnd = vandq_s32(abs_rnd, one);
  uint32x4_t not_mod2 = vreinterpretq_u32_s32(abs_rnd);
  uint32x4_t mask = vandq_u32(equal_point5, not_mod2);
  uint32x4_t more_than_zero = vcgtq_s32(rnd, zero);
  more_than_zero = vandq_u32(more_than_zero, vreinterpretq_u32_s32(one));
  mask = veorq_u32(more_than_zero, mask);
  more_than_zero = veorq_u32(more_than_zero, vreinterpretq_u32_s32(one));
  mask = vaddq_u32(more_than_zero, mask);
  int32x4_t smask = vreinterpretq_s32_u32(mask);
  smask = vsubq_s32(smask, one);
74
  rnd = vaddq_s32(rnd, smask);
H
Refine  
hjchen2 已提交
75
  return rnd;
76 77
}
#endif
78 79 80 81

template <RoundType R = ROUND_NEAREST_TOWARDS_ZERO>
inline int8_t Round(const float &x) {
  return static_cast<int8_t>(x);
82 83
}

84 85 86 87
template <>
inline int8_t Round<ROUND_NEAREST_AWAY_ZERO>(const float &x) {
  return std::round(x);
}
H
hjchen2 已提交
88

89 90 91 92
template <>
inline int8_t Round<ROUND_NEAREST_TO_EVEN>(const float &x) {
  float v = std::round(x);
  int32_t q = static_cast<int32_t>(v);
H
hjchen2 已提交
93 94
  if (std::abs(std::abs(q - v) - 0.5) <= 0) {
    if (std::abs(q) % 2 != 0) {
95
      q = q + ((q > 0) ? -1 : 1);
96 97
    }
  }
98
  return static_cast<int8_t>(q);
99 100
}

101 102
template <RoundType R>
static void Quantize(const Tensor *input, const float scale, Tensor *output) {
103
  const float *x = input->data<const float>();
H
hjchen2 已提交
104
  int8_t *y = output->mutable_data<int8_t>();
105
  size_t remain = input->numel();
H
hjchen2 已提交
106
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
107 108
  size_t loop = remain >> 4;
  remain = remain & 0xF;
H
hjchen2 已提交
109 110

  #pragma omp parallel for
111
  for (size_t i = 0; i < loop; ++i) {
H
hjchen2 已提交
112 113 114 115 116 117
    const float *local_x = x + (i << 4);
    int8_t *local_y = y + (i << 4);
    float32x4_t r0 = vld1q_f32(local_x);
    float32x4_t r1 = vld1q_f32(local_x + 4);
    float32x4_t r2 = vld1q_f32(local_x + 8);
    float32x4_t r3 = vld1q_f32(local_x + 12);
118 119 120 121
    r0 = vmulq_n_f32(r0, scale);
    r1 = vmulq_n_f32(r1, scale);
    r2 = vmulq_n_f32(r2, scale);
    r3 = vmulq_n_f32(r3, scale);
122 123 124 125
    int32x4_t q0 = vround_f32<R>(r0);
    int32x4_t q1 = vround_f32<R>(r1);
    int32x4_t q2 = vround_f32<R>(r2);
    int32x4_t q3 = vround_f32<R>(r3);
126 127 128 129
    int16x4_t d0 = vmovn_s32(q0);
    int16x4_t d1 = vmovn_s32(q1);
    int16x4_t d2 = vmovn_s32(q2);
    int16x4_t d3 = vmovn_s32(q3);
H
hjchen2 已提交
130 131
    int16x8_t q5 = vcombine_s16(d0, d1);
    int16x8_t q6 = vcombine_s16(d2, d3);
H
Refine  
hjchen2 已提交
132 133
    int8x8_t d5 = vmovn_s16(q5);
    int8x8_t d6 = vmovn_s16(q6);
H
hjchen2 已提交
134 135
    vst1_s8(local_y, d5);
    vst1_s8(local_y + 8, d6);
136
  }
H
hjchen2 已提交
137 138
  x += (loop << 4);
  y += (loop << 4);
139
#endif
140 141
  for (size_t i = 0; i < remain; ++i) {
    y[i] = Round<R>(x[i] * scale);
142 143 144
  }
}

145 146
float find_abs_max(const Tensor *input) {
  float max_abs = 0.f;
147
  const float *x = input->data<const float>();
148
  size_t remain = input->numel();
149
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
150 151 152
  size_t loop = remain >> 4;
  remain = remain & 0xF;
  float32x4_t __max = {0.f, 0.f, 0.f, 0.f};
H
hjchen2 已提交
153

154 155 156 157 158 159 160 161 162 163 164 165 166
  for (size_t i = 0; i < loop; ++i, x += 16) {
    float32x4_t r0 = vld1q_f32(x);
    float32x4_t r1 = vld1q_f32(x + 4);
    float32x4_t r2 = vld1q_f32(x + 8);
    float32x4_t r3 = vld1q_f32(x + 12);
    r0 = vabsq_f32(r0);
    r1 = vabsq_f32(r1);
    r2 = vabsq_f32(r2);
    r3 = vabsq_f32(r3);
    r0 = vmaxq_f32(r0, r1);
    r1 = vmaxq_f32(r2, r3);
    r0 = vmaxq_f32(r0, r1);
    __max = vmaxq_f32(r0, __max);
167
  }
168
  max_abs = vmaxvq_f32(__max);
169
#endif
170 171
  for (size_t i = 0; i < remain; ++i) {
    max_abs = std::max(max_abs, std::abs(x[i]));
H
hjchen2 已提交
172
  }
173
  return max_abs;
H
hjchen2 已提交
174
}
175

176
template <>
177 178 179 180
bool QuantizeKernel<CPU, float>::Init(QuantizeParam<CPU> *param) {
  return true;
}

181
template <>
L
liuruilong 已提交
182
void QuantizeKernel<CPU, float>::Compute(const QuantizeParam<CPU> &param) {
183
  const Tensor *input = param.input_;
H
hjchen2 已提交
184
  Tensor *output = param.output_;
185
  Tensor *output_scale = param.online_scale_;
H
hjchen2 已提交
186
  float max_abs = 0.f;
187 188 189 190 191
  if (param.is_static_) {
    max_abs = param.static_scale_;
  } else {
    max_abs = find_abs_max(input);
  }
H
hjchen2 已提交
192
  max_abs = std::max(max_abs, 1e-6f);
193
  // only support int8 currently
194 195
  float scale = 127 / max_abs;
  param.online_scale_->mutable_data<float>()[0] = max_abs;
196 197
  switch (param.round_type_) {
    case ROUND_NEAREST_TO_EVEN:
198
      Quantize<ROUND_NEAREST_TO_EVEN>(input, scale, output);
199 200
      break;
    case ROUND_NEAREST_TOWARDS_ZERO:
201
      Quantize<ROUND_NEAREST_TOWARDS_ZERO>(input, scale, output);
202 203
      break;
    case ROUND_NEAREST_AWAY_ZERO:
204
      Quantize<ROUND_NEAREST_AWAY_ZERO>(input, scale, output);
205
      break;
206 207 208 209
    default:
      LOG(kLOG_ERROR) << "round type is not supported.";
      break;
  }
210 211 212
}

}  // namespace operators
213
}  // namespace paddle_mobile
214 215

#endif