blob: dd3519b549e95ec96dce42e87749a8a016cefb10 [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:
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000054 void setup(TensorShape input_shape, const Pool2dAttributes &pool_attr, DataType data_type, bool mixed_precision)
55 {
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +000056 _target = compute_target(input_shape, pool_attr, data_type, mixed_precision);
57 _reference =
58 compute_reference(input_shape, convert_pool_attr_to_pool_info(pool_attr, 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
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +000085 TensorType compute_target(TensorShape input_shape,
86 const Pool2dAttributes &pool_attr,
87 const DataType data_type,
88 bool mixed_precision)
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000089 {
90 CLScheduler::get().default_reinit();
91
92 // Change shape due to NHWC data layout, test shapes are NCHW
93 permute(input_shape, PermutationVector(2U, 0U, 1U));
94
95 // Create a new workload sketch
96 auto cl_compile_ctx = CLKernelLibrary::get().get_compile_context();
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +000097 auto context = GpuWorkloadContext{&cl_compile_ctx};
98 GpuWorkloadSketch sketch{&context};
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000099
100 // Create sketch tensors
Viet-Hoa Do3fcf3dc2023-05-17 15:17:48 +0100101 auto input_info = context.create_tensor_info(TensorInfo(input_shape, 1, data_type, DataLayout::NHWC));
102 auto dst_info = context.create_tensor_info();
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000103
104 // Create Pool2dSettings
105 GpuPool2dSettings pool_settings = GpuPool2dSettings().mixed_precision(mixed_precision);
106
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +0000107 ITensorInfo *ans_info = FunctionType::create_op(sketch, input_info, pool_attr, pool_settings);
108 GpuOutput::create_op(sketch, ans_info, dst_info);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000109
110 // Configure runtime
111 ClWorkloadRuntime runtime;
112 runtime.configure(sketch);
113 // (Important) Allocate auxiliary tensor memory if there are any
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +0000114 for (auto &data : runtime.get_auxiliary_tensors())
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000115 {
Ramy Elgammal002e6532023-01-11 18:48:04 +0000116 CLTensor *tensor = std::get<0>(data);
117 TensorInfo info = std::get<1>(data);
118 AuxMemoryInfo aux_mem_req = std::get<2>(data);
119 tensor->allocator()->init(info, aux_mem_req.alignment);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000120 tensor->allocator()->allocate(); // Use ACL allocated memory
121 }
122 // Construct user tensors
123 TensorType t_input{};
124 TensorType t_dst{};
125
126 // Initialize user tensors
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +0000127 t_input.allocator()->init(*input_info);
128 t_dst.allocator()->init(*dst_info);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000129
130 // Allocate and fill user tensors
131 t_input.allocator()->allocate();
132 t_dst.allocator()->allocate();
133
134 fill(AccessorType(t_input), 0);
135
136 // Run runtime
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +0000137 runtime.run({&t_input, &t_dst});
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000138 return t_dst;
139 }
140
141 SimpleTensor<T> compute_reference(TensorShape shape, PoolingLayerInfo pool_info, DataType data_type)
142 {
143 // Create reference
144 SimpleTensor<T> src(shape, data_type, 1, QuantizationInfo());
145 // Fill reference
146 fill(src, 0);
147 return reference::pooling_layer<T>(src, pool_info, QuantizationInfo(), nullptr, DataLayout::NCHW);
148 }
149
Ramy Elgammal002e6532023-01-11 18:48:04 +0000150 TensorType _target{};
151 SimpleTensor<T> _reference{};
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000152};
153
154template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +0000155class DynamicFusionGpuPool2dValidationFixture
156 : public DynamicFusionGpuPool2dValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000157{
158public:
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +0000159 void setup(TensorShape input_shape,
160 PoolingType pool_type,
161 Size2D pool_size,
162 Padding2D pad,
163 Size2D stride,
164 bool exclude_padding,
165 DataType data_type)
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000166 {
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +0000167 DynamicFusionGpuPool2dValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(
168 input_shape,
169 Pool2dAttributes().pool_type(pool_type).pool_size(pool_size).pad(pad).stride(stride).exclude_padding(
170 exclude_padding),
171 data_type, false);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000172 }
173};
174
175template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +0000176class DynamicFusionGpuPool2dMixedPrecisionValidationFixture
177 : public DynamicFusionGpuPool2dValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000178{
179public:
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +0000180 void setup(TensorShape input_shape,
181 PoolingType pool_type,
182 Size2D pool_size,
183 Padding2D pad,
184 Size2D stride,
185 bool exclude_padding,
186 DataType data_type,
187 bool mixed_precision)
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000188 {
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +0000189 DynamicFusionGpuPool2dValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(
190 input_shape,
191 Pool2dAttributes().pool_type(pool_type).pool_size(pool_size).pad(pad).stride(stride).exclude_padding(
192 exclude_padding),
193 data_type, mixed_precision);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000194 }
195};
196
197template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +0000198class DynamicFusionGpuPool2dSpecialValidationFixture
199 : public DynamicFusionGpuPool2dValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000200{
201public:
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000202 void setup(TensorShape input_shape, Pool2dAttributes pool_attr, DataType data_type)
203 {
Viet-Hoa Dofdf56fb2024-01-18 16:10:46 +0000204 DynamicFusionGpuPool2dValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(
205 input_shape, pool_attr, data_type, false);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000206 }
207};
208
209} // namespace validation
210} // namespace test
211} // namespace arm_compute
212
Adnan AlSinan2e6d6592023-08-21 13:54:27 +0100213#endif // ACL_TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_GPU_CL_POOL2DFIXTURE_H