xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/fixtures/ReductionOperationFixture.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-2022 Arm Limited.
3  *
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"
29 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
30 #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"
36 #include "tests/validation/reference/ReductionOperation.h"
37 
38 namespace arm_compute
39 {
40 namespace test
41 {
42 namespace validation
43 {
44 template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
45 class ReductionOperationValidationFixture : public framework::Fixture
46 {
47 public:
48     template <typename...>
49     void setup(TensorShape shape, DataType data_type, unsigned int axis, ReductionOperation op, QuantizationInfo quantization_info, bool keep_dims = false)
50     {
51         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);
58     }
59 
60 protected:
61     template <typename U>
fill(U && tensor)62     void fill(U &&tensor)
63     {
64         if(tensor.data_type() == DataType::F32)
65         {
66             std::uniform_real_distribution<float> distribution(-1.0f, 1.0f);
67             library->fill(tensor, distribution, 0);
68         }
69         else if(tensor.data_type() == DataType::F16)
70         {
71             arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -1.0f, 1.0f };
72             library->fill(tensor, distribution, 0);
73         }
74         else if(is_data_type_quantized(tensor.data_type()))
75         {
76             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<uint32_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<int32_t> distribution(bounds.first, bounds.second);
87 
88                 library->fill(tensor, distribution, 0);
89             }
90             else
91             {
92                 ARM_COMPUTE_ERROR("Not supported");
93             }
94         }
95         else
96         {
97             library->fill_tensor_uniform(tensor, 0);
98         }
99     }
100 
compute_target(const TensorShape & src_shape,DataType data_type,unsigned int axis,ReductionOperation op,QuantizationInfo quantization_info)101     TensorType compute_target(const TensorShape &src_shape, DataType data_type, unsigned int axis, ReductionOperation op, QuantizationInfo quantization_info)
102     {
103         // Create tensors
104         TensorType src = create_tensor<TensorType>(src_shape, data_type, 1, quantization_info);
105         TensorType dst;
106 
107         // Create and configure function
108         FunctionType reduction_func;
109         reduction_func.configure(&src, &dst, axis, op, _keep_dims);
110 
111         ARM_COMPUTE_ASSERT(src.info()->is_resizable());
112         ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
113 
114         // Allocate tensors
115         src.allocator()->allocate();
116         dst.allocator()->allocate();
117 
118         ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
119         ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
120 
121         // Fill tensors
122         fill(AccessorType(src));
123 
124         // Compute function
125         reduction_func.run();
126 
127         return dst;
128     }
129 
compute_reference(const TensorShape & src_shape,const TensorShape & dst_shape,DataType data_type,unsigned int axis,ReductionOperation op,QuantizationInfo quantization_info)130     SimpleTensor<T> compute_reference(const TensorShape &src_shape, const TensorShape &dst_shape, DataType data_type, unsigned int axis, ReductionOperation op, QuantizationInfo quantization_info)
131     {
132         // Create reference
133         SimpleTensor<T> src{ src_shape, data_type, 1, quantization_info };
134 
135         // Fill reference
136         fill(src);
137 
138         return reference::reduction_operation<T, T>(src, dst_shape, axis, op, quantization_info);
139     }
140 
141     TensorType      _target{};
142     SimpleTensor<T> _reference{};
143 
144 private:
145     bool _keep_dims{ false };
146 };
147 
148 template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
149 class ReductionOperationQuantizedFixture : public ReductionOperationValidationFixture<TensorType, AccessorType, FunctionType, T>
150 {
151 public:
152     template <typename...>
153     void setup(TensorShape shape, DataType data_type, unsigned int axis, ReductionOperation op, QuantizationInfo quantization_info = QuantizationInfo(), bool keep_dims = false)
154     {
155         ReductionOperationValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, op, quantization_info, keep_dims);
156     }
157 };
158 
159 template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
160 class ReductionOperationFixture : public ReductionOperationValidationFixture<TensorType, AccessorType, FunctionType, T>
161 {
162 public:
163     template <typename...>
164     void setup(TensorShape shape, DataType data_type, unsigned int axis, ReductionOperation op, bool keep_dims = false)
165     {
166         ReductionOperationValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, op, QuantizationInfo(), keep_dims);
167     }
168 };
169 } // namespace validation
170 } // namespace test
171 } // namespace arm_compute
172 #endif /* ARM_COMPUTE_TEST_REDUCTION_OPERATION_FIXTURE */
173