blob: e14e5dafab777af0eddec531cb9ca758a34c245f [file] [log] [blame]
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +00001/*
2 * Copyright (c) 2018 ARM Limited.
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
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{
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010040Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding = PaddingList())
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010041{
42 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010043 ARM_COMPUTE_RETURN_ERROR_ON(padding.size() > 4);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010044
45 // Validate output if initialized
46 if(output->total_size() != 0)
47 {
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010048 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding), output->tensor_shape());
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010049 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
50 }
51
52 return Status{};
53}
54
55std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
56{
57 // Output auto inizialitation if not yet initialized
58 auto_init_if_empty(*output, *input);
59
60 // Configure window
61 const unsigned int num_elems_processed_per_iteration = 16 / input->element_size();
62
63 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
64
65 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
66 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
67
68 bool window_changed = update_window_and_padding(win, input_access, output_access);
69
70 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
71 return std::make_pair(err, win);
72}
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010073
74std::pair<Status, Window> validate_and_configure_window_with_padding(ITensorInfo *input, ITensorInfo *output, const PaddingList &padding)
75{
76 TensorShape input_shape = input->tensor_shape();
77 TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input_shape, padding);
78
79 auto_init_if_empty(*output, input->clone()->set_tensor_shape(padded_shape));
80
81 // Configure window
82 const unsigned int num_elems_processed_per_iteration = 16 / input->element_size();
83
84 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
85
86 // Pad on the x dimension accounting for the padding offset along the same dimension
87 AccessWindowHorizontal output_access(output, padding[0].first, num_elems_processed_per_iteration);
88 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
89 bool window_changed = update_window_and_padding(win, input_access, output_access);
90
91 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
92 return std::make_pair(err, win);
93}
94
95/** Generate the string "-DPAD= @p dim @p index @p padding"
96 *
97 * @param[in] dim The dimension index
98 * @param[in] index Can be 0 for the start dimension and 1 for the end dimension
99 * @param[in] padding The value to pad for that index/dimension pair
100 *
101 * @return The correct concatenated string
102 */
103std::string generate_pad_string(const size_t dim, const size_t index, const size_t padding)
104{
105 return "-DPAD" + support::cpp11::to_string(dim) + support::cpp11::to_string(index) + "=" + support::cpp11::to_string(padding);
106}
107
108/** Pass the padding as build option to the kernel.
109 *
110 * @param[in] tensor The padded tensor
111 * @param[in] padding The list of the padding for each dimension
112 * @param[out] build_opts The build option to which adding the padding
113 */
114void add_padding_as_build_options(const PaddingList &padding, CLBuildOptions &build_opts)
115{
116 size_t dim = 0;
117 for(dim = 0; dim < padding.size(); dim++)
118 {
119 build_opts.add_option(generate_pad_string(dim, 0, padding[dim].first));
120 build_opts.add_option(generate_pad_string(dim, 1, padding[dim].second));
121 }
122
123 while(dim < TensorShape::num_max_dimensions)
124 {
125 build_opts.add_option(generate_pad_string(dim, 0, 0));
126 build_opts.add_option(generate_pad_string(dim, 1, 0));
127 dim++;
128 }
129}
130
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100131} // namespace
132
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000133CLCopyKernel::CLCopyKernel()
134 : _input(nullptr), _output(nullptr)
135{
136}
137
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100138void CLCopyKernel::configure(const ICLTensor *input, ICLTensor *output, const PaddingList &padding)
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000139{
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100140 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100141 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), padding));
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000142
143 _input = input;
144 _output = output;
145
146 // Create kernel
147 CLBuildOptions build_opts;
148 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000149
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100150 const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
151 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
152
153 std::pair<Status, Window> win_config;
154
155 if(padding.empty())
156 {
157 // Build kernel
158 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("copy_tensor", build_opts.options()));
159
160 // Configure window
161 win_config = validate_and_configure_window(input->info(), output->info());
162 }
163 else
164 {
165 // Add compile time options
166 add_padding_as_build_options(padding, build_opts);
167
168 // If we are padding in the fourth dimension the kernel needs to know the depth of the
169 // different cubes
170 if(padding.size() == 4)
171 {
172 const size_t depth = input->info()->tensor_shape()[2];
173 build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(depth));
174 }
175
176 // Build kernel
177 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("copy_pad_tensor", build_opts.options()));
178
179 // Configure window
180 win_config = validate_and_configure_window_with_padding(input->info(), output->info(), padding);
181 }
182
183 // Validate and set the window
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100184 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100185 ICLKernel::configure_internal(win_config.second);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100186}
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000187
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100188Status CLCopyKernel::validate(const arm_compute::ITensorInfo *input, const arm_compute::ITensorInfo *output, const PaddingList &padding)
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100189{
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100190 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, padding));
191
192 if(padding.empty())
193 {
194 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
195 }
196 else
197 {
198 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_with_padding(input->clone().get(), output->clone().get(), padding).first);
199 }
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000200
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100201 return Status{};
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000202}
203
204void CLCopyKernel::run(const Window &window, cl::CommandQueue &queue)
205{
206 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
207 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
208
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100209 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
210 Window slice = collapsed.first_slice_window_3D();
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000211
212 do
213 {
214 unsigned int idx = 0;
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100215 add_3D_tensor_argument(idx, _input, slice);
216 add_3D_tensor_argument(idx, _output, slice);
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000217 enqueue(queue, *this, slice);
218 }
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100219 while(collapsed.slide_window_slice_3D(slice));
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000220}
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100221} // namespace arm_compute