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