Adnan AlSinan | 9104cd5 | 2022-04-06 16:19:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2022 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 | #ifndef SRC_CORE_HELPERS_POOLINGHELPERS_H |
| 25 | #define SRC_CORE_HELPERS_POOLINGHELPERS_H |
| 26 | |
| 27 | #include "src/core/NEON/NEAsymm.h" |
| 28 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | namespace cpu |
| 32 | { |
| 33 | namespace |
| 34 | { |
| 35 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 36 | inline float calculate_avg_scale_pool3d(bool exclude_padding, |
| 37 | const Coordinates &id, |
| 38 | const int pool_size_x, |
| 39 | const int pool_size_y, |
| 40 | const int pool_size_z, |
| 41 | const int upper_bound_w, |
| 42 | const int upper_bound_h, |
| 43 | const int upper_bound_d, |
| 44 | const int pad_x, |
| 45 | const int pad_y, |
| 46 | const int pad_z, |
| 47 | const int stride_x, |
| 48 | const int stride_y, |
| 49 | const int stride_z) |
Adnan AlSinan | 9104cd5 | 2022-04-06 16:19:31 +0100 | [diff] [blame] | 50 | { |
| 51 | // Based on NDHWC |
| 52 | int start_x = id[1] * stride_x - pad_x; |
| 53 | int start_y = id[2] * stride_y - pad_y; |
| 54 | int start_z = id[3] * stride_z - pad_z; |
| 55 | |
| 56 | const int end_x = std::min(start_x + pool_size_x, upper_bound_w); |
| 57 | const int end_y = std::min(start_y + pool_size_y, upper_bound_h); |
| 58 | const int end_z = std::min(start_z + pool_size_z, upper_bound_d); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 59 | if (exclude_padding) |
Adnan AlSinan | 9104cd5 | 2022-04-06 16:19:31 +0100 | [diff] [blame] | 60 | { |
| 61 | start_x = std::max(0, start_x); |
| 62 | start_y = std::max(0, start_y); |
| 63 | start_z = std::max(0, start_z); |
| 64 | } |
| 65 | return 1.f / ((end_y - start_y) * (end_x - start_x) * (end_z - start_z)); |
| 66 | } |
| 67 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 68 | inline float calculate_avg_scale_pool2d(bool exclude_padding, |
| 69 | DataLayout data_layout, |
| 70 | const Coordinates &id, |
| 71 | const int pool_size_x, |
| 72 | const int pool_size_y, |
| 73 | const int upper_bound_w, |
| 74 | const int upper_bound_h, |
| 75 | const int pad_x, |
| 76 | const int pad_y, |
| 77 | const int stride_x, |
| 78 | const int stride_y) |
Adnan AlSinan | 9104cd5 | 2022-04-06 16:19:31 +0100 | [diff] [blame] | 79 | { |
| 80 | const unsigned int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 81 | const unsigned int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 82 | |
| 83 | int start_x = id[idx_width] * stride_x - pad_x; |
| 84 | int start_y = id[idx_height] * stride_y - pad_y; |
| 85 | |
| 86 | const int end_x = std::min(start_x + pool_size_x, upper_bound_w); |
| 87 | const int end_y = std::min(start_y + pool_size_y, upper_bound_h); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 88 | if (exclude_padding) |
Adnan AlSinan | 9104cd5 | 2022-04-06 16:19:31 +0100 | [diff] [blame] | 89 | { |
| 90 | start_x = std::max(0, start_x); |
| 91 | start_y = std::max(0, start_y); |
| 92 | } |
| 93 | return 1.f / ((end_y - start_y) * (end_x - start_x)); |
| 94 | } |
| 95 | |
| 96 | template <typename T> |
| 97 | inline typename std::enable_if<std::is_same<T, int8_t>::value, int8_t>::type |
| 98 | quantize(float val, const UniformQuantizationInfo &info) |
| 99 | { |
| 100 | return quantize_qasymm8_signed(val, info); |
| 101 | } |
| 102 | |
| 103 | template <typename T> |
| 104 | inline typename std::enable_if<std::is_same<T, uint8_t>::value, uint8_t>::type |
| 105 | quantize(float val, const UniformQuantizationInfo &info) |
| 106 | { |
| 107 | return quantize_qasymm8(val, info); |
| 108 | } |
| 109 | |
| 110 | template <typename T> |
| 111 | inline T vcvtq_q32_f32(float32x4_t values); |
| 112 | |
| 113 | template <> |
| 114 | inline uint32x4_t vcvtq_q32_f32(float32x4_t values) |
| 115 | { |
| 116 | return vcvtq_u32_f32(values); |
| 117 | } |
| 118 | |
| 119 | template <> |
| 120 | inline int32x4_t vcvtq_q32_f32(float32x4_t values) |
| 121 | { |
| 122 | return vcvtq_s32_f32(values); |
| 123 | } |
| 124 | |
| 125 | template <typename T> |
| 126 | inline float32x4_t vcvtq_f32_q32(T values); |
| 127 | |
| 128 | template <> |
| 129 | inline float32x4_t vcvtq_f32_q32(uint32x4_t values) |
| 130 | { |
| 131 | return vcvtq_f32_u32(values); |
| 132 | } |
| 133 | |
| 134 | template <> |
| 135 | inline float32x4_t vcvtq_f32_q32(int32x4_t values) |
| 136 | { |
| 137 | return vcvtq_f32_s32(values); |
| 138 | } |
| 139 | |
| 140 | template <typename Tout> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 141 | inline Tout vrequantize_pooling_with_scale(const float32x4x4_t &acc, |
| 142 | const float quant_rescale, |
| 143 | const float scale_pooling, |
| 144 | const int32_t new_offset); |
Adnan AlSinan | 9104cd5 | 2022-04-06 16:19:31 +0100 | [diff] [blame] | 145 | |
| 146 | template <> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 147 | inline uint8x16_t vrequantize_pooling_with_scale(const float32x4x4_t &acc, |
| 148 | const float quant_rescale, |
| 149 | const float scale_pooling, |
| 150 | const int32_t new_offset) |
Adnan AlSinan | 9104cd5 | 2022-04-06 16:19:31 +0100 | [diff] [blame] | 151 | { |
| 152 | const float new_scale = quant_rescale / scale_pooling; |
| 153 | return vquantize(acc, UniformQuantizationInfo(new_scale, new_offset)); |
| 154 | } |
| 155 | |
| 156 | template <> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 157 | inline int8x16_t vrequantize_pooling_with_scale(const float32x4x4_t &acc, |
| 158 | const float quant_rescale, |
| 159 | const float scale_pooling, |
| 160 | const int32_t new_offset) |
Adnan AlSinan | 9104cd5 | 2022-04-06 16:19:31 +0100 | [diff] [blame] | 161 | { |
| 162 | const float new_scale = quant_rescale / scale_pooling; |
| 163 | return vquantize_signed(acc, UniformQuantizationInfo(new_scale, new_offset)); |
| 164 | } |
| 165 | |
| 166 | template <typename Tin, typename Tout> |
| 167 | inline Tout vrequantize_pooling(Tin vec1, Tin vec2, const UniformQuantizationInfo &requant_qinfo); |
| 168 | |
| 169 | template <> |
| 170 | inline uint8x16_t vrequantize_pooling(uint8x8_t vec1, uint8x8_t vec2, const UniformQuantizationInfo &requant_qinfo) |
| 171 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 172 | const float32x4x4_t acc = {{ |
| 173 | vcvtq_f32_u32(vmovl_u16(vget_low_u16(vmovl_u8((vec1))))), |
| 174 | vcvtq_f32_u32(vmovl_u16(vget_high_u16(vmovl_u8((vec1))))), |
| 175 | vcvtq_f32_u32(vmovl_u16(vget_low_u16(vmovl_u8((vec2))))), |
| 176 | vcvtq_f32_u32(vmovl_u16(vget_high_u16(vmovl_u8((vec2))))), |
| 177 | }}; |
Adnan AlSinan | 9104cd5 | 2022-04-06 16:19:31 +0100 | [diff] [blame] | 178 | return vquantize(acc, requant_qinfo); |
| 179 | } |
| 180 | |
| 181 | template <> |
| 182 | inline int8x16_t vrequantize_pooling(int8x8_t vec1, int8x8_t vec2, const UniformQuantizationInfo &requant_qinfo) |
| 183 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 184 | const float32x4x4_t acc = {{ |
| 185 | vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8((vec1))))), |
| 186 | vcvtq_f32_s32(vmovl_s16(vget_high_s16(vmovl_s8((vec1))))), |
| 187 | vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8((vec2))))), |
| 188 | vcvtq_f32_s32(vmovl_s16(vget_high_s16(vmovl_s8((vec2))))), |
| 189 | }}; |
Adnan AlSinan | 9104cd5 | 2022-04-06 16:19:31 +0100 | [diff] [blame] | 190 | return vquantize_signed(acc, requant_qinfo); |
| 191 | } |
| 192 | |
| 193 | template <typename T> |
| 194 | inline T vrequantize_pooling(T &vec, const UniformQuantizationInfo &requant_qinfo); |
| 195 | |
| 196 | template <> |
| 197 | inline uint8x8_t vrequantize_pooling(uint8x8_t &vec, const UniformQuantizationInfo &requant_qinfo) |
| 198 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 199 | const float32x4x2_t acc = {{ |
| 200 | vcvtq_f32_u32(vmovl_u16(vget_low_u16(vmovl_u8((vec))))), |
| 201 | vcvtq_f32_u32(vmovl_u16(vget_high_u16(vmovl_u8((vec))))), |
| 202 | }}; |
Adnan AlSinan | 9104cd5 | 2022-04-06 16:19:31 +0100 | [diff] [blame] | 203 | return vquantize(acc, requant_qinfo); |
| 204 | } |
| 205 | |
| 206 | template <> |
| 207 | inline int8x8_t vrequantize_pooling(int8x8_t &vec, const UniformQuantizationInfo &requant_qinfo) |
| 208 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 209 | const float32x4x2_t acc = {{ |
| 210 | vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8((vec))))), |
| 211 | vcvtq_f32_s32(vmovl_s16(vget_high_s16(vmovl_s8((vec))))), |
| 212 | }}; |
Adnan AlSinan | 9104cd5 | 2022-04-06 16:19:31 +0100 | [diff] [blame] | 213 | return vquantize_signed(acc, requant_qinfo); |
| 214 | } |
| 215 | |
| 216 | } // namespace |
| 217 | } // namespace cpu |
| 218 | } // namespace arm_compute |
| 219 | #endif /* SRC_CORE_HELPERS_POOLINGHELPERS_H */ |