Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 1 | /* |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, 2023 Arm Limited. |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +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/CLReverseKernel.h" |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/CL/ICLTensor.h" |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/Utils.h" |
| 32 | #include "arm_compute/core/utils/StringUtils.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 33 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 34 | #include "src/core/CL/CLValidate.h" |
| 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" |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | namespace |
| 42 | { |
Adnan AlSinan | 704c22f | 2023-10-24 11:05:56 +0100 | [diff] [blame] | 43 | Status |
| 44 | validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *axis, bool use_inverted_axis) |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 45 | { |
Adnan AlSinan | 704c22f | 2023-10-24 11:05:56 +0100 | [diff] [blame] | 46 | ARM_COMPUTE_UNUSED(use_inverted_axis); |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 47 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, axis); |
| 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); |
Adnan AlSinan | 704c22f | 2023-10-24 11:05:56 +0100 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(axis, 1, DataType::U32, DataType::S32); |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis->num_dimensions() > 1, "Axis must be a 1D tensor"); |
Adnan AlSinan | 704c22f | 2023-10-24 11:05:56 +0100 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 4, |
| 53 | "Current implementation only supports up to 4 dimensions."); |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 54 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis->dimension(0) > 4, "Only up to 4 dimensions can be reversed"); |
| 55 | |
| 56 | // Checks performed when output is configured |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 57 | if (output->total_size() != 0) |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 58 | { |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 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); |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | return Status{}; |
| 65 | } |
| 66 | } // namespace |
| 67 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 68 | CLReverseKernel::CLReverseKernel() : _input(nullptr), _output(nullptr), _axis(nullptr) |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 69 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 70 | _type = CLKernelType::ELEMENTWISE; |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Adnan AlSinan | 704c22f | 2023-10-24 11:05:56 +0100 | [diff] [blame] | 73 | void CLReverseKernel::configure(const ICLTensor *input, |
| 74 | ICLTensor *output, |
| 75 | const ICLTensor *axis, |
| 76 | bool use_inverted_axis) |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 77 | { |
Adnan AlSinan | 704c22f | 2023-10-24 11:05:56 +0100 | [diff] [blame] | 78 | configure(CLKernelLibrary::get().get_compile_context(), input, output, axis, use_inverted_axis); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 81 | void CLReverseKernel::configure(const CLCompileContext &compile_context, |
| 82 | const ICLTensor *input, |
| 83 | ICLTensor *output, |
Adnan AlSinan | 704c22f | 2023-10-24 11:05:56 +0100 | [diff] [blame] | 84 | const ICLTensor *axis, |
| 85 | bool use_inverted_axis) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 86 | { |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 87 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, axis); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 88 | auto padding_info = get_padding_info({input, output, axis}); |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 89 | |
| 90 | _input = input; |
| 91 | _output = output; |
| 92 | _axis = axis; |
| 93 | |
| 94 | // Output tensor auto initialization if not yet initialized |
| 95 | auto_init_if_empty(*output->info(), *input->info()->clone()); |
| 96 | |
Adnan AlSinan | 704c22f | 2023-10-24 11:05:56 +0100 | [diff] [blame] | 97 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), axis->info(), use_inverted_axis)); |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 98 | |
| 99 | // Set kernel build options |
| 100 | CLBuildOptions build_opts; |
| 101 | build_opts.add_option("-DNUM_REVERSE_DIMS=" + support::cpp11::to_string(axis->info()->dimension(0))); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 102 | build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(input->info()->element_size())); |
Adnan AlSinan | 704c22f | 2023-10-24 11:05:56 +0100 | [diff] [blame] | 103 | build_opts.add_option("-DRANK=" + support::cpp11::to_string(input->info()->num_dimensions())); |
| 104 | build_opts.add_option_if(use_inverted_axis, "-DUSE_INVERTED_AXIS"); |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 105 | |
| 106 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 107 | _kernel = create_kernel(compile_context, "reverse", build_opts.options()); |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 108 | |
| 109 | // Set static kernel arguments |
| 110 | unsigned int idx = 2 * num_arguments_per_4D_tensor() + num_arguments_per_1D_tensor(); |
| 111 | add_argument<cl_uint>(idx, input->info()->dimension(0)); |
| 112 | add_argument<cl_uint>(idx, input->info()->dimension(1)); |
| 113 | add_argument<cl_uint>(idx, input->info()->dimension(2)); |
| 114 | add_argument<cl_uint>(idx, input->info()->dimension(3)); |
| 115 | |
| 116 | // Configure kernel window |
| 117 | Window win = calculate_max_window(*output->info(), Steps()); |
| 118 | ICLKernel::configure_internal(win); |
| 119 | |
| 120 | // Set config_id for enabling LWS tuning |
| 121 | _config_id += "reverse_"; |
| 122 | _config_id += lower_string(string_from_data_type(input->info()->data_type())); |
| 123 | _config_id += "_"; |
| 124 | _config_id += support::cpp11::to_string(input->info()->dimension(0)); |
| 125 | _config_id += "_"; |
| 126 | _config_id += support::cpp11::to_string(input->info()->dimension(1)); |
| 127 | _config_id += "_"; |
| 128 | _config_id += support::cpp11::to_string(input->info()->dimension(2)); |
Manuel Bottini | b6869dd | 2020-12-16 15:34:25 +0000 | [diff] [blame] | 129 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Adnan AlSinan | 704c22f | 2023-10-24 11:05:56 +0100 | [diff] [blame] | 132 | Status CLReverseKernel::validate(const ITensorInfo *input, |
| 133 | const ITensorInfo *output, |
| 134 | const ITensorInfo *axis, |
| 135 | bool use_inverted_axis) |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 136 | { |
Adnan AlSinan | 704c22f | 2023-10-24 11:05:56 +0100 | [diff] [blame] | 137 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis, use_inverted_axis)); |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 138 | return Status{}; |
| 139 | } |
| 140 | |
| 141 | void CLReverseKernel::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 collapsed = window.collapse(ICLKernel::window(), Window::DimZ); |
| 147 | Window slice = collapsed.first_slice_window_4D(); |
| 148 | Window axis_slice = collapsed.first_slice_window_1D(); |
| 149 | |
| 150 | do |
| 151 | { |
| 152 | unsigned int idx = 0; |
| 153 | add_4D_tensor_argument(idx, _input, slice); |
| 154 | add_1D_tensor_argument(idx, _axis, axis_slice); |
| 155 | add_4D_tensor_argument(idx, _output, slice); |
| 156 | enqueue(queue, *this, slice, lws_hint()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 157 | } while (collapsed.slide_window_slice_4D(slice)); |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 158 | } |
| 159 | } // namespace arm_compute |