Michalis Spyrou | 110b920 | 2018-12-28 16:32:49 +0000 | [diff] [blame] | 1 | /* |
Luca Foschiani | fedefc3 | 2020-02-17 17:02:49 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 ARM Limited. |
Michalis Spyrou | 110b920 | 2018-12-28 16:32:49 +0000 | [diff] [blame] | 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 Spyrou | 110b920 | 2018-12-28 16:32:49 +0000 | [diff] [blame] | 34 | #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 | |
| 44 | namespace arm_compute |
| 45 | { |
| 46 | namespace |
| 47 | { |
| 48 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *axis) |
| 49 | { |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output, axis); |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Michalis Spyrou | 110b920 | 2018-12-28 16:32:49 +0000 | [diff] [blame] | 53 | 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 Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 62 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Michalis Spyrou | 110b920 | 2018-12-28 16:32:49 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | return Status{}; |
| 66 | } |
| 67 | } // namespace |
| 68 | |
| 69 | NEReverseKernel::NEReverseKernel() |
| 70 | : _input(nullptr), _output(nullptr), _axis(nullptr) |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | void 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 | |
| 91 | Status 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 | |
| 98 | template <typename T> |
| 99 | void 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 |
Luca Foschiani | fedefc3 | 2020-02-17 17:02:49 +0000 | [diff] [blame] | 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 = window.x().end(); |
Michalis Spyrou | 110b920 | 2018-12-28 16:32:49 +0000 | [diff] [blame] | 112 | |
Luca Foschiani | fedefc3 | 2020-02-17 17:02:49 +0000 | [diff] [blame] | 113 | Window win(window); |
| 114 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Michalis Spyrou | 110b920 | 2018-12-28 16:32:49 +0000 | [diff] [blame] | 115 | |
Luca Foschiani | fedefc3 | 2020-02-17 17:02:49 +0000 | [diff] [blame] | 116 | Iterator input_it(input, win); |
| 117 | execute_window_loop(win, [&](const Coordinates & id) |
Michalis Spyrou | 110b920 | 2018-12-28 16:32:49 +0000 | [diff] [blame] | 118 | { |
Luca Foschiani | fedefc3 | 2020-02-17 17:02:49 +0000 | [diff] [blame] | 119 | int x = window_start_x; |
| 120 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
Michalis Spyrou | 110b920 | 2018-12-28 16:32:49 +0000 | [diff] [blame] | 121 | { |
Luca Foschiani | fedefc3 | 2020-02-17 17:02:49 +0000 | [diff] [blame] | 122 | auto in = wrapper::vloadq(reinterpret_cast<T *>(input_it.ptr()) + x); |
Michalis Spyrou | 110b920 | 2018-12-28 16:32:49 +0000 | [diff] [blame] | 123 | |
| 124 | // Reverse 0 axis |
| 125 | if(axis_bit & 0x1) |
| 126 | { |
| 127 | in = wrapper::vrev64(in); |
| 128 | in = wrapper::vcombine(wrapper::vgethigh(in), wrapper::vgetlow(in)); |
| 129 | } |
| 130 | |
Luca Foschiani | fedefc3 | 2020-02-17 17:02:49 +0000 | [diff] [blame] | 131 | const int offset_x = (axis_bit & 0x1) ? output->info()->dimension(0) - x - window_step_x : x; |
Michalis Spyrou | 110b920 | 2018-12-28 16:32:49 +0000 | [diff] [blame] | 132 | const int offset_y = (axis_bit & 0x2) ? output->info()->dimension(1) - id.y() - 1 : id.y(); |
| 133 | const int offset_z = (axis_bit & 0x4) ? output->info()->dimension(2) - id.z() - 1 : id.z(); |
| 134 | const int offset_w = (axis_bit & 0x8) ? output->info()->dimension(3) - id[3] - 1 : id[3]; |
| 135 | |
| 136 | auto out_ptr = reinterpret_cast<T *>(output->ptr_to_element(Coordinates(offset_x, offset_y, offset_z, offset_w))); |
| 137 | wrapper::vstore(out_ptr, in); |
Michalis Spyrou | 110b920 | 2018-12-28 16:32:49 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Luca Foschiani | fedefc3 | 2020-02-17 17:02:49 +0000 | [diff] [blame] | 140 | // Compute left-over elements |
| 141 | for(; x < window_end_x; ++x) |
| 142 | { |
| 143 | const auto in = *(reinterpret_cast<T *>(input_it.ptr()) + x); |
| 144 | |
| 145 | const int offset_x = (axis_bit & 0x1) ? output->info()->dimension(0) - x - 1 : x; |
| 146 | const int offset_y = (axis_bit & 0x2) ? output->info()->dimension(1) - id.y() - 1 : id.y(); |
| 147 | const int offset_z = (axis_bit & 0x4) ? output->info()->dimension(2) - id.z() - 1 : id.z(); |
| 148 | const int offset_w = (axis_bit & 0x8) ? output->info()->dimension(3) - id[3] - 1 : id[3]; |
| 149 | |
| 150 | *reinterpret_cast<T *>(output->ptr_to_element(Coordinates(offset_x, offset_y, offset_z, offset_w))) = in; |
| 151 | } |
| 152 | }, |
| 153 | input_it); |
Michalis Spyrou | 110b920 | 2018-12-28 16:32:49 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void NEReverseKernel::run(const Window &window, const ThreadInfo &info) |
| 157 | { |
| 158 | ARM_COMPUTE_UNUSED(info); |
| 159 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 160 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 161 | |
| 162 | switch(_input->info()->data_type()) |
| 163 | { |
| 164 | case DataType::F32: |
Michalis Spyrou | a50e702 | 2019-04-09 14:03:17 +0100 | [diff] [blame] | 165 | case DataType::U32: |
| 166 | case DataType::S32: |
| 167 | run_reverse<uint32_t>(window, _input, _axis, _output); |
Michalis Spyrou | 110b920 | 2018-12-28 16:32:49 +0000 | [diff] [blame] | 168 | break; |
| 169 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 170 | case DataType::F16: |
Michalis Spyrou | 110b920 | 2018-12-28 16:32:49 +0000 | [diff] [blame] | 171 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Michalis Spyrou | 110b920 | 2018-12-28 16:32:49 +0000 | [diff] [blame] | 172 | case DataType::S16: |
Michalis Spyrou | 110b920 | 2018-12-28 16:32:49 +0000 | [diff] [blame] | 173 | case DataType::U16: |
| 174 | run_reverse<uint16_t>(window, _input, _axis, _output); |
| 175 | break; |
| 176 | case DataType::QASYMM8: |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 177 | case DataType::QASYMM8_SIGNED: |
Michalis Spyrou | 110b920 | 2018-12-28 16:32:49 +0000 | [diff] [blame] | 178 | case DataType::U8: |
Michalis Spyrou | 110b920 | 2018-12-28 16:32:49 +0000 | [diff] [blame] | 179 | case DataType::S8: |
Michalis Spyrou | a50e702 | 2019-04-09 14:03:17 +0100 | [diff] [blame] | 180 | run_reverse<uint8_t>(window, _input, _axis, _output); |
Michalis Spyrou | 110b920 | 2018-12-28 16:32:49 +0000 | [diff] [blame] | 181 | break; |
| 182 | default: |
| 183 | ARM_COMPUTE_ERROR("Data type not supported"); |
| 184 | } |
| 185 | } |
| 186 | } // namespace arm_compute |