quantize_kernel.cpp 3.4 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 19
#include <cmath>
#include <limits>
T
Tian 已提交
20

21 22 23
namespace paddle_mobile {
namespace operators {

24 25 26 27 28 29 30 31 32 33 34 35 36 37 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
static float find_abs_max(const Tensor *input) {
  float max_abs = float(0);
  const float *x = input->data<const float>();
  for (size_t i = 0; i < input->numel(); ++i) {
    float value = std::abs(x[i]);
    if (value > max_abs) {
      max_abs = value;
    }
  }
  return max_abs;
}

static void quantize_round_to_even(const Tensor *input,
                            const float scale,
                            Tensor *output) {
  const float *x = input->data<const float>();
  int8_t *y = output->data<int8_t>();
  for (size_t i = 0; i < input->numel(); ++i) {
    float value = x[i] * scale;
    long long quant = llround(value);
    if (abs(abs(round(value) - value) - 0.5) > 0) {
      y[i] = quant;
    } else {
      if (abs(quant) % 2 == 0) {
        y[i] = quant;
      } else {
        y[i] = quant + (quant > 0) ? -1 : 1;
      }
    }
  }
}

static void quantize_round_to_zero(const Tensor *input,
                            const float scale,
                            Tensor *output) {
  const float *x = input->data<const float>();
  int8_t *y = output->data<int8_t>();
  for (size_t i = 0; i < input->numel(); ++i) {
    y[i] = trunc(x[i] * scale);
  }
}

static void quantize_round_to_nearest(const Tensor *input,
                               const float scale,
                               Tensor *output) {
  const float *x = input->data<const float>();
  int8_t *y = output->data<int8_t>();
  for (size_t i = 0; i < input->numel(); ++i) {
    y[i] = round(x[i] * scale);
  }
}

76 77 78 79 80 81 82 83 84
template<>
bool QuantizeKernel<CPU, float>::Init(QuantizeParam<CPU> *param) {
  return true;
}

template<>
void QuantizeKernel<CPU, float>::Compute(
    const QuantizeParam<CPU> &param) const {
  // TODO
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  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);
  }
  if (max_abs < std::numeric_limits<float>::min()) {
    max_abs = std::numeric_limits<float>::min();
  }
  // only support int8 currently
  float online_scale = 127 / max_abs;
  param.online_scale_->mutable_data<float>()[0] = online_scale;
  switch (param.round_type_) {
    case ROUND_NEAREST_TO_EVEN:
      quantize_round_to_even(input, online_scale, output);
      break;
    case ROUND_NEAREST_TOWARDS_ZERO:
      quantize_round_to_zero(input, online_scale, output);
      break;
    case ROUND_NEAREST_AWAY_ZERO:
      quantize_round_to_nearest(input, online_scale, output);
    default:
      LOG(kLOG_ERROR) << "round type is not supported.";
      break;
  }
113 114 115 116
}

}  // namespace paddle_mobile
}  // namespace operators
117 118

#endif