blob: 2ff9196f13ca5feb60e8177392d7b969efad59f9 [file] [log] [blame]
Michele Di Giorgio56dd7262017-07-27 09:53:49 +01001/*
Matthew Bentham758b5ba2020-03-05 23:37:48 +00002 * Copyright (c) 2017-2020 ARM Limited.
Michele Di Giorgio56dd7262017-07-27 09:53:49 +01003 *
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/CLMinMaxLayerKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLKernelLibrary.h"
29#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/Validate.h"
32#include "arm_compute/core/Window.h"
Alex Gilday60954c62018-03-05 16:22:48 +000033#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000034#include "support/StringSupport.h"
Michele Di Giorgio56dd7262017-07-27 09:53:49 +010035
36#include <climits>
37
38using namespace arm_compute;
Alex Gilday60954c62018-03-05 16:22:48 +000039using namespace arm_compute::misc::shape_calculator;
40
41namespace
42{
43Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
44{
45 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
46 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
47 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() < 3);
48
49 if(output->tensor_shape().total_size() > 0)
50 {
51 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
52
53 TensorShape output_shape = compute_min_max_shape(input);
54
55 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
56 }
57
58 return Status{};
59}
60
61std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
62{
63 TensorShape output_shape = compute_min_max_shape(input);
64
65 // Output auto initialization if not yet initialized
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010066 auto_init_if_empty(*output, output_shape, 1, input->data_type());
Alex Gilday60954c62018-03-05 16:22:48 +000067
68 const unsigned int num_elems_processed_per_iteration = 1;
69
70 // Configure kernel window
71 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
72 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
73 AccessWindowStatic output_access(output, 0, 0, 2, output->dimension(1));
74
75 bool window_changed = update_window_and_padding(win, input_access, output_access);
76
77 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
78
79 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
80 return std::make_tuple(err, win);
81}
82} // namespace
Michele Di Giorgio56dd7262017-07-27 09:53:49 +010083
84CLMinMaxLayerKernel::CLMinMaxLayerKernel()
85 : _input(nullptr), _output(nullptr)
86{
87}
88
89void CLMinMaxLayerKernel::configure(const ICLTensor *input, ICLTensor *output)
90{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010091 configure(CLKernelLibrary::get().get_compile_context(), input, output);
92}
93
Manuel Bottini679fc962020-04-21 16:08:53 +010094void CLMinMaxLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010095{
Alex Gilday60954c62018-03-05 16:22:48 +000096 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
97 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
Michele Di Giorgio56dd7262017-07-27 09:53:49 +010098
99 _input = input;
100 _output = output;
101
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100102 std::set<std::string> build_opts;
103 build_opts.emplace("-DWIDTH=" + support::cpp11::to_string(input->info()->dimension(0)));
104 build_opts.emplace("-DHEIGHT=" + support::cpp11::to_string(input->info()->dimension(1)));
105 build_opts.emplace("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
106
107 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100108 _kernel = create_kernel(compile_context, "minmax_layer", build_opts);
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100109
Alex Gilday60954c62018-03-05 16:22:48 +0000110 auto win_config = validate_and_configure_window(input->info(), output->info());
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100111
Alex Gilday60954c62018-03-05 16:22:48 +0000112 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100113
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100114 ICLKernel::configure_internal(std::get<1>(win_config));
Alex Gilday60954c62018-03-05 16:22:48 +0000115}
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100116
Alex Gilday60954c62018-03-05 16:22:48 +0000117Status CLMinMaxLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output)
118{
119 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
120 ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get())));
121
122 return Status{};
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100123}
124
125void CLMinMaxLayerKernel::reset(cl::CommandQueue &queue)
126{
127 _output->map(queue, true);
128
129 Window window_output;
130 window_output.use_tensor_dimensions(_output->info()->tensor_shape());
131 window_output.set(Window::DimX, Window::Dimension(0, 1, 1));
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100132
133 Iterator output(_output, window_output);
134
135 // Reset output
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100136 execute_window_loop(window_output, [&](const Coordinates &)
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100137 {
138 auto *ptr = reinterpret_cast<float *>(output.ptr());
139 ptr[0] = std::numeric_limits<float>::max();
140 ptr[1] = std::numeric_limits<float>::min();
141 },
142 output);
143
144 _output->unmap(queue);
145}
146
147void CLMinMaxLayerKernel::run(const Window &window, cl::CommandQueue &queue)
148{
149 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
150 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
151
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100152 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), 3);
153 Window slice = window_collapsed.first_slice_window_3D();
154 slice.set(Window::DimX, Window::Dimension(0, 1, 1));
155 slice.set(Window::DimY, Window::Dimension(0, 1, 1));
156 slice.set(Window::DimZ, Window::Dimension(0, 1, 1));
157
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100158 do
159 {
Michalis Spyrou995f5522018-01-29 13:43:35 +0000160 Window output_slice = slice.shift_dimensions(2);
161
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100162 unsigned int idx = 0;
163 // Set inputs
164 add_3D_tensor_argument(idx, _input, slice);
165 add_1D_tensor_argument(idx, _output, output_slice);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100166 enqueue(queue, *this, slice, lws_hint());
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100167 }
Michalis Spyrou995f5522018-01-29 13:43:35 +0000168 while(window_collapsed.slide_window_slice_3D(slice));
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100169}