George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 1 | /* |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 2 | * Copyright (c) 2019-2020 ARM Limited. |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [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/CLCropKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 28 | #include "arm_compute/core/CL/ICLTensor.h" |
| 29 | #include "arm_compute/core/CPP/Validate.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/Helpers.h" |
| 35 | #include "arm_compute/core/Types.h" |
| 36 | #include "arm_compute/core/utils/helpers/bit_ops.h" |
| 37 | #include "arm_compute/core/utils/helpers/tensor_transform.h" |
| 38 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 39 | #include "support/StringSupport.h" |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 40 | |
| 41 | #include <map> |
| 42 | |
| 43 | namespace arm_compute |
| 44 | { |
| 45 | CLCropKernel::CLCropKernel() |
| 46 | : _input(nullptr), _output(nullptr), _start(), _batch_index(0), _extrapolation_value(0) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | void CLCropKernel::configure(const ICLTensor *input, ICLTensor *output, Coordinates2D start, Coordinates2D end, uint32_t batch_index, float extrapolation_value, Window *output_window) |
| 51 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 52 | configure(CLKernelLibrary::get().get_compile_context(), input, output, start, end, batch_index, extrapolation_value, output_window); |
| 53 | } |
| 54 | |
Manuel Bottini | 256c0b9 | 2020-04-21 13:29:30 +0100 | [diff] [blame^] | 55 | void CLCropKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, Coordinates2D start, Coordinates2D end, uint32_t batch_index, |
| 56 | float extrapolation_value, Window *output_window) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 57 | { |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 58 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 59 | ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), start, end, batch_index, extrapolation_value, output_window)); |
| 60 | |
| 61 | _input = input; |
| 62 | _output = output; |
| 63 | _start = start; |
| 64 | _batch_index = batch_index; |
| 65 | _extrapolation_value = extrapolation_value; |
| 66 | |
| 67 | const int vec_size_x = 4; |
| 68 | // Create and update the window (if needed) |
| 69 | Window win = calculate_max_window(*output->info()); |
| 70 | |
| 71 | if(output_window != nullptr) |
| 72 | { |
| 73 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(win, *output_window); |
| 74 | win = *output_window; |
| 75 | } |
| 76 | |
| 77 | const int output_width_x = win.num_iterations(0); |
| 78 | const bool multi_access_x = output_width_x >= vec_size_x; |
| 79 | const bool remainder_x = output_width_x % vec_size_x > 0; |
| 80 | |
| 81 | if(multi_access_x) |
| 82 | { |
| 83 | win.set(Window::DimX, |
| 84 | Window::Dimension(win.x().start(), ceil_to_multiple(win.x().end(), vec_size_x), vec_size_x)); |
| 85 | } |
| 86 | ICLKernel::configure_internal(win); |
| 87 | |
| 88 | // Create kernel |
| 89 | CLBuildOptions build_opts; |
| 90 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 91 | build_opts.add_option_if(multi_access_x, "-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x)); |
| 92 | build_opts.add_option_if(multi_access_x && remainder_x, "-DLAST_ACCESSED_X=" + support::cpp11::to_string(std::max<int>(output_width_x - vec_size_x, 0))); |
| 93 | build_opts.add_option_if(start.x > end.x, "-DWIDTH_FLIPPED="); |
| 94 | build_opts.add_option_if(start.y > end.y, "-DHEIGHT_FLIPPED="); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 95 | _kernel = create_kernel(compile_context, "crop_tensor", build_opts.options()); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | Status CLCropKernel::validate(const ITensorInfo *input, const ITensorInfo *output, Coordinates2D start, Coordinates2D end, uint32_t batch_index, float extrapolation_value, Window *output_window) |
| 99 | { |
| 100 | ARM_COMPUTE_UNUSED(extrapolation_value, output_window); |
| 101 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
| 102 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U16, DataType::S16, DataType::F16, DataType::U32, DataType::S32, DataType::F32); |
| 103 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(input, DataLayout::NHWC); |
| 104 | ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().num_dimensions() > 4); |
| 105 | ARM_COMPUTE_RETURN_ERROR_ON(start.x < 0 || start.y < 0 || end.x < 0 || end.y < 0); |
| 106 | ARM_COMPUTE_RETURN_ERROR_ON(start.x >= static_cast<int32_t>(input->dimension(1)) || start.y >= static_cast<int32_t>(input->dimension(2)) |
| 107 | || end.x >= static_cast<int32_t>(input->dimension(1)) || end.y >= static_cast<int32_t>(input->dimension(2))); |
| 108 | ARM_COMPUTE_RETURN_ERROR_ON(batch_index >= input->dimension(3)); |
| 109 | if(output_window != nullptr) |
| 110 | { |
| 111 | ARM_COMPUTE_RETURN_ERROR_ON(output_window->x().step() != 1); |
| 112 | } |
| 113 | if(output->total_size() > 0) |
| 114 | { |
| 115 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(output, DataType::F32); |
| 116 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
| 117 | ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 3); |
| 118 | } |
| 119 | return Status{}; |
| 120 | } |
| 121 | |
| 122 | void CLCropKernel::run(const Window &window, cl::CommandQueue &queue) |
| 123 | { |
| 124 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 125 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 126 | |
| 127 | Window in_slice = Window(); |
| 128 | in_slice.use_tensor_dimensions(_input->info()->tensor_shape()); |
| 129 | in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start(), ceil_to_multiple(in_slice.x().end(), window.x().step()), window.x().step())); |
| 130 | in_slice.set(3, Window::Dimension(_batch_index, _batch_index + 1, 1)); |
| 131 | |
| 132 | unsigned int idx = 0; |
| 133 | add_3D_tensor_argument(idx, _input, in_slice); |
| 134 | add_3D_tensor_argument(idx, _output, window); |
| 135 | add_argument(idx, _start.x); |
| 136 | add_argument(idx, _start.y); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 137 | enqueue(queue, *this, window, lws_hint()); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 138 | } |
| 139 | } // namespace arm_compute |