Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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/CLGaussianPyramidKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 27 | #include "arm_compute/core/CL/ICLTensor.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/Utils.h" |
| 30 | #include "arm_compute/core/Validate.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 31 | #include "support/StringSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | |
| 33 | using namespace arm_compute; |
| 34 | |
| 35 | CLGaussianPyramidHorKernel::CLGaussianPyramidHorKernel() |
Sanghoon Lee | 1cd4149 | 2018-03-15 11:48:48 +0000 | [diff] [blame] | 36 | : _l2_load_offset(0) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | { |
| 38 | } |
| 39 | |
| 40 | BorderSize CLGaussianPyramidHorKernel::border_size() const |
| 41 | { |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 42 | return BorderSize{ 0, 2 }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 43 | } |
| 44 | |
Sanghoon Lee | 1cd4149 | 2018-03-15 11:48:48 +0000 | [diff] [blame] | 45 | void CLGaussianPyramidHorKernel::configure(const ICLTensor *input, ICLTensor *output) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 47 | configure(CLKernelLibrary::get().get_compile_context(), input, output); |
| 48 | } |
| 49 | |
Manuel Bottini | 679fc96 | 2020-04-21 16:08:53 +0100 | [diff] [blame] | 50 | void CLGaussianPyramidHorKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 51 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 52 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 53 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U16); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 54 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != output->info()->dimension(1)); |
| 55 | |
| 56 | for(size_t i = 2; i < Coordinates::num_max_dimensions; ++i) |
| 57 | { |
| 58 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(i) != output->info()->dimension(i)); |
| 59 | } |
| 60 | |
Sanghoon Lee | 1cd4149 | 2018-03-15 11:48:48 +0000 | [diff] [blame] | 61 | _input = input; |
| 62 | _output = output; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 63 | |
| 64 | // Create kernel |
Gary Antcliffe | fffbdbc | 2019-05-28 11:40:21 +0100 | [diff] [blame] | 65 | const std::string kernel_name = std::string("gaussian1x5_sub_x"); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 66 | _kernel = create_kernel(compile_context, kernel_name); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 67 | |
| 68 | // Configure kernel window |
| 69 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 70 | constexpr unsigned int num_elems_read_per_iteration = 20; |
| 71 | constexpr unsigned int num_elems_written_per_iteration = 8; |
Sanghoon Lee | 1cd4149 | 2018-03-15 11:48:48 +0000 | [diff] [blame] | 72 | const float scale_x = static_cast<float>(output->info()->dimension(0)) / input->info()->dimension(0); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 73 | |
Sanghoon Lee | 1cd4149 | 2018-03-15 11:48:48 +0000 | [diff] [blame] | 74 | Window win = calculate_max_window_horizontal(*input->info(), Steps(num_elems_processed_per_iteration)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 75 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration, scale_x); |
| 76 | |
| 77 | // Sub sampling selects odd pixels (1, 3, 5, ...) for images with even |
| 78 | // width and even pixels (0, 2, 4, ...) for images with odd width. (Whether |
| 79 | // a pixel is even or odd is determined based on the tensor shape not the |
| 80 | // valid region!) |
| 81 | // Thus the offset from which the first pixel (L2) for the convolution is |
| 82 | // loaded depends on the anchor and shape of the valid region. |
| 83 | // In the case of an even shape (= even image width) we need to load L2 |
| 84 | // from -2 if the anchor is odd and from -1 if the anchor is even. That |
| 85 | // makes sure that L2 is always loaded from an odd pixel. |
| 86 | // On the other hand, for an odd shape (= odd image width) we need to load |
| 87 | // L2 from -1 if the anchor is odd and from -2 if the anchor is even to |
| 88 | // achieve the opposite effect. |
| 89 | // The condition can be simplified to checking whether anchor + shape is |
| 90 | // odd (-2) or even (-1) as only adding an odd and an even number will have |
| 91 | // an odd result. |
| 92 | _l2_load_offset = -border_size().left; |
| 93 | |
| 94 | if((_input->info()->valid_region().anchor[0] + _input->info()->valid_region().shape[0]) % 2 == 0) |
| 95 | { |
| 96 | _l2_load_offset += 1; |
| 97 | } |
| 98 | |
| 99 | update_window_and_padding(win, |
| 100 | AccessWindowHorizontal(input->info(), _l2_load_offset, num_elems_read_per_iteration), |
| 101 | output_access); |
| 102 | |
Sanghoon Lee | 1cd4149 | 2018-03-15 11:48:48 +0000 | [diff] [blame] | 103 | output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 104 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 105 | ICLKernel::configure_internal(win); |
Gary Antcliffe | fffbdbc | 2019-05-28 11:40:21 +0100 | [diff] [blame] | 106 | |
| 107 | // Set config_id for enabling LWS tuning |
| 108 | _config_id = kernel_name; |
| 109 | _config_id += "_"; |
| 110 | _config_id += lower_string(string_from_data_type(input->info()->data_type())); |
| 111 | _config_id += "_"; |
| 112 | _config_id += support::cpp11::to_string(input->info()->dimension(0)); |
| 113 | _config_id += "_"; |
| 114 | _config_id += support::cpp11::to_string(input->info()->dimension(1)); |
| 115 | _config_id += "_"; |
| 116 | _config_id += support::cpp11::to_string(output->info()->dimension(0)); |
| 117 | _config_id += "_"; |
| 118 | _config_id += support::cpp11::to_string(output->info()->dimension(1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void CLGaussianPyramidHorKernel::run(const Window &window, cl::CommandQueue &queue) |
| 122 | { |
| 123 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 124 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 125 | |
| 126 | Window win_in(window); |
| 127 | win_in.shift(Window::DimX, _l2_load_offset); |
| 128 | |
| 129 | //The output is half the width of the input: |
| 130 | Window win_out(window); |
| 131 | win_out.scale(Window::DimX, 0.5f); |
| 132 | |
| 133 | Window slice_in = win_in.first_slice_window_2D(); |
| 134 | Window slice_out = win_out.first_slice_window_2D(); |
| 135 | |
| 136 | do |
| 137 | { |
| 138 | unsigned int idx = 0; |
| 139 | add_2D_tensor_argument(idx, _input, slice_in); |
| 140 | add_2D_tensor_argument(idx, _output, slice_out); |
Michele Di Giorgio | 6b9f388 | 2019-07-01 16:37:04 +0100 | [diff] [blame] | 141 | enqueue(queue, *this, slice_out, lws_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 142 | } |
| 143 | while(win_in.slide_window_slice_2D(slice_in) && win_out.slide_window_slice_2D(slice_out)); |
| 144 | } |
| 145 | |
| 146 | CLGaussianPyramidVertKernel::CLGaussianPyramidVertKernel() |
| 147 | : _t2_load_offset(0) |
| 148 | { |
| 149 | } |
| 150 | |
| 151 | BorderSize CLGaussianPyramidVertKernel::border_size() const |
| 152 | { |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 153 | return BorderSize{ 2, 0 }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Sanghoon Lee | 1cd4149 | 2018-03-15 11:48:48 +0000 | [diff] [blame] | 156 | void CLGaussianPyramidVertKernel::configure(const ICLTensor *input, ICLTensor *output) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 157 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 158 | configure(CLKernelLibrary::get().get_compile_context(), input, output); |
| 159 | } |
| 160 | |
Manuel Bottini | 679fc96 | 2020-04-21 16:08:53 +0100 | [diff] [blame] | 161 | void CLGaussianPyramidVertKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 162 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 163 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U16); |
| 164 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8); |
| 165 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != output->info()->dimension(0)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 166 | |
| 167 | for(size_t i = 2; i < Coordinates::num_max_dimensions; ++i) |
| 168 | { |
| 169 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(i) != output->info()->dimension(i)); |
| 170 | } |
| 171 | |
| 172 | _input = input; |
| 173 | _output = output; |
| 174 | |
| 175 | // Create kernel |
Gary Antcliffe | fffbdbc | 2019-05-28 11:40:21 +0100 | [diff] [blame] | 176 | const std::string kernel_name = std::string("gaussian5x1_sub_y"); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 177 | _kernel = create_kernel(compile_context, "gaussian5x1_sub_y"); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 178 | |
| 179 | // Configure kernel window |
| 180 | constexpr unsigned int num_elems_processed_per_iteration = 8; |
| 181 | constexpr unsigned int num_rows_processed_per_iteration = 2; |
| 182 | constexpr unsigned int num_elems_written_per_iteration = 8; |
| 183 | constexpr unsigned int num_elems_read_per_iteration = 8; |
| 184 | constexpr unsigned int num_rows_per_iteration = 5; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 185 | |
Sanghoon Lee | 1cd4149 | 2018-03-15 11:48:48 +0000 | [diff] [blame] | 186 | const float scale_y = static_cast<float>(output->info()->dimension(1)) / input->info()->dimension(1); |
| 187 | |
| 188 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration, num_rows_processed_per_iteration)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 189 | AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_written_per_iteration, num_rows_per_iteration, 1.f, scale_y); |
| 190 | |
| 191 | // Determine whether we need to load even or odd rows. See above for a |
| 192 | // detailed explanation. |
| 193 | _t2_load_offset = -border_size().top; |
| 194 | |
| 195 | if((_input->info()->valid_region().anchor[1] + _input->info()->valid_region().shape[1]) % 2 == 0) |
| 196 | { |
| 197 | _t2_load_offset += 1; |
| 198 | } |
| 199 | |
| 200 | update_window_and_padding(win, |
| 201 | AccessWindowRectangle(input->info(), 0, _t2_load_offset, num_elems_read_per_iteration, num_rows_per_iteration), |
| 202 | output_access); |
| 203 | |
Sanghoon Lee | 1cd4149 | 2018-03-15 11:48:48 +0000 | [diff] [blame] | 204 | output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 205 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 206 | ICLKernel::configure_internal(win); |
Gary Antcliffe | fffbdbc | 2019-05-28 11:40:21 +0100 | [diff] [blame] | 207 | |
| 208 | // Set config_id for enabling LWS tuning |
| 209 | _config_id = kernel_name; |
| 210 | _config_id += "_"; |
| 211 | _config_id += lower_string(string_from_data_type(input->info()->data_type())); |
| 212 | _config_id += "_"; |
| 213 | _config_id += support::cpp11::to_string(input->info()->dimension(0)); |
| 214 | _config_id += "_"; |
| 215 | _config_id += support::cpp11::to_string(input->info()->dimension(1)); |
| 216 | _config_id += "_"; |
| 217 | _config_id += support::cpp11::to_string(output->info()->dimension(0)); |
| 218 | _config_id += "_"; |
| 219 | _config_id += support::cpp11::to_string(output->info()->dimension(1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void CLGaussianPyramidVertKernel::run(const Window &window, cl::CommandQueue &queue) |
| 223 | { |
| 224 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 225 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 226 | ARM_COMPUTE_ERROR_ON(window.x().step() != 8); |
| 227 | ARM_COMPUTE_ERROR_ON(window.y().step() % 2); |
| 228 | |
| 229 | Window win_in(window); |
| 230 | win_in.shift(Window::DimY, _t2_load_offset); |
| 231 | |
| 232 | Window win_out(window); |
| 233 | win_out.scale(Window::DimY, 0.5f); |
| 234 | |
| 235 | Window slice_in = win_in.first_slice_window_2D(); |
| 236 | Window slice_out = win_out.first_slice_window_2D(); |
| 237 | |
| 238 | do |
| 239 | { |
| 240 | unsigned int idx = 0; |
| 241 | add_2D_tensor_argument(idx, _input, slice_in); |
| 242 | add_2D_tensor_argument(idx, _output, slice_out); |
Michele Di Giorgio | 6b9f388 | 2019-07-01 16:37:04 +0100 | [diff] [blame] | 243 | enqueue(queue, *this, slice_out, lws_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 244 | } |
| 245 | while(win_in.slide_window_slice_2D(slice_in) && win_out.slide_window_slice_2D(slice_out)); |
| 246 | } |