Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 2 | // Copyright (c) 2020-2021, ARM Limited. |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 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 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 29 | class QuantUtil |
| 30 | { |
| 31 | public: |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 32 | static void reciprocal_scale(int32_t value, |
| 33 | // Output |
| 34 | int32_t& multiplier, |
| 35 | int32_t& shift) |
| 36 | { |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 37 | ASSERT_MSG(value > 0, "AvgPool2d reciprocal_scale() error: # of elements should be > 1 but is %d", value); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 38 | uint32_t value_u32 = (uint32_t)value; |
| 39 | int32_t k = 32 - LEADING_ZEROS_32(value_u32 - 1); // (1<<k)/2 < value <= (1<<k) |
| 40 | int64_t numerator = ((1L << 30) + 1) << k; |
| 41 | multiplier = numerator / value; // (1<<30) <= multiplier < (1<<31) |
| 42 | shift = 30 + k; |
| 43 | } |
| 44 | |
Kevin Cheng | 99bea14 | 2020-10-19 12:35:05 -0700 | [diff] [blame] | 45 | static int32_t apply_scale_32(int32_t value, int32_t multiplier, int32_t shift, bool double_round = true) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 46 | { |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 47 | if (multiplier < 0) |
| 48 | { |
| 49 | std::string desc = "apply_scale_32() error: multiplier should >= 0 but is " + std::to_string(multiplier); |
| 50 | throw desc; |
| 51 | } |
| 52 | if (shift < 2 || shift > 62) |
| 53 | { |
| 54 | std::string desc = |
| 55 | "apply_scale_32(): shift value should stay within [2, 62] but is " + std::to_string(shift); |
| 56 | throw desc; |
| 57 | } |
Eric Kunze | 750d27d | 2022-06-30 21:37:09 +0000 | [diff] [blame] | 58 | int64_t low_val = -1L << (shift - 1); |
| 59 | int64_t high_val = 1L << (shift - 1); |
Jeremy Johnson | 42c9bae | 2022-02-01 11:37:58 +0000 | [diff] [blame] | 60 | if (value < low_val || value >= high_val) |
| 61 | { |
Eric Kunze | 750d27d | 2022-06-30 21:37:09 +0000 | [diff] [blame] | 62 | std::string desc = "apply_scale_32(): value should stay within [" + std::to_string(low_val) + ", " + |
| 63 | std::to_string(high_val - 1) + "] but is " + std::to_string(value) + " using shift of " + |
| 64 | std::to_string(shift); |
Jeremy Johnson | 42c9bae | 2022-02-01 11:37:58 +0000 | [diff] [blame] | 65 | throw desc; |
| 66 | } |
Kevin Cheng | 99bea14 | 2020-10-19 12:35:05 -0700 | [diff] [blame] | 67 | int64_t round = 1L << (shift - 1); |
| 68 | if (double_round) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 69 | { |
Kevin Cheng | 99bea14 | 2020-10-19 12:35:05 -0700 | [diff] [blame] | 70 | if (shift > 31 && value >= 0) |
| 71 | round += (1L << 30); |
| 72 | if (shift > 31 && value < 0) |
| 73 | round -= (1L << 30); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 74 | } |
| 75 | int64_t result = (int64_t)value * multiplier + round; |
| 76 | result = result >> shift; |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 77 | if (result < -(1L << 31) || result >= (1L << 31)) |
| 78 | { |
| 79 | std::string desc = "apply_scale_32() error: scaled result exceeds int32 numeric range"; |
| 80 | throw desc; |
| 81 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 82 | return static_cast<int32_t>(result); |
| 83 | } |
Kevin Cheng | 0f87c95 | 2021-03-18 17:41:39 -0700 | [diff] [blame] | 84 | |
| 85 | static int32_t apply_scale_16(int64_t value, int16_t multiplier, int32_t shift) |
| 86 | { |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 87 | if (multiplier < 0) |
| 88 | { |
| 89 | std::string desc = "apply_scale_16() error: multiplier should >= 0 but is " + std::to_string(multiplier); |
| 90 | throw desc; |
| 91 | } |
| 92 | if (shift < 2 || shift > 62) |
| 93 | { |
| 94 | std::string desc = |
| 95 | "apply_scale_16(): shift value should stay within [2, 62] but is " + std::to_string(shift); |
| 96 | throw desc; |
| 97 | } |
Kevin Cheng | 0f87c95 | 2021-03-18 17:41:39 -0700 | [diff] [blame] | 98 | int64_t round = 1L << (shift - 1); |
| 99 | int64_t result = value * (int64_t)multiplier + round; |
| 100 | result = result >> shift; |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 101 | if (result < -(1L << 31) || result >= (1L << 31)) |
| 102 | { |
| 103 | std::string desc = "apply_scale_16() error: scaled result exceeds int32 numeric range"; |
| 104 | throw desc; |
| 105 | } |
Kevin Cheng | 0f87c95 | 2021-03-18 17:41:39 -0700 | [diff] [blame] | 106 | return static_cast<int32_t>(result); |
| 107 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | class TypeChecker |
| 111 | { |
| 112 | public: |
| 113 | static bool is_integer(DType dtype) |
| 114 | { |
Kevin Cheng | 0f87c95 | 2021-03-18 17:41:39 -0700 | [diff] [blame] | 115 | if (dtype == DType_INT4 || dtype == DType_INT8 || dtype == DType_UINT8 || dtype == DType_INT16 || |
Jeremy Johnson | f7f78ae | 2022-05-25 15:26:38 +0100 | [diff] [blame] | 116 | dtype == DType_UINT16 || dtype == DType_INT32 || dtype == DType_INT48) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 117 | { |
| 118 | return true; |
| 119 | } |
| 120 | return false; |
| 121 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 122 | }; |
| 123 | }; // namespace TosaReference |
| 124 | |
| 125 | #endif |