Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 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 | */ |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 24 | #include "src/core/CL/kernels/CLStackLayerKernel.h" |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/CL/ICLTensor.h" |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/Helpers.h" |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Utils.h" |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 33 | #include "src/core/CL/CLValidate.h" |
| 34 | #include "src/core/helpers/AutoConfiguration.h" |
| 35 | #include "src/core/helpers/WindowHelpers.h" |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 36 | |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 37 | #include "support/StringSupport.h" |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 38 | |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 39 | using namespace arm_compute::misc::shape_calculator; |
| 40 | |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 41 | namespace arm_compute |
| 42 | { |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 43 | namespace |
| 44 | { |
| 45 | Status validate_arguments(const ITensorInfo *input, unsigned int axis, unsigned int idx_input, unsigned int num_tensors, const ITensorInfo *output) |
| 46 | { |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON(idx_input >= num_tensors); |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON(axis > input->num_dimensions()); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4); |
| 53 | |
| 54 | if(output->total_size() != 0) |
| 55 | { |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_stack_shape(*input, axis, num_tensors)); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | return Status{}; |
| 62 | } |
| 63 | |
| 64 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, unsigned int axis, unsigned int num_tensors, ITensorInfo *output) |
| 65 | { |
| 66 | // Output auto inizialitation if not yet initialized |
| 67 | auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_stack_shape(*input, axis, num_tensors))); |
| 68 | |
| 69 | // Configure kernel window |
Isabella Gottardi | ee7c15d | 2018-12-17 16:15:34 +0000 | [diff] [blame] | 70 | Window win = calculate_max_window(*input); |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 71 | |
Isabella Gottardi | ee7c15d | 2018-12-17 16:15:34 +0000 | [diff] [blame] | 72 | return std::make_pair(Status{}, win); |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 73 | } |
| 74 | } // namespace |
| 75 | |
| 76 | CLStackLayerKernel::CLStackLayerKernel() |
| 77 | : _input(nullptr), _output(nullptr) |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | void CLStackLayerKernel::configure(const ICLTensor *input, unsigned int axis, unsigned int idx_input, unsigned int num_tensors, ICLTensor *output) |
| 82 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 83 | configure(CLKernelLibrary::get().get_compile_context(), input, axis, idx_input, num_tensors, output); |
| 84 | } |
| 85 | |
Manuel Bottini | 2803f70 | 2020-04-21 16:20:03 +0100 | [diff] [blame] | 86 | void CLStackLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, unsigned int axis, unsigned int idx_input, unsigned int num_tensors, ICLTensor *output) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 87 | { |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 88 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 89 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), axis, idx_input, num_tensors, output->info())); |
| 90 | |
| 91 | _input = input; |
| 92 | _output = output; |
| 93 | |
| 94 | // Configure kernel window |
| 95 | auto win_config = validate_and_configure_window(input->info(), axis, num_tensors, output->info()); |
| 96 | |
| 97 | // Add build options |
| 98 | CLBuildOptions build_opts; |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 99 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 100 | build_opts.add_option("-DAXIS=" + support::cpp11::to_string(axis)); |
| 101 | build_opts.add_option("-DSRC_DIM2=" + support::cpp11::to_string(input->info()->dimension(2))); |
| 102 | build_opts.add_option("-DDST_DIM3=" + support::cpp11::to_string(output->info()->dimension(3))); |
| 103 | |
| 104 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 105 | _kernel = create_kernel(compile_context, "stack_layer", build_opts.options()); |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 106 | |
| 107 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 108 | ICLKernel::configure_internal(win_config.second); |
| 109 | |
| 110 | const unsigned int idx = 2 * num_arguments_per_4D_tensor(); |
| 111 | _kernel.setArg<cl_uint>(idx, idx_input); |
| 112 | } |
| 113 | |
| 114 | Status CLStackLayerKernel::validate(const ITensorInfo *input, unsigned int axis, unsigned int idx_input, unsigned int num_tensors, const ITensorInfo *output) |
| 115 | { |
| 116 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, axis, idx_input, num_tensors, output)); |
| 117 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), axis, num_tensors, output->clone().get()).first); |
| 118 | return Status{}; |
| 119 | } |
| 120 | |
| 121 | void CLStackLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 122 | { |
| 123 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 124 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 125 | |
| 126 | Window window_out; |
| 127 | window_out.use_tensor_dimensions(_output->info()->tensor_shape()); |
| 128 | |
| 129 | Window collapsed = window.collapse(ICLKernel::window(), Window::DimZ); |
| 130 | |
| 131 | Window slice_in = collapsed.first_slice_window_4D(); |
| 132 | Window slice_out = window_out.first_slice_window_4D(); |
| 133 | |
| 134 | unsigned int idx = 0; |
| 135 | add_4D_tensor_argument(idx, _input, slice_in); |
| 136 | add_4D_tensor_argument(idx, _output, slice_out); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 137 | enqueue(queue, *this, slice_in, lws_hint()); |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 138 | } |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 139 | } // namespace arm_compute |