• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018-2020 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 "src/core/CL/kernels/CLStridedSliceKernel.h"
25 #include "arm_compute/core/CL/ICLTensor.h"
26 #include "arm_compute/core/TensorInfo.h"
27 #include "arm_compute/core/utils/helpers/tensor_transform.h"
28 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
29 #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"
33 #include "support/StringSupport.h"
34 
35 namespace arm_compute
36 {
37 namespace
38 {
validate_arguments(const ITensorInfo * input,const ITensorInfo * output,const Coordinates & starts,const Coordinates & ends,const BiStrides & strides,int32_t begin_mask,int32_t end_mask,int32_t shrink_axis_mask)39 Status 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);
44     ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
45 
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());
50     ARM_COMPUTE_RETURN_ERROR_ON(std::any_of(strides.cbegin(), strides.cbegin() + strides.num_dimensions(), [](int i)
51     {
52         return i == 0;
53     }));
54 
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     {
64         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);
66         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
67     }
68 
69     return Status{};
70 }
71 
validate_and_configure_window(const ITensorInfo * input,ITensorInfo * output,const Coordinates & starts,const Coordinates & ends,const BiStrides & strides,int32_t begin_mask,int32_t end_mask,int32_t shrink_axis_mask)72 std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *input, ITensorInfo *output,
73                                                         const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
74                                                         int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
75 {
76     // Output tensor auto initialization if not yet initialized
77     const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_strided_slice_shape(*input,
78                                                                                                       starts, ends, strides,
79                                                                                                       begin_mask, end_mask, shrink_axis_mask);
80     auto_init_if_empty(*output, input->clone()->set_tensor_shape(output_shape));
81 
82     // Create window
83     Window win = calculate_max_window(*output, Steps());
84     output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
85 
86     return std::make_pair(Status{}, win);
87 }
88 } // namespace
89 
configure(const CLCompileContext & compile_context,const ITensorInfo * input,ITensorInfo * output,const Coordinates & starts,const Coordinates & ends,const BiStrides & strides,int32_t begin_mask,int32_t end_mask,int32_t shrink_axis_mask)90 void CLStridedSliceKernel::configure(const CLCompileContext &compile_context, const ITensorInfo *input, ITensorInfo *output,
91                                      const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
92                                      int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
93 {
94     ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
95     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask));
96 
97     const TensorShape &input_shape = input->tensor_shape();
98 
99     Coordinates starts_abs;
100     Coordinates ends_abs;
101     Coordinates final_strides;
102     std::tie(starts_abs, ends_abs, final_strides) = arm_compute::helpers::tensor_transform::calculate_strided_slice_coords(
103                                                         input_shape,
104                                                         starts, ends, strides,
105                                                         begin_mask, end_mask, shrink_axis_mask);
106 
107     // Configure kernel window
108     auto win_config = validate_and_configure_window(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask);
109     ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
110 
111     // Enable multiple elements processing along x if stride_x is 1 and output width greater than the access vector size
112     const int  vec_size_x     = 16 / input->element_size();
113     const int  output_width_x = output->tensor_shape().x();
114     const bool is_shrink_on_x = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, 0);
115     const bool multi_access_x = !is_shrink_on_x && (final_strides.x() == 1) && (output_width_x / vec_size_x > 0);
116 
117     // Update window if needed
118     if(multi_access_x)
119     {
120         Window &updated_window = std::get<1>(win_config);
121         updated_window.set(Window::DimX,
122                            Window::Dimension(updated_window.x().start(), ceil_to_multiple(updated_window.x().end(), vec_size_x), vec_size_x));
123     }
124     ICLKernel::configure_internal(win_config.second);
125 
126     // Create build options
127     CLBuildOptions build_opts;
128     build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(data_size_from_type(input->data_type())));
129     for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i)
130     {
131         const bool is_shrink = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, i);
132         build_opts.add_option("-DSTART_" + support::cpp11::to_string(i) + "=" + support::cpp11::to_string(starts_abs[i]));
133         build_opts.add_option("-DSTRIDE_" + support::cpp11::to_string(i) + "=" + support::cpp11::to_string(final_strides[i]));
134         build_opts.add_option_if(is_shrink, "-DSHRINK_" + support::cpp11::to_string(i));
135     }
136     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)));
137     build_opts.add_option_if(multi_access_x, "-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x));
138     build_opts.add_option_if_else(input_shape.num_dimensions() > 2,
139                                   "-DSRC_DEPTH=" + support::cpp11::to_string(input_shape.z()),
140                                   "-DSRC_DEPTH=1");
141     build_opts.add_option_if_else(output->num_dimensions() > 2,
142                                   "-DDST_DEPTH=" + support::cpp11::to_string(output->tensor_shape().z()),
143                                   "-DDST_DEPTH=1");
144 
145     // Create kernel
146     _kernel = create_kernel(compile_context, "strided_slice", build_opts.options());
147 
148     // Set config_id for enabling LWS tuning
149     _config_id = "strided_slice";
150     _config_id += "_";
151     _config_id += lower_string(string_from_data_type(input->data_type()));
152     for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i)
153     {
154         _config_id += "_";
155         _config_id += support::cpp11::to_string(input->dimension(i));
156         _config_id += "_";
157         _config_id += support::cpp11::to_string(starts_abs[i]);
158         _config_id += "_";
159         _config_id += support::cpp11::to_string(ends_abs[i]);
160         _config_id += "_";
161         _config_id += support::cpp11::to_string(final_strides[i]);
162     }
163 }
164 
validate(const ITensorInfo * input,const ITensorInfo * output,const Coordinates & starts,const Coordinates & ends,const BiStrides & strides,int32_t begin_mask,int32_t end_mask,int32_t shrink_axis_mask)165 Status CLStridedSliceKernel::validate(const ITensorInfo *input, const ITensorInfo *output,
166                                       const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
167                                       int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
168 {
169     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask));
170     ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(),
171                                                               starts, ends, strides, begin_mask, end_mask, shrink_axis_mask)
172                                 .first);
173 
174     return Status{};
175 }
176 
run_op(ITensorPack & tensors,const Window & window,cl::CommandQueue & queue)177 void CLStridedSliceKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
178 {
179     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
180     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
181 
182     const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
183     auto       dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
184 
185     Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
186     Window slice            = window_collapsed.first_slice_window_4D();
187 
188     do
189     {
190         unsigned int idx = 0;
191         add_4D_tensor_argument(idx, src, slice);
192         add_4D_tensor_argument(idx, dst, slice);
193         enqueue(queue, *this, slice, lws_hint());
194     }
195     while(window_collapsed.slide_window_slice_4D(slice));
196 }
197 } // namespace arm_compute
198