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