blob: 2f103a6f65dcf836b2280e8b65a4027e3da417c7 [file] [log] [blame]
Kaizen8938bd32017-09-28 14:38:23 +01001/*
Jenkins52ba29e2018-08-29 15:32:11 +00002 * Copyright (c) 2017-2018 ARM Limited.
Kaizen8938bd32017-09-28 14:38:23 +01003 *
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
31namespace arm_compute
32{
33namespace test
34{
35namespace validation
36{
37namespace reference
38{
39namespace
40{
41template <typename T>
Jenkinsb9abeae2018-11-22 11:58:08 +000042T reduce_operation(T *ptr, int reduce_elements, ReductionOperation op, int stride)
Kaizen8938bd32017-09-28 14:38:23 +010043{
Jenkinsb9abeae2018-11-22 11:58:08 +000044 using type = typename std::remove_cv<T>::type;
45 auto res = type(0);
Kaizen8938bd32017-09-28 14:38:23 +010046
Jenkinsb9abeae2018-11-22 11:58:08 +000047 if(std::is_integral<type>::value)
Kaizen8938bd32017-09-28 14:38:23 +010048 {
Jenkinsb9abeae2018-11-22 11:58:08 +000049 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);
Kaizen8938bd32017-09-28 14:38:23 +010060 }
Jenkinsb9abeae2018-11-22 11:58:08 +000061 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;
Kaizen8938bd32017-09-28 14:38:23 +010075}
76} // namespace
77
78template <typename T>
79SimpleTensor<T> reduction_operation(const SimpleTensor<T> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op)
80{
81 // Create reference
Jenkinsb9abeae2018-11-22 11:58:08 +000082 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];
Kaizen8938bd32017-09-28 14:38:23 +010088
Jenkinsb9abeae2018-11-22 11:58:08 +000089 switch(axis)
Kaizen8938bd32017-09-28 14:38:23 +010090 {
Jenkinsb9abeae2018-11-22 11:58:08 +000091 case 0:
Kaizen8938bd32017-09-28 14:38:23 +010092 {
Jenkinsb9abeae2018-11-22 11:58:08 +000093 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 }
Kaizen8938bd32017-09-28 14:38:23 +0100100 }
Jenkinsb9abeae2018-11-22 11:58:08 +0000101 break;
102 case 1:
Kaizen8938bd32017-09-28 14:38:23 +0100103 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000104 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:
Kaizen8938bd32017-09-28 14:38:23 +0100160 ARM_COMPUTE_ERROR("Unsupported reduction axis");
Kaizen8938bd32017-09-28 14:38:23 +0100161 }
162
163 return dst;
164}
165
166template SimpleTensor<float> reduction_operation(const SimpleTensor<float> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op);
Jenkins52ba29e2018-08-29 15:32:11 +0000167template SimpleTensor<half> reduction_operation(const SimpleTensor<half> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op);
Jenkinsb9abeae2018-11-22 11:58:08 +0000168template SimpleTensor<uint8_t> reduction_operation(const SimpleTensor<uint8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op);
Kaizen8938bd32017-09-28 14:38:23 +0100169} // namespace reference
170} // namespace validation
171} // namespace test
172} // namespace arm_compute