blob: 867c08ec3a3d68f2bec8c52c4c0d6782d3876fe9 [file] [log] [blame]
Georgios Pinitasd9769582017-08-03 10:19:40 +01001/*
Michalis Spyrouaea14c62019-01-03 11:10:25 +00002 * Copyright (c) 2017-2019 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 Spyroubcf8a962018-10-12 10:51:31 +010064 if(!is_data_type_quantized(tensor.data_type()))
65 {
66 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
67 library->fill(tensor, distribution, 0);
68 }
69 else
70 {
71 std::pair<int, int> bounds = get_quantized_bounds(tensor.quantization_info(), -1.0f, 1.0f);
72 std::uniform_int_distribution<uint8_t> distribution(bounds.first, bounds.second);
73
74 library->fill(tensor, distribution, 0);
75 }
Georgios Pinitasd9769582017-08-03 10:19:40 +010076 }
77
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010078 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 +010079 {
80 // Create tensors
Michalis Spyroubcf8a962018-10-12 10:51:31 +010081 TensorType src = create_tensor<TensorType>(src_shape, data_type, 1, quantization_info);
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010082 TensorType dst;
Georgios Pinitasd9769582017-08-03 10:19:40 +010083
84 // Create and configure function
85 FunctionType reduction_func;
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010086 reduction_func.configure(&src, &dst, axis, op, _keep_dims);
Georgios Pinitasd9769582017-08-03 10:19:40 +010087
88 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
89 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
90
91 // Allocate tensors
92 src.allocator()->allocate();
93 dst.allocator()->allocate();
94
95 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
96 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
97
98 // Fill tensors
99 fill(AccessorType(src));
100
101 // Compute function
102 reduction_func.run();
103
104 return dst;
105 }
106
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100107 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 +0100108 {
109 // Create reference
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100110 SimpleTensor<T> src{ src_shape, data_type, 1, quantization_info };
Georgios Pinitasd9769582017-08-03 10:19:40 +0100111
112 // Fill reference
113 fill(src);
114
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000115 return reference::reduction_operation<T, T>(src, dst_shape, axis, op);
Georgios Pinitasd9769582017-08-03 10:19:40 +0100116 }
117
118 TensorType _target{};
119 SimpleTensor<T> _reference{};
120
121private:
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100122 bool _keep_dims{ false };
Georgios Pinitasd9769582017-08-03 10:19:40 +0100123};
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100124
125template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
126class ReductionOperationQuantizedFixture : public ReductionOperationValidationFixture<TensorType, AccessorType, FunctionType, T>
127{
128public:
129 template <typename...>
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100130 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 +0100131 {
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100132 ReductionOperationValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, op, quantization_info, keep_dims);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100133 }
134};
135
136template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
137class ReductionOperationFixture : public ReductionOperationValidationFixture<TensorType, AccessorType, FunctionType, T>
138{
139public:
140 template <typename...>
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100141 void setup(TensorShape shape, DataType data_type, unsigned int axis, ReductionOperation op, bool keep_dims = false)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100142 {
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100143 ReductionOperationValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, op, QuantizationInfo(), keep_dims);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100144 }
145};
Georgios Pinitasd9769582017-08-03 10:19:40 +0100146} // namespace validation
147} // namespace test
148} // namespace arm_compute
149#endif /* ARM_COMPUTE_TEST_REDUCTION_OPERATION_FIXTURE */