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