blob: 59352a8fb7c4e41b240d115bc049a5ef4dc25080 [file] [log] [blame]
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +01001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2018-2021, 2023 Arm Limited.
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLNormalizePlanarYUVLayerKernel.h"
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010025
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010028#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Utils.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000032#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
33#include "arm_compute/core/utils/StringUtils.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010034
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "src/core/AccessWindowStatic.h"
36#include "src/core/CL/CLValidate.h"
37#include "src/core/helpers/AutoConfiguration.h"
38#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000039#include "support/StringSupport.h"
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010040
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000041namespace arm_compute
42{
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010043namespace
44{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010045Status
46validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *mean, const ITensorInfo *std)
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010047{
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000048 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010049 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010050 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
51 DataType::F16, DataType::F32);
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010052
53 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, mean, std);
54 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, std);
55 ARM_COMPUTE_RETURN_ERROR_ON_MSG(mean->num_dimensions() > 1, "mean and std must be vectors");
56
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010057 const unsigned int channel_idx =
58 get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010059 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(channel_idx) != mean->dimension(0));
60
61 // Checks performed when output is configured
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010062 if (output->total_size() != 0)
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010063 {
64 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
65 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000066 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010067 }
68
69 return Status{};
70}
71
Sheri Zhang4f1650f2021-04-15 12:58:20 +010072std::pair<Status, Window> validate_and_configure_window_nchw(ITensorInfo *input, ITensorInfo *output)
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010073{
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010074 const unsigned int num_elems_processed_per_iteration = 16 / input->element_size();
75
76 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
77
78 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
79 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
80
81 bool window_changed = update_window_and_padding(win, input_access, output_access);
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010082
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010083 Status err =
84 (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010085 return std::make_pair(err, win);
86}
87} // namespace
88
89CLNormalizePlanarYUVLayerKernel::CLNormalizePlanarYUVLayerKernel()
90 : _input(nullptr), _output(nullptr), _mean(nullptr), _std(nullptr)
91{
Giorgio Arena4a95bba2021-06-28 11:00:27 +010092 _type = CLKernelType::ELEMENTWISE;
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010093}
94
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010095void CLNormalizePlanarYUVLayerKernel::configure(const ICLTensor *input,
96 ICLTensor *output,
97 const ICLTensor *mean,
98 const ICLTensor *std)
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010099{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100100 configure(CLKernelLibrary::get().get_compile_context(), input, output, mean, std);
101}
102
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103void CLNormalizePlanarYUVLayerKernel::configure(const CLCompileContext &compile_context,
104 const ICLTensor *input,
105 ICLTensor *output,
106 const ICLTensor *mean,
107 const ICLTensor *std)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100108{
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100109 // Perform validation step
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000110 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, mean, std);
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100111 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), mean->info(), std->info()));
112
Sheri Zhang4f1650f2021-04-15 12:58:20 +0100113 // Output tensor auto initialization if not yet initialized
114 auto_init_if_empty(*output->info(), *input->info()->clone());
115
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100116 auto padding_info = get_padding_info({input, output});
Sheri Zhang4f1650f2021-04-15 12:58:20 +0100117
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100118 _input = input;
119 _output = output;
120 _mean = mean;
121 _std = std;
122
Sheri Zhang4f1650f2021-04-15 12:58:20 +0100123 const DataLayout data_layout = input->info()->data_layout();
124
125 // Get number of elements to process per iterations
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100126 const unsigned int num_elems_processed_per_iteration =
127 (data_layout == DataLayout::NHWC)
128 ? adjust_vec_size(16 / input->info()->element_size(), input->info()->dimension(0))
129 : (16 / input->info()->element_size());
Sheri Zhang4f1650f2021-04-15 12:58:20 +0100130 const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
131 const DataType dt = input->info()->data_type();
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100132
133 // Set build options
134 CLBuildOptions build_opts;
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +0100135 build_opts.add_option(("-DDATA_TYPE=" + get_cl_type_from_data_type(dt)));
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100136 build_opts.add_option(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100137 build_opts.add_option(("-DVEC_SIZE_LEFTOVER=" +
138 support::cpp11::to_string(input->info()->dimension(0) % num_elems_processed_per_iteration)));
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100139 build_opts.add_option(("-DNUM_CHANNELS=" + support::cpp11::to_string(input->info()->dimension(channel_idx))));
140
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +0100141 std::string kernel_name = "normalize_planar_yuv_layer_";
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100142 if (is_data_type_quantized(dt))
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +0100143 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100144 const UniformQuantizationInfo qinfo = input->info()->quantization_info().uniform();
145 build_opts.add_option(("-DOFFSET=" + support::cpp11::to_string(qinfo.offset)));
146 build_opts.add_option(("-DSCALE=" + support::cpp11::to_string(qinfo.scale)));
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +0100147 kernel_name += "q8_";
148 }
149
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100150 // Create kernel
Sheri Zhang4f1650f2021-04-15 12:58:20 +0100151 kernel_name += lower_string(string_from_data_layout(data_layout));
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100152 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100153
154 // Configure kernel window
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100155 if (data_layout == DataLayout::NHWC)
Sheri Zhang4f1650f2021-04-15 12:58:20 +0100156 {
157 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
158 ICLKernel::configure_internal(win);
159 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
160 }
161 else
162 {
163 auto win_config = validate_and_configure_window_nchw(input->info(), output->info());
164 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
165 ICLKernel::configure_internal(win_config.second);
166 }
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100167
168 // Set config_id for enabling LWS tuning
169 _config_id = "normalize_planar_yuv_layer_";
170 _config_id += lower_string(string_from_data_layout(input->info()->data_layout()));
171 _config_id += "_";
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +0100172 _config_id += lower_string(string_from_data_type(dt));
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100173 _config_id += "_";
174 _config_id += support::cpp11::to_string(input->info()->dimension(0));
175 _config_id += "_";
176 _config_id += support::cpp11::to_string(input->info()->dimension(1));
177 _config_id += "_";
178 _config_id += support::cpp11::to_string(input->info()->dimension(2));
179}
180
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100181Status CLNormalizePlanarYUVLayerKernel::validate(const ITensorInfo *input,
182 const ITensorInfo *output,
183 const ITensorInfo *mean,
184 const ITensorInfo *std)
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100185{
186 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mean, std));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100187 if (input->data_layout() == DataLayout::NCHW)
Sheri Zhang4f1650f2021-04-15 12:58:20 +0100188 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100189 ARM_COMPUTE_RETURN_ON_ERROR(
190 validate_and_configure_window_nchw(input->clone().get(), output->clone().get()).first);
Sheri Zhang4f1650f2021-04-15 12:58:20 +0100191 }
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100192 return Status{};
193}
194
195void CLNormalizePlanarYUVLayerKernel::run(const Window &window, cl::CommandQueue &queue)
196{
197 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
198 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
199
200 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
201 Window slice = collapsed.first_slice_window_3D();
202
203 Window slice_in = collapsed.first_slice_window_1D();
204 slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
205
206 unsigned int idx = 2 * num_arguments_per_3D_tensor();
207 add_1D_tensor_argument(idx, _mean, slice_in);
208 add_1D_tensor_argument(idx, _std, slice_in);
209
210 do
211 {
212 idx = 0;
213 add_3D_tensor_argument(idx, _input, slice);
214 add_3D_tensor_argument(idx, _output, slice);
215 enqueue(queue, *this, slice, lws_hint());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100216 } while (collapsed.slide_window_slice_3D(slice));
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100217}
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000218} // namespace arm_compute