xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/fixtures/Pooling3dLayerFixture.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 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_POOLING_3D_LAYER_FIXTURE
25 #define ARM_COMPUTE_TEST_POOLING_3D_LAYER_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/Pooling3dLayer.h"
37 #include <random>
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 Pooling3dLayerValidationGenericFixture : public framework::Fixture
46 {
47 public:
48     template <typename...>
49     void setup(TensorShape shape, Pooling3dLayerInfo pool_info, DataType data_type, QuantizationInfo input_qinfo = QuantizationInfo(), QuantizationInfo output_qinfo = QuantizationInfo())
50     {
51         _target    = compute_target(shape, pool_info, data_type, input_qinfo, output_qinfo);
52         _reference = compute_reference(shape, pool_info, data_type, input_qinfo, output_qinfo);
53     }
54 
55 protected:
56     template <typename U>
fill(U && tensor)57     void fill(U &&tensor)
58     {
59         if(tensor.data_type() == DataType::F32)
60         {
61             std::uniform_real_distribution<float> distribution(-1.0f, 1.0f);
62             library->fill(tensor, distribution, 0);
63         }
64         else if(tensor.data_type() == DataType::F16)
65         {
66             arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -1.0f, 1.0f };
67             library->fill(tensor, distribution, 0);
68         }
69         else // data type is quantized_asymmetric
70         {
71             library->fill_tensor_uniform(tensor, 0);
72         }
73     }
74 
compute_target(TensorShape shape,Pooling3dLayerInfo info,DataType data_type,QuantizationInfo input_qinfo,QuantizationInfo output_qinfo)75     TensorType compute_target(TensorShape shape, Pooling3dLayerInfo info,
76                               DataType data_type, QuantizationInfo input_qinfo, QuantizationInfo output_qinfo)
77     {
78         // Create tensors
79         TensorType        src       = create_tensor<TensorType>(shape, data_type, 1, input_qinfo, DataLayout::NDHWC);
80         const TensorShape dst_shape = misc::shape_calculator::compute_pool3d_shape((src.info()->tensor_shape()), info);
81         TensorType        dst       = create_tensor<TensorType>(dst_shape, data_type, 1, output_qinfo, DataLayout::NDHWC);
82 
83         // Create and configure function
84         FunctionType pool_layer;
85         pool_layer.validate(src.info(), dst.info(), info);
86         pool_layer.configure(&src, &dst, info);
87 
88         ARM_COMPUTE_ASSERT(src.info()->is_resizable());
89         ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
90 
91         // Allocate tensors
92         src.allocator()->allocate();
93         dst.allocator()->allocate();
94 
95         ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
96         ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
97 
98         // Fill tensors
99         fill(AccessorType(src));
100 
101         // Compute function
102         pool_layer.run();
103         return dst;
104     }
105 
compute_reference(TensorShape shape,Pooling3dLayerInfo info,DataType data_type,QuantizationInfo input_qinfo,QuantizationInfo output_qinfo)106     SimpleTensor<T> compute_reference(TensorShape shape, Pooling3dLayerInfo info, DataType data_type, QuantizationInfo input_qinfo, QuantizationInfo output_qinfo)
107     {
108         // Create reference
109         SimpleTensor<T> src(shape, data_type, 1, input_qinfo, DataLayout::NDHWC);
110         // Fill reference
111         fill(src);
112         return reference::pooling_3d_layer<T>(src, info, output_qinfo);
113     }
114 
115     TensorType      _target{};
116     SimpleTensor<T> _reference{};
117 };
118 
119 template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
120 class Pooling3dLayerValidationFixture : public Pooling3dLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
121 {
122 public:
123     template <typename...>
setup(TensorShape shape,PoolingType pool_type,Size3D pool_size,Size3D stride,Padding3D padding,bool exclude_padding,DataType data_type)124     void setup(TensorShape shape, PoolingType pool_type, Size3D pool_size, Size3D stride, Padding3D padding, bool exclude_padding, DataType data_type)
125     {
126         Pooling3dLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, Pooling3dLayerInfo(pool_type, pool_size, stride, padding, exclude_padding),
127                                                                                                  data_type);
128     }
129 };
130 
131 template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
132 class Pooling3dLayerValidationQuantizedFixture : public Pooling3dLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
133 {
134 public:
135     template <typename...>
136     void setup(TensorShape shape, PoolingType pool_type, Size3D pool_size, Size3D stride, Padding3D padding, bool exclude_padding, DataType data_type,
137                QuantizationInfo input_qinfo = QuantizationInfo(), QuantizationInfo output_qinfo = QuantizationInfo())
138     {
139         Pooling3dLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, Pooling3dLayerInfo(pool_type, pool_size, stride, padding, exclude_padding),
140                                                                                                  data_type, input_qinfo, output_qinfo);
141     }
142 };
143 
144 template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
145 class Pooling3dLayerGlobalValidationFixture : public Pooling3dLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
146 {
147 public:
148     template <typename...>
setup(TensorShape shape,PoolingType pool_type,DataType data_type)149     void setup(TensorShape shape, PoolingType pool_type, DataType data_type)
150     {
151         Pooling3dLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, Pooling3dLayerInfo(pool_type), data_type);
152     }
153 };
154 
155 template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
156 class SpecialPooling3dLayerValidationFixture : public Pooling3dLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
157 {
158 public:
159     template <typename...>
setup(TensorShape src_shape,Pooling3dLayerInfo pool_info,DataType data_type)160     void setup(TensorShape src_shape, Pooling3dLayerInfo pool_info, DataType data_type)
161     {
162         Pooling3dLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(src_shape, pool_info, data_type);
163     }
164 };
165 
166 } // namespace validation
167 } // namespace test
168 } // namespace arm_compute
169 #endif /* ARM_COMPUTE_TEST_POOLING_3D_LAYER_FIXTURE */
170