blob: 464f74c9ebc2d74fbf5166970e678e13dff997aa [file] [log] [blame]
Georgios Pinitas77589b52018-08-21 14:41:35 +01001/*
Michalis Spyrou702dc0c2021-03-19 15:06:07 +00002 * Copyright (c) 2018-2021 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"
Georgios Pinitas77589b52018-08-21 14:41:35 +010025#include "arm_compute/core/CL/ICLTensor.h"
Georgios Pinitas77589b52018-08-21 14:41:35 +010026#include "arm_compute/core/TensorInfo.h"
Georgios Pinitas77589b52018-08-21 14:41:35 +010027#include "arm_compute/core/utils/helpers/tensor_transform.h"
28#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010029#include "src/core/helpers/AutoConfiguration.h"
30#include "src/core/helpers/WindowHelpers.h"
31#include "src/core/utils/helpers/bit_ops.h"
32#include "support/Cast.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000033#include "support/StringSupport.h"
Georgios Pinitas77589b52018-08-21 14:41:35 +010034
35namespace arm_compute
36{
37namespace
38{
39Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output,
40 const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
41 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
42{
43 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Manuel Bottini8481d832019-12-10 15:28:40 +000044 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Georgios Pinitas77589b52018-08-21 14:41:35 +010045
46 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().num_dimensions() > 4);
47 ARM_COMPUTE_RETURN_ERROR_ON(starts.num_dimensions() > input->num_dimensions());
48 ARM_COMPUTE_RETURN_ERROR_ON(ends.num_dimensions() > input->num_dimensions());
49 ARM_COMPUTE_RETURN_ERROR_ON(strides.num_dimensions() > input->num_dimensions());
Georgios Pinitasc1a72452018-08-24 11:25:32 +010050 ARM_COMPUTE_RETURN_ERROR_ON(std::any_of(strides.cbegin(), strides.cbegin() + strides.num_dimensions(), [](int i)
Georgios Pinitas77589b52018-08-21 14:41:35 +010051 {
Georgios Pinitasc1a72452018-08-24 11:25:32 +010052 return i == 0;
53 }));
Georgios Pinitas77589b52018-08-21 14:41:35 +010054
55 // Get expected output shape
56 const TensorShape exp_output_shape = arm_compute::misc::shape_calculator::compute_strided_slice_shape(*input,
57 starts, ends, strides,
58 begin_mask, end_mask, shrink_axis_mask);
59 ARM_COMPUTE_RETURN_ERROR_ON(exp_output_shape.total_size() == 0);
60
61 // Checks output if configured
62 if(output->total_size() != 0)
63 {
Georgios Pinitas2ade4522018-12-07 13:23:39 +000064 const TensorInfo exp_output_info = output->clone()->set_tensor_shape(exp_output_shape);
65 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &exp_output_info);
Georgios Pinitas77589b52018-08-21 14:41:35 +010066 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
67 }
68
69 return Status{};
70}
Georgios Pinitas77589b52018-08-21 14:41:35 +010071} // namespace
72
Giorgio Arena4a95bba2021-06-28 11:00:27 +010073CLStridedSliceKernel::CLStridedSliceKernel()
74{
75 _type = CLKernelType::ELEMENTWISE;
76}
77
Michalis Spyrouf20d6d62020-07-16 17:46:51 +010078void CLStridedSliceKernel::configure(const CLCompileContext &compile_context, const ITensorInfo *input, ITensorInfo *output,
Manuel Bottini4c6bd512020-04-08 10:15:51 +010079 const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
80 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
81{
Georgios Pinitas77589b52018-08-21 14:41:35 +010082 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Manuel Bottinib6869dd2020-12-16 15:34:25 +000083 auto padding_info = get_padding_info({ input, output });
Michalis Spyrouf20d6d62020-07-16 17:46:51 +010084 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask));
Georgios Pinitas77589b52018-08-21 14:41:35 +010085
Michalis Spyrouf20d6d62020-07-16 17:46:51 +010086 const TensorShape &input_shape = input->tensor_shape();
Georgios Pinitas77589b52018-08-21 14:41:35 +010087
Michalis Spyroua4f378d2019-04-26 14:54:54 +010088 Coordinates starts_abs;
89 Coordinates ends_abs;
90 Coordinates final_strides;
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000091 std::tie(starts_abs, ends_abs, final_strides) = arm_compute::helpers::tensor_transform::calculate_strided_slice_coords(
92 input_shape,
93 starts, ends, strides,
94 begin_mask, end_mask, shrink_axis_mask);
Georgios Pinitas77589b52018-08-21 14:41:35 +010095
96 // Configure kernel window
Michalis Spyrou702dc0c2021-03-19 15:06:07 +000097 const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_strided_slice_shape(*input,
98 starts, ends, strides,
99 begin_mask, end_mask, shrink_axis_mask);
100 auto_init_if_empty(*output, input->clone()->set_tensor_shape(output_shape));
101 Window win = calculate_max_window(*output, Steps());
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100102
103 // 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 +0100104 const int vec_size_x = 16 / input->element_size();
105 const int output_width_x = output->tensor_shape().x();
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000106 const bool is_shrink_on_x = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, 0);
107 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 +0100108
109 // Update window if needed
110 if(multi_access_x)
111 {
Michalis Spyrou702dc0c2021-03-19 15:06:07 +0000112 Window &updated_window = win;
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100113 updated_window.set(Window::DimX,
114 Window::Dimension(updated_window.x().start(), ceil_to_multiple(updated_window.x().end(), vec_size_x), vec_size_x));
115 }
Michalis Spyrou702dc0c2021-03-19 15:06:07 +0000116 ICLKernel::configure_internal(win);
Georgios Pinitas77589b52018-08-21 14:41:35 +0100117
118 // Create build options
119 CLBuildOptions build_opts;
Michalis Spyrouf20d6d62020-07-16 17:46:51 +0100120 build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(data_size_from_type(input->data_type())));
Georgios Pinitas77589b52018-08-21 14:41:35 +0100121 for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i)
122 {
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000123 const bool is_shrink = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, i);
Georgios Pinitas77589b52018-08-21 14:41:35 +0100124 build_opts.add_option("-DSTART_" + support::cpp11::to_string(i) + "=" + support::cpp11::to_string(starts_abs[i]));
125 build_opts.add_option("-DSTRIDE_" + support::cpp11::to_string(i) + "=" + support::cpp11::to_string(final_strides[i]));
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000126 build_opts.add_option_if(is_shrink, "-DSHRINK_" + support::cpp11::to_string(i));
Georgios Pinitas77589b52018-08-21 14:41:35 +0100127 }
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100128 build_opts.add_option_if(multi_access_x, "-DLAST_ACCESSED_X=" + support::cpp11::to_string(std::max<int>(output_width_x - vec_size_x, 0)));
129 build_opts.add_option_if(multi_access_x, "-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x));
Georgios Pinitas77589b52018-08-21 14:41:35 +0100130 build_opts.add_option_if_else(input_shape.num_dimensions() > 2,
131 "-DSRC_DEPTH=" + support::cpp11::to_string(input_shape.z()),
132 "-DSRC_DEPTH=1");
Michalis Spyrouf20d6d62020-07-16 17:46:51 +0100133 build_opts.add_option_if_else(output->num_dimensions() > 2,
134 "-DDST_DEPTH=" + support::cpp11::to_string(output->tensor_shape().z()),
Georgios Pinitas77589b52018-08-21 14:41:35 +0100135 "-DDST_DEPTH=1");
136
137 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100138 _kernel = create_kernel(compile_context, "strided_slice", build_opts.options());
Georgios Pinitas77589b52018-08-21 14:41:35 +0100139
140 // Set config_id for enabling LWS tuning
141 _config_id = "strided_slice";
142 _config_id += "_";
Michalis Spyrouf20d6d62020-07-16 17:46:51 +0100143 _config_id += lower_string(string_from_data_type(input->data_type()));
Georgios Pinitas77589b52018-08-21 14:41:35 +0100144 for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i)
145 {
146 _config_id += "_";
Michalis Spyrouf20d6d62020-07-16 17:46:51 +0100147 _config_id += support::cpp11::to_string(input->dimension(i));
Georgios Pinitas77589b52018-08-21 14:41:35 +0100148 _config_id += "_";
149 _config_id += support::cpp11::to_string(starts_abs[i]);
150 _config_id += "_";
151 _config_id += support::cpp11::to_string(ends_abs[i]);
152 _config_id += "_";
153 _config_id += support::cpp11::to_string(final_strides[i]);
154 }
Manuel Bottinib6869dd2020-12-16 15:34:25 +0000155 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Georgios Pinitas77589b52018-08-21 14:41:35 +0100156}
157
158Status CLStridedSliceKernel::validate(const ITensorInfo *input, const ITensorInfo *output,
159 const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
160 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
161{
162 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask));
Georgios Pinitas77589b52018-08-21 14:41:35 +0100163
164 return Status{};
165}
166
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100167void CLStridedSliceKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
Georgios Pinitas77589b52018-08-21 14:41:35 +0100168{
169 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
170 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
171
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100172 const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
173 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Michalis Spyrouf20d6d62020-07-16 17:46:51 +0100174
Georgios Pinitas77589b52018-08-21 14:41:35 +0100175 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
176 Window slice = window_collapsed.first_slice_window_4D();
177
178 do
179 {
180 unsigned int idx = 0;
Michalis Spyrouf20d6d62020-07-16 17:46:51 +0100181 add_4D_tensor_argument(idx, src, slice);
182 add_4D_tensor_argument(idx, dst, slice);
Georgios Pinitas77589b52018-08-21 14:41:35 +0100183 enqueue(queue, *this, slice, lws_hint());
184 }
185 while(window_collapsed.slide_window_slice_4D(slice));
186}
187} // namespace arm_compute