blob: 407ce6626b4dc4591260a79a9cdc0c50a35a9880 [file] [log] [blame]
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 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 */
24#include "arm_compute/core/CL/kernels/CLNormalizePlanarYUVLayerKernel.h"
25
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"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/AccessWindowStatic.h"
33#include "src/core/CL/CLValidate.h"
34#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010036
Matthew Bentham758b5ba2020-03-05 23:37:48 +000037#include "support/StringSupport.h"
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010038
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000039namespace arm_compute
40{
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010041namespace
42{
43Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *mean, const ITensorInfo *std)
44{
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000045 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010046 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000047 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 +010048
49 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, mean, std);
50 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, std);
51 ARM_COMPUTE_RETURN_ERROR_ON_MSG(mean->num_dimensions() > 1, "mean and std must be vectors");
52
53 const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
54 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(channel_idx) != mean->dimension(0));
55
56 // Checks performed when output is configured
57 if(output->total_size() != 0)
58 {
59 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
60 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000061 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010062 }
63
64 return Status{};
65}
66
67std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, ITensorInfo *mean, ITensorInfo *std)
68{
69 // Output tensor auto initialization if not yet initialized
70 auto_init_if_empty(*output, *input->clone());
71
72 const unsigned int num_elems_processed_per_iteration = 16 / input->element_size();
73
74 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
75
76 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
77 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
78
79 bool window_changed = update_window_and_padding(win, input_access, output_access);
80 output_access.set_valid_region(win, input->valid_region());
81
82 if(input->data_layout() == DataLayout::NHWC)
83 {
84 AccessWindowHorizontal mean_access(mean, 0, num_elems_processed_per_iteration);
85 AccessWindowHorizontal std_access(std, 0, num_elems_processed_per_iteration);
86 window_changed = window_changed || update_window_and_padding(win, mean_access, std_access);
87 }
88
89 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
90 return std::make_pair(err, win);
91}
92} // namespace
93
94CLNormalizePlanarYUVLayerKernel::CLNormalizePlanarYUVLayerKernel()
95 : _input(nullptr), _output(nullptr), _mean(nullptr), _std(nullptr)
96{
97}
98
99void CLNormalizePlanarYUVLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const ICLTensor *mean, const ICLTensor *std)
100{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100101 configure(CLKernelLibrary::get().get_compile_context(), input, output, mean, std);
102}
103
Manuel Bottini679fc962020-04-21 16:08:53 +0100104void CLNormalizePlanarYUVLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const ICLTensor *mean, const ICLTensor *std)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100105{
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100106 // Perform validation step
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000107 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, mean, std);
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100108 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), mean->info(), std->info()));
109
110 _input = input;
111 _output = output;
112 _mean = mean;
113 _std = std;
114
115 const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
116 const unsigned int channel_idx = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL);
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +0100117 const DataType dt = input->info()->data_type();
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100118
119 // Set build options
120 CLBuildOptions build_opts;
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +0100121 build_opts.add_option(("-DDATA_TYPE=" + get_cl_type_from_data_type(dt)));
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100122 build_opts.add_option(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)));
123 build_opts.add_option(("-DNUM_CHANNELS=" + support::cpp11::to_string(input->info()->dimension(channel_idx))));
124
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +0100125 std::string kernel_name = "normalize_planar_yuv_layer_";
126 if(is_data_type_quantized(dt))
127 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100128 const UniformQuantizationInfo qinfo = input->info()->quantization_info().uniform();
129 build_opts.add_option(("-DOFFSET=" + support::cpp11::to_string(qinfo.offset)));
130 build_opts.add_option(("-DSCALE=" + support::cpp11::to_string(qinfo.scale)));
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +0100131 kernel_name += "q8_";
132 }
133
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100134 // Create kernel
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +0100135 kernel_name += lower_string(string_from_data_layout(input->info()->data_layout()));
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100136 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100137
138 // Configure kernel window
139 auto win_config = validate_and_configure_window(input->info(), output->info(), mean->info(), std->info());
140 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
141 ICLKernel::configure_internal(win_config.second);
142
143 // Set config_id for enabling LWS tuning
144 _config_id = "normalize_planar_yuv_layer_";
145 _config_id += lower_string(string_from_data_layout(input->info()->data_layout()));
146 _config_id += "_";
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +0100147 _config_id += lower_string(string_from_data_type(dt));
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100148 _config_id += "_";
149 _config_id += support::cpp11::to_string(input->info()->dimension(0));
150 _config_id += "_";
151 _config_id += support::cpp11::to_string(input->info()->dimension(1));
152 _config_id += "_";
153 _config_id += support::cpp11::to_string(input->info()->dimension(2));
154}
155
156Status CLNormalizePlanarYUVLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *mean, const ITensorInfo *std)
157{
158 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mean, std));
159 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), mean->clone().get(), std->clone().get()).first);
160
161 return Status{};
162}
163
164void CLNormalizePlanarYUVLayerKernel::run(const Window &window, cl::CommandQueue &queue)
165{
166 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
167 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
168
169 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
170 Window slice = collapsed.first_slice_window_3D();
171
172 Window slice_in = collapsed.first_slice_window_1D();
173 slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
174
175 unsigned int idx = 2 * num_arguments_per_3D_tensor();
176 add_1D_tensor_argument(idx, _mean, slice_in);
177 add_1D_tensor_argument(idx, _std, slice_in);
178
179 do
180 {
181 idx = 0;
182 add_3D_tensor_argument(idx, _input, slice);
183 add_3D_tensor_argument(idx, _output, slice);
184 enqueue(queue, *this, slice, lws_hint());
185 }
186 while(collapsed.slide_window_slice_3D(slice));
187}
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000188} // namespace arm_compute