blob: 00241b161b5683e1262728ca90b2626639c436ba [file] [log] [blame]
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +00001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2018-2021, 2023 Arm Limited.
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +00003 *
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 Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLReverseKernel.h"
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000025
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000028#include "arm_compute/core/CL/ICLTensor.h"
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000029#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/TensorInfo.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000031#include "arm_compute/core/Utils.h"
32#include "arm_compute/core/utils/StringUtils.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010033
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/CL/CLValidate.h"
35#include "src/core/helpers/AutoConfiguration.h"
36#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000037#include "support/StringSupport.h"
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000038
39namespace arm_compute
40{
41namespace
42{
Adnan AlSinan704c22f2023-10-24 11:05:56 +010043Status
44validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *axis, bool use_inverted_axis)
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000045{
Adnan AlSinan704c22f2023-10-24 11:05:56 +010046 ARM_COMPUTE_UNUSED(use_inverted_axis);
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000047 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, axis);
48 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Manuel Bottini8481d832019-12-10 15:28:40 +000049 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Adnan AlSinan704c22f2023-10-24 11:05:56 +010050 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(axis, 1, DataType::U32, DataType::S32);
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000051 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis->num_dimensions() > 1, "Axis must be a 1D tensor");
Adnan AlSinan704c22f2023-10-24 11:05:56 +010052 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 4,
53 "Current implementation only supports up to 4 dimensions.");
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000054 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis->dimension(0) > 4, "Only up to 4 dimensions can be reversed");
55
56 // Checks performed when output is configured
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010057 if (output->total_size() != 0)
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000058 {
59 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
60 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000061 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000062 }
63
64 return Status{};
65}
66} // namespace
67
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010068CLReverseKernel::CLReverseKernel() : _input(nullptr), _output(nullptr), _axis(nullptr)
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000069{
Giorgio Arena4a95bba2021-06-28 11:00:27 +010070 _type = CLKernelType::ELEMENTWISE;
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000071}
72
Adnan AlSinan704c22f2023-10-24 11:05:56 +010073void CLReverseKernel::configure(const ICLTensor *input,
74 ICLTensor *output,
75 const ICLTensor *axis,
76 bool use_inverted_axis)
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000077{
Adnan AlSinan704c22f2023-10-24 11:05:56 +010078 configure(CLKernelLibrary::get().get_compile_context(), input, output, axis, use_inverted_axis);
Manuel Bottini4c6bd512020-04-08 10:15:51 +010079}
80
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010081void CLReverseKernel::configure(const CLCompileContext &compile_context,
82 const ICLTensor *input,
83 ICLTensor *output,
Adnan AlSinan704c22f2023-10-24 11:05:56 +010084 const ICLTensor *axis,
85 bool use_inverted_axis)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010086{
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000087 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, axis);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010088 auto padding_info = get_padding_info({input, output, axis});
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000089
90 _input = input;
91 _output = output;
92 _axis = axis;
93
94 // Output tensor auto initialization if not yet initialized
95 auto_init_if_empty(*output->info(), *input->info()->clone());
96
Adnan AlSinan704c22f2023-10-24 11:05:56 +010097 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), axis->info(), use_inverted_axis));
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000098
99 // Set kernel build options
100 CLBuildOptions build_opts;
101 build_opts.add_option("-DNUM_REVERSE_DIMS=" + support::cpp11::to_string(axis->info()->dimension(0)));
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100102 build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(input->info()->element_size()));
Adnan AlSinan704c22f2023-10-24 11:05:56 +0100103 build_opts.add_option("-DRANK=" + support::cpp11::to_string(input->info()->num_dimensions()));
104 build_opts.add_option_if(use_inverted_axis, "-DUSE_INVERTED_AXIS");
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +0000105
106 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100107 _kernel = create_kernel(compile_context, "reverse", build_opts.options());
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +0000108
109 // Set static kernel arguments
110 unsigned int idx = 2 * num_arguments_per_4D_tensor() + num_arguments_per_1D_tensor();
111 add_argument<cl_uint>(idx, input->info()->dimension(0));
112 add_argument<cl_uint>(idx, input->info()->dimension(1));
113 add_argument<cl_uint>(idx, input->info()->dimension(2));
114 add_argument<cl_uint>(idx, input->info()->dimension(3));
115
116 // Configure kernel window
117 Window win = calculate_max_window(*output->info(), Steps());
118 ICLKernel::configure_internal(win);
119
120 // Set config_id for enabling LWS tuning
121 _config_id += "reverse_";
122 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
123 _config_id += "_";
124 _config_id += support::cpp11::to_string(input->info()->dimension(0));
125 _config_id += "_";
126 _config_id += support::cpp11::to_string(input->info()->dimension(1));
127 _config_id += "_";
128 _config_id += support::cpp11::to_string(input->info()->dimension(2));
Manuel Bottinib6869dd2020-12-16 15:34:25 +0000129 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +0000130}
131
Adnan AlSinan704c22f2023-10-24 11:05:56 +0100132Status CLReverseKernel::validate(const ITensorInfo *input,
133 const ITensorInfo *output,
134 const ITensorInfo *axis,
135 bool use_inverted_axis)
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +0000136{
Adnan AlSinan704c22f2023-10-24 11:05:56 +0100137 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis, use_inverted_axis));
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +0000138 return Status{};
139}
140
141void CLReverseKernel::run(const Window &window, cl::CommandQueue &queue)
142{
143 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
144 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
145
146 Window collapsed = window.collapse(ICLKernel::window(), Window::DimZ);
147 Window slice = collapsed.first_slice_window_4D();
148 Window axis_slice = collapsed.first_slice_window_1D();
149
150 do
151 {
152 unsigned int idx = 0;
153 add_4D_tensor_argument(idx, _input, slice);
154 add_1D_tensor_argument(idx, _axis, axis_slice);
155 add_4D_tensor_argument(idx, _output, slice);
156 enqueue(queue, *this, slice, lws_hint());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100157 } while (collapsed.slide_window_slice_4D(slice));
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +0000158}
159} // namespace arm_compute