blob: e61941435cc032d772a99a8fa1f94387ee8da045 [file] [log] [blame]
Michalis Spyrou7e9391b2018-10-05 14:49:28 +01001/*
Matthew Bentham945b8da2023-07-12 11:54:59 +00002 * Copyright (c) 2018-2021, 2023 Arm Limited.
Michalis Spyrou7e9391b2018-10-05 14:49:28 +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#ifndef ARM_COMPUTE_TEST_REDUCE_MEAN_FIXTURE
25#define ARM_COMPUTE_TEST_REDUCE_MEAN_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
Manuel Bottinic58f0ad2020-08-07 16:49:15 +010029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010030#include "arm_compute/runtime/Tensor.h"
31#include "tests/AssetsLibrary.h"
32#include "tests/Globals.h"
33#include "tests/IAccessor.h"
34#include "tests/framework/Asserts.h"
35#include "tests/framework/Fixture.h"
Michele Di Giorgioed5a4922018-09-13 16:22:01 +010036#include "tests/validation/Helpers.h"
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010037#include "tests/validation/reference/ReductionOperation.h"
38#include "tests/validation/reference/ReshapeLayer.h"
39
40namespace arm_compute
41{
42namespace test
43{
44namespace validation
45{
46template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
47class ReduceMeanValidationFixture : public framework::Fixture
48{
49public:
Manuel Bottinic58f0ad2020-08-07 16:49:15 +010050 void setup(TensorShape shape, DataType data_type, Coordinates axis, bool keep_dims, QuantizationInfo quantization_info_input, QuantizationInfo quantization_info_output)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010051 {
Manuel Bottinic58f0ad2020-08-07 16:49:15 +010052 _target = compute_target(shape, data_type, axis, keep_dims, quantization_info_input, quantization_info_output);
53 _reference = compute_reference(shape, data_type, axis, keep_dims, quantization_info_input, quantization_info_output);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010054 }
55
56protected:
57 template <typename U>
58 void fill(U &&tensor)
59 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000060 if(tensor.data_type() == DataType::F32)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010061 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000062 std::uniform_real_distribution<float> distribution(-1.0f, 1.0f);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010063 library->fill(tensor, distribution, 0);
64 }
Giorgio Arena4bdd1772020-12-17 16:47:07 +000065 else if(tensor.data_type() == DataType::F16)
66 {
Giorgio Arena33b103b2021-01-08 10:37:15 +000067 arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -1.0f, 1.0f };
Giorgio Arena4bdd1772020-12-17 16:47:07 +000068 library->fill(tensor, distribution, 0);
69 }
70 else if(is_data_type_quantized(tensor.data_type()))
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010071 {
Michele Di Giorgioed5a4922018-09-13 16:22:01 +010072 std::pair<int, int> bounds = get_quantized_bounds(tensor.quantization_info(), -1.0f, 1.0f);
73 std::uniform_int_distribution<> distribution(bounds.first, bounds.second);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010074
75 library->fill(tensor, distribution, 0);
76 }
Giorgio Arena4bdd1772020-12-17 16:47:07 +000077 else
78 {
79 library->fill_tensor_uniform(tensor, 0);
80 }
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010081 }
82
Manuel Bottinic58f0ad2020-08-07 16:49:15 +010083 TensorType compute_target(TensorShape &src_shape, DataType data_type, Coordinates axis, bool keep_dims, QuantizationInfo quantization_info_input, QuantizationInfo quantization_info_output)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010084 {
85 // Create tensors
Manuel Bottinic58f0ad2020-08-07 16:49:15 +010086 TensorType src = create_tensor<TensorType>(src_shape, data_type, 1, quantization_info_input);
87 TensorShape dst_shape = arm_compute::misc::shape_calculator::calculate_reduce_mean_shape(src.info(), axis, keep_dims);
88 TensorType dst = create_tensor<TensorType>(dst_shape, data_type, 1, quantization_info_output);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010089
90 // Create and configure function
91 FunctionType reduction_mean;
92 reduction_mean.configure(&src, axis, keep_dims, &dst);
93
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010094 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
95 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010096
97 // Allocate tensors
98 src.allocator()->allocate();
99 dst.allocator()->allocate();
100
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100101 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
102 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100103
104 // Fill tensors
105 fill(AccessorType(src));
106
107 // Compute function
108 reduction_mean.run();
109
110 return dst;
111 }
112
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100113 SimpleTensor<T> compute_reference(TensorShape &src_shape, DataType data_type, Coordinates axis, bool keep_dims, QuantizationInfo quantization_info_input, QuantizationInfo quantization_info_output)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100114 {
115 // Create reference
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100116 SimpleTensor<T> src{ src_shape, data_type, 1, quantization_info_input };
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100117
118 // Fill reference
119 fill(src);
120
121 SimpleTensor<T> out;
122 for(unsigned int i = 0; i < axis.num_dimensions(); ++i)
123 {
124 TensorShape output_shape = i == 0 ? src_shape : out.shape();
125 output_shape.set(axis[i], 1);
Mohammed Suhail Munshiecaa10a2023-02-09 11:52:06 +0000126 bool is_opencl = false;
127
128#ifdef ARM_COMPUTE_OPENCL_ENABLED
129 is_opencl = std::is_same<CLTensor, TensorType>::value; // Round down to zero on opencl to match kernel
Pablo Marquez Tello4cb0bd42023-07-27 18:02:37 +0100130#endif /* ARM_COMPUTE_OPENCL_ENABLED */
131 out = reference::reduction_operation<T, T>(i == 0 ? src : out, output_shape, axis[i], ReductionOperation::MEAN_SUM, data_type, quantization_info_output,
132 is_opencl ? RoundingPolicy::TO_ZERO : RoundingPolicy::TO_NEAREST_UP);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100133 }
134
135 if(!keep_dims)
136 {
137 TensorShape output_shape = src_shape;
Michalis Spyrou96f84612018-10-24 14:01:04 +0100138 std::sort(axis.begin(), axis.begin() + axis.num_dimensions());
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100139 for(unsigned int i = 0; i < axis.num_dimensions(); ++i)
140 {
Viet-Hoa Do78da34c2023-08-08 11:03:06 +0100141 output_shape.remove_dimension(axis[i] - i, false);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100142 }
143
144 out = reference::reshape_layer(out, output_shape);
145 }
146 return out;
147 }
148
149 TensorType _target{};
150 SimpleTensor<T> _reference{};
151};
152
153template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
154class ReduceMeanQuantizedFixture : public ReduceMeanValidationFixture<TensorType, AccessorType, FunctionType, T>
155{
156public:
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100157 void setup(TensorShape shape, DataType data_type, Coordinates axis, bool keep_dims, QuantizationInfo quantization_info_input, QuantizationInfo quantization_info_output)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100158 {
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100159 ReduceMeanValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, keep_dims, quantization_info_input, quantization_info_output);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100160 }
161};
162
163template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
164class ReduceMeanFixture : public ReduceMeanValidationFixture<TensorType, AccessorType, FunctionType, T>
165{
166public:
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100167 void setup(TensorShape shape, DataType data_type, Coordinates axis, bool keep_dims)
168 {
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100169 ReduceMeanValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, keep_dims, QuantizationInfo(), QuantizationInfo());
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100170 }
171};
172} // namespace validation
173} // namespace test
174} // namespace arm_compute
175#endif /* ARM_COMPUTE_TEST_REDUCE_MEAN_FIXTURE */