blob: 184b80caa865e6b368a6e5a55740dd254c36c89e [file] [log] [blame]
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +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 */
24#include "arm_compute/core/CL/kernels/CLCopyKernel.h"
25
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +000026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
SiCong Li40192c12020-10-13 17:00:06 +010029#include "arm_compute/core/Utils.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010030#include "src/core/helpers/AutoConfiguration.h"
31#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000032#include "support/StringSupport.h"
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +000033
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010034namespace arm_compute
35{
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010036namespace
37{
SiCong Li3580c752020-10-14 17:00:56 +010038Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, Window *output_window = nullptr)
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010039{
40 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
41
42 // Validate output if initialized
43 if(output->total_size() != 0)
44 {
SiCong Li3580c752020-10-14 17:00:56 +010045 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
46 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
George Wort894066d2019-02-15 15:12:52 +000047 if(output_window == nullptr)
48 {
SiCong Li3580c752020-10-14 17:00:56 +010049 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output->tensor_shape());
George Wort894066d2019-02-15 15:12:52 +000050 }
51 else
52 {
George Wort894066d2019-02-15 15:12:52 +000053 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output_window->shape());
54 }
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010055 }
56
57 return Status{};
58}
59
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010060} // namespace
61
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +000062CLCopyKernel::CLCopyKernel()
George Wort894066d2019-02-15 15:12:52 +000063 : _input(nullptr), _output(nullptr), _output_window(), _has_output_window(false)
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +000064{
65}
66
SiCong Li3580c752020-10-14 17:00:56 +010067void CLCopyKernel::configure(const ICLTensor *input, ICLTensor *output, Window *output_window)
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +000068{
SiCong Li3580c752020-10-14 17:00:56 +010069 configure(CLKernelLibrary::get().get_compile_context(), input, output, output_window);
Manuel Bottini4c6bd512020-04-08 10:15:51 +010070}
71
SiCong Li3580c752020-10-14 17:00:56 +010072void CLCopyKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, Window *output_window)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010073{
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010074 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
SiCong Li3580c752020-10-14 17:00:56 +010075 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), output_window));
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +000076
SiCong Li40192c12020-10-13 17:00:06 +010077 auto padding_info = get_padding_info({ input, output });
78
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +000079 _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 Spyrou5c8e05c2018-03-22 11:56:01 +000085
SiCong Li3580c752020-10-14 17:00:56 +010086 // Output auto inizialitation if not yet initialized
87 auto_init_if_empty(*(output->info()), *(input->info()));
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010088
SiCong Li3580c752020-10-14 17:00:56 +010089 // Configure window
90 const unsigned int vec_size_x = adjust_vec_size(16 / input->info()->element_size(), input->info()->dimension(0));
George Wort894066d2019-02-15 15:12:52 +000091
SiCong Li3580c752020-10-14 17:00:56 +010092 const Window win_config = calculate_max_window(*(input->info()), Steps(vec_size_x));
93
94 if(output_window != nullptr)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010095 {
SiCong Li3580c752020-10-14 17:00:56 +010096 _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 Wort894066d2019-02-15 15:12:52 +0000101
SiCong Li3580c752020-10-14 17:00:56 +0100102 if(multi_access_x)
George Wort894066d2019-02-15 15:12:52 +0000103 {
SiCong Li3580c752020-10-14 17:00:56 +0100104 _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 Wort894066d2019-02-15 15:12:52 +0000105 }
106
SiCong Li3580c752020-10-14 17:00:56 +0100107 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_x_leftover));
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100108 }
109 else
110 {
SiCong Li3580c752020-10-14 17:00:56 +0100111 const int width_x = input->info()->tensor_shape().x();
112 const int vec_size_x_leftover = width_x % vec_size_x;
George Wort894066d2019-02-15 15:12:52 +0000113
SiCong Li3580c752020-10-14 17:00:56 +0100114 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_x_leftover));
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100115 }
116
SiCong Li3580c752020-10-14 17:00:56 +0100117 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 Rossinid7647d42018-07-17 18:13:13 +0100122 // Validate and set the window
SiCong Li3580c752020-10-14 17:00:56 +0100123 ICLKernel::configure_internal(win_config);
SiCong Li40192c12020-10-13 17:00:06 +0100124
125 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100126}
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000127
SiCong Li3580c752020-10-14 17:00:56 +0100128Status CLCopyKernel::validate(const arm_compute::ITensorInfo *input, const arm_compute::ITensorInfo *output, Window *output_window)
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100129{
SiCong Li3580c752020-10-14 17:00:56 +0100130 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, output_window));
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000131
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100132 return Status{};
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000133}
134
135void 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 Wort894066d2019-02-15 15:12:52 +0000140 Window slice;
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000141
George Wort894066d2019-02-15 15:12:52 +0000142 if(_has_output_window)
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000143 {
George Wort894066d2019-02-15 15:12:52 +0000144 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 Pinitas275f99c2019-08-23 12:44:11 +0100151 enqueue(queue, *this, slice, lws_hint());
George Wort894066d2019-02-15 15:12:52 +0000152 }
153 while(window.slide_window_slice_3D(slice) && _output_window.slide_window_slice_3D(out_slice));
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000154 }
George Wort894066d2019-02-15 15:12:52 +0000155 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 Pinitas275f99c2019-08-23 12:44:11 +0100164 enqueue(queue, *this, slice, lws_hint());
George Wort894066d2019-02-15 15:12:52 +0000165 }
166 while(collapsed.slide_window_slice_3D(slice));
167 }
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000168}
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100169} // namespace arm_compute