blob: 2802cd4c0a789e370360387af5486f037e2422e5 [file] [log] [blame]
Georgios Pinitasd9769582017-08-03 10:19:40 +01001/*
Michalis Spyrou0b18d972020-01-30 18:11:13 +00002 * 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 {
Michalis Spyrou0b18d972020-01-30 18:11:13 +000064 if(tensor.data_type() == DataType::QASYMM8)
Michalis Spyroubcf8a962018-10-12 10:51:31 +010065 {
Michalis Spyrou0b18d972020-01-30 18:11:13 +000066 std::pair<int, int> bounds = get_quantized_bounds(tensor.quantization_info(), -1.0f, 1.0f);
67 std::uniform_int_distribution<uint8_t> distribution(bounds.first, bounds.second);
68 library->fill(tensor, distribution, 0);
69 }
70 else if(tensor.data_type() == DataType::QASYMM8_SIGNED)
71 {
72 std::pair<int, int> bounds = get_quantized_qasymm8_signed_bounds(tensor.quantization_info(), -1.0f, 1.0f);
73 std::uniform_int_distribution<int8_t> distribution(bounds.first, bounds.second);
Michalis Spyroubcf8a962018-10-12 10:51:31 +010074 library->fill(tensor, distribution, 0);
75 }
76 else
77 {
Michalis Spyrou0b18d972020-01-30 18:11:13 +000078 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
Michalis Spyroubcf8a962018-10-12 10:51:31 +010079 library->fill(tensor, distribution, 0);
80 }
Georgios Pinitasd9769582017-08-03 10:19:40 +010081 }
82
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010083 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 +010084 {
85 // Create tensors
Michalis Spyroubcf8a962018-10-12 10:51:31 +010086 TensorType src = create_tensor<TensorType>(src_shape, data_type, 1, quantization_info);
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010087 TensorType dst;
Georgios Pinitasd9769582017-08-03 10:19:40 +010088
89 // Create and configure function
90 FunctionType reduction_func;
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010091 reduction_func.configure(&src, &dst, axis, op, _keep_dims);
Georgios Pinitasd9769582017-08-03 10:19:40 +010092
93 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
94 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
95
96 // Allocate tensors
97 src.allocator()->allocate();
98 dst.allocator()->allocate();
99
100 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
101 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
102
103 // Fill tensors
104 fill(AccessorType(src));
105
106 // Compute function
107 reduction_func.run();
108
109 return dst;
110 }
111
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100112 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 +0100113 {
114 // Create reference
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100115 SimpleTensor<T> src{ src_shape, data_type, 1, quantization_info };
Georgios Pinitasd9769582017-08-03 10:19:40 +0100116
117 // Fill reference
118 fill(src);
119
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000120 return reference::reduction_operation<T, T>(src, dst_shape, axis, op);
Georgios Pinitasd9769582017-08-03 10:19:40 +0100121 }
122
123 TensorType _target{};
124 SimpleTensor<T> _reference{};
125
126private:
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100127 bool _keep_dims{ false };
Georgios Pinitasd9769582017-08-03 10:19:40 +0100128};
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100129
130template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
131class ReductionOperationQuantizedFixture : public ReductionOperationValidationFixture<TensorType, AccessorType, FunctionType, T>
132{
133public:
134 template <typename...>
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100135 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 +0100136 {
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100137 ReductionOperationValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, op, quantization_info, keep_dims);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100138 }
139};
140
141template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
142class ReductionOperationFixture : public ReductionOperationValidationFixture<TensorType, AccessorType, FunctionType, T>
143{
144public:
145 template <typename...>
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100146 void setup(TensorShape shape, DataType data_type, unsigned int axis, ReductionOperation op, bool keep_dims = false)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100147 {
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100148 ReductionOperationValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, op, QuantizationInfo(), keep_dims);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100149 }
150};
Georgios Pinitasd9769582017-08-03 10:19:40 +0100151} // namespace validation
152} // namespace test
153} // namespace arm_compute
154#endif /* ARM_COMPUTE_TEST_REDUCTION_OPERATION_FIXTURE */