Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame^] | 1 | |
| 2 | // Copyright (c) 2020, ARM Limited. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | #ifndef TOSA_REFERENCE_QUANT_UTIL_H |
| 17 | #define TOSA_REFERENCE_QUANT_UTIL_H |
| 18 | |
| 19 | #include "arith_util.h" |
| 20 | #include "func_debug.h" |
| 21 | #include "ops/template_types.h" |
| 22 | #include "tosa_generated.h" |
| 23 | |
| 24 | using namespace tosa; |
| 25 | |
| 26 | namespace TosaReference |
| 27 | { |
| 28 | |
| 29 | template <DType AccDType> |
| 30 | class QuantUtil |
| 31 | { |
| 32 | public: |
| 33 | using T = typename GetEigenType<AccDType>::type; |
| 34 | |
| 35 | static void reciprocal_scale(int32_t value, |
| 36 | // Output |
| 37 | int32_t& multiplier, |
| 38 | int32_t& shift) |
| 39 | { |
| 40 | ASSERT_MSG(value > 0, "AvgPool2d reciprocal_scale() error: # of elements should be > 1 but is %d", value); |
| 41 | uint32_t value_u32 = (uint32_t)value; |
| 42 | int32_t k = 32 - LEADING_ZEROS_32(value_u32 - 1); // (1<<k)/2 < value <= (1<<k) |
| 43 | int64_t numerator = ((1L << 30) + 1) << k; |
| 44 | multiplier = numerator / value; // (1<<30) <= multiplier < (1<<31) |
| 45 | shift = 30 + k; |
| 46 | } |
| 47 | |
| 48 | static int32_t apply_scale(T value, int32_t multiplier, int32_t shift, bool enabled_adjusted_rounding = true) |
| 49 | { |
| 50 | if (AccDType == DType_FLOAT) |
| 51 | { |
| 52 | return value; |
| 53 | } |
| 54 | ASSERT_MSG(multiplier >= 0, "apply_scale() error: multiplier should >= 0 but is %d", multiplier); |
| 55 | int64_t round = (shift > 0) ? (1L << (shift - 1)) : 0; |
| 56 | if (enabled_adjusted_rounding) |
| 57 | { |
| 58 | if (AccDType != DType_INT48) |
| 59 | { |
| 60 | if (shift > 31 && value >= 0) |
| 61 | round += (1L << 30); |
| 62 | if (shift > 31 && value < 0) |
| 63 | round -= (1L << 30); |
| 64 | } |
| 65 | else |
| 66 | { // input data could be int16, which leads to 48 bits accumulator |
| 67 | ASSERT_MSG(multiplier < (1 << 15), "apply_scale() error: multiplier should <= %d in 48 bit mode", |
| 68 | (1 << 15)); |
| 69 | } |
| 70 | } |
| 71 | int64_t result = (int64_t)value * multiplier + round; |
| 72 | result = result >> shift; |
| 73 | ASSERT_MSG(result >= -(1L << 31) && result < (1L << 31), |
| 74 | "apply_scale() error: scaled result exceed int32 numeric range"); |
| 75 | return static_cast<int32_t>(result); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | class TypeChecker |
| 80 | { |
| 81 | public: |
| 82 | static bool is_integer(DType dtype) |
| 83 | { |
| 84 | if (dtype == DType_INT4 || dtype == DType_INT8 || dtype == DType_AINT8 || dtype == DType_UINT8 || |
| 85 | dtype == DType_INT16 || dtype == DType_INT32 || dtype == DType_INT48) |
| 86 | { |
| 87 | return true; |
| 88 | } |
| 89 | return false; |
| 90 | } |
| 91 | static bool is_symmetric(DType dtype) |
| 92 | { |
| 93 | if (dtype == DType_INT4 || dtype == DType_INT8 || dtype == DType_INT16 || dtype == DType_INT32 || |
| 94 | dtype == DType_INT48) |
| 95 | { |
| 96 | return true; |
| 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | }; |
| 101 | }; // namespace TosaReference |
| 102 | |
| 103 | #endif |