blob: 8c3c59559f90aa3b9e307eb22b985f403fbfebf0 [file] [log] [blame]
Michalis Spyrou110b9202018-12-28 16:32:49 +00001/*
Luca Foschianifedefc32020-02-17 17:02:49 +00002 * Copyright (c) 2018-2020 ARM Limited.
Michalis Spyrou110b9202018-12-28 16:32:49 +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/NEON/kernels/NEReverseKernel.h"
25
Michalis Spyrou110b9202018-12-28 16:32:49 +000026#include "arm_compute/core/NEON/wrapper/wrapper.h"
Michalis Spyrou110b9202018-12-28 16:32:49 +000027#include "arm_compute/core/TensorInfo.h"
Michalis Spyrou110b9202018-12-28 16:32:49 +000028#include "arm_compute/core/Validate.h"
29#include "arm_compute/core/Window.h"
30
Michalis Spyrou110b9202018-12-28 16:32:49 +000031namespace arm_compute
32{
33namespace
34{
35Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *axis)
36{
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000037 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output, axis);
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +010038 //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions.
Georgios Pinitas33843562019-12-10 13:33:18 +000039 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Michalis Spyrou110b9202018-12-28 16:32:49 +000040 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(axis, 1, DataType::U32);
41 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis->num_dimensions() > 1, "Axis must be a 1D tensor");
42 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis->dimension(0) > 4, "Only up to 4 dimensions can be reversed");
43
44 // Checks performed when output is configured
45 if(output->total_size() != 0)
46 {
47 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
48 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000049 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Michalis Spyrou110b9202018-12-28 16:32:49 +000050 }
51
52 return Status{};
53}
54} // namespace
55
56NEReverseKernel::NEReverseKernel()
57 : _input(nullptr), _output(nullptr), _axis(nullptr)
58{
59}
60
61void NEReverseKernel::configure(const ITensor *input, ITensor *output, const ITensor *axis)
62{
63 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, axis);
64
65 _input = input;
66 _output = output;
67 _axis = axis;
68
69 // Output tensor auto initialization if not yet initialized
70 auto_init_if_empty(*output->info(), *input->info()->clone());
71
72 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), axis->info()));
73
74 // Configure kernel window
75 INEKernel::configure(calculate_max_window(*output->info()));
76}
77
78Status NEReverseKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *axis)
79{
80 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis));
81
82 return Status{};
83}
84
85template <typename T>
86void run_reverse(const Window &window, const ITensor *input, const ITensor *axis, ITensor *output)
87{
88 int axis_bit = 0;
89 for(unsigned int i = 0; i < axis->info()->dimension(0); ++i)
90 {
91 const int axis_i = *(reinterpret_cast<const int *>(axis->buffer()) + i);
92 axis_bit |= 1 << axis_i;
93 }
94
95 // Check if we need a left-over loop for the y dimension
Luca Foschianifedefc32020-02-17 17:02:49 +000096 const int window_step_x = 16 / input->info()->element_size();
97 const int window_start_x = window.x().start();
98 const int window_end_x = window.x().end();
Michalis Spyrou110b9202018-12-28 16:32:49 +000099
Luca Foschianifedefc32020-02-17 17:02:49 +0000100 Window win(window);
101 win.set(Window::DimX, Window::Dimension(0, 1, 1));
Michalis Spyrou110b9202018-12-28 16:32:49 +0000102
Luca Foschianifedefc32020-02-17 17:02:49 +0000103 Iterator input_it(input, win);
104 execute_window_loop(win, [&](const Coordinates & id)
Michalis Spyrou110b9202018-12-28 16:32:49 +0000105 {
Luca Foschianifedefc32020-02-17 17:02:49 +0000106 int x = window_start_x;
107 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Michalis Spyrou110b9202018-12-28 16:32:49 +0000108 {
Luca Foschianifedefc32020-02-17 17:02:49 +0000109 auto in = wrapper::vloadq(reinterpret_cast<T *>(input_it.ptr()) + x);
Michalis Spyrou110b9202018-12-28 16:32:49 +0000110
111 // Reverse 0 axis
112 if(axis_bit & 0x1)
113 {
114 in = wrapper::vrev64(in);
115 in = wrapper::vcombine(wrapper::vgethigh(in), wrapper::vgetlow(in));
116 }
117
Luca Foschianifedefc32020-02-17 17:02:49 +0000118 const int offset_x = (axis_bit & 0x1) ? output->info()->dimension(0) - x - window_step_x : x;
Michalis Spyrou110b9202018-12-28 16:32:49 +0000119 const int offset_y = (axis_bit & 0x2) ? output->info()->dimension(1) - id.y() - 1 : id.y();
120 const int offset_z = (axis_bit & 0x4) ? output->info()->dimension(2) - id.z() - 1 : id.z();
121 const int offset_w = (axis_bit & 0x8) ? output->info()->dimension(3) - id[3] - 1 : id[3];
122
123 auto out_ptr = reinterpret_cast<T *>(output->ptr_to_element(Coordinates(offset_x, offset_y, offset_z, offset_w)));
124 wrapper::vstore(out_ptr, in);
Michalis Spyrou110b9202018-12-28 16:32:49 +0000125 }
126
Luca Foschianifedefc32020-02-17 17:02:49 +0000127 // Compute left-over elements
128 for(; x < window_end_x; ++x)
129 {
130 const auto in = *(reinterpret_cast<T *>(input_it.ptr()) + x);
131
132 const int offset_x = (axis_bit & 0x1) ? output->info()->dimension(0) - x - 1 : x;
133 const int offset_y = (axis_bit & 0x2) ? output->info()->dimension(1) - id.y() - 1 : id.y();
134 const int offset_z = (axis_bit & 0x4) ? output->info()->dimension(2) - id.z() - 1 : id.z();
135 const int offset_w = (axis_bit & 0x8) ? output->info()->dimension(3) - id[3] - 1 : id[3];
136
137 *reinterpret_cast<T *>(output->ptr_to_element(Coordinates(offset_x, offset_y, offset_z, offset_w))) = in;
138 }
139 },
140 input_it);
Michalis Spyrou110b9202018-12-28 16:32:49 +0000141}
142
143void NEReverseKernel::run(const Window &window, const ThreadInfo &info)
144{
145 ARM_COMPUTE_UNUSED(info);
146 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
147 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
148
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +0100149 switch(_input->info()->element_size())
Michalis Spyrou110b9202018-12-28 16:32:49 +0000150 {
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +0100151 case 4:
Michalis Spyroua50e7022019-04-09 14:03:17 +0100152 run_reverse<uint32_t>(window, _input, _axis, _output);
Michalis Spyrou110b9202018-12-28 16:32:49 +0000153 break;
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +0100154 case 2:
Michalis Spyrou110b9202018-12-28 16:32:49 +0000155 run_reverse<uint16_t>(window, _input, _axis, _output);
156 break;
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +0100157 case 1:
Michalis Spyroua50e7022019-04-09 14:03:17 +0100158 run_reverse<uint8_t>(window, _input, _axis, _output);
Michalis Spyrou110b9202018-12-28 16:32:49 +0000159 break;
160 default:
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +0100161 ARM_COMPUTE_ERROR("Element size not supported");
Michalis Spyrou110b9202018-12-28 16:32:49 +0000162 }
163}
164} // namespace arm_compute