Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 1 | /* |
Viet-Hoa Do | 6829e02 | 2024-01-16 16:23:24 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2018-2021, 2023-2024 Arm Limited. |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +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/CLGatherKernel.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 25 | |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/CL/ICLTensor.h" |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 28 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 29 | #include "src/core/helpers/AutoConfiguration.h" |
| 30 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 31 | #include "support/StringSupport.h" |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 32 | |
| 33 | #include <string> |
| 34 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | namespace |
| 38 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 39 | inline Status |
| 40 | validate_arguments(const ITensorInfo *input, const ITensorInfo *indices, const ITensorInfo *output, int axis) |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 41 | { |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 42 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, indices, output); |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 43 | const uint32_t actual_axis = wrap_around(axis, static_cast<int>(input->num_dimensions())); |
Omar Al Khatib | cdd1e03 | 2023-04-26 11:31:45 +0100 | [diff] [blame] | 44 | ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() + indices->num_dimensions() - 1) > 4); |
| 45 | |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 46 | ARM_COMPUTE_RETURN_ERROR_ON(actual_axis >= input->num_dimensions()); |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 48 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 49 | if (output->total_size() != 0) |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 50 | { |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 53 | TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape( |
| 54 | input->tensor_shape(), indices->tensor_shape(), actual_axis); |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 55 | ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size()); |
| 56 | } |
| 57 | |
Manuel Bottini | 7ad6257 | 2019-01-16 11:18:15 +0000 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32, DataType::S32); |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 59 | |
| 60 | return Status{}; |
| 61 | } |
| 62 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 63 | std::pair<Status, Window> |
| 64 | validate_and_configure_window(ITensorInfo *input, ITensorInfo *indices, ITensorInfo *output, int axis) |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 65 | { |
| 66 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, indices); |
| 67 | const uint32_t actual_axis = wrap_around(axis, static_cast<int>(input->num_dimensions())); |
| 68 | // Output auto initialization if not yet initialized |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 69 | TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape( |
| 70 | input->tensor_shape(), indices->tensor_shape(), actual_axis); |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 71 | auto_init_if_empty((*output), output_shape, 1, input->data_type()); |
| 72 | |
| 73 | // Create window |
| 74 | Window win = calculate_max_window(*output, Steps()); |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 75 | |
| 76 | return std::make_pair(Status{}, win); |
| 77 | } |
| 78 | |
| 79 | } // namespace |
| 80 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 81 | CLGatherKernel::CLGatherKernel() : _input(nullptr), _indices(nullptr), _output(nullptr), _axis(0) |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 82 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 83 | _type = CLKernelType::ELEMENTWISE; |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void CLGatherKernel::configure(const ICLTensor *input, const ICLTensor *indices, ICLTensor *output, int axis) |
| 87 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 88 | configure(CLKernelLibrary::get().get_compile_context(), input, indices, output, axis); |
| 89 | } |
| 90 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 91 | void CLGatherKernel::configure(const CLCompileContext &compile_context, |
| 92 | const ICLTensor *input, |
| 93 | const ICLTensor *indices, |
| 94 | ICLTensor *output, |
| 95 | int axis) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 96 | { |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 97 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, indices); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 98 | auto padding_info = get_padding_info({input, output, indices}); |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 99 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), indices->info(), output->info(), axis)); |
| 100 | |
| 101 | // Configure kernel window |
| 102 | auto win_config = validate_and_configure_window(input->info(), indices->info(), output->info(), axis); |
| 103 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 104 | |
| 105 | _input = input; |
| 106 | _output = output; |
| 107 | _indices = indices; |
| 108 | _axis = wrap_around(axis, static_cast<int>(input->info()->num_dimensions())); |
| 109 | |
| 110 | // Set build options |
| 111 | CLBuildOptions build_opts; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 112 | build_opts.add_option("-DDATA_TYPE=" + |
| 113 | get_cl_unsigned_type_from_element_size(data_size_from_type(input->info()->data_type()))); |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 114 | build_opts.add_option("-DOUTPUT_DIM_Z=" + support::cpp11::to_string(output->info()->dimension(2))); |
Omar Al Khatib | cdd1e03 | 2023-04-26 11:31:45 +0100 | [diff] [blame] | 115 | build_opts.add_option("-DINDICES_DIMS=" + support::cpp11::to_string(indices->info()->num_dimensions())); |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 116 | build_opts.add_option("-DAXIS=" + support::cpp11::to_string(_axis)); |
Viet-Hoa Do | a25582c | 2023-03-15 16:52:05 +0000 | [diff] [blame] | 117 | build_opts.add_option("-DINDEX_LIMIT=" + support::cpp11::to_string(input->info()->tensor_shape()[_axis])); |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 118 | |
| 119 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 120 | _kernel = create_kernel(compile_context, "gather", build_opts.options()); |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 121 | ICLKernel::configure_internal(win_config.second); |
Manuel Bottini | b6869dd | 2020-12-16 15:34:25 +0000 | [diff] [blame] | 122 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 125 | Status |
| 126 | CLGatherKernel::validate(const ITensorInfo *input, const ITensorInfo *indices, const ITensorInfo *output, int axis) |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 127 | { |
| 128 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, indices, output, axis)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 129 | ARM_COMPUTE_RETURN_ON_ERROR( |
| 130 | validate_and_configure_window(input->clone().get(), indices->clone().get(), output->clone().get(), axis).first); |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 131 | return Status{}; |
| 132 | } |
| 133 | |
| 134 | void CLGatherKernel::run(const Window &window, cl::CommandQueue &queue) |
| 135 | { |
| 136 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 137 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 138 | |
| 139 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 140 | unsigned int idx = 0; |
| 141 | add_4D_tensor_argument(idx, _input, window_collapsed); |
Omar Al Khatib | cdd1e03 | 2023-04-26 11:31:45 +0100 | [diff] [blame] | 142 | add_4D_tensor_argument(idx, _indices, window_collapsed); |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 143 | add_4D_tensor_argument(idx, _output, window_collapsed); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 144 | enqueue(queue, *this, window_collapsed, lws_hint()); |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 145 | } |
| 146 | } // namespace arm_compute |