Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 1 | /* |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +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 | |
| 26 | #include "tests/validation/Helpers.h" |
| 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 | { |
| 41 | template <typename T> |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 42 | T reduce_operation(T *ptr, int reduce_elements, ReductionOperation op, int stride) |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 43 | { |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 44 | using type = typename std::remove_cv<T>::type; |
| 45 | auto res = type(0); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 46 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 47 | if(std::is_integral<type>::value) |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 48 | { |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 49 | uint32_t int_res = 0; |
| 50 | for(int i = 0; i < reduce_elements; ++i) |
| 51 | { |
| 52 | auto elem = static_cast<uint32_t>(*(ptr + stride * i)); |
| 53 | int_res += (op == ReductionOperation::SUM_SQUARE) ? elem * elem : elem; |
| 54 | } |
| 55 | if(op == ReductionOperation::MEAN_SUM && reduce_elements > 0) |
| 56 | { |
| 57 | int_res /= reduce_elements; |
| 58 | } |
| 59 | res = saturate_cast<type>(int_res); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 60 | } |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 61 | else |
| 62 | { |
| 63 | for(int i = 0; i < reduce_elements; ++i) |
| 64 | { |
| 65 | auto elem = *(ptr + stride * i); |
| 66 | res += (op == ReductionOperation::SUM_SQUARE) ? elem * elem : elem; |
| 67 | } |
| 68 | if(op == ReductionOperation::MEAN_SUM && reduce_elements > 0) |
| 69 | { |
| 70 | res /= reduce_elements; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return res; |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 75 | } |
| 76 | } // namespace |
| 77 | |
| 78 | template <typename T> |
| 79 | SimpleTensor<T> reduction_operation(const SimpleTensor<T> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op) |
| 80 | { |
| 81 | // Create reference |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 82 | SimpleTensor<T> dst{ dst_shape, src.data_type(), 1, src.quantization_info() }; |
| 83 | const unsigned int src_width = src.shape().x(); |
| 84 | const unsigned int src_height = src.shape().y(); |
| 85 | const unsigned int src_depth = src.shape().z(); |
| 86 | const unsigned int src_batch = src.shape()[3]; |
| 87 | const int reduce_elems = src.shape()[axis]; |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 88 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 89 | switch(axis) |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 90 | { |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 91 | case 0: |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 92 | { |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 93 | const unsigned int upper_dims = src.shape().total_size_upper(1); |
| 94 | for(unsigned int du = 0; du < upper_dims; ++du) |
| 95 | { |
| 96 | const T *src_row_ptr = src.data() + du * reduce_elems; |
| 97 | auto res = reduce_operation(src_row_ptr, reduce_elems, op, 1); |
| 98 | dst[du] = res; |
| 99 | } |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 100 | } |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 101 | break; |
| 102 | case 1: |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 103 | { |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 104 | const unsigned int upper_dims = src.shape().total_size_upper(2); |
| 105 | for(unsigned int du = 0; du < upper_dims; ++du) |
| 106 | { |
| 107 | for(unsigned int x = 0; x < src_width; ++x) |
| 108 | { |
| 109 | const int in_offset = du * src_height * src_width + x; |
| 110 | const int out_offset = du * src_width + x; |
| 111 | const T *src_row_ptr = src.data() + in_offset; |
| 112 | auto res = reduce_operation(src_row_ptr, reduce_elems, op, src_width); |
| 113 | dst[out_offset] = res; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | break; |
| 118 | case 2: |
| 119 | { |
| 120 | const unsigned int upper_dims = src.shape().total_size_upper(3); |
| 121 | for(unsigned int du = 0; du < upper_dims; ++du) |
| 122 | { |
| 123 | for(unsigned int x = 0; x < src_width; ++x) |
| 124 | { |
| 125 | for(unsigned int y = 0; y < src_height; ++y) |
| 126 | { |
| 127 | const int in_offset = du * src_depth * src_height * src_width + y * src_width + x; |
| 128 | const int out_offset = du * src_width * src_height + y * src_width + x; |
| 129 | const T *src_row_ptr = src.data() + in_offset; |
| 130 | auto res = reduce_operation(src_row_ptr, reduce_elems, op, src_height * src_width); |
| 131 | dst[out_offset] = res; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | break; |
| 137 | case 3: |
| 138 | { |
| 139 | const unsigned int upper_dims = src.shape().total_size_upper(4); |
| 140 | for(unsigned int du = 0; du < upper_dims; ++du) |
| 141 | { |
| 142 | for(unsigned int z = 0; z < src_depth; ++z) |
| 143 | { |
| 144 | for(unsigned int y = 0; y < src_height; ++y) |
| 145 | { |
| 146 | for(unsigned int x = 0; x < src_width; ++x) |
| 147 | { |
| 148 | const int in_offset = du * src_batch * src_depth * src_height * src_width + z * src_width * src_height + y * src_width + x; |
| 149 | const int out_offset = du * src_depth * src_height * src_width + z * src_width * src_height + y * src_width + x; |
| 150 | const T *src_row_ptr = src.data() + in_offset; |
| 151 | auto res = reduce_operation(src_row_ptr, reduce_elems, op, src_width * src_height * src_depth); |
| 152 | dst[out_offset] = res; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | break; |
| 159 | default: |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 160 | ARM_COMPUTE_ERROR("Unsupported reduction axis"); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | return dst; |
| 164 | } |
| 165 | |
| 166 | template SimpleTensor<float> reduction_operation(const SimpleTensor<float> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 167 | template SimpleTensor<half> reduction_operation(const SimpleTensor<half> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op); |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 168 | template SimpleTensor<uint8_t> reduction_operation(const SimpleTensor<uint8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 169 | } // namespace reference |
| 170 | } // namespace validation |
| 171 | } // namespace test |
| 172 | } // namespace arm_compute |