blob: 08a9825df95c54c363caa6a2e4f426ef36f362fb [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);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000065 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Michalis Spyrou110b9202018-12-28 16:32:49 +000066 }
67
68 return Status{};
69}
70} // namespace
71
72NEReverseKernel::NEReverseKernel()
73 : _input(nullptr), _output(nullptr), _axis(nullptr)
74{
75}
76
77void NEReverseKernel::configure(const ITensor *input, ITensor *output, const ITensor *axis)
78{
79 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, axis);
80
81 _input = input;
82 _output = output;
83 _axis = axis;
84
85 // Output tensor auto initialization if not yet initialized
86 auto_init_if_empty(*output->info(), *input->info()->clone());
87
88 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), axis->info()));
89
90 // Configure kernel window
91 INEKernel::configure(calculate_max_window(*output->info()));
92}
93
94Status NEReverseKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *axis)
95{
96 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis));
97
98 return Status{};
99}
100
101template <typename T>
102void run_reverse(const Window &window, const ITensor *input, const ITensor *axis, ITensor *output)
103{
104 int axis_bit = 0;
105 for(unsigned int i = 0; i < axis->info()->dimension(0); ++i)
106 {
107 const int axis_i = *(reinterpret_cast<const int *>(axis->buffer()) + i);
108 axis_bit |= 1 << axis_i;
109 }
110
111 // Check if we need a left-over loop for the y dimension
112 const int window_step_x = 16 / input->info()->element_size();
113 const int window_start_x = window.x().start();
114 const int window_end_x = std::min(window.x().end(), static_cast<int>(input->info()->dimension(0)));
115 const int window_end_x_multiple_of = ((window_end_x - window_start_x) / window_step_x) * window_step_x;
116 bool left_over_loop_x = (((window_end_x - window_start_x) % window_step_x) != 0);
117
118 Window slice = window.first_slice_window_4D();
119
120 if(left_over_loop_x)
121 {
122 // Check if window_end_y_multiple_of is greater than window_start_y
123 if(window_end_x_multiple_of > window_start_x)
124 {
125 slice.set(Window::DimX, Window::Dimension(window_start_x, window_end_x_multiple_of, window_step_x));
126 }
127 else
128 {
129 slice.set(Window::DimX, Window::Dimension(0, 0, 1));
130 }
131 }
132
133 do
134 {
135 Iterator input_it(input, slice);
136 execute_window_loop(slice, [&](const Coordinates & id)
137 {
138 auto in = wrapper::vloadq(reinterpret_cast<T *>(input_it.ptr()));
139
140 // Reverse 0 axis
141 if(axis_bit & 0x1)
142 {
143 in = wrapper::vrev64(in);
144 in = wrapper::vcombine(wrapper::vgethigh(in), wrapper::vgetlow(in));
145 }
146
147 const int offset_x = (axis_bit & 0x1) ? output->info()->dimension(0) - id.x() - window_step_x : id.x();
148 const int offset_y = (axis_bit & 0x2) ? output->info()->dimension(1) - id.y() - 1 : id.y();
149 const int offset_z = (axis_bit & 0x4) ? output->info()->dimension(2) - id.z() - 1 : id.z();
150 const int offset_w = (axis_bit & 0x8) ? output->info()->dimension(3) - id[3] - 1 : id[3];
151
152 auto out_ptr = reinterpret_cast<T *>(output->ptr_to_element(Coordinates(offset_x, offset_y, offset_z, offset_w)));
153 wrapper::vstore(out_ptr, in);
154 },
155 input_it);
156
157 if(left_over_loop_x)
158 {
159 slice.set(Window::DimX, Window::Dimension(window_end_x_multiple_of, window_end_x, 1));
160
161 Iterator input_it(input, slice);
162
163 // Compute left-over elements along the y dimension (1x1)
164 execute_window_loop(slice, [&](const Coordinates & id)
165 {
166 const auto in = *reinterpret_cast<T *>(input_it.ptr());
167
168 const int offset_x = (axis_bit & 0x1) ? output->info()->dimension(0) - id.x() - 1 : id.x();
169 const int offset_y = (axis_bit & 0x2) ? output->info()->dimension(1) - id.y() - 1 : id.y();
170 const int offset_z = (axis_bit & 0x4) ? output->info()->dimension(2) - id.z() - 1 : id.z();
171 const int offset_w = (axis_bit & 0x8) ? output->info()->dimension(3) - id[3] - 1 : id[3];
172
173 *reinterpret_cast<T *>(output->ptr_to_element(Coordinates(offset_x, offset_y, offset_z, offset_w))) = in;
174 },
175 input_it);
176 }
177
178 }
179 while(window.slide_window_slice_4D(slice));
180}
181
182void NEReverseKernel::run(const Window &window, const ThreadInfo &info)
183{
184 ARM_COMPUTE_UNUSED(info);
185 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
186 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
187
188 switch(_input->info()->data_type())
189 {
190 case DataType::F32:
191 run_reverse<float>(window, _input, _axis, _output);
192 break;
193#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
194 case DataType::F16:
195 run_reverse<float16_t>(window, _input, _axis, _output);
196 break;
197#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
198 case DataType::U32:
199 run_reverse<uint32_t>(window, _input, _axis, _output);
200 break;
201 case DataType::S32:
202 run_reverse<int32_t>(window, _input, _axis, _output);
203 break;
204 case DataType::S16:
205 run_reverse<int16_t>(window, _input, _axis, _output);
206 break;
207 case DataType::U16:
208 run_reverse<uint16_t>(window, _input, _axis, _output);
209 break;
210 case DataType::QASYMM8:
211 case DataType::U8:
212 run_reverse<uint8_t>(window, _input, _axis, _output);
213 break;
214 case DataType::S8:
215 run_reverse<int8_t>(window, _input, _axis, _output);
216 break;
217 default:
218 ARM_COMPUTE_ERROR("Data type not supported");
219 }
220}
221} // namespace arm_compute