Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 702dc0c | 2021-03-19 15:06:07 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 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 | */ |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 24 | #include "src/core/CL/kernels/CLMinMaxLayerKernel.h" |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 25 | |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 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/Helpers.h" |
| 30 | #include "arm_compute/core/Validate.h" |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 32 | #include "src/core/AccessWindowStatic.h" |
| 33 | #include "src/core/helpers/AutoConfiguration.h" |
| 34 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 35 | #include "support/StringSupport.h" |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 36 | |
| 37 | #include <climits> |
| 38 | |
| 39 | using namespace arm_compute; |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 40 | using namespace arm_compute::misc::shape_calculator; |
| 41 | |
| 42 | namespace |
| 43 | { |
| 44 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output) |
| 45 | { |
| 46 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() < 3); |
| 49 | |
| 50 | if(output->tensor_shape().total_size() > 0) |
| 51 | { |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 53 | |
| 54 | TensorShape output_shape = compute_min_max_shape(input); |
| 55 | |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape); |
| 57 | } |
| 58 | |
| 59 | return Status{}; |
| 60 | } |
| 61 | |
| 62 | std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
| 63 | { |
| 64 | TensorShape output_shape = compute_min_max_shape(input); |
| 65 | |
| 66 | // Output auto initialization if not yet initialized |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 67 | auto_init_if_empty(*output, output_shape, 1, input->data_type()); |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 68 | |
| 69 | const unsigned int num_elems_processed_per_iteration = 1; |
| 70 | |
| 71 | // Configure kernel window |
| 72 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 73 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
| 74 | AccessWindowStatic output_access(output, 0, 0, 2, output->dimension(1)); |
| 75 | |
| 76 | bool window_changed = update_window_and_padding(win, input_access, output_access); |
| 77 | |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 78 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 79 | return std::make_tuple(err, win); |
| 80 | } |
| 81 | } // namespace |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 82 | |
| 83 | CLMinMaxLayerKernel::CLMinMaxLayerKernel() |
| 84 | : _input(nullptr), _output(nullptr) |
| 85 | { |
| 86 | } |
| 87 | |
| 88 | void CLMinMaxLayerKernel::configure(const ICLTensor *input, ICLTensor *output) |
| 89 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 90 | configure(CLKernelLibrary::get().get_compile_context(), input, output); |
| 91 | } |
| 92 | |
Manuel Bottini | 679fc96 | 2020-04-21 16:08:53 +0100 | [diff] [blame] | 93 | void CLMinMaxLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 94 | { |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 95 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 96 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info())); |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 97 | |
| 98 | _input = input; |
| 99 | _output = output; |
| 100 | |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 101 | std::set<std::string> build_opts; |
| 102 | build_opts.emplace("-DWIDTH=" + support::cpp11::to_string(input->info()->dimension(0))); |
| 103 | build_opts.emplace("-DHEIGHT=" + support::cpp11::to_string(input->info()->dimension(1))); |
| 104 | build_opts.emplace("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2))); |
| 105 | |
| 106 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 107 | _kernel = create_kernel(compile_context, "minmax_layer", build_opts); |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 108 | |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 109 | auto win_config = validate_and_configure_window(input->info(), output->info()); |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 110 | |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 111 | ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 112 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 113 | ICLKernel::configure_internal(std::get<1>(win_config)); |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 114 | } |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 115 | |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 116 | Status CLMinMaxLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output) |
| 117 | { |
| 118 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output)); |
| 119 | ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get()))); |
| 120 | |
| 121 | return Status{}; |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void CLMinMaxLayerKernel::reset(cl::CommandQueue &queue) |
| 125 | { |
| 126 | _output->map(queue, true); |
| 127 | |
| 128 | Window window_output; |
| 129 | window_output.use_tensor_dimensions(_output->info()->tensor_shape()); |
| 130 | window_output.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 131 | |
| 132 | Iterator output(_output, window_output); |
| 133 | |
| 134 | // Reset output |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 135 | execute_window_loop(window_output, [&](const Coordinates &) |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 136 | { |
| 137 | auto *ptr = reinterpret_cast<float *>(output.ptr()); |
| 138 | ptr[0] = std::numeric_limits<float>::max(); |
| 139 | ptr[1] = std::numeric_limits<float>::min(); |
| 140 | }, |
| 141 | output); |
| 142 | |
| 143 | _output->unmap(queue); |
| 144 | } |
| 145 | |
| 146 | void CLMinMaxLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 147 | { |
| 148 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 149 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 150 | |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 151 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), 3); |
| 152 | Window slice = window_collapsed.first_slice_window_3D(); |
| 153 | slice.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 154 | slice.set(Window::DimY, Window::Dimension(0, 1, 1)); |
| 155 | slice.set(Window::DimZ, Window::Dimension(0, 1, 1)); |
| 156 | |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 157 | do |
| 158 | { |
Michalis Spyrou | 995f552 | 2018-01-29 13:43:35 +0000 | [diff] [blame] | 159 | Window output_slice = slice.shift_dimensions(2); |
| 160 | |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 161 | unsigned int idx = 0; |
| 162 | // Set inputs |
| 163 | add_3D_tensor_argument(idx, _input, slice); |
| 164 | add_1D_tensor_argument(idx, _output, output_slice); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 165 | enqueue(queue, *this, slice, lws_hint()); |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 166 | } |
Michalis Spyrou | 995f552 | 2018-01-29 13:43:35 +0000 | [diff] [blame] | 167 | while(window_collapsed.slide_window_slice_3D(slice)); |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 168 | } |