xref: /aosp_15_r20/external/ComputeLibrary/src/core/NEON/kernels/convolution/common/qasymm8.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2019 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #include <algorithm>
26 #include <cassert>
27 #include <cstdint>
28 #include <cmath>
29 #include <limits>
30 
31 #include "qasymm8.hpp"
32 
33 namespace qasymm8
34 {
35 #if(__ANDROID__ || BARE_METAL)
round(T val)36 template <typename T> T round(T val) {  return ::round(val); }
exp2(T val)37 template <typename T> T exp2(T val) { return ::exp2(val); }
log2(T val)38 template <typename T> T log2(T val) { return ::log2(val); }
39 #else  /* (__ANDROID__ || BARE_METAL) */
40 template <typename T> T round(T val) { return std::round(val); }
41 template <typename T> T exp2(T val) { return std::exp2(val); }
42 template <typename T> T log2(T val) { return std::log2(val); }
43 #endif  /* (__ANDROID__ || BARE_METAL) */
44 
quantize(const float value) const45 uint8_t QAsymm8Params::quantize(const float value) const
46 {
47   const float transformed = value / scale + offset;
48   return static_cast<uint8_t>(round(std::max(0.0f, std::min(255.0f, transformed))));
49 }
50 
dequantize(const uint8_t value) const51 float QAsymm8Params::dequantize(const uint8_t value) const
52 {
53   return scale * (static_cast<float>(value) - offset);
54 }
55 
make_rescale_params(const QAsymm8Params & weight_quant,const QAsymm8Params & input_quant,const QAsymm8Params & output_quant)56 QAsymm8RescaleParams QAsymm8RescaleParams::make_rescale_params(
57   const QAsymm8Params& weight_quant,
58   const QAsymm8Params& input_quant,
59   const QAsymm8Params& output_quant
60 )
61 {
62   // Based on the gemmlowp approach: https://github.com/google/gemmlowp/blob/master/doc/quantization_example.cc
63   const float rescale = weight_quant.scale * input_quant.scale / output_quant.scale;
64   const float shiftf = round(log2(0.5f / rescale));
65   const float multf = exp2(31.0f + shiftf)*rescale;
66 
67   int64_t shift = static_cast<int64_t>(shiftf);
68   int64_t mult = static_cast<int64_t>(multf);
69 
70   if (mult == (1ll << 31))
71   {
72     mult /= 2;
73     shift--;
74   }
75 
76   assert(shift >= 0);
77   assert(mult <= std::numeric_limits<int32_t>::max());
78 
79   return QAsymm8RescaleParams(
80     static_cast<int32_t>(shift),
81     static_cast<int32_t>(mult),
82     rescale
83   );
84 }
85 
QAsymm8RescaleParams(int32_t shift,int32_t multi,float rescale)86 QAsymm8RescaleParams::QAsymm8RescaleParams(int32_t shift, int32_t multi, float rescale)
87   : shift(shift), multiplier(multi), rescale(rescale)
88 {
89 }
90 }
91