Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 1 | /* |
Isabella Gottardi | ee7c15d | 2018-12-17 16:15:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 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 | */ |
| 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" |
| 30 | #include "arm_compute/core/CL/OpenCL.h" |
| 31 | #include "arm_compute/core/Error.h" |
| 32 | #include "arm_compute/core/Helpers.h" |
| 33 | #include "arm_compute/core/IAccessWindow.h" |
| 34 | #include "arm_compute/core/TensorInfo.h" |
| 35 | #include "arm_compute/core/Utils.h" |
| 36 | #include "arm_compute/core/Window.h" |
| 37 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 38 | |
| 39 | #include "support/ToolchainSupport.h" |
| 40 | |
| 41 | using namespace arm_compute; |
| 42 | using namespace arm_compute::misc::shape_calculator; |
| 43 | |
| 44 | namespace |
| 45 | { |
| 46 | Status validate_arguments(const ITensorInfo *input, unsigned int axis, unsigned int idx_input, unsigned int num_tensors, const ITensorInfo *output) |
| 47 | { |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::U8, DataType::S8, |
| 51 | DataType::U16, DataType::S16, DataType::U32, DataType::S32, |
| 52 | DataType::F16, DataType::F32); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON(idx_input >= num_tensors); |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON(axis > input->num_dimensions()); |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4); |
| 56 | |
| 57 | if(output->total_size() != 0) |
| 58 | { |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_stack_shape(*input, axis, num_tensors)); |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 61 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | return Status{}; |
| 65 | } |
| 66 | |
| 67 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, unsigned int axis, unsigned int num_tensors, ITensorInfo *output) |
| 68 | { |
| 69 | // Output auto inizialitation if not yet initialized |
| 70 | auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_stack_shape(*input, axis, num_tensors))); |
| 71 | |
| 72 | // Configure kernel window |
Isabella Gottardi | ee7c15d | 2018-12-17 16:15:34 +0000 | [diff] [blame] | 73 | Window win = calculate_max_window(*input); |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 74 | |
Isabella Gottardi | ee7c15d | 2018-12-17 16:15:34 +0000 | [diff] [blame] | 75 | return std::make_pair(Status{}, win); |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 76 | } |
| 77 | } // namespace |
| 78 | |
| 79 | CLStackLayerKernel::CLStackLayerKernel() |
| 80 | : _input(nullptr), _output(nullptr) |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | void CLStackLayerKernel::configure(const ICLTensor *input, unsigned int axis, unsigned int idx_input, unsigned int num_tensors, ICLTensor *output) |
| 85 | { |
| 86 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 87 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), axis, idx_input, num_tensors, output->info())); |
| 88 | |
| 89 | _input = input; |
| 90 | _output = output; |
| 91 | |
| 92 | // Configure kernel window |
| 93 | auto win_config = validate_and_configure_window(input->info(), axis, num_tensors, output->info()); |
| 94 | |
| 95 | // Add build options |
| 96 | CLBuildOptions build_opts; |
| 97 | build_opts.add_option("-DDATA_TYPE=" + get_underlying_cl_type_from_data_type(input->info()->data_type())); |
| 98 | build_opts.add_option("-DAXIS=" + support::cpp11::to_string(axis)); |
| 99 | build_opts.add_option("-DSRC_DIM2=" + support::cpp11::to_string(input->info()->dimension(2))); |
| 100 | build_opts.add_option("-DDST_DIM3=" + support::cpp11::to_string(output->info()->dimension(3))); |
| 101 | |
| 102 | // Create kernel |
| 103 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("stack_layer", build_opts.options())); |
| 104 | |
| 105 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 106 | ICLKernel::configure_internal(win_config.second); |
| 107 | |
| 108 | const unsigned int idx = 2 * num_arguments_per_4D_tensor(); |
| 109 | _kernel.setArg<cl_uint>(idx, idx_input); |
| 110 | } |
| 111 | |
| 112 | Status CLStackLayerKernel::validate(const ITensorInfo *input, unsigned int axis, unsigned int idx_input, unsigned int num_tensors, const ITensorInfo *output) |
| 113 | { |
| 114 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, axis, idx_input, num_tensors, output)); |
| 115 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), axis, num_tensors, output->clone().get()).first); |
| 116 | return Status{}; |
| 117 | } |
| 118 | |
| 119 | void CLStackLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 120 | { |
| 121 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 122 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 123 | |
| 124 | Window window_out; |
| 125 | window_out.use_tensor_dimensions(_output->info()->tensor_shape()); |
| 126 | |
| 127 | Window collapsed = window.collapse(ICLKernel::window(), Window::DimZ); |
| 128 | |
| 129 | Window slice_in = collapsed.first_slice_window_4D(); |
| 130 | Window slice_out = window_out.first_slice_window_4D(); |
| 131 | |
| 132 | unsigned int idx = 0; |
| 133 | add_4D_tensor_argument(idx, _input, slice_in); |
| 134 | add_4D_tensor_argument(idx, _output, slice_out); |
| 135 | enqueue(queue, *this, slice_in); |
| 136 | } |