xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/fixtures/ReduceMeanFixture.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2018-2021 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_REDUCE_MEAN_FIXTURE
25 #define ARM_COMPUTE_TEST_REDUCE_MEAN_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/Helpers.h"
37 #include "tests/validation/reference/ReductionOperation.h"
38 #include "tests/validation/reference/ReshapeLayer.h"
39 
40 namespace arm_compute
41 {
42 namespace test
43 {
44 namespace validation
45 {
46 template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
47 class ReduceMeanValidationFixture : public framework::Fixture
48 {
49 public:
50     template <typename...>
setup(TensorShape shape,DataType data_type,Coordinates axis,bool keep_dims,QuantizationInfo quantization_info_input,QuantizationInfo quantization_info_output)51     void setup(TensorShape shape, DataType data_type, Coordinates axis, bool keep_dims, QuantizationInfo quantization_info_input, QuantizationInfo quantization_info_output)
52     {
53         _target    = compute_target(shape, data_type, axis, keep_dims, quantization_info_input, quantization_info_output);
54         _reference = compute_reference(shape, data_type, axis, keep_dims, quantization_info_input, quantization_info_output);
55     }
56 
57 protected:
58     template <typename U>
fill(U && tensor)59     void fill(U &&tensor)
60     {
61         if(tensor.data_type() == DataType::F32)
62         {
63             std::uniform_real_distribution<float> distribution(-1.0f, 1.0f);
64             library->fill(tensor, distribution, 0);
65         }
66         else if(tensor.data_type() == DataType::F16)
67         {
68             arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -1.0f, 1.0f };
69             library->fill(tensor, distribution, 0);
70         }
71         else if(is_data_type_quantized(tensor.data_type()))
72         {
73             std::pair<int, int> bounds = get_quantized_bounds(tensor.quantization_info(), -1.0f, 1.0f);
74             std::uniform_int_distribution<> distribution(bounds.first, bounds.second);
75 
76             library->fill(tensor, distribution, 0);
77         }
78         else
79         {
80             library->fill_tensor_uniform(tensor, 0);
81         }
82     }
83 
compute_target(TensorShape & src_shape,DataType data_type,Coordinates axis,bool keep_dims,QuantizationInfo quantization_info_input,QuantizationInfo quantization_info_output)84     TensorType compute_target(TensorShape &src_shape, DataType data_type, Coordinates axis, bool keep_dims, QuantizationInfo quantization_info_input, QuantizationInfo quantization_info_output)
85     {
86         // Create tensors
87         TensorType  src       = create_tensor<TensorType>(src_shape, data_type, 1, quantization_info_input);
88         TensorShape dst_shape = arm_compute::misc::shape_calculator::calculate_reduce_mean_shape(src.info(), axis, keep_dims);
89         TensorType  dst       = create_tensor<TensorType>(dst_shape, data_type, 1, quantization_info_output);
90 
91         // Create and configure function
92         FunctionType reduction_mean;
93         reduction_mean.configure(&src, axis, keep_dims, &dst);
94 
95         ARM_COMPUTE_ASSERT(src.info()->is_resizable());
96         ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
97 
98         // Allocate tensors
99         src.allocator()->allocate();
100         dst.allocator()->allocate();
101 
102         ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
103         ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
104 
105         // Fill tensors
106         fill(AccessorType(src));
107 
108         // Compute function
109         reduction_mean.run();
110 
111         return dst;
112     }
113 
compute_reference(TensorShape & src_shape,DataType data_type,Coordinates axis,bool keep_dims,QuantizationInfo quantization_info_input,QuantizationInfo quantization_info_output)114     SimpleTensor<T> compute_reference(TensorShape &src_shape, DataType data_type, Coordinates axis, bool keep_dims, QuantizationInfo quantization_info_input, QuantizationInfo quantization_info_output)
115     {
116         // Create reference
117         SimpleTensor<T> src{ src_shape, data_type, 1, quantization_info_input };
118 
119         // Fill reference
120         fill(src);
121 
122         SimpleTensor<T> out;
123         for(unsigned int i = 0; i < axis.num_dimensions(); ++i)
124         {
125             TensorShape output_shape = i == 0 ? src_shape : out.shape();
126             output_shape.set(axis[i], 1);
127             bool is_opencl = false;
128 
129 #ifdef ARM_COMPUTE_OPENCL_ENABLED
130             is_opencl = std::is_same<CLTensor, TensorType>::value; // Round down to zero on opencl to match kernel
131 #endif /* ARM_COMPUTE_OPENCL_ENABLED */
132             out = reference::reduction_operation<T, T>(i == 0 ? src : out, output_shape, axis[i], ReductionOperation::MEAN_SUM, quantization_info_output, is_opencl ? RoundingPolicy::TO_ZERO : RoundingPolicy::TO_NEAREST_UP);
133 
134         }
135 
136         if(!keep_dims)
137         {
138             TensorShape output_shape = src_shape;
139             std::sort(axis.begin(), axis.begin() + axis.num_dimensions());
140             for(unsigned int i = 0; i < axis.num_dimensions(); ++i)
141             {
142                 output_shape.remove_dimension(axis[i] - i);
143             }
144 
145             out = reference::reshape_layer(out, output_shape);
146         }
147         return out;
148     }
149 
150     TensorType      _target{};
151     SimpleTensor<T> _reference{};
152 };
153 
154 template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
155 class ReduceMeanQuantizedFixture : public ReduceMeanValidationFixture<TensorType, AccessorType, FunctionType, T>
156 {
157 public:
158     template <typename...>
setup(TensorShape shape,DataType data_type,Coordinates axis,bool keep_dims,QuantizationInfo quantization_info_input,QuantizationInfo quantization_info_output)159     void setup(TensorShape shape, DataType data_type, Coordinates axis, bool keep_dims, QuantizationInfo quantization_info_input, QuantizationInfo quantization_info_output)
160     {
161         ReduceMeanValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, keep_dims, quantization_info_input, quantization_info_output);
162     }
163 };
164 
165 template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
166 class ReduceMeanFixture : public ReduceMeanValidationFixture<TensorType, AccessorType, FunctionType, T>
167 {
168 public:
169     template <typename...>
setup(TensorShape shape,DataType data_type,Coordinates axis,bool keep_dims)170     void setup(TensorShape shape, DataType data_type, Coordinates axis, bool keep_dims)
171     {
172         ReduceMeanValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, keep_dims, QuantizationInfo(), QuantizationInfo());
173     }
174 };
175 } // namespace validation
176 } // namespace test
177 } // namespace arm_compute
178 #endif /* ARM_COMPUTE_TEST_REDUCE_MEAN_FIXTURE */
179