blob: 2c99d46929f95cbd3a142833afab71438e8e6934 [file] [log] [blame]
George Wort894066d2019-02-15 15:12:52 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * 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"
George Wort894066d2019-02-15 15:12:52 +000029#include "arm_compute/core/IAccessWindow.h"
30#include "arm_compute/core/TensorInfo.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/CPP/Validate.h"
32#include "src/core/helpers/WindowHelpers.h"
George Wort894066d2019-02-15 15:12:52 +000033
Matthew Bentham758b5ba2020-03-05 23:37:48 +000034#include "support/StringSupport.h"
George Wort894066d2019-02-15 15:12:52 +000035
36#include <map>
37
38namespace arm_compute
39{
40CLCropKernel::CLCropKernel()
41 : _input(nullptr), _output(nullptr), _start(), _batch_index(0), _extrapolation_value(0)
42{
43}
44
45void CLCropKernel::configure(const ICLTensor *input, ICLTensor *output, Coordinates2D start, Coordinates2D end, uint32_t batch_index, float extrapolation_value, Window *output_window)
46{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010047 configure(CLKernelLibrary::get().get_compile_context(), input, output, start, end, batch_index, extrapolation_value, output_window);
48}
49
Manuel Bottini256c0b92020-04-21 13:29:30 +010050void CLCropKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, Coordinates2D start, Coordinates2D end, uint32_t batch_index,
51 float extrapolation_value, Window *output_window)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010052{
George Wort894066d2019-02-15 15:12:52 +000053 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
54 ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), start, end, batch_index, extrapolation_value, output_window));
55
56 _input = input;
57 _output = output;
58 _start = start;
59 _batch_index = batch_index;
60 _extrapolation_value = extrapolation_value;
61
62 const int vec_size_x = 4;
63 // Create and update the window (if needed)
64 Window win = calculate_max_window(*output->info());
65
66 if(output_window != nullptr)
67 {
68 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(win, *output_window);
69 win = *output_window;
70 }
71
72 const int output_width_x = win.num_iterations(0);
73 const bool multi_access_x = output_width_x >= vec_size_x;
74 const bool remainder_x = output_width_x % vec_size_x > 0;
75
76 if(multi_access_x)
77 {
78 win.set(Window::DimX,
79 Window::Dimension(win.x().start(), ceil_to_multiple(win.x().end(), vec_size_x), vec_size_x));
80 }
81 ICLKernel::configure_internal(win);
82
83 // Create kernel
84 CLBuildOptions build_opts;
85 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
86 build_opts.add_option_if(multi_access_x, "-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x));
87 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)));
88 build_opts.add_option_if(start.x > end.x, "-DWIDTH_FLIPPED=");
89 build_opts.add_option_if(start.y > end.y, "-DHEIGHT_FLIPPED=");
Manuel Bottini4c6bd512020-04-08 10:15:51 +010090 _kernel = create_kernel(compile_context, "crop_tensor", build_opts.options());
George Wort894066d2019-02-15 15:12:52 +000091}
92
93Status CLCropKernel::validate(const ITensorInfo *input, const ITensorInfo *output, Coordinates2D start, Coordinates2D end, uint32_t batch_index, float extrapolation_value, Window *output_window)
94{
95 ARM_COMPUTE_UNUSED(extrapolation_value, output_window);
96 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Michele Di Giorgio7d6b5772020-06-01 13:39:52 +010097 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
George Wort894066d2019-02-15 15:12:52 +000098 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(input, DataLayout::NHWC);
99 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().num_dimensions() > 4);
100 ARM_COMPUTE_RETURN_ERROR_ON(start.x < 0 || start.y < 0 || end.x < 0 || end.y < 0);
101 ARM_COMPUTE_RETURN_ERROR_ON(start.x >= static_cast<int32_t>(input->dimension(1)) || start.y >= static_cast<int32_t>(input->dimension(2))
102 || end.x >= static_cast<int32_t>(input->dimension(1)) || end.y >= static_cast<int32_t>(input->dimension(2)));
103 ARM_COMPUTE_RETURN_ERROR_ON(batch_index >= input->dimension(3));
104 if(output_window != nullptr)
105 {
106 ARM_COMPUTE_RETURN_ERROR_ON(output_window->x().step() != 1);
107 }
108 if(output->total_size() > 0)
109 {
110 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(output, DataType::F32);
111 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
112 ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 3);
113 }
114 return Status{};
115}
116
117void CLCropKernel::run(const Window &window, cl::CommandQueue &queue)
118{
119 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
120 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
121
122 Window in_slice = Window();
123 in_slice.use_tensor_dimensions(_input->info()->tensor_shape());
124 in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start(), ceil_to_multiple(in_slice.x().end(), window.x().step()), window.x().step()));
125 in_slice.set(3, Window::Dimension(_batch_index, _batch_index + 1, 1));
126
127 unsigned int idx = 0;
128 add_3D_tensor_argument(idx, _input, in_slice);
129 add_3D_tensor_argument(idx, _output, window);
130 add_argument(idx, _start.x);
131 add_argument(idx, _start.y);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100132 enqueue(queue, *this, window, lws_hint());
George Wort894066d2019-02-15 15:12:52 +0000133}
134} // namespace arm_compute