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
Refine  
hjchen2 已提交
22

23 24 25
namespace paddle_mobile {
namespace operators {

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

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

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

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

56
  int32x4_t rnd = vround_f32<ROUND_NEAREST_AWAY_ZERO>(r);
H
Refine  
hjchen2 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  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);
72
  rnd = vaddq_s32(rnd, smask);
H
Refine  
hjchen2 已提交
73
  return rnd;
74 75
}
#endif
76 77 78 79

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

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

87 88 89 90 91 92 93
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);
  if (abs(abs(q - v) - 0.5) <= 0) {
    if (abs(q) % 2 != 0) {
      q = q + ((q > 0) ? -1 : 1);
94 95
    }
  }
96
  return static_cast<int8_t>(q);
97 98
}

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

  #pragma omp parallel for
109
  for (size_t i = 0; i < loop; ++i) {
H
hjchen2 已提交
110 111 112 113 114 115
    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);
116 117 118 119
    r0 = vmulq_n_f32(r0, scale);
    r1 = vmulq_n_f32(r1, scale);
    r2 = vmulq_n_f32(r2, scale);
    r3 = vmulq_n_f32(r3, scale);
120 121 122 123
    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);
124 125 126 127
    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 已提交
128 129
    int16x8_t q5 = vcombine_s16(d0, d1);
    int16x8_t q6 = vcombine_s16(d2, d3);
H
Refine  
hjchen2 已提交
130 131
    int8x8_t d5 = vmovn_s16(q5);
    int8x8_t d6 = vmovn_s16(q6);
H
hjchen2 已提交
132 133
    vst1_s8(local_y, d5);
    vst1_s8(local_y + 8, d6);
134
  }
H
hjchen2 已提交
135 136
  x += (loop << 4);
  y += (loop << 4);
137
#endif
138 139
  for (size_t i = 0; i < remain; ++i) {
    y[i] = Round<R>(x[i] * scale);
140 141 142
  }
}

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

152 153 154 155 156 157 158 159 160 161 162 163 164
  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);
165
  }
166
  max_abs = vmaxvq_f32(__max);
167
#endif
168 169
  for (size_t i = 0; i < remain; ++i) {
    max_abs = std::max(max_abs, std::abs(x[i]));
H
hjchen2 已提交
170
  }
171
  return max_abs;
H
hjchen2 已提交
172
}
173

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

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

}  // namespace operators
211
}  // namespace paddle_mobile
212 213

#endif