blob: efff51be9d73aca009debd889875516655a7883a [file] [log] [blame]
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +00001/*
Omar Al Khatiba2318322023-01-04 15:43:17 +00002 * Copyright (c) 2018-2021, 2023 Arm Limited.
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +00003 *
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 Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEStridedSliceKernel.h"
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +000025
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +000026#include "arm_compute/core/ITensor.h"
27#include "arm_compute/core/TensorInfo.h"
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +000028#include "arm_compute/core/Types.h"
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +000029#include "arm_compute/core/utils/helpers/tensor_transform.h"
30#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031#include "arm_compute/core/Window.h"
32
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#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 Pinitas6d9d6f42018-12-24 16:10:47 +000037
38namespace arm_compute
39{
40namespace
41{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010042Status 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 Pinitas6d9d6f42018-12-24 16:10:47 +000050{
51 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas33843562019-12-10 13:33:18 +000052 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +000053
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010058 ARM_COMPUTE_RETURN_ERROR_ON(
59 std::any_of(strides.cbegin(), strides.cbegin() + strides.num_dimensions(), [](int i) { return i == 0; }));
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +000060
61 // Get expected output shape
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010062 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 Pinitas6d9d6f42018-12-24 16:10:47 +000064 ARM_COMPUTE_RETURN_ERROR_ON(exp_output_shape.total_size() == 0);
65
66 // Checks output if configured
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010067 if (output->total_size() != 0)
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +000068 {
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010077std::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 Pinitas6d9d6f42018-12-24 16:10:47 +000085{
86 // Output tensor auto initialization if not yet initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010087 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 Pinitas6d9d6f42018-12-24 16:10:47 +000089 auto_init_if_empty(*output, input->clone()->set_tensor_shape(output_shape));
90
91 // Create window
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +010092 Window win = calculate_max_window(*output, Steps());
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +000093
94 return std::make_pair(Status{}, win);
95}
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +000096} // namespace
97
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010098NEStridedSliceKernel::NEStridedSliceKernel() : _starts_abs(), _final_strides(), _shrink_mask()
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +000099{
100}
101
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100102void 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 Pinitas6d9d6f42018-12-24 16:10:47 +0000110{
111 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100112 ARM_COMPUTE_ERROR_THROW_ON(
113 validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask));
Omar Al Khatiba2318322023-01-04 15:43:17 +0000114 _shrink_mask = shrink_axis_mask;
Michalis Spyrou1732da82020-06-19 12:40:46 +0100115 const TensorShape &input_shape = input->tensor_shape();
Omar Al Khatiba2318322023-01-04 15:43:17 +0000116 Coordinates ends_abs;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100117 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 Pinitas6d9d6f42018-12-24 16:10:47 +0000120 // Configure kernel window
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100121 auto win_config =
122 validate_and_configure_window(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask);
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +0000123 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
124 INEKernel::configure(win_config.second);
125}
126
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100127Status 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 Pinitas6d9d6f42018-12-24 16:10:47 +0000135{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100136 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 Pinitas6d9d6f42018-12-24 16:10:47 +0000141
142 return Status{};
143}
144
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100145void NEStridedSliceKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +0000146{
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 Khatiba2318322023-01-04 15:43:17 +0000151 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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100177 if (_final_strides[0] == 1 && !is_shrink_x)
Omar Al Khatiba2318322023-01-04 15:43:17 +0000178 {
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100204 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 Khatiba2318322023-01-04 15:43:17 +0000212
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100213 std::copy_n(cur_ptr, width_size, output_it.ptr());
214 },
215 output_it);
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +0000216}
217} // namespace arm_compute