Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 1 | /* |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2021 Arm Limited. |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [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/CLReverseKernel.h" |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/CL/ICLTensor.h" |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 31 | #include "src/core/CL/CLValidate.h" |
| 32 | #include "src/core/helpers/AutoConfiguration.h" |
| 33 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 34 | #include "support/StringSupport.h" |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace |
| 39 | { |
| 40 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *axis) |
| 41 | { |
| 42 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, axis); |
| 43 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 44 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 45 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(axis, 1, DataType::U32); |
| 46 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis->num_dimensions() > 1, "Axis must be a 1D tensor"); |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis->dimension(0) > 4, "Only up to 4 dimensions can be reversed"); |
| 48 | |
| 49 | // Checks performed when output is configured |
| 50 | if(output->total_size() != 0) |
| 51 | { |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 54 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | return Status{}; |
| 58 | } |
| 59 | } // namespace |
| 60 | |
| 61 | CLReverseKernel::CLReverseKernel() |
| 62 | : _input(nullptr), _output(nullptr), _axis(nullptr) |
| 63 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 64 | _type = CLKernelType::ELEMENTWISE; |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void CLReverseKernel::configure(const ICLTensor *input, ICLTensor *output, const ICLTensor *axis) |
| 68 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 69 | configure(CLKernelLibrary::get().get_compile_context(), input, output, axis); |
| 70 | } |
| 71 | |
Manuel Bottini | 2803f70 | 2020-04-21 16:20:03 +0100 | [diff] [blame] | 72 | void CLReverseKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const ICLTensor *axis) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 73 | { |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 74 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, axis); |
Manuel Bottini | b6869dd | 2020-12-16 15:34:25 +0000 | [diff] [blame] | 75 | auto padding_info = get_padding_info({ input, output, axis }); |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 76 | |
| 77 | _input = input; |
| 78 | _output = output; |
| 79 | _axis = axis; |
| 80 | |
| 81 | // Output tensor auto initialization if not yet initialized |
| 82 | auto_init_if_empty(*output->info(), *input->info()->clone()); |
| 83 | |
| 84 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), axis->info())); |
| 85 | |
| 86 | // Set kernel build options |
| 87 | CLBuildOptions build_opts; |
| 88 | build_opts.add_option("-DNUM_REVERSE_DIMS=" + support::cpp11::to_string(axis->info()->dimension(0))); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 89 | build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(input->info()->element_size())); |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 90 | |
| 91 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 92 | _kernel = create_kernel(compile_context, "reverse", build_opts.options()); |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 93 | |
| 94 | // Set static kernel arguments |
| 95 | unsigned int idx = 2 * num_arguments_per_4D_tensor() + num_arguments_per_1D_tensor(); |
| 96 | add_argument<cl_uint>(idx, input->info()->dimension(0)); |
| 97 | add_argument<cl_uint>(idx, input->info()->dimension(1)); |
| 98 | add_argument<cl_uint>(idx, input->info()->dimension(2)); |
| 99 | add_argument<cl_uint>(idx, input->info()->dimension(3)); |
| 100 | |
| 101 | // Configure kernel window |
| 102 | Window win = calculate_max_window(*output->info(), Steps()); |
| 103 | ICLKernel::configure_internal(win); |
| 104 | |
| 105 | // Set config_id for enabling LWS tuning |
| 106 | _config_id += "reverse_"; |
| 107 | _config_id += lower_string(string_from_data_type(input->info()->data_type())); |
| 108 | _config_id += "_"; |
| 109 | _config_id += support::cpp11::to_string(input->info()->dimension(0)); |
| 110 | _config_id += "_"; |
| 111 | _config_id += support::cpp11::to_string(input->info()->dimension(1)); |
| 112 | _config_id += "_"; |
| 113 | _config_id += support::cpp11::to_string(input->info()->dimension(2)); |
Manuel Bottini | b6869dd | 2020-12-16 15:34:25 +0000 | [diff] [blame] | 114 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | Status CLReverseKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *axis) |
| 118 | { |
| 119 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis)); |
| 120 | return Status{}; |
| 121 | } |
| 122 | |
| 123 | void CLReverseKernel::run(const Window &window, cl::CommandQueue &queue) |
| 124 | { |
| 125 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 126 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 127 | |
| 128 | Window collapsed = window.collapse(ICLKernel::window(), Window::DimZ); |
| 129 | Window slice = collapsed.first_slice_window_4D(); |
| 130 | Window axis_slice = collapsed.first_slice_window_1D(); |
| 131 | |
| 132 | do |
| 133 | { |
| 134 | unsigned int idx = 0; |
| 135 | add_4D_tensor_argument(idx, _input, slice); |
| 136 | add_1D_tensor_argument(idx, _axis, axis_slice); |
| 137 | add_4D_tensor_argument(idx, _output, slice); |
| 138 | enqueue(queue, *this, slice, lws_hint()); |
| 139 | } |
| 140 | while(collapsed.slide_window_slice_4D(slice)); |
| 141 | } |
| 142 | } // namespace arm_compute |