blob: c80ef664f52acce0c7d51bdf502f7fbea9646379 [file] [log] [blame]
Sheri Zhang7e20e292021-02-02 11:49:34 +00001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2018-2021, 2023 Arm Limited.
Sheri Zhang7e20e292021-02-02 11:49:34 +00003 *
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 Pinitas7891a732021-08-20 21:39:25 +010024#include "src/gpu/cl/kernels/ClCopyKernel.h"
Sheri Zhang7e20e292021-02-02 11:49:34 +000025
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 Bentham314d3e22023-06-23 10:53:52 +000032#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
Sheri Zhang7e20e292021-02-02 11:49:34 +000033#include "arm_compute/core/Validate.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010034
Sheri Zhang7e20e292021-02-02 11:49:34 +000035#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
41namespace arm_compute
42{
43namespace opencl
44{
45namespace kernels
46{
47namespace
48{
49Status 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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010054 if (dst->total_size() != 0)
Sheri Zhang7e20e292021-02-02 11:49:34 +000055 {
56 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(src, dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010058 if (dst_window == nullptr)
Sheri Zhang7e20e292021-02-02 11:49:34 +000059 {
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 Arena4a95bba2021-06-28 11:00:27 +010073ClCopyKernel::ClCopyKernel()
74{
75 _type = CLKernelType::ELEMENTWISE;
76}
77
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010078void ClCopyKernel::configure(const CLCompileContext &compile_context,
79 const ITensorInfo *src,
80 ITensorInfo *dst,
81 Window *dst_window)
Sheri Zhang7e20e292021-02-02 11:49:34 +000082{
83 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
84 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, dst_window));
85
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010086 auto padding_info = get_padding_info({src, dst});
Sheri Zhang7e20e292021-02-02 11:49:34 +000087
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100100 if (dst_window != nullptr)
Sheri Zhang7e20e292021-02-02 11:49:34 +0000101 {
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100108 if (multi_access_x)
Sheri Zhang7e20e292021-02-02 11:49:34 +0000109 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100110 _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 Zhang7e20e292021-02-02 11:49:34 +0000113 }
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100136Status
137ClCopyKernel::validate(const arm_compute::ITensorInfo *src, const arm_compute::ITensorInfo *dst, Window *dst_window)
Sheri Zhang7e20e292021-02-02 11:49:34 +0000138{
139 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, dst_window));
140
141 return Status{};
142}
143
144void 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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100149 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 Zhang7e20e292021-02-02 11:49:34 +0000152
153 Window slice;
154
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100155 if (_has_dst_window)
Sheri Zhang7e20e292021-02-02 11:49:34 +0000156 {
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100165 } while (window.slide_window_slice_3D(slice) && _dst_window.slide_window_slice_3D(out_slice));
Sheri Zhang7e20e292021-02-02 11:49:34 +0000166 }
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100177 } while (collapsed.slide_window_slice_3D(slice));
Sheri Zhang7e20e292021-02-02 11:49:34 +0000178 }
179}
180} // namespace kernels
181} // namespace opencl
182} // namespace arm_compute