blob: 23e26716e7e6655767726be57192d2c839d7f458 [file] [log] [blame]
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +00001/*
Giorgio Arena4a95bba2021-06-28 11:00:27 +01002 * Copyright (c) 2018-2021 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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLStackLayerKernel.h"
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000025
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000028#include "arm_compute/core/CL/ICLTensor.h"
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000029#include "arm_compute/core/Helpers.h"
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000030#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Utils.h"
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000032#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010033
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/CL/CLValidate.h"
35#include "src/core/helpers/AutoConfiguration.h"
36#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000037#include "support/StringSupport.h"
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000038
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000039using namespace arm_compute::misc::shape_calculator;
40
Manuel Bottini8481d832019-12-10 15:28:40 +000041namespace arm_compute
42{
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000043namespace
44{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010045Status validate_arguments(const ITensorInfo *input,
46 unsigned int axis,
47 unsigned int idx_input,
48 unsigned int num_tensors,
49 const ITensorInfo *output)
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000050{
51 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
52 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Manuel Bottini8481d832019-12-10 15:28:40 +000053 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000054 ARM_COMPUTE_RETURN_ERROR_ON(idx_input >= num_tensors);
55 ARM_COMPUTE_RETURN_ERROR_ON(axis > input->num_dimensions());
56 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
57
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010058 if (output->total_size() != 0)
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000059 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010060 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(),
61 compute_stack_shape(*input, axis, num_tensors));
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000062 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000063 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000064 }
65
66 return Status{};
67}
68
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010069std::pair<Status, Window>
70validate_and_configure_window(ITensorInfo *input, unsigned int axis, unsigned int num_tensors, ITensorInfo *output)
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000071{
72 // Output auto inizialitation if not yet initialized
73 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_stack_shape(*input, axis, num_tensors)));
74
75 // Configure kernel window
Isabella Gottardiee7c15d2018-12-17 16:15:34 +000076 Window win = calculate_max_window(*input);
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000077
Isabella Gottardiee7c15d2018-12-17 16:15:34 +000078 return std::make_pair(Status{}, win);
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000079}
80} // namespace
81
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010082CLStackLayerKernel::CLStackLayerKernel() : _input(nullptr), _output(nullptr)
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000083{
Giorgio Arena4a95bba2021-06-28 11:00:27 +010084 _type = CLKernelType::ELEMENTWISE;
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000085}
86
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010087void CLStackLayerKernel::configure(
88 const ICLTensor *input, unsigned int axis, unsigned int idx_input, unsigned int num_tensors, ICLTensor *output)
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000089{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010090 configure(CLKernelLibrary::get().get_compile_context(), input, axis, idx_input, num_tensors, output);
91}
92
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010093void CLStackLayerKernel::configure(const CLCompileContext &compile_context,
94 const ICLTensor *input,
95 unsigned int axis,
96 unsigned int idx_input,
97 unsigned int num_tensors,
98 ICLTensor *output)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010099{
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +0000100 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
101 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), axis, idx_input, num_tensors, output->info()));
102
103 _input = input;
104 _output = output;
105
106 // Configure kernel window
107 auto win_config = validate_and_configure_window(input->info(), axis, num_tensors, output->info());
108
109 // Add build options
110 CLBuildOptions build_opts;
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100111 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +0000112 build_opts.add_option("-DAXIS=" + support::cpp11::to_string(axis));
113 build_opts.add_option("-DSRC_DIM2=" + support::cpp11::to_string(input->info()->dimension(2)));
114 build_opts.add_option("-DDST_DIM3=" + support::cpp11::to_string(output->info()->dimension(3)));
115
116 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100117 _kernel = create_kernel(compile_context, "stack_layer", build_opts.options());
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +0000118
119 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
120 ICLKernel::configure_internal(win_config.second);
121
122 const unsigned int idx = 2 * num_arguments_per_4D_tensor();
123 _kernel.setArg<cl_uint>(idx, idx_input);
124}
125
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100126Status CLStackLayerKernel::validate(const ITensorInfo *input,
127 unsigned int axis,
128 unsigned int idx_input,
129 unsigned int num_tensors,
130 const ITensorInfo *output)
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +0000131{
132 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, axis, idx_input, num_tensors, output));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100133 ARM_COMPUTE_RETURN_ON_ERROR(
134 validate_and_configure_window(input->clone().get(), axis, num_tensors, output->clone().get()).first);
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +0000135 return Status{};
136}
137
138void CLStackLayerKernel::run(const Window &window, cl::CommandQueue &queue)
139{
140 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
141 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
142
143 Window window_out;
144 window_out.use_tensor_dimensions(_output->info()->tensor_shape());
145
146 Window collapsed = window.collapse(ICLKernel::window(), Window::DimZ);
147
148 Window slice_in = collapsed.first_slice_window_4D();
149 Window slice_out = window_out.first_slice_window_4D();
150
151 unsigned int idx = 0;
152 add_4D_tensor_argument(idx, _input, slice_in);
153 add_4D_tensor_argument(idx, _output, slice_out);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100154 enqueue(queue, *this, slice_in, lws_hint());
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +0000155}
Matthew Bentham758b5ba2020-03-05 23:37:48 +0000156} // namespace arm_compute