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