blob: d9ce4dff1891d994493a504dfcd4f87701a1bfb7 [file] [log] [blame]
Ramy Elgammal73f19af2022-10-23 11:44:49 +01001/*
Gunes Bayir3a1e1252023-01-03 21:26:09 +00002 * Copyright (c) 2022-2023 Arm Limited.
Ramy Elgammal73f19af2022-10-23 11:44:49 +01003 *
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 */
Ramy Elgammal404462a2022-11-08 02:14:46 +000024#ifndef TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_GPU_CL_DIRECTCONV2DFIXTURE
25#define TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_GPU_CL_DIRECTCONV2DFIXTURE
Ramy Elgammal73f19af2022-10-23 11:44:49 +010026
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/TensorInfo.h"
29#include "arm_compute/core/Types.h"
30
SiCong Li5a63d1e2023-01-06 16:28:57 +000031#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Ramy Elgammal73f19af2022-10-23 11:44:49 +010032#include "arm_compute/dynamic_fusion/runtime/gpu/cl/ClWorkloadRuntime.h"
SiCong Li5a63d1e2023-01-06 16:28:57 +000033#include "arm_compute/dynamic_fusion/sketch/attributes/Conv2dAttributes.h"
Ramy Elgammal73f19af2022-10-23 11:44:49 +010034#include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadSketch.h"
35#include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuConv2d.h"
Viet-Hoa Dob84e2532022-12-13 13:09:10 +000036#include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuOutput.h"
Ramy Elgammal73f19af2022-10-23 11:44:49 +010037
Ramy Elgammal73f19af2022-10-23 11:44:49 +010038#include "tests/CL/CLAccessor.h"
Ramy Elgammal73f19af2022-10-23 11:44:49 +010039#include "tests/framework/Fixture.h"
40#include "tests/framework/Macros.h"
Ramy Elgammal73f19af2022-10-23 11:44:49 +010041#include "tests/validation/Validation.h"
42#include "tests/validation/reference/ConvolutionLayer.h"
Ramy Elgammal73f19af2022-10-23 11:44:49 +010043#include "tests/validation/reference/Permute.h"
44
45using namespace arm_compute::experimental::dynamic_fusion;
46
47namespace arm_compute
48{
49namespace test
50{
51namespace validation
52{
SiCong Li5a63d1e2023-01-06 16:28:57 +000053namespace
54{
55template <typename U>
56void fill(U &&tensor, int i)
57{
58 switch(tensor.data_type())
59 {
60 case DataType::F16:
61 {
62 arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -1.0f, 1.0f };
63 library->fill(tensor, distribution, i);
64 break;
65 }
66 case DataType::F32:
67 {
68 std::uniform_real_distribution<float> distribution(-1.0f, 1.0f);
69 library->fill(tensor, distribution, i);
70 break;
71 }
72 default:
73 library->fill_tensor_uniform(tensor, i);
74 }
75}
76
77} // namespace
78
79/** General Conv2d fixture
80 * Adapted from tests/validation/fixtures/ConvolutionLayerFixture.h
81 * TODO: Parameterize to be fully backend agnostic: COMPMID-5760; remove Gpu from name
82 */
Ramy Elgammal73f19af2022-10-23 11:44:49 +010083template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
84class DynamicFusionGpuConv2dValidationGenericFixture : public framework::Fixture
85{
86public:
87 using TBias = typename std::conditional < std::is_same<typename std::decay<T>::type, uint8_t>::value
88 || std::is_same<typename std::decay<T>::type, int8_t>::value,
89 int32_t, T >::type; // If T: uint8_t or int8_t then TBias: int32_t, otherwise TBias: T
90
91 template <typename...>
92 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, const PadStrideInfo &info, const Size2D &dilation, DataType data_type,
93 DataLayout data_layout, QuantizationInfo quantization_info, QuantizationInfo weight_quantization_info)
94 {
95 ARM_COMPUTE_ERROR_ON(data_layout != DataLayout::NHWC); // Dynamic fusion conv2d only supports NHWC layout
96 const Conv2dAttributes conv2d_attr = convert_pad_stride_info_to_conv_attr(info, dilation);
97 _data_type = data_type;
98 _data_layout = data_layout;
99 _is_quantized = is_data_type_quantized_asymmetric(data_type);
100 _quantization_info = quantization_info;
101 _weight_quantization_info = weight_quantization_info;
102 _bias_data_type = _is_quantized ? DataType::S32 : data_type;
103 _target = compute_target(input_shape, weights_shape, bias_shape, conv2d_attr);
104 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, conv2d_attr);
105 }
106
107protected:
Ramy Elgammal73f19af2022-10-23 11:44:49 +0100108 // Given input is in nchw format
109 TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, const TensorShape &bias_shape, Conv2dAttributes conv2d_attr)
110 {
111 ARM_COMPUTE_ERROR_ON(_data_layout != DataLayout::NHWC);
112 permute(input_shape, PermutationVector(2U, 0U, 1U));
113 permute(weights_shape, PermutationVector(2U, 0U, 1U));
114 CLScheduler::get().default_reinit();
115
116 // Create a new workload sketch
117 auto cl_compile_ctx = CLKernelLibrary::get().get_compile_context();
118 auto gpu_ctx = GpuWorkloadContext{ &cl_compile_ctx };
119 GpuWorkloadSketch sketch{ &gpu_ctx };
120
121 // Create sketch tensors
Gunes Bayir3a1e1252023-01-03 21:26:09 +0000122 TensorInfo input_info = sketch.create_tensor_info(TensorInfo(input_shape, 1, _data_type, _data_layout));
123 TensorInfo weight_info = sketch.create_tensor_info(TensorInfo(weights_shape, 1, _data_type, _data_layout));
124 TensorInfo bias_info = sketch.create_tensor_info(TensorInfo(bias_shape, 1, _data_type, _data_layout));
125 TensorInfo dst_info = sketch.create_tensor_info();
Viet-Hoa Dob84e2532022-12-13 13:09:10 +0000126
Gunes Bayir3a1e1252023-01-03 21:26:09 +0000127 ITensorInfo *ans_info = FunctionType::create_op(sketch, &input_info, &weight_info, &bias_info, conv2d_attr);
128 GpuOutput::create_op(sketch, ans_info, &dst_info);
Ramy Elgammal73f19af2022-10-23 11:44:49 +0100129
130 // Configure runtime
131 ClWorkloadRuntime runtime;
132 runtime.configure(sketch);
133 // (Important) Allocate auxiliary tensor memory if there are any
134 for(auto &data : runtime.get_auxiliary_tensors())
135 {
Ramy Elgammal002e6532023-01-11 18:48:04 +0000136 CLTensor *tensor = std::get<0>(data);
137 TensorInfo info = std::get<1>(data);
138 AuxMemoryInfo aux_mem_req = std::get<2>(data);
139 tensor->allocator()->init(info, aux_mem_req.alignment);
Ramy Elgammal73f19af2022-10-23 11:44:49 +0100140 tensor->allocator()->allocate(); // Use ACL allocated memory
141 }
142 // Construct user tensors
Ramy Elgammal404462a2022-11-08 02:14:46 +0000143 TensorType t_input{};
144 TensorType t_weight{};
145 TensorType t_bias{};
146 TensorType t_dst{};
Ramy Elgammal73f19af2022-10-23 11:44:49 +0100147
148 // Initialize user tensors
149 t_input.allocator()->init(input_info);
150 t_weight.allocator()->init(weight_info);
151 t_bias.allocator()->init(bias_info);
152 t_dst.allocator()->init(dst_info);
153
154 // Allocate and fill user tensors
155 t_input.allocator()->allocate();
156 t_weight.allocator()->allocate();
157 t_bias.allocator()->allocate();
158 t_dst.allocator()->allocate();
Ramy Elgammal404462a2022-11-08 02:14:46 +0000159
160 fill(AccessorType(t_input), 0);
161 fill(AccessorType(t_weight), 1);
162 fill(AccessorType(t_bias), 2);
Ramy Elgammal73f19af2022-10-23 11:44:49 +0100163
164 // Run runtime
165 runtime.run({ &t_input, &t_weight, &t_bias, &t_dst });
166 return t_dst;
167 }
168
169 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape,
170 const TensorShape &output_shape, Conv2dAttributes conv2d_attr)
171 {
172 // Create reference
173 SimpleTensor<T> src{ input_shape, _data_type, 1, _quantization_info };
174 SimpleTensor<T> weight{ weights_shape, _data_type, 1, _weight_quantization_info };
175 SimpleTensor<TBias> bias{ bias_shape, _data_type, 1, _quantization_info };
176
177 fill(src, 0);
178 fill(weight, 1);
179 fill(bias, 2);
180
181 auto src_nchw = src;
182 auto weights_nchw = weight;
183 auto bias_nchw = bias;
184 auto output_shape_nchw = output_shape;
185
186 PadStrideInfo legacy_pad_stride(conv2d_attr.stride().x(), conv2d_attr.stride().y(), conv2d_attr.pad().left, conv2d_attr.pad().right, conv2d_attr.pad().top, conv2d_attr.pad().bottom,
187 DimensionRoundingType{});
188 auto dst_nchw = reference::convolution_layer(src_nchw, weights_nchw, bias_nchw, output_shape_nchw, legacy_pad_stride, conv2d_attr.dilation());
189 return dst_nchw;
190 }
191
192 TensorType _target{};
193 SimpleTensor<T> _reference{};
194 DataType _data_type{};
Ramy Elgammal73f19af2022-10-23 11:44:49 +0100195 DataType _bias_data_type{};
Ramy Elgammal73f19af2022-10-23 11:44:49 +0100196 DataLayout _data_layout{};
197 QuantizationInfo _quantization_info{};
198 QuantizationInfo _weight_quantization_info{};
199 bool _is_quantized = false;
Ramy Elgammal73f19af2022-10-23 11:44:49 +0100200};
201
202template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
203class DynamicFusionGpuConv2dValidationFixture : public DynamicFusionGpuConv2dValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
204{
205public:
206 template <typename...>
207 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape output_shape, TensorShape bias_shape,
208 const PadStrideInfo &info, const Size2D &dialation, DataType data_type, DataLayout data_layout, QuantizationInfo quantization_info)
209 {
210 DynamicFusionGpuConv2dValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, output_shape, bias_shape, info, dialation,
Ramy Elgammal404462a2022-11-08 02:14:46 +0000211 data_type, data_layout, quantization_info, quantization_info);
Ramy Elgammal73f19af2022-10-23 11:44:49 +0100212 }
213};
SiCong Li5a63d1e2023-01-06 16:28:57 +0000214
215/** Specific Conv2d method: Direct Conv2d fixture
216 * Adapted from tests/validation/fixtures/DirectConvolutionLayerFixture.h
217 * TODO: Parameterize to be fully backend agnostic: COMPMID-5760
218 */
219template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
220class DynamicFusionDirectConv2dValidationGenericFixture : public framework::Fixture
221{
222public:
223 using TBias = typename std::conditional < std::is_same<T, uint8_t>::value || std::is_same<T, int8_t>::value, int32_t, T >::type;
224
225 template <typename...>
226 void setup(TensorShape input_shape, int stride_x, int stride_y, int pad_x, int pad_y, unsigned int kernel_size, unsigned int num_kernels,
227 DataType data_type, QuantizationInfo quantization_info, DataLayout data_layout)
228 {
229 ARM_COMPUTE_ERROR_ON(data_layout != DataLayout::NHWC); // Dynamic fusion conv2d only supports NHWC layout
230
231 TensorShape weights_shape(kernel_size, kernel_size, input_shape.z(), num_kernels);
232 const TensorShape bias_shape(num_kernels);
233 const PadStrideInfo info(stride_x, stride_y, pad_x, pad_y, DimensionRoundingType::FLOOR);
234 const DataType bias_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::S32 : data_type;
235
236 const Conv2dAttributes conv2d_attr = convert_pad_stride_info_to_conv_attr(info, { 1U, 1U } /* dilation */);
237
238 TensorInfo input_info = TensorInfo(input_shape, 1, data_type);
239 TensorInfo weights_info = TensorInfo(weights_shape, 1, data_type);
240
241 const TensorShape output_shape = misc::shape_calculator::compute_deep_convolution_shape(input_info, weights_info, info);
242
243 _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, conv2d_attr, data_type, bias_data_type, quantization_info, data_layout);
244 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, data_type, bias_data_type, quantization_info);
245 }
246
247protected:
248 TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, const TensorShape &bias_shape, TensorShape output_shape, const Conv2dAttributes &conv2d_attr,
249 DataType data_type, DataType bias_data_type, QuantizationInfo quantization_info, const DataLayout &data_layout)
250 {
251 ARM_COMPUTE_ERROR_ON(data_layout != DataLayout::NHWC);
252 ARM_COMPUTE_UNUSED(quantization_info);
253 // Dataset shapes are in NCHW layout
254 permute(input_shape, PermutationVector(2U, 0U, 1U));
255 permute(weights_shape, PermutationVector(2U, 0U, 1U));
256 permute(output_shape, PermutationVector(2U, 0U, 1U));
257
258 auto cl_compile_ctx = CLKernelLibrary::get().get_compile_context();
259 auto gpu_ctx = GpuWorkloadContext{ &cl_compile_ctx };
260 GpuWorkloadSketch sketch{ &gpu_ctx };
261
262 // Create sketch tensors
263 auto input_info = sketch.create_tensor_info(TensorInfo(input_shape, 1, data_type, data_layout));
264 auto weight_info = sketch.create_tensor_info(TensorInfo(weights_shape, 1, data_type, data_layout));
265 auto bias_info = sketch.create_tensor_info(TensorInfo(bias_shape, 1, bias_data_type, data_layout));
266 auto dst_info = sketch.create_tensor_info();
267
268 ITensorInfo *ans_info = FunctionType::create_op(sketch, &input_info, &weight_info, &bias_info, conv2d_attr);
269 GpuOutput::create_op(sketch, ans_info, &dst_info);
270
271 // Configure runtime
272 ClWorkloadRuntime runtime;
273 runtime.configure(sketch);
274
275 for(auto &data : runtime.get_auxiliary_tensors())
276 {
Ramy Elgammal002e6532023-01-11 18:48:04 +0000277 CLTensor *tensor = std::get<0>(data);
278 TensorInfo info = std::get<1>(data);
279 AuxMemoryInfo aux_mem_req = std::get<2>(data);
280 tensor->allocator()->init(info, aux_mem_req.alignment);
281 tensor->allocator()->allocate(); // Use ACL allocated memory
SiCong Li5a63d1e2023-01-06 16:28:57 +0000282 }
283 // Construct user tensors
284 TensorType t_input{};
285 TensorType t_weight{};
286 TensorType t_bias{};
287 TensorType t_dst{};
288
289 // Initialize user tensors
290 t_input.allocator()->init(input_info);
291 t_weight.allocator()->init(weight_info);
292 t_bias.allocator()->init(bias_info);
293 t_dst.allocator()->init(dst_info);
294
295 ARM_COMPUTE_ASSERT(t_input.info()->is_resizable());
296 ARM_COMPUTE_ASSERT(t_weight.info()->is_resizable());
297 ARM_COMPUTE_ASSERT(t_bias.info()->is_resizable());
298 ARM_COMPUTE_ASSERT(t_dst.info()->is_resizable());
299
300 // Allocate and fill user tensors
301 t_input.allocator()->allocate();
302 t_weight.allocator()->allocate();
303 t_bias.allocator()->allocate();
304 t_dst.allocator()->allocate();
305
306 ARM_COMPUTE_ASSERT(!t_input.info()->is_resizable());
307 ARM_COMPUTE_ASSERT(!t_weight.info()->is_resizable());
308 ARM_COMPUTE_ASSERT(!t_bias.info()->is_resizable());
309 ARM_COMPUTE_ASSERT(!t_dst.info()->is_resizable());
310
311 fill(AccessorType(t_input), 0);
312 fill(AccessorType(t_weight), 1);
313 fill(AccessorType(t_bias), 2);
314
315 // Run runtime
316 runtime.run({ &t_input, &t_weight, &t_bias, &t_dst });
317 return t_dst;
318 }
319
320 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info,
321 DataType data_type, DataType bias_data_type, QuantizationInfo quantization_info)
322 {
323 // Create reference
324 SimpleTensor<T> src{ input_shape, data_type, 1, quantization_info };
325 SimpleTensor<T> weights{ weights_shape, data_type, 1, quantization_info };
326 SimpleTensor<TBias> bias{ bias_shape, bias_data_type, 1, quantization_info };
327
328 // Fill reference
329 fill(src, 0);
330 fill(weights, 1);
331 fill(bias, 2);
332
333 SimpleTensor<T> dst = reference::convolution_layer<T>(src, weights, bias, output_shape, info);
334 return dst;
335 }
336 TensorType _target{};
337 SimpleTensor<T> _reference{};
338};
339
340template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
341class DynamicFusionDirectConv2dValidationFixture : public DynamicFusionDirectConv2dValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
342{
343public:
344 template <typename...>
345 void setup(TensorShape input_shape, int stride_x, int stride_y, int pad_x, int pad_y, unsigned int kernel_size, unsigned int num_kernels, DataType data_type,
346 DataLayout data_layout)
347 {
348 DynamicFusionDirectConv2dValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, stride_x, stride_y, pad_x, pad_y, kernel_size, num_kernels, data_type,
349 QuantizationInfo(),
350 data_layout);
351 }
352};
353
Ramy Elgammal73f19af2022-10-23 11:44:49 +0100354} // namespace validation
355} // namespace test
356} // namespace arm_compute
Ramy Elgammal404462a2022-11-08 02:14:46 +0000357#endif /* TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_GPU_CL_DIRECTCONV2DFIXTURE */