blob: f92bb8ee906db249269cb3a7c46fe83fee9127e7 [file] [log] [blame]
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +00001/*
Manuel Bottini8481d832019-12-10 15:28:40 +00002 * Copyright (c) 2018-2020 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 */
24#include "arm_compute/core/CL/kernels/CLReverseKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/CLValidate.h"
29#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/Error.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Window.h"
34
35namespace arm_compute
36{
37namespace
38{
39Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *axis)
40{
41 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, axis);
42 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Manuel Bottini8481d832019-12-10 15:28:40 +000043 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000044 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(axis, 1, DataType::U32);
45 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis->num_dimensions() > 1, "Axis must be a 1D tensor");
46 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis->dimension(0) > 4, "Only up to 4 dimensions can be reversed");
47
48 // Checks performed when output is configured
49 if(output->total_size() != 0)
50 {
51 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
52 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000053 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000054 }
55
56 return Status{};
57}
58} // namespace
59
60CLReverseKernel::CLReverseKernel()
61 : _input(nullptr), _output(nullptr), _axis(nullptr)
62{
63}
64
65void CLReverseKernel::configure(const ICLTensor *input, ICLTensor *output, const ICLTensor *axis)
66{
67 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, axis);
68
69 _input = input;
70 _output = output;
71 _axis = axis;
72
73 // Output tensor auto initialization if not yet initialized
74 auto_init_if_empty(*output->info(), *input->info()->clone());
75
76 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), axis->info()));
77
78 // Set kernel build options
79 CLBuildOptions build_opts;
80 build_opts.add_option("-DNUM_REVERSE_DIMS=" + support::cpp11::to_string(axis->info()->dimension(0)));
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010081 build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(input->info()->element_size()));
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000082
83 // Create kernel
84 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("reverse", build_opts.options()));
85
86 // Set static kernel arguments
87 unsigned int idx = 2 * num_arguments_per_4D_tensor() + num_arguments_per_1D_tensor();
88 add_argument<cl_uint>(idx, input->info()->dimension(0));
89 add_argument<cl_uint>(idx, input->info()->dimension(1));
90 add_argument<cl_uint>(idx, input->info()->dimension(2));
91 add_argument<cl_uint>(idx, input->info()->dimension(3));
92
93 // Configure kernel window
94 Window win = calculate_max_window(*output->info(), Steps());
95 ICLKernel::configure_internal(win);
96
97 // Set config_id for enabling LWS tuning
98 _config_id += "reverse_";
99 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
100 _config_id += "_";
101 _config_id += support::cpp11::to_string(input->info()->dimension(0));
102 _config_id += "_";
103 _config_id += support::cpp11::to_string(input->info()->dimension(1));
104 _config_id += "_";
105 _config_id += support::cpp11::to_string(input->info()->dimension(2));
106}
107
108Status CLReverseKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *axis)
109{
110 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis));
111 return Status{};
112}
113
114void CLReverseKernel::run(const Window &window, cl::CommandQueue &queue)
115{
116 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
117 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
118
119 Window collapsed = window.collapse(ICLKernel::window(), Window::DimZ);
120 Window slice = collapsed.first_slice_window_4D();
121 Window axis_slice = collapsed.first_slice_window_1D();
122
123 do
124 {
125 unsigned int idx = 0;
126 add_4D_tensor_argument(idx, _input, slice);
127 add_1D_tensor_argument(idx, _axis, axis_slice);
128 add_4D_tensor_argument(idx, _output, slice);
129 enqueue(queue, *this, slice, lws_hint());
130 }
131 while(collapsed.slide_window_slice_4D(slice));
132}
133} // namespace arm_compute