Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 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 | */ |
| 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 Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 35 | #include "arm_compute/core/utils/helpers/bit_ops.h" |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 36 | #include "arm_compute/core/utils/helpers/tensor_transform.h" |
| 37 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 38 | #include "support/StringSupport.h" |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | namespace |
| 43 | { |
| 44 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, |
| 45 | const Coordinates &starts, const Coordinates &ends, const BiStrides &strides, |
| 46 | int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask) |
| 47 | { |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 51 | |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().num_dimensions() > 4); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON(starts.num_dimensions() > input->num_dimensions()); |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON(ends.num_dimensions() > input->num_dimensions()); |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON(strides.num_dimensions() > input->num_dimensions()); |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 56 | 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] | 57 | { |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 58 | return i == 0; |
| 59 | })); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 60 | |
| 61 | // Get expected output shape |
| 62 | const TensorShape exp_output_shape = arm_compute::misc::shape_calculator::compute_strided_slice_shape(*input, |
| 63 | starts, ends, strides, |
| 64 | begin_mask, end_mask, shrink_axis_mask); |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON(exp_output_shape.total_size() == 0); |
| 66 | |
| 67 | // Checks output if configured |
| 68 | if(output->total_size() != 0) |
| 69 | { |
Georgios Pinitas | 2ade452 | 2018-12-07 13:23:39 +0000 | [diff] [blame] | 70 | const TensorInfo exp_output_info = output->clone()->set_tensor_shape(exp_output_shape); |
| 71 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &exp_output_info); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 72 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 73 | } |
| 74 | |
| 75 | return Status{}; |
| 76 | } |
| 77 | |
| 78 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, |
| 79 | const Coordinates &starts, const Coordinates &ends, const BiStrides &strides, |
| 80 | int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask) |
| 81 | { |
| 82 | // Output tensor auto initialization if not yet initialized |
| 83 | const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_strided_slice_shape(*input, |
| 84 | starts, ends, strides, |
| 85 | begin_mask, end_mask, shrink_axis_mask); |
| 86 | auto_init_if_empty(*output, input->clone()->set_tensor_shape(output_shape)); |
| 87 | |
| 88 | // Create window |
Michele Di Giorgio | 4646d2e | 2019-06-19 12:28:47 +0100 | [diff] [blame] | 89 | Window win = calculate_max_window(*output, Steps()); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 90 | output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape())); |
| 91 | |
| 92 | return std::make_pair(Status{}, win); |
| 93 | } |
| 94 | } // namespace |
| 95 | |
| 96 | CLStridedSliceKernel::CLStridedSliceKernel() |
| 97 | : _input(nullptr), _output(nullptr) |
| 98 | { |
| 99 | } |
| 100 | |
| 101 | void CLStridedSliceKernel::configure(const ICLTensor *input, ICLTensor *output, |
| 102 | const Coordinates &starts, const Coordinates &ends, const BiStrides &strides, |
| 103 | int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask) |
| 104 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 105 | configure(CLKernelLibrary::get().get_compile_context(), input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask); |
| 106 | } |
| 107 | |
Manuel Bottini | 2803f70 | 2020-04-21 16:20:03 +0100 | [diff] [blame] | 108 | void CLStridedSliceKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 109 | const Coordinates &starts, const Coordinates &ends, const BiStrides &strides, |
| 110 | int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask) |
| 111 | { |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 112 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 113 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), starts, ends, strides, begin_mask, end_mask, shrink_axis_mask)); |
| 114 | |
| 115 | _input = input; |
| 116 | _output = output; |
| 117 | |
| 118 | const TensorShape &input_shape = input->info()->tensor_shape(); |
| 119 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 120 | Coordinates starts_abs; |
| 121 | Coordinates ends_abs; |
| 122 | Coordinates final_strides; |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 123 | std::tie(starts_abs, ends_abs, final_strides) = arm_compute::helpers::tensor_transform::calculate_strided_slice_coords( |
| 124 | input_shape, |
| 125 | starts, ends, strides, |
| 126 | begin_mask, end_mask, shrink_axis_mask); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 127 | |
| 128 | // Configure kernel window |
| 129 | auto win_config = validate_and_configure_window(input->info(), output->info(), starts, ends, strides, begin_mask, end_mask, shrink_axis_mask); |
| 130 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 131 | |
| 132 | // Enable multiple elements processing along x if stride_x is 1 and output width greater than the access vector size |
| 133 | const int vec_size_x = 16 / input->info()->element_size(); |
| 134 | const int output_width_x = output->info()->tensor_shape().x(); |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 135 | const bool is_shrink_on_x = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, 0); |
| 136 | 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] | 137 | |
| 138 | // Update window if needed |
| 139 | if(multi_access_x) |
| 140 | { |
| 141 | Window &updated_window = std::get<1>(win_config); |
| 142 | updated_window.set(Window::DimX, |
| 143 | Window::Dimension(updated_window.x().start(), ceil_to_multiple(updated_window.x().end(), vec_size_x), vec_size_x)); |
| 144 | } |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 145 | ICLKernel::configure_internal(win_config.second); |
| 146 | |
| 147 | // Create build options |
| 148 | CLBuildOptions build_opts; |
| 149 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 150 | for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i) |
| 151 | { |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 152 | 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] | 153 | build_opts.add_option("-DSTART_" + support::cpp11::to_string(i) + "=" + support::cpp11::to_string(starts_abs[i])); |
| 154 | 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] | 155 | 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] | 156 | } |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 157 | 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))); |
| 158 | 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] | 159 | build_opts.add_option_if_else(input_shape.num_dimensions() > 2, |
| 160 | "-DSRC_DEPTH=" + support::cpp11::to_string(input_shape.z()), |
| 161 | "-DSRC_DEPTH=1"); |
| 162 | build_opts.add_option_if_else(_output->info()->num_dimensions() > 2, |
| 163 | "-DDST_DEPTH=" + support::cpp11::to_string(_output->info()->tensor_shape().z()), |
| 164 | "-DDST_DEPTH=1"); |
| 165 | |
| 166 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 167 | _kernel = create_kernel(compile_context, "strided_slice", build_opts.options()); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 168 | |
| 169 | // Set config_id for enabling LWS tuning |
| 170 | _config_id = "strided_slice"; |
| 171 | _config_id += "_"; |
| 172 | _config_id += lower_string(string_from_data_type(input->info()->data_type())); |
| 173 | for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i) |
| 174 | { |
| 175 | _config_id += "_"; |
| 176 | _config_id += support::cpp11::to_string(input->info()->dimension(i)); |
| 177 | _config_id += "_"; |
| 178 | _config_id += support::cpp11::to_string(starts_abs[i]); |
| 179 | _config_id += "_"; |
| 180 | _config_id += support::cpp11::to_string(ends_abs[i]); |
| 181 | _config_id += "_"; |
| 182 | _config_id += support::cpp11::to_string(final_strides[i]); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | Status CLStridedSliceKernel::validate(const ITensorInfo *input, const ITensorInfo *output, |
| 187 | const Coordinates &starts, const Coordinates &ends, const BiStrides &strides, |
| 188 | int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask) |
| 189 | { |
| 190 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask)); |
| 191 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), |
| 192 | starts, ends, strides, begin_mask, end_mask, shrink_axis_mask) |
| 193 | .first); |
| 194 | |
| 195 | return Status{}; |
| 196 | } |
| 197 | |
| 198 | void CLStridedSliceKernel::run(const Window &window, cl::CommandQueue &queue) |
| 199 | { |
| 200 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 201 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 202 | |
| 203 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 204 | Window slice = window_collapsed.first_slice_window_4D(); |
| 205 | |
| 206 | do |
| 207 | { |
| 208 | unsigned int idx = 0; |
| 209 | add_4D_tensor_argument(idx, _input, slice); |
| 210 | add_4D_tensor_argument(idx, _output, slice); |
| 211 | enqueue(queue, *this, slice, lws_hint()); |
| 212 | } |
| 213 | while(window_collapsed.slide_window_slice_4D(slice)); |
| 214 | } |
| 215 | } // namespace arm_compute |