blob: 3510b6970cb6555871d8b4cb4fd27679534fb4a4 [file] [log] [blame]
Gian Marco Iodice76335eb2022-11-17 11:03:39 +00001/*
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +00002 * Copyright (c) 2022-2023 Arm Limited.
Gian Marco Iodice76335eb2022-11-17 11:03:39 +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 */
24#include "src/gpu/cl/kernels/ClIndirectConv2dKernel.h"
25
26#include "arm_compute/core/CL/CLKernelLibrary.h"
27#include "arm_compute/core/CL/ICLTensor.h"
28#include "arm_compute/core/KernelDescriptors.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010029#include "arm_compute/core/utils/ActivationFunctionUtils.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000030#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
Gian Marco Iodice76335eb2022-11-17 11:03:39 +000031#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000032#include "arm_compute/core/utils/StringUtils.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010033
Gian Marco Iodice76335eb2022-11-17 11:03:39 +000034#include "src/core/CL/CLUtils.h"
35#include "src/core/CL/CLValidate.h"
36#include "src/core/helpers/AutoConfiguration.h"
37#include "src/core/helpers/WindowHelpers.h"
38#include "src/gpu/cl/kernels/gemm/ClGemmHelpers.h"
39#include "support/Cast.h"
40#include "support/StringSupport.h"
41
42namespace arm_compute
43{
44namespace opencl
45{
46namespace kernels
47{
48namespace
49{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010050Status validate_arguments(const ITensorInfo *src,
51 const ITensorInfo *weights,
52 const ITensorInfo *biases,
53 const ITensorInfo *indirect_buffer,
54 const ITensorInfo *dst,
55 const PadStrideInfo &conv_info,
56 const ActivationLayerInfo &act_info,
57 const DirectConvComputeKernelInfo &desc)
Gian Marco Iodice76335eb2022-11-17 11:03:39 +000058{
59 ARM_COMPUTE_UNUSED(act_info);
60 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
61 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32);
62 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indirect_buffer, 1, DataType::S32);
63 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(src, DataLayout::NHWC);
64 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, weights);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010065 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(
66 indirect_buffer->tensor_shape(),
67 misc::shape_calculator::compute_indirect_buffer_shape(src->tensor_shape(), src->data_layout(),
68 weights->tensor_shape(), conv_info, desc));
Gian Marco Iodice76335eb2022-11-17 11:03:39 +000069
70 constexpr int channel_idx = 0;
71 constexpr int batch_idx = 3;
72
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010073 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(channel_idx) != src->dimension(channel_idx),
74 "Weights feature map dimension should match the respective src's one");
Gian Marco Iodice76335eb2022-11-17 11:03:39 +000075 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->num_dimensions() > 4, "Weights can be at most 4 dimensional");
76
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010077 ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.m0 <= 0 || desc.m0 > 8,
78 "M0 can only be greater than 0 and less than or equal to 8");
Gian Marco Iodice76335eb2022-11-17 11:03:39 +000079
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010080 ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.n0 != 1 && desc.n0 != 2 && desc.n0 != 3 && desc.n0 != 4 && desc.n0 != 8 &&
81 desc.n0 != 16,
Gian Marco Iodice76335eb2022-11-17 11:03:39 +000082 "N0 can only be: 1, 2, 3, 4, 8, and 16");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010083 ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.k0 != 1 && desc.k0 != 2 && desc.k0 != 3 && desc.k0 != 4 && desc.k0 != 8 &&
84 desc.k0 != 16,
Gian Marco Iodice76335eb2022-11-17 11:03:39 +000085 "K0 can only be: 1, 2, 3, 4, 8, and 16");
86
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010087 if (desc.export_weights_to_cl_image)
Gian Marco Iodice76335eb2022-11-17 11:03:39 +000088 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010089 ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.k0 != 4 && desc.k0 != 8 && desc.k0 != 16, "K0 can only be: 4, 8, and 16");
Gian Marco Iodice76335eb2022-11-17 11:03:39 +000090 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!export_to_cl_image(weights),
91 "Export to CLImage is not supported for this weight configuration");
92 }
93
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010094 if (biases != nullptr)
Gian Marco Iodice76335eb2022-11-17 11:03:39 +000095 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010096 if (is_data_type_quantized_asymmetric(src->data_type()))
Gian Marco Iodice76335eb2022-11-17 11:03:39 +000097 {
98 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
99 }
100 else
101 {
102 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
103 }
104 ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->dimension(channel_idx) != weights->dimension(batch_idx),
105 "Biases size and number of dst feature maps should match");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100106 ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->num_dimensions() > 1, "Biases should be one dimensional");
Gian Marco Iodice76335eb2022-11-17 11:03:39 +0000107 }
108
109 // Checks performed when dst is configured
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100110 if (dst->total_size() != 0)
Gian Marco Iodice76335eb2022-11-17 11:03:39 +0000111 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100112 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(
113 dst->tensor_shape(), misc::shape_calculator::compute_deep_convolution_shape(*src, *weights, conv_info));
Gian Marco Iodice76335eb2022-11-17 11:03:39 +0000114 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
115 }
116
117 return Status{};
118}
119} // namespace
120
121ClIndirectConv2dKernel::ClIndirectConv2dKernel()
122{
123 _type = CLKernelType::DIRECT;
124}
125
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100126void ClIndirectConv2dKernel::configure(const CLCompileContext &compile_context,
127 ITensorInfo *src,
128 ITensorInfo *weights,
129 ITensorInfo *biases,
130 ITensorInfo *indirect_buffer,
131 ITensorInfo *dst,
132 const PadStrideInfo &conv_info,
133 const ActivationLayerInfo &act_info,
134 const DirectConvComputeKernelInfo &desc)
Gian Marco Iodice76335eb2022-11-17 11:03:39 +0000135{
136 ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, indirect_buffer, dst);
137
138 // Perform validation
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100139 ARM_COMPUTE_ERROR_THROW_ON(
140 validate_arguments(src, weights, biases, indirect_buffer, dst, conv_info, act_info, desc));
Gian Marco Iodice76335eb2022-11-17 11:03:39 +0000141
142 constexpr unsigned int channel_idx = 0;
143 constexpr unsigned int width_idx = 1;
144 constexpr unsigned int height_idx = 2;
145 const unsigned int kernel_width = weights->dimension(width_idx);
146 const unsigned int kernel_height = weights->dimension(height_idx);
147 const DataType data_type = src->data_type();
148
149 const GPUTarget gpu_target = get_target();
150
151 // Get dst shape
152 TensorShape output_shape = misc::shape_calculator::compute_deep_convolution_shape(*src, *weights, conv_info);
153
154 // Output auto inizialitation if not yet initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100155 auto_init_if_empty(*dst, output_shape, 1, src->data_type(), src->quantization_info());
Gian Marco Iodice76335eb2022-11-17 11:03:39 +0000156
157 // Configure kernel window
158 Window win;
159 output_shape.collapse(2U, 1U);
160 const unsigned int n0 = adjust_vec_size(desc.n0, output_shape[0]);
161 const unsigned int m0 = adjust_vec_size(desc.m0, output_shape[1]);
162 const unsigned int k0 = adjust_vec_size(desc.k0, src->dimension(channel_idx));
163
164 const unsigned int partial_store_n0 = dst->dimension(channel_idx) % n0;
165
166 // Create window and update padding
167 win = calculate_max_window(output_shape, Steps(n0, m0));
168
169 ICLKernel::configure_internal(win);
170
171 std::stringstream kernel_name;
172 CLBuildOptions build_options;
173
174 kernel_name << "indirect_convolution_nhwc";
175
176 _export_to_cl_image = desc.export_weights_to_cl_image;
177
178 // Update the padding for the weights tensor if we can export to cl_image
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100179 if (_export_to_cl_image)
Gian Marco Iodice76335eb2022-11-17 11:03:39 +0000180 {
181 gemm::update_padding_for_cl_image(weights);
182 }
183
184 // Add padding to indirect buffer to avoid out-of-bound reads
185 // When M0 is 5, 6, and 7, we use vload8 to fetch the data from the buffer
186 const unsigned int load_indirect_buf_size = m0 > 4 ? 8 : m0;
187 const unsigned int indirect_buf_width = indirect_buffer->tensor_shape()[0];
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100188 const unsigned int round_up_width =
189 ((indirect_buf_width + load_indirect_buf_size - 1) / load_indirect_buf_size) * load_indirect_buf_size;
190 const unsigned int padding = round_up_width - indirect_buf_width;
Gian Marco Iodice76335eb2022-11-17 11:03:39 +0000191 indirect_buffer->extend_padding(PaddingSize(0, indirect_buffer->padding().right + padding, 0, 0));
192
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100193 if (biases != nullptr)
Gian Marco Iodice76335eb2022-11-17 11:03:39 +0000194 {
195 build_options.add_option(std::string("-DHAS_BIAS"));
196 build_options.add_option(std::string("-DBIA_DATA_TYPE=" + get_cl_type_from_data_type(biases->data_type())));
197 }
198
199 // Conditions of -cl-fast-relaxed-math causing accuracy issues can be traced from COMPMID-5324
200 const auto act_function = act_info.activation();
201
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100202 if ((gpu_target != GPUTarget::G71 && (gpu_target & GPUTarget::GPU_ARCH_MASK) == GPUTarget::BIFROST) &&
203 (act_function == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU ||
204 act_function == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU) &&
205 (data_type == DataType::F32 || data_type == DataType::F16))
Gian Marco Iodice76335eb2022-11-17 11:03:39 +0000206 {
207 // -cl-fast-relaxed-math also sets -cl-finite-math-only and -cl-unsafe-math-optimizations
208 // to disable -cl-finite-math-only, we only include -cl-unsafe-math-optimizations
209 build_options.add_option("-cl-unsafe-math-optimizations");
210 }
211 else
212 {
213 build_options.add_option("-cl-fast-relaxed-math");
214 }
215
216 build_options.add_option("-DSRC_TENSOR_TYPE=BUFFER");
217 build_options.add_option("-DSRC_DATA_TYPE=" + get_cl_type_from_data_type(data_type));
218 build_options.add_option("-DSRC_CHANNELS=" + support::cpp11::to_string(src->dimension(channel_idx)));
219 build_options.add_option("-DOFF_TENSOR_TYPE=BUFFER");
220 build_options.add_option("-DDST_WIDTH=" + support::cpp11::to_string(dst->dimension(width_idx)));
221 build_options.add_option("-DDST_HEIGHT=" + support::cpp11::to_string(dst->dimension(height_idx)));
222 build_options.add_option("-DDST_TENSOR_TYPE=BUFFER");
223 build_options.add_option("-DDST_DATA_TYPE=" + get_cl_type_from_data_type(data_type));
224 build_options.add_option_if_else(_export_to_cl_image, "-DWEI_TENSOR_TYPE=IMAGE", "-DWEI_TENSOR_TYPE=BUFFER");
225 build_options.add_option("-DWEI_WIDTH=" + support::cpp11::to_string(kernel_width));
226 build_options.add_option("-DWEI_HEIGHT=" + support::cpp11::to_string(kernel_height));
227 build_options.add_option("-DWEI_DATA_TYPE=" + get_cl_type_from_data_type(data_type));
228 build_options.add_option("-DN0=" + support::cpp11::to_string(n0));
229 build_options.add_option("-DM0=" + support::cpp11::to_string(m0));
230 build_options.add_option("-DK0=" + support::cpp11::to_string(k0));
231 build_options.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_store_n0));
232 build_options.add_option("-DIND_BUFF_VEC_SIZE=" + support::cpp11::to_string(load_indirect_buf_size));
233 build_options.add_option_if((src->dimension(channel_idx) % k0) != 0, "-DLEFTOVER_LOOP");
234 build_options.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_function)));
235 build_options.add_option_if(act_info.enabled(), "-DA_VAL=" + float_to_string_with_full_precision(act_info.a()));
236 build_options.add_option_if(act_info.enabled(), "-DB_VAL=" + float_to_string_with_full_precision(act_info.b()));
237
238 // A macro guard to compile ONLY the kernel of interest
239 build_options.add_option("-D" + upper_string(kernel_name.str()));
240
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100241 if (compile_context.get_ddk_version() >= 30)
Gian Marco Iodice76335eb2022-11-17 11:03:39 +0000242 {
243 build_options.add_option("-fregister-allocation=64");
244 }
245
246 _kernel = create_kernel(compile_context, kernel_name.str(), build_options.options());
247
248 // Set config_id for enabling LWS tuning
249 _config_id = kernel_name.str();
250 _config_id += "_";
251 _config_id += lower_string(string_from_data_type(data_type));
252 _config_id += "_";
253 _config_id += support::cpp11::to_string(kernel_width);
254 _config_id += "_";
255 _config_id += support::cpp11::to_string(kernel_height);
256 _config_id += "_";
257 _config_id += support::cpp11::to_string(src->dimension(width_idx));
258 _config_id += "_";
259 _config_id += support::cpp11::to_string(src->dimension(height_idx));
260 _config_id += "_";
261 _config_id += support::cpp11::to_string(src->dimension(channel_idx));
262 _config_id += "_";
263 _config_id += support::cpp11::to_string(dst->dimension(width_idx));
264 _config_id += "_";
265 _config_id += support::cpp11::to_string(dst->dimension(height_idx));
266 _config_id += "_";
267 _config_id += support::cpp11::to_string(dst->dimension(channel_idx));
268}
269
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100270Status ClIndirectConv2dKernel::validate(const ITensorInfo *src,
271 const ITensorInfo *weights,
272 const ITensorInfo *biases,
273 const ITensorInfo *indirect_buffer,
274 const ITensorInfo *dst,
275 const PadStrideInfo &conv_info,
276 const ActivationLayerInfo &act_info,
277 const DirectConvComputeKernelInfo &desc)
Gian Marco Iodice76335eb2022-11-17 11:03:39 +0000278{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100279 ARM_COMPUTE_RETURN_ON_ERROR(
280 validate_arguments(src, weights, biases, indirect_buffer, dst, conv_info, act_info, desc));
Gian Marco Iodice76335eb2022-11-17 11:03:39 +0000281 return Status{};
282}
283
284void ClIndirectConv2dKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
285{
286 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
287 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
288
289 // Get initial windows
290 Window slice = window.first_slice_window_3D();
291
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100292 const auto src =
293 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
294 const auto weights =
295 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
296 const auto biases =
297 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2));
298 const auto indirect_buffer =
299 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_3));
300 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Gian Marco Iodice76335eb2022-11-17 11:03:39 +0000301
302 cl::Image2D weights_cl_image;
303
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100304 if (_export_to_cl_image)
Gian Marco Iodice76335eb2022-11-17 11:03:39 +0000305 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100306 const size_t image_w = weights->info()->dimension(0) / 4;
307 const size_t image_h =
308 weights->info()->dimension(1) * weights->info()->dimension(2) * weights->info()->dimension(3);
Gian Marco Iodice76335eb2022-11-17 11:03:39 +0000309 const TensorShape shape2d(image_w, image_h);
310 const size_t image_row_pitch = weights->info()->strides_in_bytes()[1];
311
312 // Export cl_buffer to cl_image
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100313 weights_cl_image =
314 create_image2d_from_buffer(CLKernelLibrary::get().context(), weights->cl_buffer(), shape2d,
315 weights->info()->data_type(), image_row_pitch, CLImage2DType::ReadOnly);
Gian Marco Iodice76335eb2022-11-17 11:03:39 +0000316 }
317
318 unsigned int idx = 0;
319 add_4d_tensor_nhwc_argument(idx, src);
320 add_4d_tensor_nhwc_argument(idx, indirect_buffer);
321 add_4d_tensor_nhwc_argument(idx, dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100322 if (_export_to_cl_image)
Gian Marco Iodice76335eb2022-11-17 11:03:39 +0000323 {
324 _kernel.setArg(idx++, weights_cl_image);
325 }
326 add_4d_tensor_nhwc_argument(idx, weights);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100327 if (biases != nullptr)
Gian Marco Iodice76335eb2022-11-17 11:03:39 +0000328 {
329 add_1D_tensor_argument(idx, biases, slice);
330 }
331 enqueue(queue, *this, slice, lws_hint());
332}
333} // namespace kernels
334} // namespace opencl
335} // namespace arm_compute