blob: 07d18f72903291bd4fd3575209fd0d8d814481cc [file] [log] [blame]
Michalis Spyrou110b9202018-12-28 16:32:49 +00001/*
2 * Copyright (c) 2018-2019 ARM Limited.
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 */
24#include "arm_compute/core/NEON/kernels/NEReverseKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CPP/Validate.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/ITensor.h"
30#include "arm_compute/core/NEON/NEAsymm.h"
31#include "arm_compute/core/NEON/NEFixedPoint.h"
32#include "arm_compute/core/NEON/NEMath.h"
33#include "arm_compute/core/NEON/wrapper/wrapper.h"
34#include "arm_compute/core/QAsymm8.h"
35#include "arm_compute/core/TensorInfo.h"
36#include "arm_compute/core/Utils.h"
37#include "arm_compute/core/Validate.h"
38#include "arm_compute/core/Window.h"
39
40#include <arm_neon.h>
41#include <array>
42#include <cmath>
43#include <map>
44
45namespace arm_compute
46{
47namespace
48{
49Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *axis)
50{
51 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, axis);
52 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QASYMM8,
53 DataType::U16, DataType::S16,
54 DataType::U32, DataType::S32,
55 DataType::F16, DataType::F32);
56 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(axis, 1, DataType::U32);
57 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis->num_dimensions() > 1, "Axis must be a 1D tensor");
58 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis->dimension(0) > 4, "Only up to 4 dimensions can be reversed");
59
60 // Checks performed when output is configured
61 if(output->total_size() != 0)
62 {
63 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
64 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
65 }
66
67 return Status{};
68}
69} // namespace
70
71NEReverseKernel::NEReverseKernel()
72 : _input(nullptr), _output(nullptr), _axis(nullptr)
73{
74}
75
76void NEReverseKernel::configure(const ITensor *input, ITensor *output, const ITensor *axis)
77{
78 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, axis);
79
80 _input = input;
81 _output = output;
82 _axis = axis;
83
84 // Output tensor auto initialization if not yet initialized
85 auto_init_if_empty(*output->info(), *input->info()->clone());
86
87 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), axis->info()));
88
89 // Configure kernel window
90 INEKernel::configure(calculate_max_window(*output->info()));
91}
92
93Status NEReverseKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *axis)
94{
95 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis));
96
97 return Status{};
98}
99
100template <typename T>
101void run_reverse(const Window &window, const ITensor *input, const ITensor *axis, ITensor *output)
102{
103 int axis_bit = 0;
104 for(unsigned int i = 0; i < axis->info()->dimension(0); ++i)
105 {
106 const int axis_i = *(reinterpret_cast<const int *>(axis->buffer()) + i);
107 axis_bit |= 1 << axis_i;
108 }
109
110 // Check if we need a left-over loop for the y dimension
111 const int window_step_x = 16 / input->info()->element_size();
112 const int window_start_x = window.x().start();
113 const int window_end_x = std::min(window.x().end(), static_cast<int>(input->info()->dimension(0)));
114 const int window_end_x_multiple_of = ((window_end_x - window_start_x) / window_step_x) * window_step_x;
115 bool left_over_loop_x = (((window_end_x - window_start_x) % window_step_x) != 0);
116
117 Window slice = window.first_slice_window_4D();
118
119 if(left_over_loop_x)
120 {
121 // Check if window_end_y_multiple_of is greater than window_start_y
122 if(window_end_x_multiple_of > window_start_x)
123 {
124 slice.set(Window::DimX, Window::Dimension(window_start_x, window_end_x_multiple_of, window_step_x));
125 }
126 else
127 {
128 slice.set(Window::DimX, Window::Dimension(0, 0, 1));
129 }
130 }
131
132 do
133 {
134 Iterator input_it(input, slice);
135 execute_window_loop(slice, [&](const Coordinates & id)
136 {
137 auto in = wrapper::vloadq(reinterpret_cast<T *>(input_it.ptr()));
138
139 // Reverse 0 axis
140 if(axis_bit & 0x1)
141 {
142 in = wrapper::vrev64(in);
143 in = wrapper::vcombine(wrapper::vgethigh(in), wrapper::vgetlow(in));
144 }
145
146 const int offset_x = (axis_bit & 0x1) ? output->info()->dimension(0) - id.x() - window_step_x : id.x();
147 const int offset_y = (axis_bit & 0x2) ? output->info()->dimension(1) - id.y() - 1 : id.y();
148 const int offset_z = (axis_bit & 0x4) ? output->info()->dimension(2) - id.z() - 1 : id.z();
149 const int offset_w = (axis_bit & 0x8) ? output->info()->dimension(3) - id[3] - 1 : id[3];
150
151 auto out_ptr = reinterpret_cast<T *>(output->ptr_to_element(Coordinates(offset_x, offset_y, offset_z, offset_w)));
152 wrapper::vstore(out_ptr, in);
153 },
154 input_it);
155
156 if(left_over_loop_x)
157 {
158 slice.set(Window::DimX, Window::Dimension(window_end_x_multiple_of, window_end_x, 1));
159
160 Iterator input_it(input, slice);
161
162 // Compute left-over elements along the y dimension (1x1)
163 execute_window_loop(slice, [&](const Coordinates & id)
164 {
165 const auto in = *reinterpret_cast<T *>(input_it.ptr());
166
167 const int offset_x = (axis_bit & 0x1) ? output->info()->dimension(0) - id.x() - 1 : id.x();
168 const int offset_y = (axis_bit & 0x2) ? output->info()->dimension(1) - id.y() - 1 : id.y();
169 const int offset_z = (axis_bit & 0x4) ? output->info()->dimension(2) - id.z() - 1 : id.z();
170 const int offset_w = (axis_bit & 0x8) ? output->info()->dimension(3) - id[3] - 1 : id[3];
171
172 *reinterpret_cast<T *>(output->ptr_to_element(Coordinates(offset_x, offset_y, offset_z, offset_w))) = in;
173 },
174 input_it);
175 }
176
177 }
178 while(window.slide_window_slice_4D(slice));
179}
180
181void NEReverseKernel::run(const Window &window, const ThreadInfo &info)
182{
183 ARM_COMPUTE_UNUSED(info);
184 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
185 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
186
187 switch(_input->info()->data_type())
188 {
189 case DataType::F32:
190 run_reverse<float>(window, _input, _axis, _output);
191 break;
192#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
193 case DataType::F16:
194 run_reverse<float16_t>(window, _input, _axis, _output);
195 break;
196#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
197 case DataType::U32:
198 run_reverse<uint32_t>(window, _input, _axis, _output);
199 break;
200 case DataType::S32:
201 run_reverse<int32_t>(window, _input, _axis, _output);
202 break;
203 case DataType::S16:
204 run_reverse<int16_t>(window, _input, _axis, _output);
205 break;
206 case DataType::U16:
207 run_reverse<uint16_t>(window, _input, _axis, _output);
208 break;
209 case DataType::QASYMM8:
210 case DataType::U8:
211 run_reverse<uint8_t>(window, _input, _axis, _output);
212 break;
213 case DataType::S8:
214 run_reverse<int8_t>(window, _input, _axis, _output);
215 break;
216 default:
217 ARM_COMPUTE_ERROR("Data type not supported");
218 }
219}
220} // namespace arm_compute