Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, 2023 Arm Limited. |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [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/CLReorgLayerKernel.h" |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 28 | #include "arm_compute/core/CL/ICLTensor.h" |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/utils/StringUtils.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 33 | #include "arm_compute/core/Validate.h" |
| 34 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 35 | #include "src/core/helpers/AutoConfiguration.h" |
| 36 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 37 | #include "support/StringSupport.h" |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 38 | |
| 39 | #include <string> |
| 40 | |
| 41 | namespace arm_compute |
| 42 | { |
| 43 | namespace |
| 44 | { |
| 45 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, int32_t stride) |
| 46 | { |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN); |
| 50 | |
| 51 | const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH); |
| 52 | const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT); |
| 53 | |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON(stride <= 0); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 55 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((input->tensor_shape()[idx_width] % stride) != 0, |
| 56 | "The width of the input tensor must be a multiple of stride"); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((input->tensor_shape()[idx_height] % stride) != 0, |
| 58 | "The height of the input tensor must be a multiple of stride"); |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 59 | |
| 60 | // Validate output if initialized |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 61 | if (output->total_size() != 0) |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 62 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 63 | const TensorInfo tensor_info_output = |
| 64 | output->clone()->set_tensor_shape(misc::shape_calculator::compute_reorg_output_shape(*input, stride)); |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 65 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output); |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 67 | } |
| 68 | |
| 69 | return Status{}; |
| 70 | } |
| 71 | } // namespace |
| 72 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 73 | CLReorgLayerKernel::CLReorgLayerKernel() : _input(nullptr), _output(nullptr) |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 74 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 75 | _type = CLKernelType::ELEMENTWISE; |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void CLReorgLayerKernel::configure(const ICLTensor *input, ICLTensor *output, int32_t stride) |
| 79 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 80 | configure(CLKernelLibrary::get().get_compile_context(), input, output, stride); |
| 81 | } |
| 82 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 83 | void CLReorgLayerKernel::configure(const CLCompileContext &compile_context, |
| 84 | const ICLTensor *input, |
| 85 | ICLTensor *output, |
| 86 | int32_t stride) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 87 | { |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 88 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 89 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), stride)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 90 | auto padding_info = get_padding_info({input, output}); |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 91 | |
| 92 | _input = input; |
| 93 | _output = output; |
| 94 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 95 | std::string kernel_name = |
| 96 | std::string("reorg_layer_") + lower_string(string_from_data_layout(input->info()->data_layout())); |
| 97 | const size_t idx_channel = |
| 98 | get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL); |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 99 | |
| 100 | // Create kernel |
| 101 | CLBuildOptions build_opts; |
| 102 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 103 | build_opts.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(input->info()->dimension(idx_channel))); |
| 104 | build_opts.add_option("-DSTRIDE=" + support::cpp11::to_string(stride)); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 105 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 106 | |
| 107 | // Configure window |
| 108 | // auto inizialize the output tensor if not yet initialized |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 109 | auto_init_if_empty(*output->info(), |
| 110 | input->info()->clone()->set_tensor_shape( |
| 111 | misc::shape_calculator::compute_reorg_output_shape(*input->info(), stride))); |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 112 | |
| 113 | Window win = calculate_max_window(*output->info(), Steps()); |
| 114 | |
| 115 | // The CLWeightsReshapeKernel doesn't need padding so update_window_and_padding() can be skipped |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 116 | ICLKernel::configure_internal(win); |
| 117 | |
| 118 | _config_id = kernel_name; |
| 119 | _config_id += "_"; |
| 120 | _config_id += string_from_data_type(input->info()->data_type()); |
| 121 | _config_id += "_"; |
| 122 | _config_id += support::cpp11::to_string(input->info()->dimension(0)); |
| 123 | _config_id += "_"; |
| 124 | _config_id += support::cpp11::to_string(input->info()->dimension(1)); |
| 125 | _config_id += "_"; |
| 126 | _config_id += support::cpp11::to_string(input->info()->dimension(2)); |
| 127 | _config_id += "_"; |
| 128 | _config_id += support::cpp11::to_string(stride); |
Manuel Bottini | b6869dd | 2020-12-16 15:34:25 +0000 | [diff] [blame] | 129 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 132 | Status CLReorgLayerKernel::validate(const arm_compute::ITensorInfo *input, |
| 133 | const arm_compute::ITensorInfo *output, |
| 134 | int32_t stride) |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 135 | { |
| 136 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, stride)); |
| 137 | |
| 138 | return Status{}; |
| 139 | } |
| 140 | |
| 141 | void CLReorgLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 142 | { |
| 143 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 144 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 145 | |
| 146 | Window slice = window.first_slice_window_3D(); |
| 147 | |
| 148 | do |
| 149 | { |
| 150 | unsigned int idx = 0; |
| 151 | add_3D_tensor_argument(idx, _input, slice); |
| 152 | add_3D_tensor_argument(idx, _output, slice); |
| 153 | enqueue(queue, *this, slice, lws_hint()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 154 | } while (window.slide_window_slice_3D(slice)); |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 155 | } |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 156 | } // namespace arm_compute |