Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 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; |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 45 | auto res = (op == ReductionOperation::PROD) ? type(1) : type(0); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 46 | |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 47 | if(std::is_integral<type>::value) |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 48 | { |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 49 | auto int_res = static_cast<uint32_t>(res); |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 50 | for(int i = 0; i < reduce_elements; ++i) |
| 51 | { |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 52 | auto elem = *(ptr + stride * i); |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 53 | |
| 54 | switch(op) |
| 55 | { |
| 56 | case ReductionOperation::ARG_IDX_MIN: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 57 | if(*(ptr + stride * static_cast<uint32_t>(int_res)) > elem) |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 58 | { |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 59 | int_res = static_cast<uint32_t>(i); |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 60 | } |
| 61 | break; |
| 62 | case ReductionOperation::ARG_IDX_MAX: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 63 | if(*(ptr + stride * static_cast<uint32_t>(int_res)) < elem) |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 64 | { |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 65 | int_res = static_cast<uint32_t>(i); |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 66 | } |
| 67 | break; |
| 68 | case ReductionOperation::SUM_SQUARE: |
| 69 | int_res += elem * elem; |
| 70 | break; |
| 71 | case ReductionOperation::MEAN_SUM: |
| 72 | case ReductionOperation::SUM: |
| 73 | int_res += elem; |
| 74 | break; |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 75 | case ReductionOperation::PROD: |
| 76 | int_res *= elem; |
| 77 | break; |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 78 | default: |
| 79 | ARM_COMPUTE_ERROR("Operation not supported"); |
| 80 | } |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 81 | } |
| 82 | if(op == ReductionOperation::MEAN_SUM && reduce_elements > 0) |
| 83 | { |
| 84 | int_res /= reduce_elements; |
| 85 | } |
| 86 | res = saturate_cast<type>(int_res); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 87 | } |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 88 | else |
| 89 | { |
| 90 | for(int i = 0; i < reduce_elements; ++i) |
| 91 | { |
| 92 | auto elem = *(ptr + stride * i); |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 93 | switch(op) |
| 94 | { |
| 95 | case ReductionOperation::ARG_IDX_MIN: |
| 96 | if(*(ptr + stride * static_cast<uint32_t>(res)) > elem) |
| 97 | { |
| 98 | res = static_cast<uint32_t>(i); |
| 99 | } |
| 100 | break; |
| 101 | case ReductionOperation::ARG_IDX_MAX: |
| 102 | if(*(ptr + stride * static_cast<uint32_t>(res)) < elem) |
| 103 | { |
| 104 | res = static_cast<uint32_t>(i); |
| 105 | } |
| 106 | break; |
| 107 | case ReductionOperation::SUM_SQUARE: |
| 108 | res += elem * elem; |
| 109 | break; |
| 110 | case ReductionOperation::MEAN_SUM: |
| 111 | case ReductionOperation::SUM: |
| 112 | res += elem; |
| 113 | break; |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 114 | case ReductionOperation::PROD: |
| 115 | res *= elem; |
| 116 | break; |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 117 | default: |
| 118 | ARM_COMPUTE_ERROR("Operation not supported"); |
| 119 | } |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 120 | } |
| 121 | if(op == ReductionOperation::MEAN_SUM && reduce_elements > 0) |
| 122 | { |
| 123 | res /= reduce_elements; |
| 124 | } |
| 125 | } |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 126 | return res; |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 127 | } |
| 128 | } // namespace |
| 129 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 130 | template <typename T, typename OT> |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 131 | 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] | 132 | { |
| 133 | // Create reference |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 134 | const bool is_arg_min_max = (op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::ARG_IDX_MAX); |
| 135 | DataType output_data_type = is_arg_min_max ? DataType::U32 : src.data_type(); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 136 | SimpleTensor<OT> dst{ dst_shape, output_data_type, 1, src.quantization_info() }; |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 137 | const unsigned int src_width = src.shape().x(); |
| 138 | const unsigned int src_height = src.shape().y(); |
| 139 | const unsigned int src_depth = src.shape().z(); |
| 140 | const unsigned int src_batch = src.shape()[3]; |
| 141 | const int reduce_elems = src.shape()[axis]; |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 142 | |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 143 | switch(axis) |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 144 | { |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 145 | case 0: |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 146 | { |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 147 | const unsigned int upper_dims = src.shape().total_size_upper(1); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 148 | for(unsigned int du = 0; du < upper_dims; ++du) |
| 149 | { |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 150 | const T *src_row_ptr = src.data() + du * reduce_elems; |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 151 | dst[du] = reduce_operation<T, OT>(src_row_ptr, reduce_elems, op, 1); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 152 | } |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 153 | } |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 154 | break; |
| 155 | case 1: |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 156 | { |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 157 | const unsigned int upper_dims = src.shape().total_size_upper(2); |
| 158 | for(unsigned int du = 0; du < upper_dims; ++du) |
| 159 | { |
| 160 | for(unsigned int x = 0; x < src_width; ++x) |
| 161 | { |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 162 | const int in_offset = du * src_height * src_width + x; |
| 163 | const int out_offset = du * src_width + x; |
| 164 | const T *src_row_ptr = src.data() + in_offset; |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 165 | dst[out_offset] = reduce_operation<T, OT>(src_row_ptr, reduce_elems, op, src_width); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | } |
| 169 | break; |
| 170 | case 2: |
| 171 | { |
| 172 | const unsigned int upper_dims = src.shape().total_size_upper(3); |
| 173 | for(unsigned int du = 0; du < upper_dims; ++du) |
| 174 | { |
| 175 | for(unsigned int x = 0; x < src_width; ++x) |
| 176 | { |
| 177 | for(unsigned int y = 0; y < src_height; ++y) |
| 178 | { |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 179 | const int in_offset = du * src_depth * src_height * src_width + y * src_width + x; |
| 180 | const int out_offset = du * src_width * src_height + y * src_width + x; |
| 181 | const T *src_row_ptr = src.data() + in_offset; |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 182 | dst[out_offset] = reduce_operation<T, OT>(src_row_ptr, reduce_elems, op, src_height * src_width); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | break; |
| 188 | case 3: |
| 189 | { |
| 190 | const unsigned int upper_dims = src.shape().total_size_upper(4); |
| 191 | for(unsigned int du = 0; du < upper_dims; ++du) |
| 192 | { |
| 193 | for(unsigned int z = 0; z < src_depth; ++z) |
| 194 | { |
| 195 | for(unsigned int y = 0; y < src_height; ++y) |
| 196 | { |
| 197 | for(unsigned int x = 0; x < src_width; ++x) |
| 198 | { |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 199 | const int in_offset = du * src_batch * src_depth * src_height * src_width + z * src_width * src_height + y * src_width + x; |
| 200 | const int out_offset = du * src_depth * src_height * src_width + z * src_width * src_height + y * src_width + x; |
| 201 | const T *src_row_ptr = src.data() + in_offset; |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 202 | dst[out_offset] = 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] | 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | break; |
| 209 | default: |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 210 | ARM_COMPUTE_ERROR("Unsupported reduction axis"); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | return dst; |
| 214 | } |
| 215 | |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 216 | template <typename T, typename OT> |
| 217 | SimpleTensor<OT> reduction_operation(const SimpleTensor<T> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op) |
| 218 | { |
| 219 | return compute_reduction_operation<T, OT>(src, dst_shape, axis, op); |
| 220 | } |
| 221 | |
| 222 | template <> |
| 223 | SimpleTensor<uint8_t> reduction_operation(const SimpleTensor<uint8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op) |
| 224 | { |
| 225 | if(src.data_type() == DataType::QASYMM8 && op != ReductionOperation::MEAN_SUM) |
| 226 | { |
| 227 | SimpleTensor<float> src_f = convert_from_asymmetric(src); |
| 228 | SimpleTensor<float> dst_f = reference::reduction_operation<float, float>(src_f, dst_shape, axis, op); |
| 229 | return convert_to_asymmetric(dst_f, src.quantization_info()); |
| 230 | } |
| 231 | else |
| 232 | { |
| 233 | return compute_reduction_operation<uint8_t, uint8_t>(src, dst_shape, axis, op); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | template SimpleTensor<float> reduction_operation(const SimpleTensor<float> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op); |
| 238 | template SimpleTensor<half> reduction_operation(const SimpleTensor<half> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op); |
| 239 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 240 | template SimpleTensor<uint32_t> reduction_operation(const SimpleTensor<float> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op); |
| 241 | template SimpleTensor<uint32_t> reduction_operation(const SimpleTensor<half> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op); |
| 242 | template SimpleTensor<uint32_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] | 243 | |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 244 | } // namespace reference |
| 245 | } // namespace validation |
| 246 | } // namespace test |
| 247 | } // namespace arm_compute |