Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 1 | /* |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 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 | */ |
| 24 | #include "arm_compute/core/CL/kernels/CLNormalizePlanarYUVLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/CL/CLHelpers.h" |
| 28 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 29 | #include "arm_compute/core/CL/CLValidate.h" |
| 30 | #include "arm_compute/core/CL/ICLTensor.h" |
| 31 | #include "arm_compute/core/Helpers.h" |
| 32 | #include "arm_compute/core/TensorInfo.h" |
| 33 | #include "arm_compute/core/Utils.h" |
| 34 | #include "arm_compute/core/Window.h" |
| 35 | |
| 36 | #include "support/ToolchainSupport.h" |
| 37 | |
| 38 | using namespace arm_compute; |
| 39 | |
| 40 | namespace |
| 41 | { |
| 42 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *mean, const ITensorInfo *std) |
| 43 | { |
| 44 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Michele Di Giorgio | d63dfa2 | 2018-09-12 10:18:54 +0100 | [diff] [blame] | 45 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 46 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); |
| 47 | |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, mean, std); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, std); |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(mean->num_dimensions() > 1, "mean and std must be vectors"); |
| 51 | |
| 52 | const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(channel_idx) != mean->dimension(0)); |
| 54 | |
| 55 | // Checks performed when output is configured |
| 56 | if(output->total_size() != 0) |
| 57 | { |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 60 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | return Status{}; |
| 64 | } |
| 65 | |
| 66 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, ITensorInfo *mean, ITensorInfo *std) |
| 67 | { |
| 68 | // Output tensor auto initialization if not yet initialized |
| 69 | auto_init_if_empty(*output, *input->clone()); |
| 70 | |
| 71 | 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); |
| 79 | output_access.set_valid_region(win, input->valid_region()); |
| 80 | |
| 81 | if(input->data_layout() == DataLayout::NHWC) |
| 82 | { |
| 83 | AccessWindowHorizontal mean_access(mean, 0, num_elems_processed_per_iteration); |
| 84 | AccessWindowHorizontal std_access(std, 0, num_elems_processed_per_iteration); |
| 85 | window_changed = window_changed || update_window_and_padding(win, mean_access, std_access); |
| 86 | } |
| 87 | |
| 88 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 89 | return std::make_pair(err, win); |
| 90 | } |
| 91 | } // namespace |
| 92 | |
| 93 | CLNormalizePlanarYUVLayerKernel::CLNormalizePlanarYUVLayerKernel() |
| 94 | : _input(nullptr), _output(nullptr), _mean(nullptr), _std(nullptr) |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | void CLNormalizePlanarYUVLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const ICLTensor *mean, const ICLTensor *std) |
| 99 | { |
| 100 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, mean, std); |
| 101 | |
| 102 | // Output tensor auto initialization if not yet initialized |
| 103 | auto_init_if_empty(*output->info(), *input->info()->clone()); |
| 104 | |
| 105 | // Perform validation step |
| 106 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), mean->info(), std->info())); |
| 107 | |
| 108 | _input = input; |
| 109 | _output = output; |
| 110 | _mean = mean; |
| 111 | _std = std; |
| 112 | |
| 113 | const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size(); |
| 114 | const unsigned int channel_idx = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL); |
Michele Di Giorgio | d63dfa2 | 2018-09-12 10:18:54 +0100 | [diff] [blame] | 115 | const DataType dt = input->info()->data_type(); |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 116 | |
| 117 | // Set build options |
| 118 | CLBuildOptions build_opts; |
Michele Di Giorgio | d63dfa2 | 2018-09-12 10:18:54 +0100 | [diff] [blame] | 119 | 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] | 120 | build_opts.add_option(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration))); |
| 121 | build_opts.add_option(("-DNUM_CHANNELS=" + support::cpp11::to_string(input->info()->dimension(channel_idx)))); |
| 122 | |
Michele Di Giorgio | d63dfa2 | 2018-09-12 10:18:54 +0100 | [diff] [blame] | 123 | std::string kernel_name = "normalize_planar_yuv_layer_"; |
| 124 | if(is_data_type_quantized(dt)) |
| 125 | { |
| 126 | build_opts.add_option(("-DOFFSET=" + support::cpp11::to_string(input->info()->quantization_info().offset))); |
| 127 | build_opts.add_option(("-DSCALE=" + support::cpp11::to_string(input->info()->quantization_info().scale))); |
| 128 | kernel_name += "q8_"; |
| 129 | } |
| 130 | |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 131 | // Create kernel |
Michele Di Giorgio | d63dfa2 | 2018-09-12 10:18:54 +0100 | [diff] [blame] | 132 | kernel_name += lower_string(string_from_data_layout(input->info()->data_layout())); |
| 133 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 134 | |
| 135 | // Configure kernel window |
| 136 | auto win_config = validate_and_configure_window(input->info(), output->info(), mean->info(), std->info()); |
| 137 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 138 | ICLKernel::configure_internal(win_config.second); |
| 139 | |
| 140 | // Set config_id for enabling LWS tuning |
| 141 | _config_id = "normalize_planar_yuv_layer_"; |
| 142 | _config_id += lower_string(string_from_data_layout(input->info()->data_layout())); |
| 143 | _config_id += "_"; |
Michele Di Giorgio | d63dfa2 | 2018-09-12 10:18:54 +0100 | [diff] [blame] | 144 | _config_id += lower_string(string_from_data_type(dt)); |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 145 | _config_id += "_"; |
| 146 | _config_id += support::cpp11::to_string(input->info()->dimension(0)); |
| 147 | _config_id += "_"; |
| 148 | _config_id += support::cpp11::to_string(input->info()->dimension(1)); |
| 149 | _config_id += "_"; |
| 150 | _config_id += support::cpp11::to_string(input->info()->dimension(2)); |
| 151 | } |
| 152 | |
| 153 | Status CLNormalizePlanarYUVLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *mean, const ITensorInfo *std) |
| 154 | { |
| 155 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mean, std)); |
| 156 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), mean->clone().get(), std->clone().get()).first); |
| 157 | |
| 158 | return Status{}; |
| 159 | } |
| 160 | |
| 161 | void CLNormalizePlanarYUVLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 162 | { |
| 163 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 164 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 165 | |
| 166 | Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 167 | Window slice = collapsed.first_slice_window_3D(); |
| 168 | |
| 169 | Window slice_in = collapsed.first_slice_window_1D(); |
| 170 | slice_in.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 171 | |
| 172 | unsigned int idx = 2 * num_arguments_per_3D_tensor(); |
| 173 | add_1D_tensor_argument(idx, _mean, slice_in); |
| 174 | add_1D_tensor_argument(idx, _std, slice_in); |
| 175 | |
| 176 | do |
| 177 | { |
| 178 | idx = 0; |
| 179 | add_3D_tensor_argument(idx, _input, slice); |
| 180 | add_3D_tensor_argument(idx, _output, slice); |
| 181 | enqueue(queue, *this, slice, lws_hint()); |
| 182 | } |
| 183 | while(collapsed.slide_window_slice_3D(slice)); |
| 184 | } |