blob: c40f3c9f0ba47f5e736260f032c7d0eba3686c8b [file] [log] [blame]
Georgios Pinitas77589b52018-08-21 14:41:35 +01001/*
2 * Copyright (c) 2018 ARM Limited.
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 */
24#include "arm_compute/core/CL/kernels/CLStridedSliceKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/CLValidate.h"
29#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/IAccessWindow.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Window.h"
33
34#include "arm_compute/core/Types.h"
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000035#include "arm_compute/core/utils/helpers/bit_ops.h"
Georgios Pinitas77589b52018-08-21 14:41:35 +010036#include "arm_compute/core/utils/helpers/tensor_transform.h"
37#include "arm_compute/core/utils/misc/ShapeCalculator.h"
38
39namespace arm_compute
40{
41namespace
42{
43Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output,
44 const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
45 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
46{
47 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
48 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
49 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1,
50 DataType::U8, DataType::S8, DataType::QASYMM8,
51 DataType::U16, DataType::S16,
52 DataType::U32, DataType::S32,
53 DataType::F16, DataType::F32);
54
55 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().num_dimensions() > 4);
56 ARM_COMPUTE_RETURN_ERROR_ON(starts.num_dimensions() > input->num_dimensions());
57 ARM_COMPUTE_RETURN_ERROR_ON(ends.num_dimensions() > input->num_dimensions());
58 ARM_COMPUTE_RETURN_ERROR_ON(strides.num_dimensions() > input->num_dimensions());
Georgios Pinitasc1a72452018-08-24 11:25:32 +010059 ARM_COMPUTE_RETURN_ERROR_ON(std::any_of(strides.cbegin(), strides.cbegin() + strides.num_dimensions(), [](int i)
Georgios Pinitas77589b52018-08-21 14:41:35 +010060 {
Georgios Pinitasc1a72452018-08-24 11:25:32 +010061 return i == 0;
62 }));
Georgios Pinitas77589b52018-08-21 14:41:35 +010063
64 // Get expected output shape
65 const TensorShape exp_output_shape = arm_compute::misc::shape_calculator::compute_strided_slice_shape(*input,
66 starts, ends, strides,
67 begin_mask, end_mask, shrink_axis_mask);
68 ARM_COMPUTE_RETURN_ERROR_ON(exp_output_shape.total_size() == 0);
69
70 // Checks output if configured
71 if(output->total_size() != 0)
72 {
Georgios Pinitas2ade4522018-12-07 13:23:39 +000073 const TensorInfo exp_output_info = output->clone()->set_tensor_shape(exp_output_shape);
74 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &exp_output_info);
Georgios Pinitas77589b52018-08-21 14:41:35 +010075 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
76 }
77
78 return Status{};
79}
80
81std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output,
82 const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
83 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
84{
85 // Output tensor auto initialization if not yet initialized
86 const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_strided_slice_shape(*input,
87 starts, ends, strides,
88 begin_mask, end_mask, shrink_axis_mask);
89 auto_init_if_empty(*output, input->clone()->set_tensor_shape(output_shape));
90
91 // Create window
92 const unsigned int num_elems_processed_per_iteration = 1;
93
94 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
95 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
96
97 return std::make_pair(Status{}, win);
98}
99} // namespace
100
101CLStridedSliceKernel::CLStridedSliceKernel()
102 : _input(nullptr), _output(nullptr)
103{
104}
105
106void CLStridedSliceKernel::configure(const ICLTensor *input, ICLTensor *output,
107 const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
108 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
109{
110 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
111 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), starts, ends, strides, begin_mask, end_mask, shrink_axis_mask));
112
113 _input = input;
114 _output = output;
115
116 const TensorShape &input_shape = input->info()->tensor_shape();
117
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000118 Coordinates starts_abs, ends_abs, final_strides;
119 std::tie(starts_abs, ends_abs, final_strides) = arm_compute::helpers::tensor_transform::calculate_strided_slice_coords(
120 input_shape,
121 starts, ends, strides,
122 begin_mask, end_mask, shrink_axis_mask);
Georgios Pinitas77589b52018-08-21 14:41:35 +0100123
124 // Configure kernel window
125 auto win_config = validate_and_configure_window(input->info(), output->info(), starts, ends, strides, begin_mask, end_mask, shrink_axis_mask);
126 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100127
128 // Enable multiple elements processing along x if stride_x is 1 and output width greater than the access vector size
129 const int vec_size_x = 16 / input->info()->element_size();
130 const int output_width_x = output->info()->tensor_shape().x();
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000131 const bool is_shrink_on_x = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, 0);
132 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 +0100133
134 // Update window if needed
135 if(multi_access_x)
136 {
137 Window &updated_window = std::get<1>(win_config);
138 updated_window.set(Window::DimX,
139 Window::Dimension(updated_window.x().start(), ceil_to_multiple(updated_window.x().end(), vec_size_x), vec_size_x));
140 }
Georgios Pinitas77589b52018-08-21 14:41:35 +0100141 ICLKernel::configure_internal(win_config.second);
142
143 // Create build options
144 CLBuildOptions build_opts;
145 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
146 for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i)
147 {
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000148 const bool is_shrink = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, i);
Georgios Pinitas77589b52018-08-21 14:41:35 +0100149 build_opts.add_option("-DSTART_" + support::cpp11::to_string(i) + "=" + support::cpp11::to_string(starts_abs[i]));
150 build_opts.add_option("-DSTRIDE_" + support::cpp11::to_string(i) + "=" + support::cpp11::to_string(final_strides[i]));
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000151 build_opts.add_option_if(is_shrink, "-DSHRINK_" + support::cpp11::to_string(i));
Georgios Pinitas77589b52018-08-21 14:41:35 +0100152 }
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100153 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)));
154 build_opts.add_option_if(multi_access_x, "-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x));
Georgios Pinitas77589b52018-08-21 14:41:35 +0100155 build_opts.add_option_if_else(input_shape.num_dimensions() > 2,
156 "-DSRC_DEPTH=" + support::cpp11::to_string(input_shape.z()),
157 "-DSRC_DEPTH=1");
158 build_opts.add_option_if_else(_output->info()->num_dimensions() > 2,
159 "-DDST_DEPTH=" + support::cpp11::to_string(_output->info()->tensor_shape().z()),
160 "-DDST_DEPTH=1");
161
162 // Create kernel
163 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("strided_slice", build_opts.options()));
164
165 // Set config_id for enabling LWS tuning
166 _config_id = "strided_slice";
167 _config_id += "_";
168 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
169 for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i)
170 {
171 _config_id += "_";
172 _config_id += support::cpp11::to_string(input->info()->dimension(i));
173 _config_id += "_";
174 _config_id += support::cpp11::to_string(starts_abs[i]);
175 _config_id += "_";
176 _config_id += support::cpp11::to_string(ends_abs[i]);
177 _config_id += "_";
178 _config_id += support::cpp11::to_string(final_strides[i]);
179 }
180}
181
182Status CLStridedSliceKernel::validate(const ITensorInfo *input, const ITensorInfo *output,
183 const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
184 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
185{
186 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask));
187 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(),
188 starts, ends, strides, begin_mask, end_mask, shrink_axis_mask)
189 .first);
190
191 return Status{};
192}
193
194void CLStridedSliceKernel::run(const Window &window, cl::CommandQueue &queue)
195{
196 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
197 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
198
199 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
200 Window slice = window_collapsed.first_slice_window_4D();
201
202 do
203 {
204 unsigned int idx = 0;
205 add_4D_tensor_argument(idx, _input, slice);
206 add_4D_tensor_argument(idx, _output, slice);
207 enqueue(queue, *this, slice, lws_hint());
208 }
209 while(window_collapsed.slide_window_slice_4D(slice));
210}
211} // namespace arm_compute