quantize_kernel.cpp 32.5 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
#ifndef __aarch64__
H
hjchen2 已提交
24
inline float32_t vmaxvq_f32(float32x4_t r) {
25 26 27 28 29
  float32x2_t v = vmax_f32(vget_high_f32(r), vget_low_f32(r));
  return vget_lane_f32(vpmax_f32(v, v), 0);
}
#endif

H
hjchen2 已提交
30
inline int32x4_t vrnd_towards_zero(float32x4_t r) { return vcvtq_s32_f32(r); }
31

H
hjchen2 已提交
32
inline 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
  int32x4_t ret = vcvtq_s32_f32(temp);
  return ret;
}

H
hjchen2 已提交
43
inline 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 89 90
namespace paddle_mobile {
namespace operators {

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

H
hjchen2 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
#if 0
static void quantize_round_to_zero(const Tensor *input, const float scale,
                                   const std::vector<int> &paddings,
                                   const int8_t padding_val, Tensor *output) {
  const float *x = input->data<const float>();
  int8_t *y = output->mutable_data<int8_t>();
  size_t size = input->numel();
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
  size_t loop = size >> 4;
  size_t remain = size & 0xF;

#pragma omp parallel for
  for (size_t i = 0; i < loop; ++i) {
    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);
    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);
    int16x8_t q5 = vcombine_s16(d0, d1);
    int16x8_t q6 = vcombine_s16(d2, d3);
    int8x8_t d5 = vmovn_s16(q5);
    int8x8_t d6 = vmovn_s16(q6);
    vst1_s8(local_y, d5);
    vst1_s8(local_y + 8, d6);
  }
  size = remain;
  x += (loop << 4);
  y += (loop << 4);
#endif
  for (size_t i = 0; i < size; ++i) {
    y[i] = static_cast<int8_t>(x[i] * scale);
  }
}
#endif

H
hjchen2 已提交
177
#ifdef __aarch64__
178
static void quantize_round_to_even(const Tensor *input, const float scale,
179
                                   Tensor *output) {
180
  const float *x = input->data<const float>();
H
hjchen2 已提交
181
  int8_t *y = output->mutable_data<int8_t>();
182 183 184 185
  size_t size = input->numel();
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
  size_t loop = size >> 4;
  size_t remain = size & 0xF;
H
hjchen2 已提交
186 187

  #pragma omp parallel for
188
  for (size_t i = 0; i < loop; ++i) {
H
hjchen2 已提交
189 190 191 192 193 194
    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);
195 196 197 198 199 200 201 202 203 204 205 206
    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 已提交
207 208
    int16x8_t q5 = vcombine_s16(d0, d1);
    int16x8_t q6 = vcombine_s16(d2, d3);
H
Refine  
hjchen2 已提交
209 210
    int8x8_t d5 = vmovn_s16(q5);
    int8x8_t d6 = vmovn_s16(q6);
H
hjchen2 已提交
211 212
    vst1_s8(local_y, d5);
    vst1_s8(local_y + 8, d6);
213 214
  }
  size = remain;
H
hjchen2 已提交
215 216
  x += (loop << 4);
  y += (loop << 4);
217 218
#endif
  for (size_t i = 0; i < size; ++i) {
219
    float value = x[i] * scale;
H
Refine  
hjchen2 已提交
220 221 222 223
    float v = round(value);
    int32_t q = (int32_t)v;
    if (abs(abs(q - value) - 0.5) > 0) {
      y[i] = q;
224
    } else {
H
Refine  
hjchen2 已提交
225 226
      if (abs(q) % 2 == 0) {
        y[i] = q;
227
      } else {
H
hjchen2 已提交
228
        y[i] = q + ((q > 0) ? -1 : 1);
229 230 231 232 233
      }
    }
  }
}

234 235
static void quantize_round_to_zero(const Tensor *input, const float scale,
                                   Tensor *output) {
236
  const float *x = input->data<const float>();
H
hjchen2 已提交
237
  int8_t *y = output->mutable_data<int8_t>();
238
  size_t size = input->numel();
H
hjchen2 已提交
239
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
240 241
  size_t loop = size >> 4;
  size_t remain = size & 0xF;
H
hjchen2 已提交
242 243

  #pragma omp parallel for
244
  for (size_t i = 0; i < loop; ++i) {
H
hjchen2 已提交
245 246 247 248 249 250
    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);
251 252 253 254 255 256 257 258 259 260 261 262
    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 已提交
263 264
    int16x8_t q5 = vcombine_s16(d0, d1);
    int16x8_t q6 = vcombine_s16(d2, d3);
H
Refine  
hjchen2 已提交
265 266
    int8x8_t d5 = vmovn_s16(q5);
    int8x8_t d6 = vmovn_s16(q6);
H
hjchen2 已提交
267 268
    vst1_s8(local_y, d5);
    vst1_s8(local_y + 8, d6);
269 270
  }
  size = remain;
