Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 1 | /* |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, 2023 Arm Limited. |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +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 | */ |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 24 | #include "src/gpu/cl/kernels/ClCopyKernel.h" |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 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" |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 33 | #include "arm_compute/core/Validate.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 34 | |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 35 | #include "src/core/CL/CLValidate.h" |
| 36 | #include "src/core/helpers/AutoConfiguration.h" |
| 37 | #include "src/core/helpers/WindowHelpers.h" |
| 38 | #include "support/Cast.h" |
| 39 | #include "support/StringSupport.h" |
| 40 | |
| 41 | namespace arm_compute |
| 42 | { |
| 43 | namespace opencl |
| 44 | { |
| 45 | namespace kernels |
| 46 | { |
| 47 | namespace |
| 48 | { |
| 49 | Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, Window *dst_window = nullptr) |
| 50 | { |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
| 52 | |
| 53 | // Validate dst if initialized |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 54 | if (dst->total_size() != 0) |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 55 | { |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(src, dst); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 58 | if (dst_window == nullptr) |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 59 | { |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(src->tensor_shape(), dst->tensor_shape()); |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(src->tensor_shape(), dst_window->shape()); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return Status{}; |
| 69 | } |
| 70 | |
| 71 | } // namespace |
| 72 | |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 73 | ClCopyKernel::ClCopyKernel() |
| 74 | { |
| 75 | _type = CLKernelType::ELEMENTWISE; |
| 76 | } |
| 77 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 78 | void ClCopyKernel::configure(const CLCompileContext &compile_context, |
| 79 | const ITensorInfo *src, |
| 80 | ITensorInfo *dst, |
| 81 | Window *dst_window) |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 82 | { |
| 83 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
| 84 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, dst_window)); |
| 85 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 86 | auto padding_info = get_padding_info({src, dst}); |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 87 | |
| 88 | // Create kernel |
| 89 | CLBuildOptions build_opts; |
| 90 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src->data_type())); |
| 91 | |
| 92 | // Output auto inizialitation if not yet initialized |
| 93 | auto_init_if_empty(*dst, *src); |
| 94 | |
| 95 | // Configure window |
| 96 | const unsigned int vec_size_x = adjust_vec_size(16 / src->element_size(), src->dimension(0)); |
| 97 | |
| 98 | const Window win_config = calculate_max_window(*src, Steps(vec_size_x)); |
| 99 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 100 | if (dst_window != nullptr) |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 101 | { |
| 102 | _has_dst_window = true; |
| 103 | _dst_window = Window(*dst_window); |
| 104 | const int width_x = dst_window->num_iterations(0); |
| 105 | const int vec_size_x_leftover = width_x % vec_size_x; |
| 106 | const bool multi_access_x = width_x >= static_cast<int32_t>(vec_size_x); |
| 107 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 108 | if (multi_access_x) |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 109 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 110 | _dst_window.set(Window::DimX, |
| 111 | Window::Dimension(dst_window->x().start(), |
| 112 | ceil_to_multiple(dst_window->x().end(), vec_size_x), vec_size_x)); |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_x_leftover)); |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | const int width_x = src->tensor_shape().x(); |
| 120 | const int vec_size_x_leftover = width_x % vec_size_x; |
| 121 | |
| 122 | build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_x_leftover)); |
| 123 | } |
| 124 | |
| 125 | build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x)); |
| 126 | |
| 127 | // Build kernel |
| 128 | _kernel = create_kernel(compile_context, "copy_tensor", build_opts.options()); |
| 129 | |
| 130 | // Validate and set the window |
| 131 | ICLKernel::configure_internal(win_config); |
| 132 | |
| 133 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
| 134 | } |
| 135 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 136 | Status |
| 137 | ClCopyKernel::validate(const arm_compute::ITensorInfo *src, const arm_compute::ITensorInfo *dst, Window *dst_window) |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 138 | { |
| 139 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, dst_window)); |
| 140 | |
| 141 | return Status{}; |
| 142 | } |
| 143 | |
| 144 | void ClCopyKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) |
| 145 | { |
| 146 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 147 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 148 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 149 | const auto src = |
| 150 | utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC)); |
| 151 | auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST)); |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 152 | |
| 153 | Window slice; |
| 154 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 155 | if (_has_dst_window) |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 156 | { |
| 157 | slice = window.first_slice_window_3D(); |
| 158 | Window out_slice = _dst_window.first_slice_window_3D(); |
| 159 | do |
| 160 | { |
| 161 | unsigned int idx = 0; |
| 162 | add_3D_tensor_argument(idx, src, slice); |
| 163 | add_3D_tensor_argument(idx, dst, out_slice); |
| 164 | enqueue(queue, *this, slice, lws_hint()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 165 | } while (window.slide_window_slice_3D(slice) && _dst_window.slide_window_slice_3D(out_slice)); |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 166 | } |
| 167 | else |
| 168 | { |
| 169 | Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 170 | slice = collapsed.first_slice_window_3D(); |
| 171 | do |
| 172 | { |
| 173 | unsigned int idx = 0; |
| 174 | add_3D_tensor_argument(idx, src, slice); |
| 175 | add_3D_tensor_argument(idx, dst, slice); |
| 176 | enqueue(queue, *this, slice, lws_hint()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 177 | } while (collapsed.slide_window_slice_3D(slice)); |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | } // namespace kernels |
| 181 | } // namespace opencl |
| 182 | } // namespace arm_compute |