blob: 646518d2e8554c089c74a187b28b9f62ff39de41 [file] [log] [blame]
Georgios Pinitasd9769582017-08-03 10:19:40 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 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 {
Luca Foschianiee939fb2020-01-28 10:38:07 +000064 if(!is_data_type_quantized(tensor.data_type()))
Michalis Spyroubcf8a962018-10-12 10:51:31 +010065 {
Luca Foschianiee939fb2020-01-28 10:38:07 +000066 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
Michalis Spyroubcf8a962018-10-12 10:51:31 +010067 library->fill(tensor, distribution, 0);
68 }
69 else
70 {
Luca Foschianiee939fb2020-01-28 10:38:07 +000071 if(tensor.data_type() == DataType::QASYMM8)
72 {
73 std::pair<int, int> bounds = get_quantized_bounds(tensor.quantization_info(), -1.0f, 1.0f);
74 std::uniform_int_distribution<uint8_t> distribution(bounds.first, bounds.second);
75
76 library->fill(tensor, distribution, 0);
77 }
78 else if(tensor.data_type() == DataType::QASYMM8_SIGNED)
79 {
80 std::pair<int, int> bounds = get_quantized_qasymm8_signed_bounds(tensor.quantization_info(), -1.0f, 1.0f);
81 std::uniform_int_distribution<int8_t> distribution(bounds.first, bounds.second);
82
83 library->fill(tensor, distribution, 0);
84 }
85 else
86 {
87 ARM_COMPUTE_ERROR("Not supported");
88 }
Michalis Spyroubcf8a962018-10-12 10:51:31 +010089 }
Georgios Pinitasd9769582017-08-03 10:19:40 +010090 }
91
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010092 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 +010093 {
94 // Create tensors
Michalis Spyroubcf8a962018-10-12 10:51:31 +010095 TensorType src = create_tensor<TensorType>(src_shape, data_type, 1, quantization_info);
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010096 TensorType dst;
Georgios Pinitasd9769582017-08-03 10:19:40 +010097
98 // Create and configure function
99 FunctionType reduction_func;
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100100 reduction_func.configure(&src, &dst, axis, op, _keep_dims);
Georgios Pinitasd9769582017-08-03 10:19:40 +0100101
102 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
103 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
104
105 // Allocate tensors
106 src.allocator()->allocate();
107 dst.allocator()->allocate();
108
109 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
110 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
111
112 // Fill tensors
113 fill(AccessorType(src));
114
115 // Compute function
116 reduction_func.run();
117
118 return dst;
119 }
120
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100121 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 +0100122 {
123 // Create reference
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100124 SimpleTensor<T> src{ src_shape, data_type, 1, quantization_info };
Georgios Pinitasd9769582017-08-03 10:19:40 +0100125
126 // Fill reference
127 fill(src);
128
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100129 return reference::reduction_operation<T, T>(src, dst_shape, axis, op, quantization_info);
Georgios Pinitasd9769582017-08-03 10:19:40 +0100130 }
131
132 TensorType _target{};
133 SimpleTensor<T> _reference{};
134
135private:
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100136 bool _keep_dims{ false };
Georgios Pinitasd9769582017-08-03 10:19:40 +0100137};
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100138
139template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
140class ReductionOperationQuantizedFixture : public ReductionOperationValidationFixture<TensorType, AccessorType, FunctionType, T>
141{
142public:
143 template <typename...>
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100144 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 +0100145 {
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100146 ReductionOperationValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, op, quantization_info, keep_dims);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100147 }
148};
149
150template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
151class ReductionOperationFixture : public ReductionOperationValidationFixture<TensorType, AccessorType, FunctionType, T>
152{
153public:
154 template <typename...>
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100155 void setup(TensorShape shape, DataType data_type, unsigned int axis, ReductionOperation op, bool keep_dims = false)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100156 {
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100157 ReductionOperationValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, op, QuantizationInfo(), keep_dims);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100158 }
159};
Georgios Pinitasd9769582017-08-03 10:19:40 +0100160} // namespace validation
161} // namespace test
162} // namespace arm_compute
163#endif /* ARM_COMPUTE_TEST_REDUCTION_OPERATION_FIXTURE */