Georgios Pinitas | aaa2718 | 2018-11-21 16:32: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. |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32: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/CLSelectKernel.h" |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/CL/ICLTensor.h" |
| 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Utils.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/utils/helpers/AdjustVecSize.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" |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | namespace |
| 42 | { |
| 43 | Status validate_arguments(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output) |
| 44 | { |
Manuel Bottini | 6cca993 | 2020-12-08 12:33:30 +0000 | [diff] [blame] | 45 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(c, x, y, output); |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 46 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(x); |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON(x->data_type() == DataType::UNKNOWN); |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(x, y); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(x, y); |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(c, 1, DataType::U8); |
| 51 | |
| 52 | const bool is_same_rank = (c->tensor_shape().num_dimensions() == x->tensor_shape().num_dimensions()); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON(is_same_rank && (x->tensor_shape() != c->tensor_shape())); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 54 | ARM_COMPUTE_RETURN_ERROR_ON(!is_same_rank && |
| 55 | ((c->tensor_shape().num_dimensions() > 1) || |
| 56 | (c->tensor_shape().x() != x->tensor_shape()[x->tensor_shape().num_dimensions() - 1]))); |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 57 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 58 | if (output->total_size() != 0) |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 59 | { |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(x, output); |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(x, output); |
| 62 | } |
| 63 | |
| 64 | return Status{}; |
| 65 | } |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 66 | } // namespace |
| 67 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 68 | CLSelectKernel::CLSelectKernel() : _c(nullptr), _x(nullptr), _y(nullptr), _output(nullptr), _has_same_rank(false) |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 69 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 70 | _type = CLKernelType::ELEMENTWISE; |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 71 | } |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 72 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 73 | void CLSelectKernel::configure(const CLCompileContext &compile_context, |
| 74 | const ICLTensor *c, |
| 75 | const ICLTensor *x, |
| 76 | const ICLTensor *y, |
| 77 | ICLTensor *output) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 78 | { |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 79 | ARM_COMPUTE_ERROR_ON_NULLPTR(c, x, y, output); |
| 80 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(c->info(), x->info(), y->info(), output->info())); |
| 81 | |
| 82 | _c = c; |
| 83 | _x = x; |
| 84 | _y = y; |
| 85 | _output = output; |
| 86 | _has_same_rank = (c->info()->tensor_shape().num_dimensions() == x->info()->tensor_shape().num_dimensions()); |
| 87 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 88 | auto padding_info = get_padding_info({c, x, y, output}); |
Manuel Bottini | 6cca993 | 2020-12-08 12:33:30 +0000 | [diff] [blame] | 89 | const unsigned int vec_size_x = adjust_vec_size(16 / x->info()->element_size(), x->info()->dimension(0)); |
| 90 | const int vec_size_x_leftovers = output->info()->dimension(0) % vec_size_x; |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 91 | |
| 92 | // Set build options |
| 93 | CLBuildOptions build_opts; |
Manuel Bottini | 6cca993 | 2020-12-08 12:33:30 +0000 | [diff] [blame] | 94 | build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(x->info()->element_size())); |
| 95 | build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x)); |
| 96 | build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_x_leftovers)); |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 97 | |
| 98 | // Create kernel |
| 99 | std::string kernel_name = "select"; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 100 | if (_has_same_rank) |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 101 | { |
| 102 | kernel_name += "_same_rank"; |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | const bool is_input_rank_greater_than_two = x->info()->tensor_shape().num_dimensions() > 2; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 107 | if (is_input_rank_greater_than_two) |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 108 | { |
| 109 | const size_t width = x->info()->tensor_shape().x(); |
| 110 | const size_t height = x->info()->tensor_shape().y(); |
| 111 | const size_t outer_size = x->info()->tensor_shape()[x->info()->tensor_shape().num_dimensions() - 1]; |
| 112 | const size_t depth_size = x->info()->tensor_shape().total_size() / (width * height * outer_size); |
| 113 | build_opts.add_option("-DDEPTH_SIZE=" + support::cpp11::to_string(depth_size)); |
| 114 | } |
| 115 | kernel_name += "_different_rank"; |
| 116 | kernel_name += is_input_rank_greater_than_two ? "_n" : "_2"; |
| 117 | } |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 118 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 119 | |
| 120 | // Configure kernel window |
Manuel Bottini | 6cca993 | 2020-12-08 12:33:30 +0000 | [diff] [blame] | 121 | auto_init_if_empty(*output->info(), *x->info()->clone()); |
| 122 | Window win = calculate_max_window(*x->info(), Steps(vec_size_x)); |
| 123 | ICLKernel::configure_internal(win); |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 124 | |
| 125 | _config_id = "select_"; |
| 126 | _config_id += string_from_data_type(x->info()->data_type()); |
| 127 | _config_id += "_"; |
| 128 | _config_id += support::cpp11::to_string(x->info()->dimension(0)); |
| 129 | _config_id += "_"; |
| 130 | _config_id += support::cpp11::to_string(x->info()->dimension(1)); |
| 131 | _config_id += "_"; |
| 132 | _config_id += support::cpp11::to_string(x->info()->dimension(2)); |
Manuel Bottini | 6cca993 | 2020-12-08 12:33:30 +0000 | [diff] [blame] | 133 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 136 | Status |
| 137 | CLSelectKernel::validate(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output) |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 138 | { |
| 139 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(c, x, y, output)); |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 140 | return Status{}; |
| 141 | } |
| 142 | |
| 143 | void CLSelectKernel::run(const arm_compute::Window &window, cl::CommandQueue &queue) |
| 144 | { |
| 145 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 146 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 147 | |
| 148 | Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 149 | Window slice = collapsed.first_slice_window_3D(); |
| 150 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 151 | if (!_has_same_rank) |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 152 | { |
| 153 | Window vector_slice = window.first_slice_window_1D(); |
| 154 | vector_slice.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 155 | unsigned int idx = 0; |
| 156 | add_1D_tensor_argument(idx, _c, vector_slice); |
| 157 | } |
| 158 | |
| 159 | do |
| 160 | { |
| 161 | unsigned int idx = _has_same_rank ? 0 : num_arguments_per_1D_tensor(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 162 | if (_has_same_rank) |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 163 | { |
| 164 | add_3D_tensor_argument(idx, _c, slice); |
| 165 | } |
| 166 | add_3D_tensor_argument(idx, _x, slice); |
| 167 | add_3D_tensor_argument(idx, _y, slice); |
| 168 | add_3D_tensor_argument(idx, _output, slice); |
| 169 | |
| 170 | enqueue(queue, *this, slice, lws_hint()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 171 | } while (collapsed.slide_window_slice_3D(slice)); |
Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [diff] [blame] | 172 | } |
| 173 | } // namespace arm_compute |