blob: fc422ad35b4833fba1f840e34f14bb775e0f6ed3 [file] [log] [blame]
Georgios Pinitasd9769582017-08-03 10:19:40 +01001/*
Giorgio Arena33b103b2021-01-08 10:37:15 +00002 * Copyright (c) 2017-2021 Arm Limited.
Georgios Pinitasd9769582017-08-03 10:19:40 +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_REDUCTION_OPERATION_FIXTURE
25#define ARM_COMPUTE_TEST_REDUCTION_OPERATION_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010030#include "arm_compute/runtime/Tensor.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010031#include "tests/AssetsLibrary.h"
32#include "tests/Globals.h"
33#include "tests/IAccessor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010034#include "tests/framework/Asserts.h"
35#include "tests/framework/Fixture.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000036#include "tests/validation/reference/ReductionOperation.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010037
38namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
44template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
45class ReductionOperationValidationFixture : public framework::Fixture
46{
47public:
48 template <typename...>
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010049 void setup(TensorShape shape, DataType data_type, unsigned int axis, ReductionOperation op, QuantizationInfo quantization_info, bool keep_dims = false)
Georgios Pinitasd9769582017-08-03 10:19:40 +010050 {
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010051 const bool is_arg_min_max = (op == ReductionOperation::ARG_IDX_MAX) || (op == ReductionOperation::ARG_IDX_MIN);
52 _keep_dims = keep_dims && !is_arg_min_max;
53
54 const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_reduced_shape(shape, axis, _keep_dims);
55
56 _target = compute_target(shape, data_type, axis, op, quantization_info);
57 _reference = compute_reference(shape, output_shape, data_type, axis, op, quantization_info);
Georgios Pinitasd9769582017-08-03 10:19:40 +010058 }
59
60protected:
61 template <typename U>
62 void fill(U &&tensor)
63 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000064 if(tensor.data_type() == DataType::F32)
Michalis Spyroubcf8a962018-10-12 10:51:31 +010065 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000066 std::uniform_real_distribution<float> distribution(-1.0f, 1.0f);
Michalis Spyroubcf8a962018-10-12 10:51:31 +010067 library->fill(tensor, distribution, 0);
68 }
Giorgio Arena4bdd1772020-12-17 16:47:07 +000069 else if(tensor.data_type() == DataType::F16)
70 {
Giorgio Arena33b103b2021-01-08 10:37:15 +000071 arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -1.0f, 1.0f };
Giorgio Arena4bdd1772020-12-17 16:47:07 +000072 library->fill(tensor, distribution, 0);
73 }
74 else if(is_data_type_quantized(tensor.data_type()))
Michalis Spyroubcf8a962018-10-12 10:51:31 +010075 {
Luca Foschianiee939fb2020-01-28 10:38:07 +000076 if(tensor.data_type() == DataType::QASYMM8)
77 {
78 std::pair<int, int> bounds = get_quantized_bounds(tensor.quantization_info(), -1.0f, 1.0f);
79 std::uniform_int_distribution<uint8_t> distribution(bounds.first, bounds.second);
80
81 library->fill(tensor, distribution, 0);
82 }
83 else if(tensor.data_type() == DataType::QASYMM8_SIGNED)
84 {
85 std::pair<int, int> bounds = get_quantized_qasymm8_signed_bounds(tensor.quantization_info(), -1.0f, 1.0f);
86 std::uniform_int_distribution<int8_t> distribution(bounds.first, bounds.second);
87
88 library->fill(tensor, distribution, 0);
89 }
90 else
91 {
92 ARM_COMPUTE_ERROR("Not supported");
93 }
Michalis Spyroubcf8a962018-10-12 10:51:31 +010094 }
Giorgio Arena4bdd1772020-12-17 16:47:07 +000095 else
96 {
97 library->fill_tensor_uniform(tensor, 0);
98 }
Georgios Pinitasd9769582017-08-03 10:19:40 +010099 }
100
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100101 TensorType compute_target(const TensorShape &src_shape, DataType data_type, unsigned int axis, ReductionOperation op, QuantizationInfo quantization_info)
Georgios Pinitasd9769582017-08-03 10:19:40 +0100102 {
103 // Create tensors
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100104 TensorType src = create_tensor<TensorType>(src_shape, data_type, 1, quantization_info);
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100105 TensorType dst;
Georgios Pinitasd9769582017-08-03 10:19:40 +0100106
107 // Create and configure function
108 FunctionType reduction_func;
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100109 reduction_func.configure(&src, &dst, axis, op, _keep_dims);
Georgios Pinitasd9769582017-08-03 10:19:40 +0100110
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100111 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
112 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Georgios Pinitasd9769582017-08-03 10:19:40 +0100113
114 // Allocate tensors
115 src.allocator()->allocate();
116 dst.allocator()->allocate();
117
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100118 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
119 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Georgios Pinitasd9769582017-08-03 10:19:40 +0100120
121 // Fill tensors
122 fill(AccessorType(src));
123
124 // Compute function
125 reduction_func.run();
126
127 return dst;
128 }
129
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100130 SimpleTensor<T> compute_reference(const TensorShape &src_shape, const TensorShape &dst_shape, DataType data_type, unsigned int axis, ReductionOperation op, QuantizationInfo quantization_info)
Georgios Pinitasd9769582017-08-03 10:19:40 +0100131 {
132 // Create reference
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100133 SimpleTensor<T> src{ src_shape, data_type, 1, quantization_info };
Georgios Pinitasd9769582017-08-03 10:19:40 +0100134
135 // Fill reference
136 fill(src);
137
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100138 return reference::reduction_operation<T, T>(src, dst_shape, axis, op, quantization_info);
Georgios Pinitasd9769582017-08-03 10:19:40 +0100139 }
140
141 TensorType _target{};
142 SimpleTensor<T> _reference{};
143
144private:
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100145 bool _keep_dims{ false };
Georgios Pinitasd9769582017-08-03 10:19:40 +0100146};
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100147
148template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
149class ReductionOperationQuantizedFixture : public ReductionOperationValidationFixture<TensorType, AccessorType, FunctionType, T>
150{
151public:
152 template <typename...>
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100153 void setup(TensorShape shape, DataType data_type, unsigned int axis, ReductionOperation op, QuantizationInfo quantization_info = QuantizationInfo(), bool keep_dims = false)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100154 {
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100155 ReductionOperationValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, op, quantization_info, keep_dims);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100156 }
157};
158
159template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
160class ReductionOperationFixture : public ReductionOperationValidationFixture<TensorType, AccessorType, FunctionType, T>
161{
162public:
163 template <typename...>
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100164 void setup(TensorShape shape, DataType data_type, unsigned int axis, ReductionOperation op, bool keep_dims = false)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100165 {
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100166 ReductionOperationValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, op, QuantizationInfo(), keep_dims);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100167 }
168};
Georgios Pinitasd9769582017-08-03 10:19:40 +0100169} // namespace validation
170} // namespace test
171} // namespace arm_compute
172#endif /* ARM_COMPUTE_TEST_REDUCTION_OPERATION_FIXTURE */