blob: 6b0400d50e9ddeeaaea1f311df7a4afff1c0fa2d [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"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/AccessWindowStatic.h"
35#include "src/core/CL/CLValidate.h"
36#include "src/core/helpers/AutoConfiguration.h"
37#include "src/core/helpers/WindowHelpers.h"
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010038
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{
45Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *mean, const ITensorInfo *std)
46{
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000047 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010048 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000049 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010050
51 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, mean, std);
52 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, std);
53 ARM_COMPUTE_RETURN_ERROR_ON_MSG(mean->num_dimensions() > 1, "mean and std must be vectors");
54
55 const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
56 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(channel_idx) != mean->dimension(0));
57
58 // Checks performed when output is configured
59 if(output->total_size() != 0)
60 {
61 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
62 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000063 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010064 }
65
66 return Status{};
67}
68
Sheri Zhang4f1650f2021-04-15 12:58:20 +010069std::pair<Status, Window> validate_and_configure_window_nchw(ITensorInfo *input, ITensorInfo *output)
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010070{
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010071 const unsigned int num_elems_processed_per_iteration = 16 / input->element_size();
72
73 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
74
75 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
76 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
77
78 bool window_changed = update_window_and_padding(win, input_access, output_access);
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010079
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010080 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
81 return std::make_pair(err, win);
82}
83} // namespace
84
85CLNormalizePlanarYUVLayerKernel::CLNormalizePlanarYUVLayerKernel()
86 : _input(nullptr), _output(nullptr), _mean(nullptr), _std(nullptr)
87{
Giorgio Arena4a95bba2021-06-28 11:00:27 +010088 _type = CLKernelType::ELEMENTWISE;
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010089}
90
91void CLNormalizePlanarYUVLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const ICLTensor *mean, const ICLTensor *std)
92{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010093 configure(CLKernelLibrary::get().get_compile_context(), input, output, mean, std);
94}
95
Manuel Bottini679fc962020-04-21 16:08:53 +010096void CLNormalizePlanarYUVLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const ICLTensor *mean, const ICLTensor *std)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010097{
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010098 // Perform validation step
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000099 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, mean, std);
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100100 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), mean->info(), std->info()));
101
Sheri Zhang4f1650f2021-04-15 12:58:20 +0100102 // Output tensor auto initialization if not yet initialized
103 auto_init_if_empty(*output->info(), *input->info()->clone());
104
105 auto padding_info = get_padding_info({ input, output });
106
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100107 _input = input;
108 _output = output;
109 _mean = mean;
110 _std = std;
111
Sheri Zhang4f1650f2021-04-15 12:58:20 +0100112 const DataLayout data_layout = input->info()->data_layout();
113
114 // Get number of elements to process per iterations
115 const unsigned int num_elems_processed_per_iteration = (data_layout == DataLayout::NHWC) ? adjust_vec_size(16 / input->info()->element_size(),
116 input->info()->dimension(0)) :
117 (16 / input->info()->element_size());
118 const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
119 const DataType dt = input->info()->data_type();
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100120
121 // Set build options
122 CLBuildOptions build_opts;
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +0100123 build_opts.add_option(("-DDATA_TYPE=" + get_cl_type_from_data_type(dt)));
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100124 build_opts.add_option(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)));
Sheri Zhang4f1650f2021-04-15 12:58:20 +0100125 build_opts.add_option(("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(input->info()->dimension(0) % num_elems_processed_per_iteration)));
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100126 build_opts.add_option(("-DNUM_CHANNELS=" + support::cpp11::to_string(input->info()->dimension(channel_idx))));
127
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +0100128 std::string kernel_name = "normalize_planar_yuv_layer_";
129 if(is_data_type_quantized(dt))
130 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100131 const UniformQuantizationInfo qinfo = input->info()->quantization_info().uniform();
132 build_opts.add_option(("-DOFFSET=" + support::cpp11::to_string(qinfo.offset)));
133 build_opts.add_option(("-DSCALE=" + support::cpp11::to_string(qinfo.scale)));
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +0100134 kernel_name += "q8_";
135 }
136
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100137 // Create kernel
Sheri Zhang4f1650f2021-04-15 12:58:20 +0100138 kernel_name += lower_string(string_from_data_layout(data_layout));
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100139 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100140
141 // Configure kernel window
Sheri Zhang4f1650f2021-04-15 12:58:20 +0100142 if(data_layout == DataLayout::NHWC)
143 {
144 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
145 ICLKernel::configure_internal(win);
146 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
147 }
148 else
149 {
150 auto win_config = validate_and_configure_window_nchw(input->info(), output->info());
151 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
152 ICLKernel::configure_internal(win_config.second);
153 }
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100154
155 // Set config_id for enabling LWS tuning
156 _config_id = "normalize_planar_yuv_layer_";
157 _config_id += lower_string(string_from_data_layout(input->info()->data_layout()));
158 _config_id += "_";
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +0100159 _config_id += lower_string(string_from_data_type(dt));
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100160 _config_id += "_";
161 _config_id += support::cpp11::to_string(input->info()->dimension(0));
162 _config_id += "_";
163 _config_id += support::cpp11::to_string(input->info()->dimension(1));
164 _config_id += "_";
165 _config_id += support::cpp11::to_string(input->info()->dimension(2));
166}
167
168Status CLNormalizePlanarYUVLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *mean, const ITensorInfo *std)
169{
170 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mean, std));
Sheri Zhang4f1650f2021-04-15 12:58:20 +0100171 if(input->data_layout() == DataLayout::NCHW)
172 {
173 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_nchw(input->clone().get(), output->clone().get()).first);
174 }
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100175 return Status{};
176}
177
178void CLNormalizePlanarYUVLayerKernel::run(const Window &window, cl::CommandQueue &queue)
179{
180 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
181 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
182
183 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
184 Window slice = collapsed.first_slice_window_3D();
185
186 Window slice_in = collapsed.first_slice_window_1D();
187 slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
188
189 unsigned int idx = 2 * num_arguments_per_3D_tensor();
190 add_1D_tensor_argument(idx, _mean, slice_in);
191 add_1D_tensor_argument(idx, _std, slice_in);
192
193 do
194 {
195 idx = 0;
196 add_3D_tensor_argument(idx, _input, slice);
197 add_3D_tensor_argument(idx, _output, slice);
198 enqueue(queue, *this, slice, lws_hint());
199 }
200 while(collapsed.slide_window_slice_3D(slice));
201}
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000202} // namespace arm_compute