Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, 2023 Arm Limited. |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 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 | */ |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 24 | #include "src/core/CL/kernels/CLStridedSliceKernel.h" |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 25 | #include "arm_compute/core/CL/ICLTensor.h" |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/TensorInfo.h" |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/utils/helpers/tensor_transform.h" |
| 28 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/utils/StringUtils.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 30 | #include "src/core/helpers/AutoConfiguration.h" |
| 31 | #include "src/core/helpers/WindowHelpers.h" |
| 32 | #include "src/core/utils/helpers/bit_ops.h" |
| 33 | #include "support/Cast.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 34 | #include "support/StringSupport.h" |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace |
| 39 | { |
| 40 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, |
| 41 | const Coordinates &starts, const Coordinates &ends, const BiStrides &strides, |
| 42 | int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask) |
| 43 | { |
| 44 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 45 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 46 | |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().num_dimensions() > 4); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON(starts.num_dimensions() > input->num_dimensions()); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON(ends.num_dimensions() > input->num_dimensions()); |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON(strides.num_dimensions() > input->num_dimensions()); |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON(std::any_of(strides.cbegin(), strides.cbegin() + strides.num_dimensions(), [](int i) |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 52 | { |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 53 | return i == 0; |
| 54 | })); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 55 | |
| 56 | // Get expected output shape |
| 57 | const TensorShape exp_output_shape = arm_compute::misc::shape_calculator::compute_strided_slice_shape(*input, |
| 58 | starts, ends, strides, |
| 59 | begin_mask, end_mask, shrink_axis_mask); |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON(exp_output_shape.total_size() == 0); |
| 61 | |
| 62 | // Checks output if configured |
| 63 | if(output->total_size() != 0) |
| 64 | { |
Georgios Pinitas | 2ade452 | 2018-12-07 13:23:39 +0000 | [diff] [blame] | 65 | const TensorInfo exp_output_info = output->clone()->set_tensor_shape(exp_output_shape); |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &exp_output_info); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 67 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 68 | } |
| 69 | |
| 70 | return Status{}; |
| 71 | } |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 72 | } // namespace |
| 73 | |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 74 | CLStridedSliceKernel::CLStridedSliceKernel() |
| 75 | { |
| 76 | _type = CLKernelType::ELEMENTWISE; |
| 77 | } |
| 78 | |
Michalis Spyrou | f20d6d6 | 2020-07-16 17:46:51 +0100 | [diff] [blame] | 79 | void CLStridedSliceKernel::configure(const CLCompileContext &compile_context, const ITensorInfo *input, ITensorInfo *output, |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 80 | const Coordinates &starts, const Coordinates &ends, const BiStrides &strides, |
| 81 | int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask) |
| 82 | { |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 83 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Manuel Bottini | b6869dd | 2020-12-16 15:34:25 +0000 | [diff] [blame] | 84 | auto padding_info = get_padding_info({ input, output }); |
Michalis Spyrou | f20d6d6 | 2020-07-16 17:46:51 +0100 | [diff] [blame] | 85 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask)); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 86 | |
Michalis Spyrou | f20d6d6 | 2020-07-16 17:46:51 +0100 | [diff] [blame] | 87 | const TensorShape &input_shape = input->tensor_shape(); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 88 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 89 | Coordinates starts_abs; |
| 90 | Coordinates ends_abs; |
| 91 | Coordinates final_strides; |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 92 | std::tie(starts_abs, ends_abs, final_strides) = arm_compute::helpers::tensor_transform::calculate_strided_slice_coords( |
| 93 | input_shape, |
| 94 | starts, ends, strides, |
| 95 | begin_mask, end_mask, shrink_axis_mask); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 96 | |
| 97 | // Configure kernel window |
Michalis Spyrou | 702dc0c | 2021-03-19 15:06:07 +0000 | [diff] [blame] | 98 | const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_strided_slice_shape(*input, |
| 99 | starts, ends, strides, |
| 100 | begin_mask, end_mask, shrink_axis_mask); |
| 101 | auto_init_if_empty(*output, input->clone()->set_tensor_shape(output_shape)); |
| 102 | Window win = calculate_max_window(*output, Steps()); |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 103 | |
| 104 | // Enable multiple elements processing along x if stride_x is 1 and output width greater than the access vector size |
Michalis Spyrou | f20d6d6 | 2020-07-16 17:46:51 +0100 | [diff] [blame] | 105 | const int vec_size_x = 16 / input->element_size(); |
| 106 | const int output_width_x = output->tensor_shape().x(); |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 107 | const bool is_shrink_on_x = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, 0); |
| 108 | const bool multi_access_x = !is_shrink_on_x && (final_strides.x() == 1) && (output_width_x / vec_size_x > 0); |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 109 | |
| 110 | // Update window if needed |
| 111 | if(multi_access_x) |
| 112 | { |
Michalis Spyrou | 702dc0c | 2021-03-19 15:06:07 +0000 | [diff] [blame] | 113 | Window &updated_window = win; |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 114 | updated_window.set(Window::DimX, |
| 115 | Window::Dimension(updated_window.x().start(), ceil_to_multiple(updated_window.x().end(), vec_size_x), vec_size_x)); |
| 116 | } |
Michalis Spyrou | 702dc0c | 2021-03-19 15:06:07 +0000 | [diff] [blame] | 117 | ICLKernel::configure_internal(win); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 118 | |
| 119 | // Create build options |
| 120 | CLBuildOptions build_opts; |
Michalis Spyrou | f20d6d6 | 2020-07-16 17:46:51 +0100 | [diff] [blame] | 121 | build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(data_size_from_type(input->data_type()))); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 122 | for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i) |
| 123 | { |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 124 | const bool is_shrink = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, i); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 125 | build_opts.add_option("-DSTART_" + support::cpp11::to_string(i) + "=" + support::cpp11::to_string(starts_abs[i])); |
| 126 | build_opts.add_option("-DSTRIDE_" + support::cpp11::to_string(i) + "=" + support::cpp11::to_string(final_strides[i])); |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 127 | build_opts.add_option_if(is_shrink, "-DSHRINK_" + support::cpp11::to_string(i)); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 128 | } |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 129 | 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))); |
| 130 | build_opts.add_option_if(multi_access_x, "-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x)); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 131 | build_opts.add_option_if_else(input_shape.num_dimensions() > 2, |
| 132 | "-DSRC_DEPTH=" + support::cpp11::to_string(input_shape.z()), |
| 133 | "-DSRC_DEPTH=1"); |
Michalis Spyrou | f20d6d6 | 2020-07-16 17:46:51 +0100 | [diff] [blame] | 134 | build_opts.add_option_if_else(output->num_dimensions() > 2, |
| 135 | "-DDST_DEPTH=" + support::cpp11::to_string(output->tensor_shape().z()), |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 136 | "-DDST_DEPTH=1"); |
| 137 | |
| 138 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 139 | _kernel = create_kernel(compile_context, "strided_slice", build_opts.options()); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 140 | |
| 141 | // Set config_id for enabling LWS tuning |
| 142 | _config_id = "strided_slice"; |
| 143 | _config_id += "_"; |
Michalis Spyrou | f20d6d6 | 2020-07-16 17:46:51 +0100 | [diff] [blame] | 144 | _config_id += lower_string(string_from_data_type(input->data_type())); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 145 | for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i) |
| 146 | { |
| 147 | _config_id += "_"; |
Michalis Spyrou | f20d6d6 | 2020-07-16 17:46:51 +0100 | [diff] [blame] | 148 | _config_id += support::cpp11::to_string(input->dimension(i)); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 149 | _config_id += "_"; |
| 150 | _config_id += support::cpp11::to_string(starts_abs[i]); |
| 151 | _config_id += "_"; |
| 152 | _config_id += support::cpp11::to_string(ends_abs[i]); |
| 153 | _config_id += "_"; |
| 154 | _config_id += support::cpp11::to_string(final_strides[i]); |
| 155 | } |
Manuel Bottini | b6869dd | 2020-12-16 15:34:25 +0000 | [diff] [blame] | 156 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | Status CLStridedSliceKernel::validate(const ITensorInfo *input, const ITensorInfo *output, |
| 160 | const Coordinates &starts, const Coordinates &ends, const BiStrides &strides, |
| 161 | int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask) |
| 162 | { |
| 163 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask)); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 164 | |
| 165 | return Status{}; |
| 166 | } |
| 167 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 168 | void CLStridedSliceKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 169 | { |
| 170 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 171 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 172 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 173 | const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC)); |
| 174 | auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST)); |
Michalis Spyrou | f20d6d6 | 2020-07-16 17:46:51 +0100 | [diff] [blame] | 175 | |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 176 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 177 | Window slice = window_collapsed.first_slice_window_4D(); |
| 178 | |
| 179 | do |
| 180 | { |
| 181 | unsigned int idx = 0; |
Michalis Spyrou | f20d6d6 | 2020-07-16 17:46:51 +0100 | [diff] [blame] | 182 | add_4D_tensor_argument(idx, src, slice); |
| 183 | add_4D_tensor_argument(idx, dst, slice); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 184 | enqueue(queue, *this, slice, lws_hint()); |
| 185 | } |
| 186 | while(window_collapsed.slide_window_slice_4D(slice)); |
| 187 | } |
| 188 | } // namespace arm_compute |