quantize_kernel.cpp 8.7 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 16
#ifdef PADDLE_MOBILE_CPU

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 26 27 28 29
#ifndef __aarch64__
float32_t vmaxvq_f32(float32x4_t r) {
  float32x2_t v = vmax_f32(vget_high_f32(r), vget_low_f32(r));
  return vget_lane_f32(vpmax_f32(v, v), 0);
}
#endif

30
int32x4_t vrnd_towards_zero(float32x4_t r) { return vcvtq_s32_f32(r); }
31 32

int32x4_t vrnd_away_zero(float32x4_t r) {
33
  float32x4_t plus = vdupq_n_f32(0.5);
34
  float32x4_t minus = vdupq_n_f32(-0.5);
35
  float32x4_t zero = vdupq_n_f32(0);
H
Refine  
hjchen2 已提交
36
  uint32x4_t more_than_zero = vcgtq_f32(r, zero);
37
  float32x4_t temp = vbslq_f32(more_than_zero, plus, minus);
H
Refine  
hjchen2 已提交
38
  temp = vaddq_f32(r, temp);
39 40 41 42 43
  int32x4_t ret = vcvtq_s32_f32(temp);
  return ret;
}

int32x4_t vrnd_to_even(float32x4_t r) {
H
Refine  
hjchen2 已提交
44
#if 0
45
  int32x4_t ret;
H
Refine  
hjchen2 已提交
46 47
  float value[4];
  vst1q_f32(value, r);
48
  for (int i = 0; i < 4; ++i) {
H
Refine  
hjchen2 已提交
49
    float v = round(value[i]);
50
    int32_t q = (int32_t)v;
H
Refine  
hjchen2 已提交
51
    if (abs(abs(v - value[i]) - 0.5) > 0) {
52 53 54 55 56
      ret[i] = q;
    } else {
      if (abs(q) % 2 == 0) {
        ret[i] = q;
      } else {
H
hjchen2 已提交
57
        ret[i] = q + ((q > 0) ? -1 : 1);
58 59 60 61
      }
    }
  }
  return ret;
62
#else
H
Refine  
hjchen2 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  float32x4_t point5 = vdupq_n_f32(0.5);
  int32x4_t one = vdupq_n_s32(1);
  int32x4_t zero = vdupq_n_s32(0);

  int32x4_t rnd = vrnd_away_zero(r);
  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);
83
  rnd = vaddq_s32(rnd, smask);
H
Refine  
hjchen2 已提交
84
  return rnd;
85
#endif
86 87 88
}
#endif

