Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [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/CLCopyKernel.h" |
| 25 | |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 28 | #include "arm_compute/core/CL/ICLTensor.h" |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
SiCong Li | 40192c1 | 2020-10-13 17:00:06 +0100 | [diff] [blame^] | 31 | #include "arm_compute/core/Utils.h" |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 33 | #include "src/core/AccessWindowStatic.h" |
| 34 | #include "src/core/helpers/AutoConfiguration.h" |
| 35 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 36 | #include "support/StringSupport.h" |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 37 | |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 38 | namespace arm_compute |
| 39 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 40 | namespace |
| 41 | { |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 42 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding = PaddingList(), Window *output_window = nullptr) |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 43 | { |
| 44 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 45 | ARM_COMPUTE_ERROR_ON(!padding.empty() && output_window != nullptr); |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 46 | ARM_COMPUTE_RETURN_ERROR_ON(padding.size() > 4); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 47 | |
| 48 | // Validate output if initialized |
| 49 | if(output->total_size() != 0) |
| 50 | { |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 51 | if(output_window == nullptr) |
| 52 | { |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding), output->tensor_shape()); |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output_window->shape()); |
| 60 | } |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 61 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 62 | } |
| 63 | |
| 64 | return Status{}; |
| 65 | } |
| 66 | |
SiCong Li | 40192c1 | 2020-10-13 17:00:06 +0100 | [diff] [blame^] | 67 | std::pair<Status, Window> configure_window(ITensorInfo *input, ITensorInfo *output) |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 68 | { |
| 69 | // Output auto inizialitation if not yet initialized |
| 70 | auto_init_if_empty(*output, *input); |
| 71 | |
| 72 | // Configure window |
SiCong Li | 40192c1 | 2020-10-13 17:00:06 +0100 | [diff] [blame^] | 73 | const unsigned int vec_size_x = adjust_vec_size(16 / input->element_size(), input->dimension(0)); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 74 | |
SiCong Li | 40192c1 | 2020-10-13 17:00:06 +0100 | [diff] [blame^] | 75 | const Window win = calculate_max_window(*input, Steps(vec_size_x)); |
| 76 | return std::make_pair(Status{}, win); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 77 | } |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 78 | |
| 79 | std::pair<Status, Window> validate_and_configure_window_with_padding(ITensorInfo *input, ITensorInfo *output, const PaddingList &padding) |
| 80 | { |
| 81 | TensorShape input_shape = input->tensor_shape(); |
| 82 | TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input_shape, padding); |
| 83 | |
| 84 | auto_init_if_empty(*output, input->clone()->set_tensor_shape(padded_shape)); |
| 85 | |
| 86 | // Configure window |
| 87 | const unsigned int num_elems_processed_per_iteration = 16 / input->element_size(); |
| 88 | |
| 89 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 90 | |
| 91 | // Pad on the x dimension accounting for the padding offset along the same dimension |
| 92 | AccessWindowHorizontal output_access(output, padding[0].first, num_elems_processed_per_iteration); |
| 93 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
| 94 | bool window_changed = update_window_and_padding(win, input_access, output_access); |
| 95 | |
| 96 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 97 | return std::make_pair(err, win); |
| 98 | } |
| 99 | |
| 100 | /** Generate the string "-DPAD= @p dim @p index @p padding" |
| 101 | * |
| 102 | * @param[in] dim The dimension index |
| 103 | * @param[in] index Can be 0 for the start dimension and 1 for the end dimension |
| 104 | * @param[in] padding The value to pad for that index/dimension pair |
| 105 | * |
| 106 | * @return The correct concatenated string |
| 107 | */ |
| 108 | std::string generate_pad_string(const size_t dim, const size_t index, const size_t padding) |
| 109 | { |
| 110 | return "-DPAD" + support::cpp11::to_string(dim) + support::cpp11::to_string(index) + "=" + support::cpp11::to_string(padding); |
| 111 | } |
| 112 | |
| 113 | /** Pass the padding as build option to the kernel. |
| 114 | * |
| 115 | * @param[in] tensor The padded tensor |
| 116 | * @param[in] padding The list of the padding for each dimension |
| 117 | * @param[out] build_opts The build option to which adding the padding |
| 118 | */ |
| 119 | void add_padding_as_build_options(const PaddingList &padding, CLBuildOptions &build_opts) |
| 120 | { |
| 121 | size_t dim = 0; |
| 122 | for(dim = 0; dim < padding.size(); dim++) |
| 123 | { |
| 124 | build_opts.add_option(generate_pad_string(dim, 0, padding[dim].first)); |
| 125 | build_opts.add_option(generate_pad_string(dim, 1, padding[dim].second)); |
| 126 | } |
| 127 | |
| 128 | while(dim < TensorShape::num_max_dimensions) |
| 129 | { |
| 130 | build_opts.add_option(generate_pad_string(dim, 0, 0)); |
| 131 | build_opts.add_option(generate_pad_string(dim, 1, 0)); |
| 132 | dim++; |
| 133 | } |
| 134 | } |
| 135 | |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 136 | } // namespace |
| 137 | |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 138 | CLCopyKernel::CLCopyKernel() |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 139 | : _input(nullptr), _output(nullptr), _output_window(), _has_output_window(false) |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 140 | { |
| 141 | } |
| 142 | |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 143 | void CLCopyKernel::configure(const ICLTensor *input, ICLTensor *output, const PaddingList &padding, Window *output_window) |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 144 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 145 | configure(CLKernelLibrary::get().get_compile_context(), input, output, padding, output_window); |
| 146 | } |
| 147 | |
Manuel Bottini | 256c0b9 | 2020-04-21 13:29:30 +0100 | [diff] [blame] | 148 | void CLCopyKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const PaddingList &padding, Window *output_window) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 149 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 150 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 151 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), padding, output_window)); |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 152 | |
SiCong Li | 40192c1 | 2020-10-13 17:00:06 +0100 | [diff] [blame^] | 153 | auto padding_info = get_padding_info({ input, output }); |
| 154 | |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 155 | _input = input; |
| 156 | _output = output; |
| 157 | |
| 158 | // Create kernel |
| 159 | CLBuildOptions build_opts; |
| 160 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 161 | |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 162 | std::pair<Status, Window> win_config; |
| 163 | |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 164 | const unsigned int vec_size_x = 16 / input->info()->element_size(); |
| 165 | |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 166 | if(padding.empty()) |
| 167 | { |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 168 | // Configure window |
SiCong Li | 40192c1 | 2020-10-13 17:00:06 +0100 | [diff] [blame^] | 169 | win_config = configure_window(input->info(), output->info()); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 170 | |
| 171 | if(output_window != nullptr) |
| 172 | { |
SiCong Li | 40192c1 | 2020-10-13 17:00:06 +0100 | [diff] [blame^] | 173 | _has_output_window = true; |
| 174 | _output_window = Window(*output_window); |
| 175 | const int width_x = output_window->num_iterations(0); |
| 176 | const int vec_size_x_leftover = width_x % vec_size_x; |
| 177 | const bool multi_access_x = width_x >= static_cast<int32_t>(vec_size_x); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 178 | |
| 179 | if(multi_access_x) |
| 180 | { |
| 181 | _output_window.set(Window::DimX, Window::Dimension(output_window->x().start(), ceil_to_multiple(output_window->x().end(), vec_size_x), vec_size_x)); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 182 | } |
| 183 | |
SiCong Li | 40192c1 | 2020-10-13 17:00:06 +0100 | [diff] [blame^] | 184 | build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_x_leftover)); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 185 | } |
| 186 | else |
| 187 | { |
SiCong Li | 40192c1 | 2020-10-13 17:00:06 +0100 | [diff] [blame^] | 188 | const int width_x = input->info()->tensor_shape().x(); |
| 189 | const int vec_size_x_leftover = width_x % vec_size_x; |
| 190 | |
| 191 | build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_x_leftover)); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 192 | } |
| 193 | |
SiCong Li | 40192c1 | 2020-10-13 17:00:06 +0100 | [diff] [blame^] | 194 | build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x)); |
| 195 | |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 196 | // Build kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 197 | _kernel = create_kernel(compile_context, "copy_tensor", build_opts.options()); |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 198 | } |
| 199 | else |
| 200 | { |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 201 | build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x)); |
| 202 | |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 203 | // Add compile time options |
| 204 | add_padding_as_build_options(padding, build_opts); |
| 205 | |
| 206 | // If we are padding in the fourth dimension the kernel needs to know the depth of the |
| 207 | // different cubes |
| 208 | if(padding.size() == 4) |
| 209 | { |
| 210 | const size_t depth = input->info()->tensor_shape()[2]; |
| 211 | build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(depth)); |
| 212 | } |
| 213 | |
| 214 | // Build kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 215 | _kernel = create_kernel(compile_context, "copy_pad_tensor", build_opts.options()); |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 216 | |
| 217 | // Configure window |
| 218 | win_config = validate_and_configure_window_with_padding(input->info(), output->info(), padding); |
| 219 | } |
| 220 | |
| 221 | // Validate and set the window |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 222 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 223 | ICLKernel::configure_internal(win_config.second); |
SiCong Li | 40192c1 | 2020-10-13 17:00:06 +0100 | [diff] [blame^] | 224 | |
| 225 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 226 | } |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 227 | |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 228 | Status CLCopyKernel::validate(const arm_compute::ITensorInfo *input, const arm_compute::ITensorInfo *output, const PaddingList &padding, Window *output_window) |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 229 | { |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 230 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, padding, output_window)); |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 231 | |
SiCong Li | 40192c1 | 2020-10-13 17:00:06 +0100 | [diff] [blame^] | 232 | if(!padding.empty()) |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 233 | { |
| 234 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_with_padding(input->clone().get(), output->clone().get(), padding).first); |
| 235 | } |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 236 | |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 237 | return Status{}; |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void CLCopyKernel::run(const Window &window, cl::CommandQueue &queue) |
| 241 | { |
| 242 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 243 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 244 | |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 245 | Window slice; |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 246 | |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 247 | if(_has_output_window) |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 248 | { |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 249 | slice = window.first_slice_window_3D(); |
| 250 | Window out_slice = _output_window.first_slice_window_3D(); |
| 251 | do |
| 252 | { |
| 253 | unsigned int idx = 0; |
| 254 | add_3D_tensor_argument(idx, _input, slice); |
| 255 | add_3D_tensor_argument(idx, _output, out_slice); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 256 | enqueue(queue, *this, slice, lws_hint()); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 257 | } |
| 258 | while(window.slide_window_slice_3D(slice) && _output_window.slide_window_slice_3D(out_slice)); |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 259 | } |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 260 | else |
| 261 | { |
| 262 | Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 263 | slice = collapsed.first_slice_window_3D(); |
| 264 | do |
| 265 | { |
| 266 | unsigned int idx = 0; |
| 267 | add_3D_tensor_argument(idx, _input, slice); |
| 268 | add_3D_tensor_argument(idx, _output, slice); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 269 | enqueue(queue, *this, slice, lws_hint()); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 270 | } |
| 271 | while(collapsed.slide_window_slice_3D(slice)); |
| 272 | } |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 273 | } |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 274 | } // namespace arm_compute |