blob: e437c440d09c34073d47f18e8abe74134421e77a [file] [log] [blame]
Ramy Elgammal73f19af2022-10-23 11:44:49 +01001/*
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 */
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
Ramy Elgammal73f19af2022-10-23 11:44:49 +010031#include "arm_compute/dynamic_fusion/runtime/gpu/cl/ClWorkloadRuntime.h"
32#include "arm_compute/dynamic_fusion/sketch/OperatorAttributes.h"
33#include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadSketch.h"
34#include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuConv2d.h"
35
Ramy Elgammal73f19af2022-10-23 11:44:49 +010036#include "tests/CL/CLAccessor.h"
Ramy Elgammal73f19af2022-10-23 11:44:49 +010037#include "tests/framework/Fixture.h"
38#include "tests/framework/Macros.h"
Ramy Elgammal73f19af2022-10-23 11:44:49 +010039#include "tests/validation/Validation.h"
40#include "tests/validation/reference/ConvolutionLayer.h"
Ramy Elgammal73f19af2022-10-23 11:44:49 +010041#include "tests/validation/reference/Permute.h"
42
43using namespace arm_compute::experimental::dynamic_fusion;
44
45namespace arm_compute
46{
47namespace test
48{
49namespace validation
50{
51template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
52class DynamicFusionGpuConv2dValidationGenericFixture : public framework::Fixture
53{
54public:
55 using TBias = typename std::conditional < std::is_same<typename std::decay<T>::type, uint8_t>::value
56 || std::is_same<typename std::decay<T>::type, int8_t>::value,
57 int32_t, T >::type; // If T: uint8_t or int8_t then TBias: int32_t, otherwise TBias: T
58
59 template <typename...>
60 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, const PadStrideInfo &info, const Size2D &dilation, DataType data_type,
61 DataLayout data_layout, QuantizationInfo quantization_info, QuantizationInfo weight_quantization_info)
62 {
63 ARM_COMPUTE_ERROR_ON(data_layout != DataLayout::NHWC); // Dynamic fusion conv2d only supports NHWC layout
64 const Conv2dAttributes conv2d_attr = convert_pad_stride_info_to_conv_attr(info, dilation);
65 _data_type = data_type;
66 _data_layout = data_layout;
67 _is_quantized = is_data_type_quantized_asymmetric(data_type);
68 _quantization_info = quantization_info;
69 _weight_quantization_info = weight_quantization_info;
70 _bias_data_type = _is_quantized ? DataType::S32 : data_type;
71 _target = compute_target(input_shape, weights_shape, bias_shape, conv2d_attr);
72 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, conv2d_attr);
73 }
74
75protected:
76 template <typename U>
77 void fill(U &&tensor, int i)
78 {
79 switch(tensor.data_type())
80 {
81 case DataType::F16:
82 {
83 arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -1.0f, 1.0f };
84 library->fill(tensor, distribution, i);
85 break;
86 }
87 case DataType::F32:
88 {
89 std::uniform_real_distribution<float> distribution(-1.0f, 1.0f);
90 library->fill(tensor, distribution, i);
91 break;
92 }
93 default:
94 library->fill_tensor_uniform(tensor, i);
95 }
96 }
97
98 // Given input is in nchw format
99 TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, const TensorShape &bias_shape, Conv2dAttributes conv2d_attr)
100 {
101 ARM_COMPUTE_ERROR_ON(_data_layout != DataLayout::NHWC);
102 permute(input_shape, PermutationVector(2U, 0U, 1U));
103 permute(weights_shape, PermutationVector(2U, 0U, 1U));
104 CLScheduler::get().default_reinit();
105
106 // Create a new workload sketch
107 auto cl_compile_ctx = CLKernelLibrary::get().get_compile_context();
108 auto gpu_ctx = GpuWorkloadContext{ &cl_compile_ctx };
109 GpuWorkloadSketch sketch{ &gpu_ctx };
110
111 // Create sketch tensors
112 auto input_info = sketch.create_tensor_info(TensorInfo(input_shape, 1, _data_type, _data_layout));
113 auto weight_info = sketch.create_tensor_info(TensorInfo(weights_shape, 1, _data_type, _data_layout));
114 auto bias_info = sketch.create_tensor_info(TensorInfo(bias_shape, 1, _data_type, _data_layout));
115 auto dst_info = sketch.create_tensor_info();
116 FunctionType::create_op(sketch, &input_info, &weight_info, &bias_info, &dst_info, conv2d_attr);
117
118 // Configure runtime
119 ClWorkloadRuntime runtime;
120 runtime.configure(sketch);
121 // (Important) Allocate auxiliary tensor memory if there are any
122 for(auto &data : runtime.get_auxiliary_tensors())
123 {
124 auto tensor = data.first;
125 const auto aux_mem_req = data.second;
126 tensor->allocator()->init(*data.first->info(), aux_mem_req.alignment);
127 tensor->allocator()->allocate(); // Use ACL allocated memory
128 }
129 // Construct user tensors
Ramy Elgammal404462a2022-11-08 02:14:46 +0000130 TensorType t_input{};
131 TensorType t_weight{};
132 TensorType t_bias{};
133 TensorType t_dst{};
Ramy Elgammal73f19af2022-10-23 11:44:49 +0100134
135 // Initialize user tensors
136 t_input.allocator()->init(input_info);
137 t_weight.allocator()->init(weight_info);
138 t_bias.allocator()->init(bias_info);
139 t_dst.allocator()->init(dst_info);
140
141 // Allocate and fill user tensors
142 t_input.allocator()->allocate();
143 t_weight.allocator()->allocate();
144 t_bias.allocator()->allocate();
145 t_dst.allocator()->allocate();
Ramy Elgammal404462a2022-11-08 02:14:46 +0000146
147 fill(AccessorType(t_input), 0);
148 fill(AccessorType(t_weight), 1);
149 fill(AccessorType(t_bias), 2);
Ramy Elgammal73f19af2022-10-23 11:44:49 +0100150
151 // Run runtime
152 runtime.run({ &t_input, &t_weight, &t_bias, &t_dst });
153 return t_dst;
154 }
155
156 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape,
157 const TensorShape &output_shape, Conv2dAttributes conv2d_attr)
158 {
159 // Create reference
160 SimpleTensor<T> src{ input_shape, _data_type, 1, _quantization_info };
161 SimpleTensor<T> weight{ weights_shape, _data_type, 1, _weight_quantization_info };
162 SimpleTensor<TBias> bias{ bias_shape, _data_type, 1, _quantization_info };
163
164 fill(src, 0);
165 fill(weight, 1);
166 fill(bias, 2);
167
168 auto src_nchw = src;
169 auto weights_nchw = weight;
170 auto bias_nchw = bias;
171 auto output_shape_nchw = output_shape;
172
173 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,
174 DimensionRoundingType{});
175 auto dst_nchw = reference::convolution_layer(src_nchw, weights_nchw, bias_nchw, output_shape_nchw, legacy_pad_stride, conv2d_attr.dilation());
176 return dst_nchw;
177 }
178
179 TensorType _target{};
180 SimpleTensor<T> _reference{};
181 DataType _data_type{};
Ramy Elgammal73f19af2022-10-23 11:44:49 +0100182 DataType _bias_data_type{};
Ramy Elgammal73f19af2022-10-23 11:44:49 +0100183 DataLayout _data_layout{};
184 QuantizationInfo _quantization_info{};
185 QuantizationInfo _weight_quantization_info{};
186 bool _is_quantized = false;
Ramy Elgammal73f19af2022-10-23 11:44:49 +0100187};
188
189template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
190class DynamicFusionGpuConv2dValidationFixture : public DynamicFusionGpuConv2dValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
191{
192public:
193 template <typename...>
194 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape output_shape, TensorShape bias_shape,
195 const PadStrideInfo &info, const Size2D &dialation, DataType data_type, DataLayout data_layout, QuantizationInfo quantization_info)
196 {
197 DynamicFusionGpuConv2dValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, output_shape, bias_shape, info, dialation,
Ramy Elgammal404462a2022-11-08 02:14:46 +0000198 data_type, data_layout, quantization_info, quantization_info);
Ramy Elgammal73f19af2022-10-23 11:44:49 +0100199 }
200};
201} // namespace validation
202} // namespace test
203} // namespace arm_compute
Ramy Elgammal404462a2022-11-08 02:14:46 +0000204#endif /* TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_GPU_CL_DIRECTCONV2DFIXTURE */