H
hjchen2 已提交
271 272
  x += (loop << 4);
  y += (loop << 4);
273 274
#endif
  for (size_t i = 0; i < size; ++i) {
H
hjchen2 已提交
275
    y[i] = static_cast<int8_t>(x[i] * scale);
276 277 278
  }
}

279 280
static void quantize_round_to_nearest(const Tensor *input, const float scale,
                                      Tensor *output) {
281
  const float *x = input->data<const float>();
H
hjchen2 已提交
282
  int8_t *y = output->mutable_data<int8_t>();
283
  size_t size = input->numel();
284
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
285 286
  size_t loop = size >> 4;
  size_t remain = size & 0xF;
H
hjchen2 已提交
287 288

  #pragma omp parallel for
289
  for (size_t i = 0; i < loop; ++i) {
H
hjchen2 已提交
290 291 292 293 294 295
    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);
296 297 298 299 300 301 302 303 304 305 306 307
    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 已提交
308 309
    int16x8_t q5 = vcombine_s16(d0, d1);
    int16x8_t q6 = vcombine_s16(d2, d3);
H
Refine  
hjchen2 已提交
310 311
    int8x8_t d5 = vmovn_s16(q5);
    int8x8_t d6 = vmovn_s16(q6);
H
hjchen2 已提交
312 313
    vst1_s8(local_y, d5);
    vst1_s8(local_y + 8, d6);
314 315
  }
  size = remain;
H
hjchen2 已提交
316 317
  x += (loop << 4);
  y += (loop << 4);
318 319
#endif
  for (size_t i = 0; i < size; ++i) {
H
hjchen2 已提交
320
    y[i] = round(x[i] * scale);
321 322
  }
}
H
hjchen2 已提交
323
#else  // __aarch64__
H
hjchen2 已提交
324 325 326 327 328 329 330 331 332

static void quantize_round_to_even(const Tensor *input, const float scale,
                                   const std::vector<int> &paddings,
                                   const int8_t padding_val, Tensor *output) {}

static void quantize_round_to_nearest(const Tensor *input, const float scale,
                                      const std::vector<int> &paddings,
                                      const int8_t padding_val,
                                      Tensor *output) {}
