Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 1 | /* |
Omar Al Khatib | a231832 | 2023-01-04 15:43:17 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, 2023 Arm Limited. |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +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 | */ |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 24 | #include "src/core/NEON/kernels/NEStridedSliceKernel.h" |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 25 | |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/ITensor.h" |
| 27 | #include "arm_compute/core/TensorInfo.h" |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/Types.h" |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/utils/helpers/tensor_transform.h" |
| 30 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/Window.h" |
| 32 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 33 | #include "src/core/CPP/Validate.h" |
| 34 | #include "src/core/helpers/AutoConfiguration.h" |
| 35 | #include "src/core/helpers/WindowHelpers.h" |
| 36 | #include "src/core/utils/helpers/bit_ops.h" |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace |
| 41 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 42 | Status validate_arguments(const ITensorInfo *input, |
| 43 | const ITensorInfo *output, |
| 44 | const Coordinates &starts, |
| 45 | const Coordinates &ends, |
| 46 | const BiStrides &strides, |
| 47 | int32_t begin_mask, |
| 48 | int32_t end_mask, |
| 49 | int32_t shrink_axis_mask) |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 50 | { |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 53 | |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().num_dimensions() > 4); |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON(starts.num_dimensions() > input->num_dimensions()); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON(ends.num_dimensions() > input->num_dimensions()); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON(strides.num_dimensions() > input->num_dimensions()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ERROR_ON( |
| 59 | std::any_of(strides.cbegin(), strides.cbegin() + strides.num_dimensions(), [](int i) { return i == 0; })); |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 60 | |
| 61 | // Get expected output shape |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 62 | const TensorShape exp_output_shape = arm_compute::misc::shape_calculator::compute_strided_slice_shape( |
| 63 | *input, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask); |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 64 | ARM_COMPUTE_RETURN_ERROR_ON(exp_output_shape.total_size() == 0); |
| 65 | |
| 66 | // Checks output if configured |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 67 | if (output->total_size() != 0) |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 68 | { |
| 69 | const TensorInfo exp_output_info = output->clone()->set_tensor_shape(exp_output_shape); |
| 70 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &exp_output_info); |
| 71 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 72 | } |
| 73 | |
| 74 | return Status{}; |
| 75 | } |
| 76 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 77 | std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *input, |
| 78 | ITensorInfo *output, |
| 79 | const Coordinates &starts, |
| 80 | const Coordinates &ends, |
| 81 | const BiStrides &strides, |
| 82 | int32_t begin_mask, |
| 83 | int32_t end_mask, |
| 84 | int32_t shrink_axis_mask) |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 85 | { |
| 86 | // Output tensor auto initialization if not yet initialized |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 87 | const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_strided_slice_shape( |
| 88 | *input, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask); |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 89 | auto_init_if_empty(*output, input->clone()->set_tensor_shape(output_shape)); |
| 90 | |
| 91 | // Create window |
Michele Di Giorgio | 4646d2e | 2019-06-19 12:28:47 +0100 | [diff] [blame] | 92 | Window win = calculate_max_window(*output, Steps()); |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 93 | |
| 94 | return std::make_pair(Status{}, win); |
| 95 | } |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 96 | } // namespace |
| 97 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 98 | NEStridedSliceKernel::NEStridedSliceKernel() : _starts_abs(), _final_strides(), _shrink_mask() |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 99 | { |
| 100 | } |
| 101 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 102 | void NEStridedSliceKernel::configure(const ITensorInfo *input, |
| 103 | ITensorInfo *output, |
| 104 | const Coordinates &starts, |
| 105 | const Coordinates &ends, |
| 106 | const BiStrides &strides, |
| 107 | int32_t begin_mask, |
| 108 | int32_t end_mask, |
| 109 | int32_t shrink_axis_mask) |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 110 | { |
| 111 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 112 | ARM_COMPUTE_ERROR_THROW_ON( |
| 113 | validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask)); |
Omar Al Khatib | a231832 | 2023-01-04 15:43:17 +0000 | [diff] [blame] | 114 | _shrink_mask = shrink_axis_mask; |
Michalis Spyrou | 1732da8 | 2020-06-19 12:40:46 +0100 | [diff] [blame] | 115 | const TensorShape &input_shape = input->tensor_shape(); |
Omar Al Khatib | a231832 | 2023-01-04 15:43:17 +0000 | [diff] [blame] | 116 | Coordinates ends_abs; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 117 | std::tie(_starts_abs, ends_abs, _final_strides) = |
| 118 | arm_compute::helpers::tensor_transform::calculate_strided_slice_coords(input_shape, starts, ends, strides, |
| 119 | begin_mask, end_mask, shrink_axis_mask); |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 120 | // Configure kernel window |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 121 | auto win_config = |
| 122 | validate_and_configure_window(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask); |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 123 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 124 | INEKernel::configure(win_config.second); |
| 125 | } |
| 126 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 127 | Status NEStridedSliceKernel::validate(const ITensorInfo *input, |
| 128 | const ITensorInfo *output, |
| 129 | const Coordinates &starts, |
| 130 | const Coordinates &ends, |
| 131 | const BiStrides &strides, |
| 132 | int32_t begin_mask, |
| 133 | int32_t end_mask, |
| 134 | int32_t shrink_axis_mask) |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 135 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 136 | ARM_COMPUTE_RETURN_ON_ERROR( |
| 137 | validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask)); |
| 138 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), starts, ends, |
| 139 | strides, begin_mask, end_mask, shrink_axis_mask) |
| 140 | .first); |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 141 | |
| 142 | return Status{}; |
| 143 | } |
| 144 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 145 | void NEStridedSliceKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 146 | { |
| 147 | ARM_COMPUTE_UNUSED(info); |
| 148 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 149 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 150 | |
Omar Al Khatib | a231832 | 2023-01-04 15:43:17 +0000 | [diff] [blame] | 151 | const ITensor *input = tensors.get_const_tensor(TensorType::ACL_SRC_0); |
| 152 | const ITensor *output = tensors.get_tensor(TensorType::ACL_DST); |
| 153 | |
| 154 | size_t width_size = input->info()->element_size(); |
| 155 | |
| 156 | const bool is_shrink_x = arm_compute::helpers::bit_ops::is_bit_set(_shrink_mask, 0); |
| 157 | const bool is_shrink_y = arm_compute::helpers::bit_ops::is_bit_set(_shrink_mask, 1); |
| 158 | const bool is_shrink_z = arm_compute::helpers::bit_ops::is_bit_set(_shrink_mask, 2); |
| 159 | const bool is_shrink_w = arm_compute::helpers::bit_ops::is_bit_set(_shrink_mask, 3); |
| 160 | |
| 161 | unsigned int index = 0; |
| 162 | const int idx_x = is_shrink_x ? 0 : index++; |
| 163 | const int idx_y = is_shrink_y ? 0 : index++; |
| 164 | const int idx_z = is_shrink_z ? 0 : index++; |
| 165 | const int idx_w = is_shrink_w ? 0 : index; |
| 166 | |
| 167 | BiStrides shrinked_strides; |
| 168 | shrinked_strides.set(0, is_shrink_x ? 0 : _final_strides[0]); |
| 169 | shrinked_strides.set(1, is_shrink_y ? 0 : _final_strides[1]); |
| 170 | shrinked_strides.set(2, is_shrink_z ? 0 : _final_strides[2]); |
| 171 | shrinked_strides.set(3, is_shrink_w ? 0 : _final_strides[3]); |
| 172 | |
| 173 | Window win = window; |
| 174 | |
| 175 | size_t length_x = win.shape()[0]; |
| 176 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 177 | if (_final_strides[0] == 1 && !is_shrink_x) |
Omar Al Khatib | a231832 | 2023-01-04 15:43:17 +0000 | [diff] [blame] | 178 | { |
| 179 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 180 | width_size = width_size * length_x; |
| 181 | } |
| 182 | |
| 183 | Iterator output_it(output, win); |
| 184 | |
| 185 | const int start_0 = _starts_abs[0]; |
| 186 | const int start_1 = _starts_abs[1]; |
| 187 | const int start_2 = _starts_abs[2]; |
| 188 | const int start_3 = _starts_abs[3]; |
| 189 | |
| 190 | const int shrinked_stride_0 = shrinked_strides[0]; |
| 191 | const int shrinked_stride_1 = shrinked_strides[1]; |
| 192 | const int shrinked_stride_2 = shrinked_strides[2]; |
| 193 | const int shrinked_stride_3 = shrinked_strides[3]; |
| 194 | |
| 195 | const int byte_increment_0 = static_cast<int>(input->info()->strides_in_bytes()[0]); |
| 196 | const int byte_increment_1 = static_cast<int>(input->info()->strides_in_bytes()[1]); |
| 197 | const int byte_increment_2 = static_cast<int>(input->info()->strides_in_bytes()[2]); |
| 198 | const int byte_increment_3 = static_cast<int>(input->info()->strides_in_bytes()[3]); |
| 199 | |
| 200 | uint8_t *input_base = input->ptr_to_element(Coordinates(0, 0, 0, 0)); |
| 201 | uint8_t *cur_ptr; |
| 202 | |
| 203 | execute_window_loop( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 204 | win, |
| 205 | [&](const Coordinates &id) |
| 206 | { |
| 207 | cur_ptr = input_base; |
| 208 | cur_ptr += (start_0 + (id[idx_x] * shrinked_stride_0)) * byte_increment_0; |
| 209 | cur_ptr += (start_1 + (id[idx_y] * shrinked_stride_1)) * byte_increment_1; |
| 210 | cur_ptr += (start_2 + (id[idx_z] * shrinked_stride_2)) * byte_increment_2; |
| 211 | cur_ptr += (start_3 + (id[idx_w] * shrinked_stride_3)) * byte_increment_3; |
Omar Al Khatib | a231832 | 2023-01-04 15:43:17 +0000 | [diff] [blame] | 212 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 213 | std::copy_n(cur_ptr, width_size, output_it.ptr()); |
| 214 | }, |
| 215 | output_it); |
Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 216 | } |
| 217 | } // namespace arm_compute |