blob: 20cd835069b6ea3c8b3a4b180dcdcc27e56e66a3 [file] [log] [blame]
Georgios Pinitas77589b52018-08-21 14:41:35 +01001/*
Viet-Hoa Do6829e022024-01-16 16:23:24 +00002 * Copyright (c) 2018-2021, 2023-2024 Arm Limited.
Georgios Pinitas77589b52018-08-21 14:41:35 +01003 *
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 Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLStridedSliceKernel.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010025
Georgios Pinitas77589b52018-08-21 14:41:35 +010026#include "arm_compute/core/CL/ICLTensor.h"
Georgios Pinitas77589b52018-08-21 14:41:35 +010027#include "arm_compute/core/TensorInfo.h"
Georgios Pinitas77589b52018-08-21 14:41:35 +010028#include "arm_compute/core/utils/helpers/tensor_transform.h"
29#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000030#include "arm_compute/core/utils/StringUtils.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/helpers/AutoConfiguration.h"
33#include "src/core/helpers/WindowHelpers.h"
34#include "src/core/utils/helpers/bit_ops.h"
35#include "support/Cast.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000036#include "support/StringSupport.h"
Georgios Pinitas77589b52018-08-21 14:41:35 +010037
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 Pinitas77589b52018-08-21 14:41:35 +010050{
51 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Manuel Bottini8481d832019-12-10 15:28:40 +000052 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Georgios Pinitas77589b52018-08-21 14:41:35 +010053
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 Pinitas77589b52018-08-21 14:41:35 +010060
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 Pinitas77589b52018-08-21 14:41:35 +010064 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 Pinitas77589b52018-08-21 14:41:35 +010068 {
Georgios Pinitas2ade4522018-12-07 13:23:39 +000069 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);
Georgios Pinitas77589b52018-08-21 14:41:35 +010071 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
72 }
73
74 return Status{};
75}
Georgios Pinitas77589b52018-08-21 14:41:35 +010076} // namespace
77
Giorgio Arena4a95bba2021-06-28 11:00:27 +010078CLStridedSliceKernel::CLStridedSliceKernel()
79{
80 _type = CLKernelType::ELEMENTWISE;
81}
82
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010083void CLStridedSliceKernel::configure(const CLCompileContext &compile_context,
84 const ITensorInfo *input,
85 ITensorInfo *output,
86 const Coordinates &starts,
87 const Coordinates &ends,
88 const BiStrides &strides,
89 int32_t begin_mask,
90 int32_t end_mask,
91 int32_t shrink_axis_mask)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010092{
Georgios Pinitas77589b52018-08-21 14:41:35 +010093 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010094 auto padding_info = get_padding_info({input, output});
95 ARM_COMPUTE_ERROR_THROW_ON(
96 validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask));
Georgios Pinitas77589b52018-08-21 14:41:35 +010097
Michalis Spyrouf20d6d62020-07-16 17:46:51 +010098 const TensorShape &input_shape = input->tensor_shape();
Georgios Pinitas77589b52018-08-21 14:41:35 +010099
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100100 Coordinates starts_abs;
101 Coordinates ends_abs;
102 Coordinates final_strides;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103 std::tie(starts_abs, ends_abs, final_strides) =
104 arm_compute::helpers::tensor_transform::calculate_strided_slice_coords(input_shape, starts, ends, strides,
105 begin_mask, end_mask, shrink_axis_mask);
Georgios Pinitas77589b52018-08-21 14:41:35 +0100106
107 // Configure kernel window
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100108 const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_strided_slice_shape(
109 *input, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask);
Michalis Spyrou702dc0c2021-03-19 15:06:07 +0000110 auto_init_if_empty(*output, input->clone()->set_tensor_shape(output_shape));
111 Window win = calculate_max_window(*output, Steps());
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100112
113 // Enable multiple elements processing along x if stride_x is 1 and output width greater than the access vector size
Michalis Spyrouf20d6d62020-07-16 17:46:51 +0100114 const int vec_size_x = 16 / input->element_size();
115 const int output_width_x = output->tensor_shape().x();
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000116 const bool is_shrink_on_x = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, 0);
117 const bool multi_access_x = !is_shrink_on_x && (final_strides.x() == 1) && (output_width_x / vec_size_x > 0);
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100118
119 // Update window if needed
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100120 if (multi_access_x)
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100121 {
Michalis Spyrou702dc0c2021-03-19 15:06:07 +0000122 Window &updated_window = win;
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100123 updated_window.set(Window::DimX,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100124 Window::Dimension(updated_window.x().start(),
125 ceil_to_multiple(updated_window.x().end(), vec_size_x), vec_size_x));
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100126 }
Michalis Spyrou702dc0c2021-03-19 15:06:07 +0000127 ICLKernel::configure_internal(win);
Georgios Pinitas77589b52018-08-21 14:41:35 +0100128
129 // Create build options
130 CLBuildOptions build_opts;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100131 build_opts.add_option("-DDATA_TYPE=" +
132 get_cl_unsigned_type_from_element_size(data_size_from_type(input->data_type())));
133 for (unsigned int i = 0; i < input_shape.num_dimensions(); ++i)
Georgios Pinitas77589b52018-08-21 14:41:35 +0100134 {
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000135 const bool is_shrink = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, i);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100136 build_opts.add_option("-DSTART_" + support::cpp11::to_string(i) + "=" +
137 support::cpp11::to_string(starts_abs[i]));
138 build_opts.add_option("-DSTRIDE_" + support::cpp11::to_string(i) + "=" +
139 support::cpp11::to_string(final_strides[i]));
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000140 build_opts.add_option_if(is_shrink, "-DSHRINK_" + support::cpp11::to_string(i));
Georgios Pinitas77589b52018-08-21 14:41:35 +0100141 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100142 build_opts.add_option_if(multi_access_x, "-DLAST_ACCESSED_X=" + support::cpp11::to_string(
143 std::max<int>(output_width_x - vec_size_x, 0)));
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100144 build_opts.add_option_if(multi_access_x, "-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x));
Michalis Spyrouf20d6d62020-07-16 17:46:51 +0100145 build_opts.add_option_if_else(output->num_dimensions() > 2,
146 "-DDST_DEPTH=" + support::cpp11::to_string(output->tensor_shape().z()),
Georgios Pinitas77589b52018-08-21 14:41:35 +0100147 "-DDST_DEPTH=1");
148
149 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100150 _kernel = create_kernel(compile_context, "strided_slice", build_opts.options());
Georgios Pinitas77589b52018-08-21 14:41:35 +0100151
152 // Set config_id for enabling LWS tuning
153 _config_id = "strided_slice";
154 _config_id += "_";
Michalis Spyrouf20d6d62020-07-16 17:46:51 +0100155 _config_id += lower_string(string_from_data_type(input->data_type()));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100156 for (unsigned int i = 0; i < input_shape.num_dimensions(); ++i)
Georgios Pinitas77589b52018-08-21 14:41:35 +0100157 {
158 _config_id += "_";
Michalis Spyrouf20d6d62020-07-16 17:46:51 +0100159 _config_id += support::cpp11::to_string(input->dimension(i));
Georgios Pinitas77589b52018-08-21 14:41:35 +0100160 _config_id += "_";
161 _config_id += support::cpp11::to_string(starts_abs[i]);
162 _config_id += "_";
163 _config_id += support::cpp11::to_string(ends_abs[i]);
164 _config_id += "_";
165 _config_id += support::cpp11::to_string(final_strides[i]);
166 }
Manuel Bottinib6869dd2020-12-16 15:34:25 +0000167 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Georgios Pinitas77589b52018-08-21 14:41:35 +0100168}
169
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100170Status CLStridedSliceKernel::validate(const ITensorInfo *input,
171 const ITensorInfo *output,
172 const Coordinates &starts,
173 const Coordinates &ends,
174 const BiStrides &strides,
175 int32_t begin_mask,
176 int32_t end_mask,
177 int32_t shrink_axis_mask)
Georgios Pinitas77589b52018-08-21 14:41:35 +0100178{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100179 ARM_COMPUTE_RETURN_ON_ERROR(
180 validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask));
Georgios Pinitas77589b52018-08-21 14:41:35 +0100181
182 return Status{};
183}
184
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100185void CLStridedSliceKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
Georgios Pinitas77589b52018-08-21 14:41:35 +0100186{
187 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
188 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
189
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100190 const auto src =
191 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
192 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Michalis Spyrouf20d6d62020-07-16 17:46:51 +0100193
Georgios Pinitas77589b52018-08-21 14:41:35 +0100194 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
195 Window slice = window_collapsed.first_slice_window_4D();
196
197 do
198 {
199 unsigned int idx = 0;
Michalis Spyrouf20d6d62020-07-16 17:46:51 +0100200 add_4D_tensor_argument(idx, src, slice);
201 add_4D_tensor_argument(idx, dst, slice);
Georgios Pinitas77589b52018-08-21 14:41:35 +0100202 enqueue(queue, *this, slice, lws_hint());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100203 } while (window_collapsed.slide_window_slice_4D(slice));
Georgios Pinitas77589b52018-08-21 14:41:35 +0100204}
205} // namespace arm_compute