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" |
SiCong Li | 40192c1 | 2020-10-13 17:00:06 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Utils.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 30 | #include "src/core/helpers/AutoConfiguration.h" |
| 31 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 32 | #include "support/StringSupport.h" |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 33 | |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 34 | namespace arm_compute |
| 35 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 36 | namespace |
| 37 | { |
SiCong Li | 3580c75 | 2020-10-14 17:00:56 +0100 | [diff] [blame^] | 38 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, Window *output_window = nullptr) |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 39 | { |
| 40 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 41 | |
| 42 | // Validate output if initialized |
| 43 | if(output->total_size() != 0) |
| 44 | { |
SiCong Li | 3580c75 | 2020-10-14 17:00:56 +0100 | [diff] [blame^] | 45 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 46 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 47 | if(output_window == nullptr) |
| 48 | { |
SiCong Li | 3580c75 | 2020-10-14 17:00:56 +0100 | [diff] [blame^] | 49 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output->tensor_shape()); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 50 | } |
| 51 | else |
| 52 | { |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 53 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output_window->shape()); |
| 54 | } |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | return Status{}; |
| 58 | } |
| 59 | |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 60 | } // namespace |
| 61 | |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 62 | CLCopyKernel::CLCopyKernel() |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 63 | : _input(nullptr), _output(nullptr), _output_window(), _has_output_window(false) |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 64 | { |
| 65 | } |
| 66 | |
SiCong Li | 3580c75 | 2020-10-14 17:00:56 +0100 | [diff] [blame^] | 67 | void CLCopyKernel::configure(const ICLTensor *input, ICLTensor *output, Window *output_window) |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 68 | { |
SiCong Li | 3580c75 | 2020-10-14 17:00:56 +0100 | [diff] [blame^] | 69 | configure(CLKernelLibrary::get().get_compile_context(), input, output, output_window); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 70 | } |
| 71 | |
SiCong Li | 3580c75 | 2020-10-14 17:00:56 +0100 | [diff] [blame^] | 72 | void CLCopyKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, Window *output_window) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 73 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 74 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
SiCong Li | 3580c75 | 2020-10-14 17:00:56 +0100 | [diff] [blame^] | 75 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), output_window)); |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 76 | |
SiCong Li | 40192c1 | 2020-10-13 17:00:06 +0100 | [diff] [blame] | 77 | auto padding_info = get_padding_info({ input, output }); |
| 78 | |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 79 | _input = input; |
| 80 | _output = output; |
| 81 | |
| 82 | // Create kernel |
| 83 | CLBuildOptions build_opts; |
| 84 | 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] | 85 | |
SiCong Li | 3580c75 | 2020-10-14 17:00:56 +0100 | [diff] [blame^] | 86 | // Output auto inizialitation if not yet initialized |
| 87 | auto_init_if_empty(*(output->info()), *(input->info())); |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 88 | |
SiCong Li | 3580c75 | 2020-10-14 17:00:56 +0100 | [diff] [blame^] | 89 | // Configure window |
| 90 | const unsigned int vec_size_x = adjust_vec_size(16 / input->info()->element_size(), input->info()->dimension(0)); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 91 | |
SiCong Li | 3580c75 | 2020-10-14 17:00:56 +0100 | [diff] [blame^] | 92 | const Window win_config = calculate_max_window(*(input->info()), Steps(vec_size_x)); |
| 93 | |
| 94 | if(output_window != nullptr) |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 95 | { |
SiCong Li | 3580c75 | 2020-10-14 17:00:56 +0100 | [diff] [blame^] | 96 | _has_output_window = true; |
| 97 | _output_window = Window(*output_window); |
| 98 | const int width_x = output_window->num_iterations(0); |
| 99 | const int vec_size_x_leftover = width_x % vec_size_x; |
| 100 | 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] | 101 | |
SiCong Li | 3580c75 | 2020-10-14 17:00:56 +0100 | [diff] [blame^] | 102 | if(multi_access_x) |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 103 | { |
SiCong Li | 3580c75 | 2020-10-14 17:00:56 +0100 | [diff] [blame^] | 104 | _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] | 105 | } |
| 106 | |
SiCong Li | 3580c75 | 2020-10-14 17:00:56 +0100 | [diff] [blame^] | 107 | build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_x_leftover)); |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 108 | } |
| 109 | else |
| 110 | { |
SiCong Li | 3580c75 | 2020-10-14 17:00:56 +0100 | [diff] [blame^] | 111 | const int width_x = input->info()->tensor_shape().x(); |
| 112 | const int vec_size_x_leftover = width_x % vec_size_x; |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 113 | |
SiCong Li | 3580c75 | 2020-10-14 17:00:56 +0100 | [diff] [blame^] | 114 | build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_x_leftover)); |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 115 | } |
| 116 | |
SiCong Li | 3580c75 | 2020-10-14 17:00:56 +0100 | [diff] [blame^] | 117 | build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x)); |
| 118 | |
| 119 | // Build kernel |
| 120 | _kernel = create_kernel(compile_context, "copy_tensor", build_opts.options()); |
| 121 | |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 122 | // Validate and set the window |
SiCong Li | 3580c75 | 2020-10-14 17:00:56 +0100 | [diff] [blame^] | 123 | ICLKernel::configure_internal(win_config); |
SiCong Li | 40192c1 | 2020-10-13 17:00:06 +0100 | [diff] [blame] | 124 | |
| 125 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 126 | } |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 127 | |
SiCong Li | 3580c75 | 2020-10-14 17:00:56 +0100 | [diff] [blame^] | 128 | Status CLCopyKernel::validate(const arm_compute::ITensorInfo *input, const arm_compute::ITensorInfo *output, Window *output_window) |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 129 | { |
SiCong Li | 3580c75 | 2020-10-14 17:00:56 +0100 | [diff] [blame^] | 130 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, output_window)); |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 131 | |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 132 | return Status{}; |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void CLCopyKernel::run(const Window &window, cl::CommandQueue &queue) |
| 136 | { |
| 137 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 138 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 139 | |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 140 | Window slice; |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 141 | |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 142 | if(_has_output_window) |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 143 | { |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 144 | slice = window.first_slice_window_3D(); |
| 145 | Window out_slice = _output_window.first_slice_window_3D(); |
| 146 | do |
| 147 | { |
| 148 | unsigned int idx = 0; |
| 149 | add_3D_tensor_argument(idx, _input, slice); |
| 150 | add_3D_tensor_argument(idx, _output, out_slice); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 151 | enqueue(queue, *this, slice, lws_hint()); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 152 | } |
| 153 | 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] | 154 | } |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 155 | else |
| 156 | { |
| 157 | Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 158 | slice = collapsed.first_slice_window_3D(); |
| 159 | do |
| 160 | { |
| 161 | unsigned int idx = 0; |
| 162 | add_3D_tensor_argument(idx, _input, slice); |
| 163 | add_3D_tensor_argument(idx, _output, slice); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 164 | enqueue(queue, *this, slice, lws_hint()); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 165 | } |
| 166 | while(collapsed.slide_window_slice_3D(slice)); |
| 167 | } |
Michalis Spyrou | 5c8e05c | 2018-03-22 11:56:01 +0000 | [diff] [blame] | 168 | } |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 169 | } // namespace arm_compute |