blob: 11e33b5bccd6d5dd599a57e8e413d83fb571c8fd [file] [log] [blame]
Georgios Pinitas47d39dc2019-03-11 14:03:23 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019 Arm Limited.
Georgios Pinitas47d39dc2019-03-11 14:03:23 +00003 *
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
Georgios Pinitas30271c72019-06-24 14:56:34 +010031#include "qasymm8.hpp"
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000032
33namespace qasymm8
34{
35#if(__ANDROID__ || BARE_METAL)
36template <typename T> T round(T val) { return ::round(val); }
37template <typename T> T exp2(T val) { return ::exp2(val); }
38template <typename T> T log2(T val) { return ::log2(val); }
39#else /* (__ANDROID__ || BARE_METAL) */
40template <typename T> T round(T val) { return std::round(val); }
41template <typename T> T exp2(T val) { return std::exp2(val); }
42template <typename T> T log2(T val) { return std::log2(val); }
43#endif /* (__ANDROID__ || BARE_METAL) */
44
45uint8_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
51float QAsymm8Params::dequantize(const uint8_t value) const
52{
53 return scale * (static_cast<float>(value) - offset);
54}
55
56QAsymm8RescaleParams 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
86QAsymm8RescaleParams::QAsymm8RescaleParams(int32_t shift, int32_t multi, float rescale)
87 : shift(shift), multiplier(multi), rescale(rescale)
88{
89}
90}