89 90 91
namespace paddle_mobile {
namespace operators {

92
static float find_abs_max(const Tensor *input) {
93
  float max_abs = 0.f;
94
  const float *x = input->data<const float>();
95 96 97 98 99 100
  size_t size = input->numel();
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
  size_t loop = size >> 4;
  size_t remain = size & 0xF;
  for (size_t i = 0; i < loop; ++i) {
    float32x4_t max;
H
Refine  
hjchen2 已提交
101 102 103 104 105
    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);
106 107 108
    r1 = vabsq_f32(r1);
    r2 = vabsq_f32(r2);
    r3 = vabsq_f32(r3);
H
Refine  
hjchen2 已提交
109 110 111 112
    max[0] = vmaxvq_f32(r0);
    max[1] = vmaxvq_f32(r1);
    max[2] = vmaxvq_f32(r2);
    max[3] = vmaxvq_f32(r3);
113 114 115 116 117 118 119 120 121
    max[0] = vmaxvq_f32(max);
    if (max[0] > max_abs) {
      max_abs = max[0];
    }
    x += 16;
  }
  size = remain;
#endif
  for (size_t i = 0; i < size; ++i) {
122 123 124 125 126 127 128 129
    float value = std::abs(x[i]);
    if (value > max_abs) {
      max_abs = value;
    }
  }
  return max_abs;
}

130
static void quantize_round_to_even(const Tensor *input, const float scale,
131
                                   Tensor *output) {
132
  const float *x = input->data<const float>();
H
hjchen2 已提交
133
  int8_t *y = output->mutable_data<int8_t>();
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  size_t size = input->numel();
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
  size_t loop = size >> 4;
  size_t remain = size & 0xF;
  for (size_t i = 0; i < loop; ++i) {
    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 = vmulq_n_f32(r0, scale);
    r1 = vmulq_n_f32(r1, scale);
    r2 = vmulq_n_f32(r2, scale);
    r3 = vmulq_n_f32(r3, scale);
    int32x4_t q0 = vrnd_to_even(r0);
    int32x4_t q1 = vrnd_to_even(r1);
    int32x4_t q2 = vrnd_to_even(r2);
    int32x4_t q3 = vrnd_to_even(r3);
    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 已提交
155 156
    int16x8_t q5 = vcombine_s16(d0, d1);
    int16x8_t q6 = vcombine_s16(d2, d3);
H
Refine  
hjchen2 已提交
157 158 159 160
    int8x8_t d5 = vmovn_s16(q5);
    int8x8_t d6 = vmovn_s16(q6);
    vst1_s8(y, d5);
    vst1_s8(y + 8, d6);
161 162 163 164 165 166
    x += 16;
    y += 16;
  }
  size = remain;
#endif
  for (size_t i = 0; i < size; ++i) {
167
    float value = x[i] * scale;
H
Refine  
hjchen2 已提交
168 169 170 171
    float v = round(value);
    int32_t q = (int32_t)v;
    if (abs(abs(q - value) - 0.5) > 0) {
      y[i] = q;
172
    } else {
H
Refine  
hjchen2 已提交
173 174
      if (abs(q) % 2 == 0) {
        y[i] = q;
175
      } else {
H
hjchen2 已提交
176
        y[i] = q + ((q > 0) ? -1 : 1);
177 178 179 180 181
      }
    }
  }
}

182 183
static void quantize_round_to_zero(const Tensor *input, const float scale,
                                   Tensor *output) {
184
  const float *x = input->data<const float>();
H
hjchen2 已提交
185
  int8_t *y = output->mutable_data<int8_t>();
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
  size_t size = input->numel();
#ifdef defined(__ARM_NEON__) || defined(__ARM_NEON)
  size_t loop = size >> 4;
  size_t remain = size & 0xF;
  for (size_t i = 0; i < loop; ++i) {
    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 = vmulq_n_f32(r0, scale);
    r1 = vmulq_n_f32(r1, scale);
    r2 = vmulq_n_f32(r2, scale);
    r3 = vmulq_n_f32(r3, scale);
    int32x4_t q0 = vrnd_towards_zero(r0);
    int32x4_t q1 = vrnd_towards_zero(r1);
    int32x4_t q2 = vrnd_towards_zero(r2);
    int32x4_t q3 = vrnd_towards_zero(r3);
    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 已提交
207 208
    int16x8_t q5 = vcombine_s16(d0, d1);
    int16x8_t q6 = vcombine_s16(d2, d3);
H
Refine  
hjchen2 已提交
209 210 211 212
    int8x8_t d5 = vmovn_s16(q5);
    int8x8_t d6 = vmovn_s16(q6);
    vst1_s8(y, d5);
    vst1_s8(y + 8, d6);
213 214 215 216 217 218
    x += 16;
    y += 16;
  }
  size = remain;
#endif
  for (size_t i = 0; i < size; ++i) {
219 220 221 222
    y[i] = trunc(x[i] * scale);
  }
}

223 224
static void quantize_round_to_nearest(const Tensor *input, const float scale,
                                      Tensor *output) {
225
  const float *x = input->data<const float>();
H
hjchen2 已提交
226
  int8_t *y = output->mutable_data<int8_t>();
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
  size_t size = input->numel();
#ifdef defined(__ARM_NEON__) || defined(__ARM_NEON)
  size_t loop = size >> 4;
  size_t remain = size & 0xF;
  for (size_t i = 0; i < loop; ++i) {
    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 = vmulq_n_f32(r0, scale);
    r1 = vmulq_n_f32(r1, scale);
    r2 = vmulq_n_f32(r2, scale);
    r3 = vmulq_n_f32(r3, scale);
    int32x4_t q0 = vrnd_away_zero(r0);
    int32x4_t q1 = vrnd_away_zero(r1);
    int32x4_t q2 = vrnd_away_zero(r2);
    int32x4_t q3 = vrnd_away_zero(r3);
    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 已提交
248 249
    int16x8_t q5 = vcombine_s16(d0, d1);
    int16x8_t q6 = vcombine_s16(d2, d3);
H
Refine  
hjchen2 已提交
250 251 252 253
    int8x8_t d5 = vmovn_s16(q5);
    int8x8_t d6 = vmovn_s16(q6);
    vst1_s8(y, d5);
    vst1_s8(y + 8, d6);
254 255 256 257 258 259
    x += 16;
    y += 16;
  }
  size = remain;
#endif
  for (size_t i = 0; i < size; ++i) {
H
hjchen2 已提交
260
    y[i] = round(x[i] * scale);
261 262 263
  }
}

264
template <>
265 266 267 268
bool QuantizeKernel<CPU, float>::Init(QuantizeParam<CPU> *param) {
  return true;
}

269
template <>
270 271
void QuantizeKernel<CPU, float>::Compute(
    const QuantizeParam<CPU> &param) const {
272 273 274 275 276 277 278 279 280
  float max_abs = 0.f;
  const Tensor *input = param.input_;
  Tensor *output = param.out_;
  Tensor *output_scale = param.online_scale_;
  if (param.is_static_) {
    max_abs = param.static_scale_;
  } else {
    max_abs = find_abs_max(input);
  }
H
hjchen2 已提交
281
  max_abs = std::max(max_abs, 1e-6f);
282
  // only support int8 currently
283 284
  float scale = 127 / max_abs;
  param.online_scale_->mutable_data<float>()[0] = max_abs;
285 286
  switch (param.round_type_) {
    case ROUND_NEAREST_TO_EVEN:
287
      quantize_round_to_even(input, scale, output);
288 289
      break;
    case ROUND_NEAREST_TOWARDS_ZERO:
290
      quantize_round_to_zero(input, scale, output);
291 292
      break;
    case ROUND_NEAREST_AWAY_ZERO:
293 294
      quantize_round_to_nearest(input, scale, output);
      break;
295 296 297 298
    default:
      LOG(kLOG_ERROR) << "round type is not supported.";
      break;
  }
299 300 301
}

}  // namespace operators
302
}  // namespace paddle_mobile
303 304

#endif