blob: e51861e9aa0d7a3cc2f5e967183a0859d5af8673 [file] [log] [blame]
George Wort894066d2019-02-15 15:12:52 +00001/*
Matthew Bentham758b5ba2020-03-05 23:37:48 +00002 * Copyright (c) 2019-2020 ARM Limited.
George Wort894066d2019-02-15 15:12:52 +00003 *
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 Bentham758b5ba2020-03-05 23:37:48 +000039#include "support/StringSupport.h"
George Wort894066d2019-02-15 15:12:52 +000040
41#include <map>
42
43namespace arm_compute
44{
45CLCropKernel::CLCropKernel()
46 : _input(nullptr), _output(nullptr), _start(), _batch_index(0), _extrapolation_value(0)
47{
48}
49
50void CLCropKernel::configure(const ICLTensor *input, ICLTensor *output, Coordinates2D start, Coordinates2D end, uint32_t batch_index, float extrapolation_value, Window *output_window)
51{
52 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
53 ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), start, end, batch_index, extrapolation_value, output_window));
54
55 _input = input;
56 _output = output;
57 _start = start;
58 _batch_index = batch_index;
59 _extrapolation_value = extrapolation_value;
60
61 const int vec_size_x = 4;
62 // Create and update the window (if needed)
63 Window win = calculate_max_window(*output->info());
64
65 if(output_window != nullptr)
66 {
67 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(win, *output_window);
68 win = *output_window;
69 }
70
71 const int output_width_x = win.num_iterations(0);
72 const bool multi_access_x = output_width_x >= vec_size_x;
73 const bool remainder_x = output_width_x % vec_size_x > 0;
74
75 if(multi_access_x)
76 {
77 win.set(Window::DimX,
78 Window::Dimension(win.x().start(), ceil_to_multiple(win.x().end(), vec_size_x), vec_size_x));
79 }
80 ICLKernel::configure_internal(win);
81
82 // Create kernel
83 CLBuildOptions build_opts;
84 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
85 build_opts.add_option_if(multi_access_x, "-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x));
86 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)));
87 build_opts.add_option_if(start.x > end.x, "-DWIDTH_FLIPPED=");
88 build_opts.add_option_if(start.y > end.y, "-DHEIGHT_FLIPPED=");
89 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("crop_tensor", build_opts.options()));
90}
91
92Status CLCropKernel::validate(const ITensorInfo *input, const ITensorInfo *output, Coordinates2D start, Coordinates2D end, uint32_t batch_index, float extrapolation_value, Window *output_window)
93{
94 ARM_COMPUTE_UNUSED(extrapolation_value, output_window);
95 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
96 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U16, DataType::S16, DataType::F16, DataType::U32, DataType::S32, DataType::F32);
97 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(input, DataLayout::NHWC);
98 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().num_dimensions() > 4);
99 ARM_COMPUTE_RETURN_ERROR_ON(start.x < 0 || start.y < 0 || end.x < 0 || end.y < 0);
100 ARM_COMPUTE_RETURN_ERROR_ON(start.x >= static_cast<int32_t>(input->dimension(1)) || start.y >= static_cast<int32_t>(input->dimension(2))
101 || end.x >= static_cast<int32_t>(input->dimension(1)) || end.y >= static_cast<int32_t>(input->dimension(2)));
102 ARM_COMPUTE_RETURN_ERROR_ON(batch_index >= input->dimension(3));
103 if(output_window != nullptr)
104 {
105 ARM_COMPUTE_RETURN_ERROR_ON(output_window->x().step() != 1);
106 }
107 if(output->total_size() > 0)
108 {
109 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(output, DataType::F32);
110 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
111 ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 3);
112 }
113 return Status{};
114}
115
116void CLCropKernel::run(const Window &window, cl::CommandQueue &queue)
117{
118 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
119 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
120
121 Window in_slice = Window();
122 in_slice.use_tensor_dimensions(_input->info()->tensor_shape());
123 in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start(), ceil_to_multiple(in_slice.x().end(), window.x().step()), window.x().step()));
124 in_slice.set(3, Window::Dimension(_batch_index, _batch_index + 1, 1));
125
126 unsigned int idx = 0;
127 add_3D_tensor_argument(idx, _input, in_slice);
128 add_3D_tensor_argument(idx, _output, window);
129 add_argument(idx, _start.x);
130 add_argument(idx, _start.y);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100131 enqueue(queue, *this, window, lws_hint());
George Wort894066d2019-02-15 15:12:52 +0000132}
133} // namespace arm_compute