blob: c283c440a3a2014501de7e88b132afcd0141b16a [file] [log] [blame]
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +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/CLStackLayerKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/CLValidate.h"
29#include "arm_compute/core/CL/ICLTensor.h"
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000030#include "arm_compute/core/Helpers.h"
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000031#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Utils.h"
33#include "arm_compute/core/Window.h"
34#include "arm_compute/core/utils/misc/ShapeCalculator.h"
35
Matthew Bentham758b5ba2020-03-05 23:37:48 +000036#include "support/StringSupport.h"
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000037
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000038using namespace arm_compute::misc::shape_calculator;
39
Manuel Bottini8481d832019-12-10 15:28:40 +000040namespace arm_compute
41{
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000042namespace
43{
44Status validate_arguments(const ITensorInfo *input, unsigned int axis, unsigned int idx_input, unsigned int num_tensors, const ITensorInfo *output)
45{
46 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
47 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Manuel Bottini8481d832019-12-10 15:28:40 +000048 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000049 ARM_COMPUTE_RETURN_ERROR_ON(idx_input >= num_tensors);
50 ARM_COMPUTE_RETURN_ERROR_ON(axis > input->num_dimensions());
51 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
52
53 if(output->total_size() != 0)
54 {
55 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_stack_shape(*input, axis, num_tensors));
56 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000057 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000058 }
59
60 return Status{};
61}
62
63std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, unsigned int axis, unsigned int num_tensors, ITensorInfo *output)
64{
65 // Output auto inizialitation if not yet initialized
66 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_stack_shape(*input, axis, num_tensors)));
67
68 // Configure kernel window
Isabella Gottardiee7c15d2018-12-17 16:15:34 +000069 Window win = calculate_max_window(*input);
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000070
Isabella Gottardiee7c15d2018-12-17 16:15:34 +000071 return std::make_pair(Status{}, win);
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000072}
73} // namespace
74
75CLStackLayerKernel::CLStackLayerKernel()
76 : _input(nullptr), _output(nullptr)
77{
78}
79
80void CLStackLayerKernel::configure(const ICLTensor *input, unsigned int axis, unsigned int idx_input, unsigned int num_tensors, ICLTensor *output)
81{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010082 configure(CLKernelLibrary::get().get_compile_context(), input, axis, idx_input, num_tensors, output);
83}
84
Manuel Bottini2803f702020-04-21 16:20:03 +010085void CLStackLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, unsigned int axis, unsigned int idx_input, unsigned int num_tensors, ICLTensor *output)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010086{
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000087 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
88 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), axis, idx_input, num_tensors, output->info()));
89
90 _input = input;
91 _output = output;
92
93 // Configure kernel window
94 auto win_config = validate_and_configure_window(input->info(), axis, num_tensors, output->info());
95
96 // Add build options
97 CLBuildOptions build_opts;
Michele Di Giorgiof6f78762020-07-06 11:27:21 +010098 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000099 build_opts.add_option("-DAXIS=" + support::cpp11::to_string(axis));
100 build_opts.add_option("-DSRC_DIM2=" + support::cpp11::to_string(input->info()->dimension(2)));
101 build_opts.add_option("-DDST_DIM3=" + support::cpp11::to_string(output->info()->dimension(3)));
102
103 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100104 _kernel = create_kernel(compile_context, "stack_layer", build_opts.options());
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +0000105
106 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
107 ICLKernel::configure_internal(win_config.second);
108
109 const unsigned int idx = 2 * num_arguments_per_4D_tensor();
110 _kernel.setArg<cl_uint>(idx, idx_input);
111}
112
113Status CLStackLayerKernel::validate(const ITensorInfo *input, unsigned int axis, unsigned int idx_input, unsigned int num_tensors, const ITensorInfo *output)
114{
115 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, axis, idx_input, num_tensors, output));
116 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), axis, num_tensors, output->clone().get()).first);
117 return Status{};
118}
119
120void CLStackLayerKernel::run(const Window &window, cl::CommandQueue &queue)
121{
122 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
123 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
124
125 Window window_out;
126 window_out.use_tensor_dimensions(_output->info()->tensor_shape());
127
128 Window collapsed = window.collapse(ICLKernel::window(), Window::DimZ);
129
130 Window slice_in = collapsed.first_slice_window_4D();
131 Window slice_out = window_out.first_slice_window_4D();
132
133 unsigned int idx = 0;
134 add_4D_tensor_argument(idx, _input, slice_in);
135 add_4D_tensor_argument(idx, _output, slice_out);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100136 enqueue(queue, *this, slice_in, lws_hint());
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +0000137}
Matthew Bentham758b5ba2020-03-05 23:37:48 +0000138} // namespace arm_compute