blob: f7b7290a3f65e146ca94ab7cac8fb387ff11ddb8 [file] [log] [blame]
Georgios Pinitas77589b52018-08-21 14:41:35 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 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 */
24#include "arm_compute/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 Pinitasb4af2c62018-12-10 18:45:35 +000027#include "arm_compute/core/utils/helpers/bit_ops.h"
Georgios Pinitas77589b52018-08-21 14:41:35 +010028#include "arm_compute/core/utils/helpers/tensor_transform.h"
Michalis Spyrouf20d6d62020-07-16 17:46:51 +010029#include "arm_compute/core/utils/misc/Cast.h"
Georgios Pinitas77589b52018-08-21 14:41:35 +010030#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000031#include "support/StringSupport.h"
Georgios Pinitas77589b52018-08-21 14:41:35 +010032
33namespace arm_compute
34{
35namespace
36{
37Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output,
38 const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
39 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
40{
41 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Manuel Bottini8481d832019-12-10 15:28:40 +000042 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Georgios Pinitas77589b52018-08-21 14:41:35 +010043
44 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().num_dimensions() > 4);
45 ARM_COMPUTE_RETURN_ERROR_ON(starts.num_dimensions() > input->num_dimensions());
46 ARM_COMPUTE_RETURN_ERROR_ON(ends.num_dimensions() > input->num_dimensions());
47 ARM_COMPUTE_RETURN_ERROR_ON(strides.num_dimensions() > input->num_dimensions());
Georgios Pinitasc1a72452018-08-24 11:25:32 +010048 ARM_COMPUTE_RETURN_ERROR_ON(std::any_of(strides.cbegin(), strides.cbegin() + strides.num_dimensions(), [](int i)
Georgios Pinitas77589b52018-08-21 14:41:35 +010049 {
Georgios Pinitasc1a72452018-08-24 11:25:32 +010050 return i == 0;
51 }));
Georgios Pinitas77589b52018-08-21 14:41:35 +010052
53 // Get expected output shape
54 const TensorShape exp_output_shape = arm_compute::misc::shape_calculator::compute_strided_slice_shape(*input,
55 starts, ends, strides,
56 begin_mask, end_mask, shrink_axis_mask);
57 ARM_COMPUTE_RETURN_ERROR_ON(exp_output_shape.total_size() == 0);
58
59 // Checks output if configured
60 if(output->total_size() != 0)
61 {
Georgios Pinitas2ade4522018-12-07 13:23:39 +000062 const TensorInfo exp_output_info = output->clone()->set_tensor_shape(exp_output_shape);
63 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &exp_output_info);
Georgios Pinitas77589b52018-08-21 14:41:35 +010064 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
65 }
66
67 return Status{};
68}
69
Michalis Spyrouf20d6d62020-07-16 17:46:51 +010070std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *input, ITensorInfo *output,
Georgios Pinitas77589b52018-08-21 14:41:35 +010071 const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
72 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
73{
74 // Output tensor auto initialization if not yet initialized
75 const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_strided_slice_shape(*input,
76 starts, ends, strides,
77 begin_mask, end_mask, shrink_axis_mask);
78 auto_init_if_empty(*output, input->clone()->set_tensor_shape(output_shape));
79
80 // Create window
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +010081 Window win = calculate_max_window(*output, Steps());
Georgios Pinitas77589b52018-08-21 14:41:35 +010082 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
83
84 return std::make_pair(Status{}, win);
85}
86} // namespace
87
Michalis Spyrouf20d6d62020-07-16 17:46:51 +010088void CLStridedSliceKernel::configure(const CLCompileContext &compile_context, const ITensorInfo *input, ITensorInfo *output,
Manuel Bottini4c6bd512020-04-08 10:15:51 +010089 const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
90 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
91{
Georgios Pinitas77589b52018-08-21 14:41:35 +010092 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Michalis Spyrouf20d6d62020-07-16 17:46:51 +010093 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 +010094
Michalis Spyrouf20d6d62020-07-16 17:46:51 +010095 const TensorShape &input_shape = input->tensor_shape();
Georgios Pinitas77589b52018-08-21 14:41:35 +010096
Michalis Spyroua4f378d2019-04-26 14:54:54 +010097 Coordinates starts_abs;
98 Coordinates ends_abs;
99 Coordinates final_strides;
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000100 std::tie(starts_abs, ends_abs, final_strides) = arm_compute::helpers::tensor_transform::calculate_strided_slice_coords(
101 input_shape,
102 starts, ends, strides,
103 begin_mask, end_mask, shrink_axis_mask);
Georgios Pinitas77589b52018-08-21 14:41:35 +0100104
105 // Configure kernel window
Michalis Spyrouf20d6d62020-07-16 17:46:51 +0100106 auto win_config = validate_and_configure_window(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask);
Georgios Pinitas77589b52018-08-21 14:41:35 +0100107 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100108
109 // 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 +0100110 const int vec_size_x = 16 / input->element_size();
111 const int output_width_x = output->tensor_shape().x();
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000112 const bool is_shrink_on_x = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, 0);
113 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 +0100114
115 // Update window if needed
116 if(multi_access_x)
117 {
118 Window &updated_window = std::get<1>(win_config);
119 updated_window.set(Window::DimX,
120 Window::Dimension(updated_window.x().start(), ceil_to_multiple(updated_window.x().end(), vec_size_x), vec_size_x));
121 }
Georgios Pinitas77589b52018-08-21 14:41:35 +0100122 ICLKernel::configure_internal(win_config.second);
123
124 // Create build options
125 CLBuildOptions build_opts;
Michalis Spyrouf20d6d62020-07-16 17:46:51 +0100126 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 +0100127 for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i)
128 {
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000129 const bool is_shrink = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, i);
Georgios Pinitas77589b52018-08-21 14:41:35 +0100130 build_opts.add_option("-DSTART_" + support::cpp11::to_string(i) + "=" + support::cpp11::to_string(starts_abs[i]));
131 build_opts.add_option("-DSTRIDE_" + support::cpp11::to_string(i) + "=" + support::cpp11::to_string(final_strides[i]));
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000132 build_opts.add_option_if(is_shrink, "-DSHRINK_" + support::cpp11::to_string(i));
Georgios Pinitas77589b52018-08-21 14:41:35 +0100133 }
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100134 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)));
135 build_opts.add_option_if(multi_access_x, "-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x));
Georgios Pinitas77589b52018-08-21 14:41:35 +0100136 build_opts.add_option_if_else(input_shape.num_dimensions() > 2,
137 "-DSRC_DEPTH=" + support::cpp11::to_string(input_shape.z()),
138 "-DSRC_DEPTH=1");
Michalis Spyrouf20d6d62020-07-16 17:46:51 +0100139 build_opts.add_option_if_else(output->num_dimensions() > 2,
140 "-DDST_DEPTH=" + support::cpp11::to_string(output->tensor_shape().z()),
Georgios Pinitas77589b52018-08-21 14:41:35 +0100141 "-DDST_DEPTH=1");
142
143 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100144 _kernel = create_kernel(compile_context, "strided_slice", build_opts.options());
Georgios Pinitas77589b52018-08-21 14:41:35 +0100145
146 // Set config_id for enabling LWS tuning
147 _config_id = "strided_slice";
148 _config_id += "_";
Michalis Spyrouf20d6d62020-07-16 17:46:51 +0100149 _config_id += lower_string(string_from_data_type(input->data_type()));
Georgios Pinitas77589b52018-08-21 14:41:35 +0100150 for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i)
151 {
152 _config_id += "_";
Michalis Spyrouf20d6d62020-07-16 17:46:51 +0100153 _config_id += support::cpp11::to_string(input->dimension(i));
Georgios Pinitas77589b52018-08-21 14:41:35 +0100154 _config_id += "_";
155 _config_id += support::cpp11::to_string(starts_abs[i]);
156 _config_id += "_";
157 _config_id += support::cpp11::to_string(ends_abs[i]);
158 _config_id += "_";
159 _config_id += support::cpp11::to_string(final_strides[i]);
160 }
161}
162
163Status CLStridedSliceKernel::validate(const ITensorInfo *input, const ITensorInfo *output,
164 const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
165 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
166{
167 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask));
168 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(),
169 starts, ends, strides, begin_mask, end_mask, shrink_axis_mask)
170 .first);
171
172 return Status{};
173}
174
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100175void CLStridedSliceKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
Georgios Pinitas77589b52018-08-21 14:41:35 +0100176{
177 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
178 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
179
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100180 const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
181 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Michalis Spyrouf20d6d62020-07-16 17:46:51 +0100182
Georgios Pinitas77589b52018-08-21 14:41:35 +0100183 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
184 Window slice = window_collapsed.first_slice_window_4D();
185
186 do
187 {
188 unsigned int idx = 0;
Michalis Spyrouf20d6d62020-07-16 17:46:51 +0100189 add_4D_tensor_argument(idx, src, slice);
190 add_4D_tensor_argument(idx, dst, slice);
Georgios Pinitas77589b52018-08-21 14:41:35 +0100191 enqueue(queue, *this, slice, lws_hint());
192 }
193 while(window_collapsed.slide_window_slice_4D(slice));
194}
195} // namespace arm_compute