H
hjchen2 已提交
333
#if 1
H
hjchen2 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
static void quantize_round_to_zero(const Tensor *input, const float scale,
                                   const std::vector<int> &paddings,
                                   const int8_t padding_val, Tensor *output) {
  int channels = input->dims()[1];
  int input_h = input->dims()[2];
  int input_w = input->dims()[3];
  int output_h = output->dims()[2];
  int output_w = output->dims()[3];
  int input_spatial_size = input_h * input_w;
  int output_spatial_size = output_h * output_w;
  const float *x = input->data<float>();
  int8_t *y = output->mutable_data<int8_t>();
  // valid area start
  int start = paddings[0] * output_w + paddings[1];

  for (int batch = 0; batch < input->dims()[0]; ++batch) {
    for (int c = 0; c < channels - 3; c += 4) {
H
hjchen2 已提交
351 352 353 354 355
      const float *input0 = x + (batch * channels + c) * input_spatial_size;
      const float *input1 = input0 + input_spatial_size;
      const float *input2 = input1 + input_spatial_size;
      const float *input3 = input2 + input_spatial_size;
      size_t offset = (batch * channels + c) * output_spatial_size;
H
hjchen2 已提交
356 357 358 359 360 361 362
      for (int h = 0; h < 2; ++h) {
        int8_t *y0 =
            y + offset + h * ((input_h + paddings[0]) * output_w - paddings[1]);
        int8_t *y1 = y0 + output_spatial_size;
        int8_t *y2 = y1 + output_spatial_size;
        int8_t *y3 = y2 + output_spatial_size;
        int loop = start >> 4;
H
hjchen2 已提交
363
        int remain = start & 0xF;
H
hjchen2 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
        asm volatile(
            "vdup.s8    q0,     %[val]      \n"
            "cmp        %[loop], #0         \n"
            "ble        start_remain_%=     \n"

            "store_16w_%=:                  \n"
            "vst1.32    {q0}, [%[y0]]!      \n"
            "vst1.32    {q0}, [%[y1]]!      \n"
            "vst1.32    {q0}, [%[y2]]!      \n"
            "vst1.32    {q0}, [%[y3]]!      \n"
            "subs       %[loop], #1         \n"
            "bne        store_16w_%=        \n"

            "start_remain_%=:               \n"
            "cmp        %[remain], #8       \n"
            "blt        store_4w_%=         \n"
            "vst1.32    {d0}, [%[y0]]!      \n"
            "vst1.32    {d0}, [%[y1]]!      \n"
            "vst1.32    {d0}, [%[y2]]!      \n"
            "vst1.32    {d0}, [%[y3]]!      \n"
            "sub        %[remain], #8       \n"

            "store_4w_%=:                   \n"
            "cmp        %[remain], #4       \n"
            "blt        store_2w_%=         \n"
            "vst1.32    {d0[0]}, [%[y0]]!   \n"
            "vst1.32    {d0[0]}, [%[y1]]!   \n"
            "vst1.32    {d0[0]}, [%[y2]]!   \n"
            "vst1.32    {d0[0]}, [%[y3]]!   \n"
            "sub        %[remain], #4       \n"

            "store_2w_%=:                   \n"
            "cmp        %[remain], #4       \n"
            "blt        store_1w_%=         \n"
            "vst1.16    {d0[0]}, [%[y0]]!   \n"
            "vst1.16    {d0[0]}, [%[y1]]!   \n"
            "vst1.16    {d0[0]}, [%[y2]]!   \n"
            "vst1.16    {d0[0]}, [%[y3]]!   \n"
            "sub        %[remain], #2       \n"

            "store_1w_%=:                   \n"
            "cmp        %[remain], #1       \n"
            "blt        end_%=              \n"
            "vst1.8     {d0[0]}, [%[y0]]!   \n"
            "vst1.8     {d0[0]}, [%[y1]]!   \n"
            "vst1.8     {d0[0]}, [%[y2]]!   \n"
            "vst1.8     {d0[0]}, [%[y3]]!   \n"
            "end_%=:                        \n"
            : [y0] "+r"(y0), [y1] "+r"(y1), [y2] "+r"(y2), [y3] "+r"(y3),
              [loop] "+r"(loop), [remain] "+r"(remain)
            : [val] "r"(padding_val)
            : "cc", "memory", "q0");
      }
      // quantize valid area
      int8_t *y0 = y + offset + start;
      int8_t *y1 = y0 + output_spatial_size;
      int8_t *y2 = y1 + output_spatial_size;
      int8_t *y3 = y2 + output_spatial_size;
      for (int h = 0; h < input_h; ++h) {
H
hjchen2 已提交
423 424 425 426
        const float *x0 = input0 + h * input_w;
        const float *x1 = input1 + h * input_w;
        const float *x2 = input2 + h * input_w;
        const float *x3 = input3 + h * input_w;
H
hjchen2 已提交
427
        int loop = input_w >> 4;
H
hjchen2 已提交
428
        int remain = input_w & 0xF;
H
hjchen2 已提交
429
        int pad_loop = paddings[1] >> 1;
H
hjchen2 已提交
430 431
        int pad_remain = paddings[1] & 0x1;
        int remain_steps = remain;
H
hjchen2 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
        asm volatile(
            "vdup.f32   q0, %[scale]        \n"
            "cmp        %[loop], #0         \n"
            "ble        quantize_remain_%=  \n"

            "loop_quantize_%=:              \n"
            "vld1.32    {q1, q2}, [%[x0]]!  \n"
            "vld1.32    {q3, q4}, [%[x1]]!  \n"
            "vld1.32    {q5, q6}, [%[x2]]!  \n"
            "vld1.32    {q7, q8}, [%[x3]]!  \n"
            "vmul.f32  q1, q1, q0           \n"
            "vmul.f32  q2, q2, q0           \n"
            "vmul.f32  q3, q3, q0           \n"
            "vmul.f32  q4, q4, q0           \n"
            "vmul.f32  q5, q5, q0           \n"
            "vmul.f32  q6, q6, q0           \n"
            "vmul.f32  q7, q7, q0           \n"
            "vmul.f32  q8, q8, q0           \n"
            "vcvt.s32.f32  q1, q1           \n"
            "vcvt.s32.f32  q2, q2           \n"
            "vcvt.s32.f32  q3, q3           \n"
            "vcvt.s32.f32  q4, q4           \n"
            "vcvt.s32.f32  q5, q5           \n"
            "vcvt.s32.f32  q6, q6           \n"
            "vcvt.s32.f32  q7, q7           \n"
            "vcvt.s32.f32  q8, q8           \n"
            "vmovn.s32  d2, q1              \n"
            "vmovn.s32  d3, q2              \n"
            "vmovn.s32  d4, q3              \n"
            "vmovn.s32  d5, q4              \n"
            "vmovn.s32  d6, q5              \n"
            "vmovn.s32  d7, q6              \n"
            "vmovn.s32  d8, q7              \n"
            "vmovn.s32  d9, q8              \n"
            "vmovn.s16  d18, q1             \n"
            "vmovn.s16  d20, q2             \n"
            "vmovn.s16  d22, q3             \n"
            "vmovn.s16  d24, q4             \n"
            "vld1.32    {q1, q2}, [%[x0]]!  \n"
            "vld1.32    {q3, q4}, [%[x1]]!  \n"
            "vld1.32    {q5, q6}, [%[x2]]!  \n"
            "vld1.32    {q7, q8}, [%[x3]]!  \n"
            "vmul.f32  q1, q1, q0           \n"
            "vmul.f32  q2, q2, q0           \n"
            "vmul.f32  q3, q3, q0           \n"
            "vmul.f32  q4, q4, q0           \n"
            "vmul.f32  q5, q5, q0           \n"
            "vmul.f32  q6, q6, q0           \n"
            "vmul.f32  q7, q7, q0           \n"
            "vmul.f32  q8, q8, q0           \n"
            "vcvt.s32.f32  q1, q1           \n"
            "vcvt.s32.f32  q2, q2           \n"
            "vcvt.s32.f32  q3, q3           \n"
            "vcvt.s32.f32  q4, q4           \n"
            "vcvt.s32.f32  q5, q5           \n"
            "vcvt.s32.f32  q6, q6           \n"
            "vcvt.s32.f32  q7, q7           \n"
            "vcvt.s32.f32  q8, q8           \n"
            "vmovn.s32  d2, q1              \n"
            "vmovn.s32  d3, q2              \n"
            "vmovn.s32  d4, q3              \n"
            "vmovn.s32  d5, q4              \n"
            "vmovn.s32  d6, q5              \n"
            "vmovn.s32  d7, q6              \n"
            "vmovn.s32  d8, q7              \n"
            "vmovn.s32  d9, q8              \n"
            "vmovn.s16  d19, q1             \n"
            "vmovn.s16  d21, q2             \n"
            "vmovn.s16  d23, q3             \n"
            "vmovn.s16  d25, q4             \n"
H
hjchen2 已提交
502 503 504 505
            "vst1.32    {q9}, [%[y0]]!      \n"
            "vst1.32    {q10}, [%[y1]]!     \n"
            "vst1.32    {q11}, [%[y2]]!     \n"
            "vst1.32    {q12}, [%[y3]]!     \n"
H
hjchen2 已提交
506 507 508 509 510 511 512 513

            "subs       %[loop], #1         \n"
            "bne        loop_quantize_%=    \n"

            "quantize_remain_%=:            \n"
            "cmp        %[remain], #0       \n"
            "ble        end_%=              \n"

H
hjchen2 已提交
514 515 516 517
            "vld1.32    {q1, q2}, [%[x0]]!  \n"
            "vld1.32    {q3, q4}, [%[x1]]!  \n"
            "vld1.32    {q5, q6}, [%[x2]]!  \n"
            "vld1.32    {q7, q8}, [%[x3]]!  \n"
H
hjchen2 已提交
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
            "vmul.f32  q1, q1, q0           \n"
            "vmul.f32  q2, q2, q0           \n"
            "vmul.f32  q3, q3, q0           \n"
            "vmul.f32  q4, q4, q0           \n"
            "vmul.f32  q5, q5, q0           \n"
            "vmul.f32  q6, q6, q0           \n"
            "vmul.f32  q7, q7, q0           \n"
            "vmul.f32  q8, q8, q0           \n"
            "vcvt.s32.f32  q1, q1           \n"
            "vcvt.s32.f32  q2, q2           \n"
            "vcvt.s32.f32  q3, q3           \n"
            "vcvt.s32.f32  q4, q4           \n"
            "vcvt.s32.f32  q5, q5           \n"
            "vcvt.s32.f32  q6, q6           \n"
            "vcvt.s32.f32  q7, q7           \n"
            "vcvt.s32.f32  q8, q8           \n"
            "vmovn.s32  d2, q1              \n"
            "vmovn.s32  d3, q2              \n"
            "vmovn.s32  d4, q3              \n"
            "vmovn.s32  d5, q4              \n"
            "vmovn.s32  d6, q5              \n"
            "vmovn.s32  d7, q6              \n"
            "vmovn.s32  d8, q7              \n"
            "vmovn.s32  d9, q8              \n"
            "vmovn.s16  d18, q1             \n"
            "vmovn.s16  d20, q2             \n"
            "vmovn.s16  d22, q3             \n"
            "vmovn.s16  d24, q4             \n"
H
hjchen2 已提交
546 547 548 549
            "vld1.32    {q1, q2}, [%[x0]]   \n"
            "vld1.32    {q3, q4}, [%[x1]]   \n"
            "vld1.32    {q5, q6}, [%[x2]]   \n"
            "vld1.32    {q7, q8}, [%[x3]]   \n"
H
hjchen2 已提交
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
            "vmul.f32  q1, q1, q0           \n"
            "vmul.f32  q2, q2, q0           \n"
            "vmul.f32  q3, q3, q0           \n"
            "vmul.f32  q4, q4, q0           \n"
            "vmul.f32  q5, q5, q0           \n"
            "vmul.f32  q6, q6, q0           \n"
            "vmul.f32  q7, q7, q0           \n"
            "vmul.f32  q8, q8, q0           \n"
            "vcvt.s32.f32  q1, q1           \n"
            "vcvt.s32.f32  q2, q2           \n"
            "vcvt.s32.f32  q3, q3           \n"
            "vcvt.s32.f32  q4, q4           \n"
            "vcvt.s32.f32  q5, q5           \n"
            "vcvt.s32.f32  q6, q6           \n"
            "vcvt.s32.f32  q7, q7           \n"
            "vcvt.s32.f32  q8, q8           \n"
            "vmovn.s32  d2, q1              \n"
            "vmovn.s32  d3, q2              \n"
            "vmovn.s32  d4, q3              \n"
            "vmovn.s32  d5, q4              \n"
            "vmovn.s32  d6, q5              \n"
            "vmovn.s32  d7, q6              \n"
            "vmovn.s32  d8, q7              \n"
            "vmovn.s32  d9, q8              \n"
            "vmovn.s16  d19, q1             \n"
            "vmovn.s16  d21, q2             \n"
            "vmovn.s16  d23, q3             \n"
            "vmovn.s16  d25, q4             \n"

            "cmp        %[remain], #8       \n"
            "blt        store_4w_%=         \n"
            "vst1.32    {d18}, [%[y0]]!     \n"
            "vst1.32    {d20}, [%[y1]]!     \n"
            "vst1.32    {d22}, [%[y2]]!     \n"
            "vst1.32    {d24}, [%[y3]]!     \n"
            "vmov.32    d18, d19            \n"
            "vmov.32    d20, d21            \n"
            "vmov.32    d22, d23            \n"
            "vmov.32    d24, d25            \n"
            "sub        %[remain], #8       \n"

            "store_4w_%=:                   \n"
            "cmp        %[remain], #4       \n"
            "blt        store_2w_%=         \n"
            "vst1.32    {d18[0]}, [%[y0]]!  \n"
            "vst1.32    {d20[0]}, [%[y1]]!  \n"
            "vst1.32    {d22[0]}, [%[y2]]!  \n"
            "vst1.32    {d24[0]}, [%[y3]]!  \n"
            "vext.32    d18, d18, d18, #1   \n"
            "vext.32    d20, d20, d20, #1   \n"
            "vext.32    d22, d22, d22, #1   \n"
            "vext.32    d24, d24, d24, #1   \n"
            "sub        %[remain], #4       \n"

            "store_2w_%=:                   \n"
            "cmp        %[remain], #2       \n"
            "blt        store_1w_%=         \n"
            "vst1.16    {d18[0]}, [%[y0]]!  \n"
            "vst1.16    {d20[0]}, [%[y1]]!  \n"
            "vst1.16    {d22[0]}, [%[y2]]!  \n"
            "vst1.16    {d24[0]}, [%[y3]]!  \n"
            "vext.16    d18, d18, d18, #1   \n"
            "vext.16    d20, d20, d20, #1   \n"
            "vext.16    d22, d22, d22, #1   \n"
            "vext.16    d24, d24, d24, #1   \n"
            "sub        %[remain], #2       \n"

            "store_1w_%=:"
            "cmp        %[remain], #1       \n"
            "blt        end_%=              \n"
            "vst1.8     {d18[0]}, [%[y0]]!  \n"
            "vst1.8     {d20[0]}, [%[y1]]!  \n"
            "vst1.8     {d22[0]}, [%[y2]]!  \n"
            "vst1.8     {d24[0]}, [%[y3]]!  \n"

            "end_%=:                        \n"
            : [x0] "+r"(x0), [x1] "+r"(x1), [x2] "+r"(x2), [x3] "+r"(x3),
              [y0] "+r"(y0), [y1] "+r"(y1), [y2] "+r"(y2), [y3] "+r"(y3),
              [loop] "+r"(loop), [remain] "+r"(remain)
            : [scale] "r"(scale)
H
hjchen2 已提交
630 631
            : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
              "q8", "q9", "q10", "q11", "q12");
H
hjchen2 已提交
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
        asm volatile(
            "vdup.s8    d0, %[val]          \n"
            "cmp        %[pad_loop], #0     \n"
            "ble        store_pad_2w_%=     \n"
            "loop_pad_4w_%=:                \n"
            "vst1.32    {d0[0]}, [%[y0]]!   \n"
            "vst1.32    {d0[0]}, [%[y1]]!   \n"
            "vst1.32    {d0[0]}, [%[y2]]!   \n"
            "vst1.32    {d0[0]}, [%[y3]]!   \n"
            "subs       %[pad_loop], #1     \n"
            "bne        loop_pad_4w_%=      \n"

            "store_pad_2w_%=:               \n"
            "cmp        %[pad_remain], #2   \n"
            "ble        store_pad_1w_%=     \n"
            "vst1.16    {d0[0]}, [%[y0]]!   \n"
            "vst1.16    {d0[0]}, [%[y1]]!   \n"
            "vst1.16    {d0[0]}, [%[y2]]!   \n"
            "vst1.16    {d0[0]}, [%[y3]]!   \n"
            "sub        %[pad_remain], #2   \n"

            "store_pad_1w_%=:               \n"
            "cmp        %[pad_remain], #1   \n"
            "ble        end_%=              \n"
            "vst1.8    {d0[0]}, [%[y0]]!    \n"
            "vst1.8    {d0[0]}, [%[y1]]!    \n"
            "vst1.8    {d0[0]}, [%[y2]]!    \n"
            "vst1.8    {d0[0]}, [%[y3]]!    \n"
            "end_%=:                        \n"
            : [y0] "+r"(y0), [y1] "+r"(y1), [y2] "+r"(y2), [y3] "+r"(y3),
              [pad_loop] "+r"(pad_loop), [pad_remain] "+r"(pad_remain)
            : [val] "r"(padding_val)
H
hjchen2 已提交
664 665
            : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
              "q8", "q9", "q10", "q11", "q12");
H
hjchen2 已提交
666 667 668
      }
    }
    for (int c = (channels & 0xFFFC); c < channels; ++c) {
H
hjchen2 已提交
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
      const float *input0 = x + (batch * channels + c) * input_spatial_size;
      size_t offset = (batch * channels + c) * output_spatial_size;
      for (int h = 0; h < 2; ++h) {
        int8_t *y0 =
            y + offset + h * ((input_h + paddings[0]) * output_w - paddings[1]);
        int loop = start >> 4;
        int remain = start & 0xF;
        asm volatile(
            "vdup.s8    q0,     %[val]      \n"
            "cmp        %[loop], #0         \n"
            "ble        start_remain_%=     \n"

            "store_16w_%=:                  \n"
            "vst1.32    {q0}, [%[y0]]!      \n"
            "subs       %[loop], #1         \n"
            "bne        store_16w_%=        \n"

            "start_remain_%=:               \n"
            "cmp        %[remain], #8       \n"
            "blt        store_4w_%=         \n"
            "vst1.32    {d0}, [%[y0]]!      \n"
            "sub        %[remain], #8       \n"

            "store_4w_%=:                   \n"
            "cmp        %[remain], #4       \n"
            "blt        store_2w_%=         \n"
            "vst1.32    {d0[0]}, [%[y0]]!   \n"
            "sub        %[remain], #4       \n"

            "store_2w_%=:                   \n"
            "cmp        %[remain], #4       \n"
            "blt        store_1w_%=         \n"
            "vst1.16    {d0[0]}, [%[y0]]!   \n"
            "sub        %[remain], #2       \n"

            "store_1w_%=:                   \n"
            "cmp        %[remain], #1       \n"
            "blt        end_%=              \n"
            "vst1.8     {d0[0]}, [%[y0]]!   \n"
            "end_%=:                        \n"
            : [y0] "+r"(y0), [loop] "+r"(loop), [remain] "+r"(remain)
            : [val] "r"(padding_val)
            : "cc", "memory", "q0");
      }
      // quantize valid area
      int8_t *y0 = y + offset + start;
      for (int h = 0; h < input_h; ++h) {
        const float *x0 = input0 + h * input_w;
H
hjchen2 已提交
717
        int loop = input_w >> 4;
H
hjchen2 已提交
718
        int remain = input_w & 0xF;
H
hjchen2 已提交
719
        int pad_loop = paddings[1] >> 1;
H
hjchen2 已提交
720
        int pad_remain = paddings[1] & 0x1;
H
hjchen2 已提交
721 722 723 724 725 726 727
        asm volatile(
            "vdup.f32   q0, %[scale]        \n"
            "cmp        %[loop], #0         \n"
            "ble        quantize_remain_%=  \n"

            "loop_quantize_%=:              \n"
            "vld1.32    {q1, q2}, [%[x0]]!  \n"
H
hjchen2 已提交
728 729
            "vmul.f32   q1, q1, q0          \n"
            "vmul.f32   q2, q2, q0          \n"
H
hjchen2 已提交
730 731 732 733 734 735
            "vcvt.s32.f32  q1, q1           \n"
            "vcvt.s32.f32  q2, q2           \n"
            "vmovn.s32  d2, q1              \n"
            "vmovn.s32  d3, q2              \n"
            "vmovn.s16  d18, q1             \n"
            "vld1.32    {q1, q2}, [%[x0]]!  \n"
H
hjchen2 已提交
736 737
            "vmul.f32   q1, q1, q0          \n"
            "vmul.f32   q2, q2, q0          \n"
H
hjchen2 已提交
738 739 740 741 742
            "vcvt.s32.f32  q1, q1           \n"
            "vcvt.s32.f32  q2, q2           \n"
            "vmovn.s32  d2, q1              \n"
            "vmovn.s32  d3, q2              \n"
            "vmovn.s16  d19, q1             \n"
H
hjchen2 已提交
743
            "vst1.32    {q9}, [%[y0]]!      \n"
H
hjchen2 已提交
744 745 746 747 748 749 750 751

            "subs       %[loop], #1         \n"
            "bne        loop_quantize_%=    \n"

            "quantize_remain_%=:            \n"
            "cmp        %[remain], #0       \n"
            "ble        start_pad_%=        \n"

H
hjchen2 已提交
752 753 754
            "vldm       %[x0], {d2-d9}      \n"
            "vmul.f32   q1, q1, q0          \n"
            "vmul.f32   q2, q2, q0          \n"
H
hjchen2 已提交
755 756 757 758 759
            "vcvt.s32.f32  q1, q1           \n"
            "vcvt.s32.f32  q2, q2           \n"
            "vmovn.s32  d2, q1              \n"
            "vmovn.s32  d3, q2              \n"
            "vmovn.s16  d18, q1             \n"
H
hjchen2 已提交
760 761 762 763
            "vmul.f32   q3, q3, q0          \n"
            "vmul.f32   q4, q4, q0          \n"
            "vcvt.s32.f32  q1, q3           \n"
            "vcvt.s32.f32  q2, q4           \n"
H
hjchen2 已提交
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
            "vmovn.s32  d2, q1              \n"
            "vmovn.s32  d3, q2              \n"
            "vmovn.s16  d19, q1             \n"

            "cmp        %[remain], #8       \n"
            "blt        store_4w_%=         \n"
            "vst1.32    {d18}, [%[y0]]!     \n"
            "vmov.32    d18, d19            \n"
            "sub        %[remain], #8       \n"

            "store_4w_%=:                   \n"
            "cmp        %[remain], #4       \n"
            "blt        store_2w_%=         \n"
            "vst1.32    {d18[0]}, [%[y0]]!  \n"
            "vext.32    d18, d18, d18, #1   \n"
            "sub        %[remain], #4       \n"

            "store_2w_%=:                   \n"
            "cmp        %[remain], #2       \n"
            "blt        store_1w_%=         \n"
            "vst1.16    {d18[0]}, [%[y0]]!  \n"
            "vext.16    d18, d18, d18, #1   \n"
            "sub        %[remain], #2       \n"

            "store_1w_%=:"
            "cmp        %[remain], #1       \n"
            "blt        start_pad_%=        \n"
            "vst1.8     {d18[0]}, [%[y0]]!  \n"

            "start_pad_%=:                  \n"
            "vdup.s8    d0, %[val]          \n"
            "cmp        %[pad_loop], #0     \n"
            "ble        pad_remain_%=       \n"
            "loop_pad_4w_%=:                \n"
            "vst1.32    {d0[0]}, [%[y0]]!   \n"
            "subs       %[pad_loop], #1     \n"
            "bne        loop_pad_4w_%=      \n"

            "pad_remain_%=:                 \n"
            "cmp        %[pad_remain], #2   \n"
            "ble        store_pad_1w_%=     \n"
            "vst1.16    {d0[0]}, [%[y0]]!   \n"
            "sub        %[pad_remain], #2   \n"

            "store_pad_1w_%=:               \n"
            "cmp        %[pad_remain], #1   \n"
            "ble        end_%=              \n"
            "vst1.8    {d0[0]}, [%[y0]]!    \n"
            "end_%=:                        \n"
            : [x0] "+r"(x0), [y0] "+r"(y0), [loop] "+r"(loop),
              [remain] "+r"(remain), [pad_loop] "+r"(pad_loop),
              [pad_remain] "+r"(pad_remain)
            : [scale] "r"(scale), [val] "r"(padding_val)
H
hjchen2 已提交
817
            : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q9");
H
hjchen2 已提交
818 819 820 821
      }
    }
  }
}
H
hjchen2 已提交
822
#endif
H
hjchen2 已提交
823 824
#endif  // __aarch64__
#endif  // ARM_NEON
825

826
template <>
827 828 829 830
bool QuantizeKernel<CPU, float>::Init(QuantizeParam<CPU> *param) {
  return true;
}

831
template <>
L
liuruilong 已提交
832
void QuantizeKernel<CPU, float>::Compute(const QuantizeParam<CPU> &param) {
833
  const Tensor *input = param.input_;
H
hjchen2 已提交
834
  Tensor *output = param.output_;
835
  Tensor *output_scale = param.online_scale_;
H
hjchen2 已提交
836
  float max_abs = 0.f;
837 838 839 840 841
  if (param.is_static_) {
    max_abs = param.static_scale_;
  } else {
    max_abs = find_abs_max(input);
  }
H
hjchen2 已提交
842
  max_abs = std::max(max_abs, 1e-6f);
843
  // only support int8 currently
844 845
  float scale = 127 / max_abs;
  param.online_scale_->mutable_data<float>()[0] = max_abs;
H
hjchen2 已提交
846 847 848 849
  //  const auto &paddings = param.paddings_;
  std::vector<int> paddings = {0, 0};
  //  const auto padding_val = param.padding_val_;
  int8_t padding_val = 127;
850 851
  switch (param.round_type_) {
    case ROUND_NEAREST_TO_EVEN:
H
hjchen2 已提交
852
      quantize_round_to_even(input, scale, paddings, padding_val, output);
853 854
      break;
    case ROUND_NEAREST_TOWARDS_ZERO:
H
hjchen2 已提交
855
      quantize_round_to_zero(input, scale, paddings, padding_val, output);
856 857
      break;
    case ROUND_NEAREST_AWAY_ZERO:
H
hjchen2 已提交
858
      quantize_round_to_nearest(input, scale, paddings, padding_val, output);
859
      break;
860 861 862 863
    default:
      LOG(kLOG_ERROR) << "round type is not supported.";
      break;
  }
864 865 866
}

}  // namespace operators
867
}  // namespace paddle_mobile
868 869

#endif