Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1 | /* |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020, 2023 Arm Limited. |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 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 | #include "ReductionOperation.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 25 | #include "tests/validation/Helpers.h" |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 26 | |
| 27 | #include <algorithm> |
| 28 | #include <cmath> |
| 29 | |
| 30 | namespace arm_compute |
| 31 | { |
| 32 | namespace test |
| 33 | { |
| 34 | namespace validation |
| 35 | { |
| 36 | namespace reference |
| 37 | { |
| 38 | namespace |
| 39 | { |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 40 | template <typename T, typename OT> |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 41 | OT reduce_operation(const T *ptr, int reduce_elements, ReductionOperation op, int stride, RoundingPolicy policy) |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 42 | { |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 43 | using type = typename std::remove_cv<OT>::type; |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 44 | T res; |
| 45 | switch(op) |
| 46 | { |
| 47 | case ReductionOperation::PROD: |
| 48 | { |
| 49 | res = type(1); |
| 50 | } |
| 51 | break; |
| 52 | case ReductionOperation::MIN: |
Usama Arif | 28f0dd9 | 2019-05-20 13:44:34 +0100 | [diff] [blame] | 53 | case ReductionOperation::MAX: |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 54 | { |
| 55 | res = *ptr; |
| 56 | } |
| 57 | break; |
| 58 | default: |
| 59 | { |
| 60 | res = type(0); |
| 61 | } |
| 62 | } |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 63 | |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 64 | if(std::is_integral<type>::value) |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 65 | { |
Luca Foschiani | d239053 | 2020-02-20 12:19:12 +0000 | [diff] [blame] | 66 | auto int_res = static_cast<int32_t>(res); |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 67 | for(int i = 0; i < reduce_elements; ++i) |
| 68 | { |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 69 | auto elem = *(ptr + stride * i); |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 70 | |
| 71 | switch(op) |
| 72 | { |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 73 | case ReductionOperation::MIN: |
| 74 | if(static_cast<T>(int_res) > elem) |
| 75 | { |
| 76 | int_res = elem; |
| 77 | } |
| 78 | break; |
Usama Arif | 28f0dd9 | 2019-05-20 13:44:34 +0100 | [diff] [blame] | 79 | case ReductionOperation::MAX: |
| 80 | if(static_cast<T>(int_res) < elem) |
| 81 | { |
| 82 | int_res = elem; |
| 83 | } |
| 84 | break; |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 85 | case ReductionOperation::SUM_SQUARE: |
| 86 | int_res += elem * elem; |
| 87 | break; |
| 88 | case ReductionOperation::MEAN_SUM: |
| 89 | case ReductionOperation::SUM: |
| 90 | int_res += elem; |
| 91 | break; |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 92 | case ReductionOperation::PROD: |
| 93 | int_res *= elem; |
| 94 | break; |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 95 | default: |
| 96 | ARM_COMPUTE_ERROR("Operation not supported"); |
| 97 | } |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 98 | } |
| 99 | if(op == ReductionOperation::MEAN_SUM && reduce_elements > 0) |
| 100 | { |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 101 | // Only use rounding in aarch64 to be consistent with kernel |
| 102 | #ifdef __aarch64__ |
| 103 | // Divide in float format, then rounded to nearest and implicitly cast back to int |
| 104 | int_res = round(static_cast<float>(int_res) / static_cast<float>(reduce_elements), policy); |
| 105 | #else // defined(__aarch64__) |
| 106 | ARM_COMPUTE_UNUSED(policy); |
| 107 | int_res /= reduce_elements; // Legacy compatibility |
| 108 | #endif // __aarch64 |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 109 | } |
Luca Foschiani | d239053 | 2020-02-20 12:19:12 +0000 | [diff] [blame] | 110 | res = static_cast<type>(int_res); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 111 | } |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 112 | else |
| 113 | { |
| 114 | for(int i = 0; i < reduce_elements; ++i) |
| 115 | { |
| 116 | auto elem = *(ptr + stride * i); |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 117 | switch(op) |
| 118 | { |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 119 | case ReductionOperation::MIN: |
| 120 | if(res > elem) |
| 121 | { |
| 122 | res = elem; |
| 123 | } |
| 124 | break; |
Usama Arif | 28f0dd9 | 2019-05-20 13:44:34 +0100 | [diff] [blame] | 125 | case ReductionOperation::MAX: |
| 126 | if(res < elem) |
| 127 | { |
| 128 | res = elem; |
| 129 | } |
| 130 | break; |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 131 | case ReductionOperation::SUM_SQUARE: |
| 132 | res += elem * elem; |
| 133 | break; |
| 134 | case ReductionOperation::MEAN_SUM: |
| 135 | case ReductionOperation::SUM: |
| 136 | res += elem; |
| 137 | break; |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 138 | case ReductionOperation::PROD: |
| 139 | res *= elem; |
| 140 | break; |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 141 | default: |
| 142 | ARM_COMPUTE_ERROR("Operation not supported"); |
| 143 | } |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 144 | } |
| 145 | if(op == ReductionOperation::MEAN_SUM && reduce_elements > 0) |
| 146 | { |
| 147 | res /= reduce_elements; |
| 148 | } |
| 149 | } |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 150 | return res; |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 151 | } |
Manuel Bottini | 62404a5 | 2019-06-10 17:06:39 +0100 | [diff] [blame] | 152 | |
| 153 | template <typename T, typename OT> |
| 154 | OT reduce_operation_arg_min_max(const T *ptr, int reduce_elements, ReductionOperation op, int stride) |
| 155 | { |
| 156 | uint32_t res = 0; |
| 157 | for(int i = 0; i < reduce_elements; ++i) |
| 158 | { |
| 159 | auto elem = *(ptr + stride * i); |
| 160 | switch(op) |
| 161 | { |
| 162 | case ReductionOperation::ARG_IDX_MIN: |
| 163 | if(*(ptr + stride * res) > elem) |
| 164 | { |
| 165 | res = static_cast<uint32_t>(i); |
| 166 | } |
| 167 | break; |
| 168 | case ReductionOperation::ARG_IDX_MAX: |
| 169 | if(*(ptr + stride * res) < elem) |
| 170 | { |
| 171 | res = static_cast<uint32_t>(i); |
| 172 | } |
| 173 | break; |
| 174 | default: |
| 175 | ARM_COMPUTE_ERROR("Operation not supported"); |
| 176 | } |
| 177 | } |
| 178 | return static_cast<OT>(res); |
| 179 | } |
| 180 | |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 181 | } // namespace |
| 182 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 183 | template <typename T, typename OT> |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 184 | SimpleTensor<OT> compute_reduction_operation(const SimpleTensor<T> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op, RoundingPolicy policy) |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 185 | { |
| 186 | // Create reference |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 187 | const bool is_arg_min_max = (op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::ARG_IDX_MAX); |
Sang-Hoon Park | eaa01ab | 2019-11-11 17:33:28 +0000 | [diff] [blame] | 188 | DataType output_data_type = is_arg_min_max ? DataType::S32 : src.data_type(); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 189 | SimpleTensor<OT> dst{ dst_shape, output_data_type, 1, src.quantization_info() }; |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 190 | const unsigned int src_width = src.shape().x(); |
| 191 | const unsigned int src_height = src.shape().y(); |
| 192 | const unsigned int src_depth = src.shape().z(); |
| 193 | const unsigned int src_batch = src.shape()[3]; |
| 194 | const int reduce_elems = src.shape()[axis]; |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 195 | |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 196 | switch(axis) |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 197 | { |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 198 | case 0: |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 199 | { |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 200 | const unsigned int upper_dims = src.shape().total_size_upper(1); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 201 | for(unsigned int du = 0; du < upper_dims; ++du) |
| 202 | { |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 203 | const T *src_row_ptr = src.data() + du * reduce_elems; |
Manuel Bottini | 62404a5 | 2019-06-10 17:06:39 +0100 | [diff] [blame] | 204 | dst[du] = is_arg_min_max ? |
| 205 | reduce_operation_arg_min_max<T, OT>(src_row_ptr, reduce_elems, op, 1) : |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 206 | reduce_operation<T, OT>(src_row_ptr, reduce_elems, op, 1, policy); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 207 | } |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 208 | } |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 209 | break; |
| 210 | case 1: |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 211 | { |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 212 | const unsigned int upper_dims = src.shape().total_size_upper(2); |
| 213 | for(unsigned int du = 0; du < upper_dims; ++du) |
| 214 | { |
| 215 | for(unsigned int x = 0; x < src_width; ++x) |
| 216 | { |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 217 | const int in_offset = du * src_height * src_width + x; |
| 218 | const int out_offset = du * src_width + x; |
| 219 | const T *src_row_ptr = src.data() + in_offset; |
Manuel Bottini | 62404a5 | 2019-06-10 17:06:39 +0100 | [diff] [blame] | 220 | dst[out_offset] = is_arg_min_max ? |
| 221 | reduce_operation_arg_min_max<T, OT>(src_row_ptr, reduce_elems, op, src_width) : |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 222 | reduce_operation<T, OT>(src_row_ptr, reduce_elems, op, src_width, policy); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | } |
| 226 | break; |
| 227 | case 2: |
| 228 | { |
| 229 | const unsigned int upper_dims = src.shape().total_size_upper(3); |
| 230 | for(unsigned int du = 0; du < upper_dims; ++du) |
| 231 | { |
| 232 | for(unsigned int x = 0; x < src_width; ++x) |
| 233 | { |
| 234 | for(unsigned int y = 0; y < src_height; ++y) |
| 235 | { |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 236 | const int in_offset = du * src_depth * src_height * src_width + y * src_width + x; |
| 237 | const int out_offset = du * src_width * src_height + y * src_width + x; |
| 238 | const T *src_row_ptr = src.data() + in_offset; |
Manuel Bottini | 62404a5 | 2019-06-10 17:06:39 +0100 | [diff] [blame] | 239 | dst[out_offset] = is_arg_min_max ? |
| 240 | reduce_operation_arg_min_max<T, OT>(src_row_ptr, reduce_elems, op, src_width * src_height) : |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 241 | reduce_operation<T, OT>(src_row_ptr, reduce_elems, op, src_width * src_height, policy); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | break; |
| 247 | case 3: |
| 248 | { |
| 249 | const unsigned int upper_dims = src.shape().total_size_upper(4); |
| 250 | for(unsigned int du = 0; du < upper_dims; ++du) |
| 251 | { |
| 252 | for(unsigned int z = 0; z < src_depth; ++z) |
| 253 | { |
| 254 | for(unsigned int y = 0; y < src_height; ++y) |
| 255 | { |
| 256 | for(unsigned int x = 0; x < src_width; ++x) |
| 257 | { |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 258 | const int in_offset = du * src_batch * src_depth * src_height * src_width + z * src_width * src_height + y * src_width + x; |
| 259 | const int out_offset = du * src_depth * src_height * src_width + z * src_width * src_height + y * src_width + x; |
| 260 | const T *src_row_ptr = src.data() + in_offset; |
Manuel Bottini | 62404a5 | 2019-06-10 17:06:39 +0100 | [diff] [blame] | 261 | dst[out_offset] = is_arg_min_max ? |
| 262 | reduce_operation_arg_min_max<T, OT>(src_row_ptr, reduce_elems, op, src_width * src_height * src_depth) : |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 263 | reduce_operation<T, OT>(src_row_ptr, reduce_elems, op, src_width * src_height * src_depth, policy); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | break; |
| 270 | default: |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 271 | ARM_COMPUTE_ERROR("Unsupported reduction axis"); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | return dst; |
| 275 | } |
| 276 | |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 277 | template <typename T, typename OT> |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 278 | SimpleTensor<OT> reduction_operation(const SimpleTensor<T> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op, QuantizationInfo quantization_info_output, RoundingPolicy policy) |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 279 | { |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 280 | ARM_COMPUTE_UNUSED(quantization_info_output); |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 281 | return compute_reduction_operation<T, OT>(src, dst_shape, axis, op, policy); |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | template <> |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 285 | SimpleTensor<uint8_t> reduction_operation(const SimpleTensor<uint8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op, QuantizationInfo quantization_info_output, RoundingPolicy policy) |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 286 | { |
Michalis Spyrou | 19bd412 | 2020-01-22 10:27:06 +0000 | [diff] [blame] | 287 | if(src.data_type() == DataType::QASYMM8) |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 288 | { |
Luca Foschiani | d239053 | 2020-02-20 12:19:12 +0000 | [diff] [blame] | 289 | // If the operation is MEAN_SUM, we can directly use the uint8 implementation without taking into account scale and offset |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 290 | if(op == ReductionOperation::MEAN_SUM && src.quantization_info() == quantization_info_output) |
Luca Foschiani | d239053 | 2020-02-20 12:19:12 +0000 | [diff] [blame] | 291 | { |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 292 | return compute_reduction_operation<uint8_t, uint8_t>(src, dst_shape, axis, op, policy); |
Luca Foschiani | d239053 | 2020-02-20 12:19:12 +0000 | [diff] [blame] | 293 | } |
| 294 | else |
| 295 | { |
| 296 | SimpleTensor<float> src_f = convert_from_asymmetric(src); |
| 297 | SimpleTensor<float> dst_f = reference::reduction_operation<float, float>(src_f, dst_shape, axis, op); |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 298 | return convert_to_asymmetric<uint8_t>(dst_f, quantization_info_output); |
Luca Foschiani | d239053 | 2020-02-20 12:19:12 +0000 | [diff] [blame] | 299 | } |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 300 | } |
| 301 | else |
| 302 | { |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 303 | return compute_reduction_operation<uint8_t, uint8_t>(src, dst_shape, axis, op, policy); |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
Michalis Spyrou | 0b18d97 | 2020-01-30 18:11:13 +0000 | [diff] [blame] | 307 | template <> |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 308 | SimpleTensor<int8_t> reduction_operation(const SimpleTensor<int8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op, QuantizationInfo quantization_info_output, RoundingPolicy policy) |
Michalis Spyrou | 0b18d97 | 2020-01-30 18:11:13 +0000 | [diff] [blame] | 309 | { |
| 310 | if(src.data_type() == DataType::QASYMM8_SIGNED) |
| 311 | { |
Luca Foschiani | d239053 | 2020-02-20 12:19:12 +0000 | [diff] [blame] | 312 | // If the operation is MEAN_SUM, we can directly use the int8 implementation without taking into account scale and offset |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 313 | if(op == ReductionOperation::MEAN_SUM && src.quantization_info() == quantization_info_output) |
Luca Foschiani | d239053 | 2020-02-20 12:19:12 +0000 | [diff] [blame] | 314 | { |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 315 | return compute_reduction_operation<int8_t, int8_t>(src, dst_shape, axis, op, policy); |
Luca Foschiani | d239053 | 2020-02-20 12:19:12 +0000 | [diff] [blame] | 316 | } |
| 317 | else |
| 318 | { |
| 319 | SimpleTensor<float> src_f = convert_from_asymmetric(src); |
| 320 | SimpleTensor<float> dst_f = reference::reduction_operation<float, float>(src_f, dst_shape, axis, op); |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 321 | return convert_to_asymmetric<int8_t>(dst_f, quantization_info_output); |
Luca Foschiani | d239053 | 2020-02-20 12:19:12 +0000 | [diff] [blame] | 322 | } |
Michalis Spyrou | 0b18d97 | 2020-01-30 18:11:13 +0000 | [diff] [blame] | 323 | } |
| 324 | else |
| 325 | { |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 326 | return compute_reduction_operation<int8_t, int8_t>(src, dst_shape, axis, op, policy); |
Michalis Spyrou | 0b18d97 | 2020-01-30 18:11:13 +0000 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 330 | template SimpleTensor<float> reduction_operation(const SimpleTensor<float> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op, |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 331 | QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO); |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 332 | template SimpleTensor<half> reduction_operation(const SimpleTensor<half> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op, |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 333 | QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO); |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 334 | |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 335 | template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<float> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op, |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 336 | QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO); |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 337 | template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<int32_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op, |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 338 | QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO); |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 339 | template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<half> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op, |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 340 | QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO); |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 341 | template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<uint8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op, |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 342 | QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO); |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 343 | template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<int8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op, |
Mohammed Suhail Munshi | 470cc5d | 2023-02-09 11:52:06 +0000 | [diff] [blame] | 344 | QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO); |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 345 | |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 346 | } // namespace reference |
| 347 | } // namespace validation |
| 348 | } // namespace test |
| 349 | } // namespace arm_compute |