blob: b0c7143d91d779165202520ea3f31d9a3681be29 [file] [log] [blame]
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +00001/*
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +00002 * Copyright (c) 2023-2024 Arm Limited.
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +00003 *
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 */
Adnan AlSinan2e6d6592023-08-21 13:54:27 +010024#ifndef ACL_TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_GPU_CL_POOL2DFIXTURE_H
25#define ACL_TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_GPU_CL_POOL2DFIXTURE_H
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000026
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/TensorInfo.h"
29#include "arm_compute/core/Types.h"
30#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000031#include "arm_compute/dynamic_fusion/runtime/gpu/cl/ClWorkloadRuntime.h"
32#include "arm_compute/dynamic_fusion/sketch/attributes/Pool2dAttributes.h"
33#include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadSketch.h"
Adnan AlSinan2e6d6592023-08-21 13:54:27 +010034#include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuOutput.h"
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +000035#include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuPool2d.h"
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000036
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +000037#include "src/dynamic_fusion/utils/Utils.h"
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000038#include "tests/CL/CLAccessor.h"
39#include "tests/framework/Fixture.h"
40#include "tests/validation/reference/PoolingLayer.h"
41
42using namespace arm_compute::experimental::dynamic_fusion;
43
44namespace arm_compute
45{
46namespace test
47{
48namespace validation
49{
50template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
51class DynamicFusionGpuPool2dValidationGenericFixture : public framework::Fixture
52{
53public:
Gunes Bayir2b9fa592024-01-17 16:07:03 +000054 void setup(TensorShape input_shape, const Pool2dAttributes &pool_attr, DataType data_type)
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000055 {
Gunes Bayir2b9fa592024-01-17 16:07:03 +000056 _target = compute_target(input_shape, pool_attr, data_type);
57 _reference = compute_reference(
58 input_shape, convert_pool_attr_to_pool_info(pool_attr, true /* mixed_precision */), data_type);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000059 }
60
61protected:
62 template <typename U>
63 void fill(U &&tensor, int i)
64 {
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +000065 switch (tensor.data_type())
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000066 {
67 case DataType::F16:
68 {
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +000069 arm_compute::utils::uniform_real_distribution_16bit<half> distribution{-1.0f, 1.0f};
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000070 library->fill(tensor, distribution, i);
71 break;
72 }
73 case DataType::F32:
74 {
75 std::uniform_real_distribution<float> distribution(-1.0f, 1.0f);
76 library->fill(tensor, distribution, i);
77 break;
78 }
79 default:
80 library->fill_tensor_uniform(tensor, i);
81 }
82 }
83
84 // Given input is in nchw format
Gunes Bayir2b9fa592024-01-17 16:07:03 +000085 TensorType compute_target(TensorShape input_shape, const Pool2dAttributes &pool_attr, const DataType data_type)
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000086 {
87 CLScheduler::get().default_reinit();
88
89 // Change shape due to NHWC data layout, test shapes are NCHW
90 permute(input_shape, PermutationVector(2U, 0U, 1U));
91
92 // Create a new workload sketch
93 auto cl_compile_ctx = CLKernelLibrary::get().get_compile_context();
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +000094 auto context = GpuWorkloadContext{&cl_compile_ctx};
95 GpuWorkloadSketch sketch{&context};
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000096
97 // Create sketch tensors
Viet-Hoa Do3fcf3dc2023-05-17 15:17:48 +010098 auto input_info = context.create_tensor_info(TensorInfo(input_shape, 1, data_type, DataLayout::NHWC));
99 auto dst_info = context.create_tensor_info();
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000100
101 // Create Pool2dSettings
Gunes Bayir2b9fa592024-01-17 16:07:03 +0000102 GpuPool2dSettings pool_settings = GpuPool2dSettings();
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000103
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +0000104 ITensorInfo *ans_info = FunctionType::create_op(sketch, input_info, pool_attr, pool_settings);
105 GpuOutput::create_op(sketch, ans_info, dst_info);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000106
107 // Configure runtime
108 ClWorkloadRuntime runtime;
109 runtime.configure(sketch);
110 // (Important) Allocate auxiliary tensor memory if there are any
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +0000111 for (auto &data : runtime.get_auxiliary_tensors())
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000112 {
Ramy Elgammal002e6532023-01-11 18:48:04 +0000113 CLTensor *tensor = std::get<0>(data);
114 TensorInfo info = std::get<1>(data);
115 AuxMemoryInfo aux_mem_req = std::get<2>(data);
116 tensor->allocator()->init(info, aux_mem_req.alignment);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000117 tensor->allocator()->allocate(); // Use ACL allocated memory
118 }
119 // Construct user tensors
120 TensorType t_input{};
121 TensorType t_dst{};
122
123 // Initialize user tensors
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +0000124 t_input.allocator()->init(*input_info);
125 t_dst.allocator()->init(*dst_info);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000126
127 // Allocate and fill user tensors
128 t_input.allocator()->allocate();
129 t_dst.allocator()->allocate();
130
131 fill(AccessorType(t_input), 0);
132
133 // Run runtime
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +0000134 runtime.run({&t_input, &t_dst});
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000135 return t_dst;
136 }
137
138 SimpleTensor<T> compute_reference(TensorShape shape, PoolingLayerInfo pool_info, DataType data_type)
139 {
140 // Create reference
141 SimpleTensor<T> src(shape, data_type, 1, QuantizationInfo());
142 // Fill reference
143 fill(src, 0);
144 return reference::pooling_layer<T>(src, pool_info, QuantizationInfo(), nullptr, DataLayout::NCHW);
145 }
146
Ramy Elgammal002e6532023-01-11 18:48:04 +0000147 TensorType _target{};
148 SimpleTensor<T> _reference{};
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000149};
150
151template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +0000152class DynamicFusionGpuPool2dValidationFixture
153 : public DynamicFusionGpuPool2dValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000154{
155public:
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +0000156 void setup(TensorShape input_shape,
157 PoolingType pool_type,
158 Size2D pool_size,
159 Padding2D pad,
160 Size2D stride,
161 bool exclude_padding,
162 DataType data_type)
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000163 {
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +0000164 DynamicFusionGpuPool2dValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(
165 input_shape,
166 Pool2dAttributes().pool_type(pool_type).pool_size(pool_size).pad(pad).stride(stride).exclude_padding(
167 exclude_padding),
Gunes Bayir2b9fa592024-01-17 16:07:03 +0000168 data_type);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000169 }
170};
171
172template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +0000173class DynamicFusionGpuPool2dSpecialValidationFixture
174 : public DynamicFusionGpuPool2dValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000175{
176public:
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000177 void setup(TensorShape input_shape, Pool2dAttributes pool_attr, DataType data_type)
178 {
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +0000179 DynamicFusionGpuPool2dValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(
Gunes Bayir2b9fa592024-01-17 16:07:03 +0000180 input_shape, pool_attr, data_type);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000181 }
182};
183
184} // namespace validation
185} // namespace test
186} // namespace arm_compute
187
Adnan AlSinan2e6d6592023-08-21 13:54:27 +0100188#endif // ACL_TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_GPU_CL_POOL2DFIXTURE_H