blob: c87768aeb7b9750f6fbf10185ffdcf40033fb2d7 [file] [log] [blame]
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +00001/*
George Wort894066d2019-02-15 15:12:52 +00002 * Copyright (c) 2018-2019 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
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLKernelLibrary.h"
29#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/Error.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/TensorInfo.h"
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +000033#include "arm_compute/core/Window.h"
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010034#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +000035
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010036namespace arm_compute
37{
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010038namespace
39{
George Wort894066d2019-02-15 15:12:52 +000040Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding = PaddingList(), Window *output_window = nullptr)
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010041{
42 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
George Wort894066d2019-02-15 15:12:52 +000043 ARM_COMPUTE_ERROR_ON(!padding.empty() && output_window != nullptr);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010044 ARM_COMPUTE_RETURN_ERROR_ON(padding.size() > 4);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010045
46 // Validate output if initialized
47 if(output->total_size() != 0)
48 {
George Wort894066d2019-02-15 15:12:52 +000049 if(output_window == nullptr)
50 {
51 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding), output->tensor_shape());
52 }
53 else
54 {
55 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
George Wort894066d2019-02-15 15:12:52 +000056 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output_window->shape());
58 }
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010059 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
60 }
61
62 return Status{};
63}
64
George Wort894066d2019-02-15 15:12:52 +000065std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, Window *output_window)
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010066{
67 // Output auto inizialitation if not yet initialized
68 auto_init_if_empty(*output, *input);
69
70 // Configure window
George Wort894066d2019-02-15 15:12:52 +000071 const unsigned int vec_size_x = 16 / input->element_size();
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010072
George Wort894066d2019-02-15 15:12:52 +000073 if(output_window == nullptr)
74 {
75 // Create and update the window (if needed)
76 Window win = calculate_max_window(*input, Steps(vec_size_x));
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010077
George Wort894066d2019-02-15 15:12:52 +000078 AccessWindowHorizontal input_access(input, 0, vec_size_x);
79 AccessWindowHorizontal output_access(output, 0, vec_size_x);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010080
George Wort894066d2019-02-15 15:12:52 +000081 bool window_changed = update_window_and_padding(win, input_access, output_access);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010082
George Wort894066d2019-02-15 15:12:52 +000083 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
84 return std::make_pair(err, win);
85 }
86 else
87 {
88 Window win = calculate_max_window(*input);
89 return std::make_pair(Status{}, win);
90 }
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010091}
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010092
93std::pair<Status, Window> validate_and_configure_window_with_padding(ITensorInfo *input, ITensorInfo *output, const PaddingList &padding)
94{
95 TensorShape input_shape = input->tensor_shape();
96 TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input_shape, padding);
97
98 auto_init_if_empty(*output, input->clone()->set_tensor_shape(padded_shape));
99
100 // Configure window
101 const unsigned int num_elems_processed_per_iteration = 16 / input->element_size();
102
103 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
104
105 // Pad on the x dimension accounting for the padding offset along the same dimension
106 AccessWindowHorizontal output_access(output, padding[0].first, num_elems_processed_per_iteration);
107 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
108 bool window_changed = update_window_and_padding(win, input_access, output_access);
109
110 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
111 return std::make_pair(err, win);
112}
113
114/** Generate the string "-DPAD= @p dim @p index @p padding"
115 *
116 * @param[in] dim The dimension index
117 * @param[in] index Can be 0 for the start dimension and 1 for the end dimension
118 * @param[in] padding The value to pad for that index/dimension pair
119 *
120 * @return The correct concatenated string
121 */
122std::string generate_pad_string(const size_t dim, const size_t index, const size_t padding)
123{
124 return "-DPAD" + support::cpp11::to_string(dim) + support::cpp11::to_string(index) + "=" + support::cpp11::to_string(padding);
125}
126
127/** Pass the padding as build option to the kernel.
128 *
129 * @param[in] tensor The padded tensor
130 * @param[in] padding The list of the padding for each dimension
131 * @param[out] build_opts The build option to which adding the padding
132 */
133void add_padding_as_build_options(const PaddingList &padding, CLBuildOptions &build_opts)
134{
135 size_t dim = 0;
136 for(dim = 0; dim < padding.size(); dim++)
137 {
138 build_opts.add_option(generate_pad_string(dim, 0, padding[dim].first));
139 build_opts.add_option(generate_pad_string(dim, 1, padding[dim].second));
140 }
141
142 while(dim < TensorShape::num_max_dimensions)
143 {
144 build_opts.add_option(generate_pad_string(dim, 0, 0));
145 build_opts.add_option(generate_pad_string(dim, 1, 0));
146 dim++;
147 }
148}
149
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100150} // namespace
151
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000152CLCopyKernel::CLCopyKernel()
George Wort894066d2019-02-15 15:12:52 +0000153 : _input(nullptr), _output(nullptr), _output_window(), _has_output_window(false)
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000154{
155}
156
George Wort894066d2019-02-15 15:12:52 +0000157void CLCopyKernel::configure(const ICLTensor *input, ICLTensor *output, const PaddingList &padding, Window *output_window)
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000158{
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100159 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
George Wort894066d2019-02-15 15:12:52 +0000160 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), padding, output_window));
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000161
162 _input = input;
163 _output = output;
164
165 // Create kernel
166 CLBuildOptions build_opts;
167 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000168
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100169 std::pair<Status, Window> win_config;
170
George Wort894066d2019-02-15 15:12:52 +0000171 const unsigned int vec_size_x = 16 / input->info()->element_size();
172
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100173 if(padding.empty())
174 {
George Wort894066d2019-02-15 15:12:52 +0000175 // Configure window
176 win_config = validate_and_configure_window(input->info(), output->info(), output_window);
177
178 if(output_window != nullptr)
179 {
180 _has_output_window = true;
181 _output_window = Window(*output_window);
182 const int width_x = output_window->num_iterations(0);
183 const bool multi_access_x = width_x >= static_cast<int32_t>(vec_size_x);
184 const bool remainder_x = width_x % vec_size_x > 0;
185
186 if(multi_access_x)
187 {
188 _output_window.set(Window::DimX, Window::Dimension(output_window->x().start(), ceil_to_multiple(output_window->x().end(), vec_size_x), vec_size_x));
189 win_config.second.set(Window::DimX, Window::Dimension(win_config.second.x().start(), ceil_to_multiple(win_config.second.x().end(), vec_size_x), vec_size_x));
190 }
191
192 build_opts.add_option_if(multi_access_x, "-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x));
193 build_opts.add_option_if(multi_access_x && remainder_x, "-DLAST_ACCESSED_X=" + support::cpp11::to_string(std::max<int>(width_x - vec_size_x, 0)));
194 }
195 else
196 {
197 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x));
198 }
199
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100200 // Build kernel
201 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("copy_tensor", build_opts.options()));
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100202 }
203 else
204 {
George Wort894066d2019-02-15 15:12:52 +0000205 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x));
206
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100207 // Add compile time options
208 add_padding_as_build_options(padding, build_opts);
209
210 // If we are padding in the fourth dimension the kernel needs to know the depth of the
211 // different cubes
212 if(padding.size() == 4)
213 {
214 const size_t depth = input->info()->tensor_shape()[2];
215 build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(depth));
216 }
217
218 // Build kernel
219 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("copy_pad_tensor", build_opts.options()));
220
221 // Configure window
222 win_config = validate_and_configure_window_with_padding(input->info(), output->info(), padding);
223 }
224
225 // Validate and set the window
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100226 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100227 ICLKernel::configure_internal(win_config.second);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100228}
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000229
George Wort894066d2019-02-15 15:12:52 +0000230Status CLCopyKernel::validate(const arm_compute::ITensorInfo *input, const arm_compute::ITensorInfo *output, const PaddingList &padding, Window *output_window)
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100231{
George Wort894066d2019-02-15 15:12:52 +0000232 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, padding, output_window));
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100233
234 if(padding.empty())
235 {
George Wort894066d2019-02-15 15:12:52 +0000236 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), output_window).first);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100237 }
238 else
239 {
240 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_with_padding(input->clone().get(), output->clone().get(), padding).first);
241 }
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000242
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100243 return Status{};
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000244}
245
246void CLCopyKernel::run(const Window &window, cl::CommandQueue &queue)
247{
248 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
249 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
250
George Wort894066d2019-02-15 15:12:52 +0000251 Window slice;
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000252
George Wort894066d2019-02-15 15:12:52 +0000253 if(_has_output_window)
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000254 {
George Wort894066d2019-02-15 15:12:52 +0000255 slice = window.first_slice_window_3D();
256 Window out_slice = _output_window.first_slice_window_3D();
257 do
258 {
259 unsigned int idx = 0;
260 add_3D_tensor_argument(idx, _input, slice);
261 add_3D_tensor_argument(idx, _output, out_slice);
262 enqueue(queue, *this, slice);
263 }
264 while(window.slide_window_slice_3D(slice) && _output_window.slide_window_slice_3D(out_slice));
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000265 }
George Wort894066d2019-02-15 15:12:52 +0000266 else
267 {
268 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
269 slice = collapsed.first_slice_window_3D();
270 do
271 {
272 unsigned int idx = 0;
273 add_3D_tensor_argument(idx, _input, slice);
274 add_3D_tensor_argument(idx, _output, slice);
275 enqueue(queue, *this, slice);
276 }
277 while(collapsed.slide_window_slice_3D(slice));
278 }
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000279}
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100280} // namespace arm_compute