• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019-2020 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 "src/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/IAccessWindow.h"
30 #include "arm_compute/core/TensorInfo.h"
31 #include "src/core/CPP/Validate.h"
32 #include "src/core/helpers/WindowHelpers.h"
33 
34 #include "support/StringSupport.h"
35 
36 #include <map>
37 
38 namespace arm_compute
39 {
CLCropKernel()40 CLCropKernel::CLCropKernel()
41     : _input(nullptr), _output(nullptr), _start(), _batch_index(0), _extrapolation_value(0)
42 {
43 }
44 
configure(const ICLTensor * input,ICLTensor * output,Coordinates2D start,Coordinates2D end,uint32_t batch_index,float extrapolation_value,Window * output_window)45 void CLCropKernel::configure(const ICLTensor *input, ICLTensor *output, Coordinates2D start, Coordinates2D end, uint32_t batch_index, float extrapolation_value, Window *output_window)
46 {
47     configure(CLKernelLibrary::get().get_compile_context(), input, output, start, end, batch_index, extrapolation_value, output_window);
48 }
49 
configure(const CLCompileContext & compile_context,const ICLTensor * input,ICLTensor * output,Coordinates2D start,Coordinates2D end,uint32_t batch_index,float extrapolation_value,Window * output_window)50 void 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)
52 {
53     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=");
90     _kernel = create_kernel(compile_context, "crop_tensor", build_opts.options());
91 }
92 
validate(const ITensorInfo * input,const ITensorInfo * output,Coordinates2D start,Coordinates2D end,uint32_t batch_index,float extrapolation_value,Window * output_window)93 Status 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);
97     ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
98     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 
run(const Window & window,cl::CommandQueue & queue)117 void 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);
132     enqueue(queue, *this, window, lws_hint());
133 }
134 } // namespace arm_compute
135