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