Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, 2023 Arm Limited. |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 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 | */ |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 24 | #include "src/core/CL/kernels/CLNormalizePlanarYUVLayerKernel.h" |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 25 | |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 28 | #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 Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/utils/helpers/AdjustVecSize.h" |
| 33 | #include "arm_compute/core/utils/StringUtils.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 34 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 35 | #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 Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 39 | #include "support/StringSupport.h" |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 40 | |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 41 | namespace arm_compute |
| 42 | { |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 43 | namespace |
| 44 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 45 | Status |
| 46 | validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *mean, const ITensorInfo *std) |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 47 | { |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, |
| 51 | DataType::F16, DataType::F32); |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 52 | |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 57 | const unsigned int channel_idx = |
| 58 | get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL); |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 59 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(channel_idx) != mean->dimension(0)); |
| 60 | |
| 61 | // Checks performed when output is configured |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 62 | if (output->total_size() != 0) |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 63 | { |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 66 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | return Status{}; |
| 70 | } |
| 71 | |
Sheri Zhang | 4f1650f | 2021-04-15 12:58:20 +0100 | [diff] [blame] | 72 | std::pair<Status, Window> validate_and_configure_window_nchw(ITensorInfo *input, ITensorInfo *output) |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 73 | { |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 74 | 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 Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 82 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 83 | Status err = |
| 84 | (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 85 | return std::make_pair(err, win); |
| 86 | } |
| 87 | } // namespace |
| 88 | |
| 89 | CLNormalizePlanarYUVLayerKernel::CLNormalizePlanarYUVLayerKernel() |
| 90 | : _input(nullptr), _output(nullptr), _mean(nullptr), _std(nullptr) |
| 91 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 92 | _type = CLKernelType::ELEMENTWISE; |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 95 | void CLNormalizePlanarYUVLayerKernel::configure(const ICLTensor *input, |
| 96 | ICLTensor *output, |
| 97 | const ICLTensor *mean, |
| 98 | const ICLTensor *std) |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 99 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 100 | configure(CLKernelLibrary::get().get_compile_context(), input, output, mean, std); |
| 101 | } |
| 102 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 103 | void CLNormalizePlanarYUVLayerKernel::configure(const CLCompileContext &compile_context, |
| 104 | const ICLTensor *input, |
| 105 | ICLTensor *output, |
| 106 | const ICLTensor *mean, |
| 107 | const ICLTensor *std) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 108 | { |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 109 | // Perform validation step |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 110 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, mean, std); |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 111 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), mean->info(), std->info())); |
| 112 | |
Sheri Zhang | 4f1650f | 2021-04-15 12:58:20 +0100 | [diff] [blame] | 113 | // Output tensor auto initialization if not yet initialized |
| 114 | auto_init_if_empty(*output->info(), *input->info()->clone()); |
| 115 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 116 | auto padding_info = get_padding_info({input, output}); |
Sheri Zhang | 4f1650f | 2021-04-15 12:58:20 +0100 | [diff] [blame] | 117 | |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 118 | _input = input; |
| 119 | _output = output; |
| 120 | _mean = mean; |
| 121 | _std = std; |
| 122 | |
Sheri Zhang | 4f1650f | 2021-04-15 12:58:20 +0100 | [diff] [blame] | 123 | const DataLayout data_layout = input->info()->data_layout(); |
| 124 | |
| 125 | // Get number of elements to process per iterations |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 126 | 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 Zhang | 4f1650f | 2021-04-15 12:58:20 +0100 | [diff] [blame] | 130 | const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 131 | const DataType dt = input->info()->data_type(); |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 132 | |
| 133 | // Set build options |
| 134 | CLBuildOptions build_opts; |
Michele Di Giorgio | d63dfa2 | 2018-09-12 10:18:54 +0100 | [diff] [blame] | 135 | build_opts.add_option(("-DDATA_TYPE=" + get_cl_type_from_data_type(dt))); |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 136 | build_opts.add_option(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration))); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 137 | build_opts.add_option(("-DVEC_SIZE_LEFTOVER=" + |
| 138 | support::cpp11::to_string(input->info()->dimension(0) % num_elems_processed_per_iteration))); |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 139 | build_opts.add_option(("-DNUM_CHANNELS=" + support::cpp11::to_string(input->info()->dimension(channel_idx)))); |
| 140 | |
Michele Di Giorgio | d63dfa2 | 2018-09-12 10:18:54 +0100 | [diff] [blame] | 141 | std::string kernel_name = "normalize_planar_yuv_layer_"; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 142 | if (is_data_type_quantized(dt)) |
Michele Di Giorgio | d63dfa2 | 2018-09-12 10:18:54 +0100 | [diff] [blame] | 143 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 144 | 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 Giorgio | d63dfa2 | 2018-09-12 10:18:54 +0100 | [diff] [blame] | 147 | kernel_name += "q8_"; |
| 148 | } |
| 149 | |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 150 | // Create kernel |
Sheri Zhang | 4f1650f | 2021-04-15 12:58:20 +0100 | [diff] [blame] | 151 | kernel_name += lower_string(string_from_data_layout(data_layout)); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 152 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 153 | |
| 154 | // Configure kernel window |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 155 | if (data_layout == DataLayout::NHWC) |
Sheri Zhang | 4f1650f | 2021-04-15 12:58:20 +0100 | [diff] [blame] | 156 | { |
| 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 Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 167 | |
| 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 Giorgio | d63dfa2 | 2018-09-12 10:18:54 +0100 | [diff] [blame] | 172 | _config_id += lower_string(string_from_data_type(dt)); |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 173 | _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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 181 | Status CLNormalizePlanarYUVLayerKernel::validate(const ITensorInfo *input, |
| 182 | const ITensorInfo *output, |
| 183 | const ITensorInfo *mean, |
| 184 | const ITensorInfo *std) |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 185 | { |
| 186 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mean, std)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 187 | if (input->data_layout() == DataLayout::NCHW) |
Sheri Zhang | 4f1650f | 2021-04-15 12:58:20 +0100 | [diff] [blame] | 188 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 189 | ARM_COMPUTE_RETURN_ON_ERROR( |
| 190 | validate_and_configure_window_nchw(input->clone().get(), output->clone().get()).first); |
Sheri Zhang | 4f1650f | 2021-04-15 12:58:20 +0100 | [diff] [blame] | 191 | } |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 192 | return Status{}; |
| 193 | } |
| 194 | |
| 195 | void 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 216 | } while (collapsed.slide_window_slice_3D(slice)); |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 217 | } |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 218 | } // namespace arm_compute |