Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2020 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [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/NEGEMMTranspose1xWKernel.h" |
| 25 | |
Michele Di Giorgio | 9d3e7f9 | 2019-08-13 14:23:21 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/AccessWindowStatic.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/ITensor.h" |
| 28 | #include "arm_compute/core/NEON/INEKernel.h" |
| 29 | #include "arm_compute/core/TensorInfo.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/Validate.h" |
| 31 | #include "arm_compute/core/Window.h" |
| 32 | |
| 33 | #include <arm_neon.h> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
Michele Di Giorgio | f9b595a | 2020-07-03 13:34:52 +0100 | [diff] [blame] | 35 | namespace arm_compute |
| 36 | { |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 37 | namespace |
| 38 | { |
| 39 | TensorShape get_output_shape(const ITensorInfo *input) |
| 40 | { |
| 41 | TensorShape output_shape{ input->tensor_shape() }; |
| 42 | const size_t transpose_w = 16 / input->element_size(); |
| 43 | output_shape.set(0, input->dimension(1) * transpose_w); |
| 44 | output_shape.set(1, static_cast<size_t>(std::ceil((input->dimension(0) / static_cast<float>(transpose_w))))); |
| 45 | return output_shape; |
| 46 | } |
| 47 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 48 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 49 | { |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 52 | //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions. |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 53 | |
| 54 | if(output->total_size() != 0) |
| 55 | { |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), get_output_shape(input)); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 61 | return Status{}; |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 64 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 65 | { |
| 66 | const unsigned int num_elems_processed_per_iteration = 16 / input->element_size(); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 67 | |
| 68 | // Configure kernel window |
| 69 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 70 | |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 71 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 72 | |
| 73 | // Configure window in case of configured output |
| 74 | if(output->total_size() != 0) |
| 75 | { |
Michele Di Giorgio | 9d3e7f9 | 2019-08-13 14:23:21 +0100 | [diff] [blame] | 76 | AccessWindowStatic output_access(output, 0, 0, output->dimension(0), output->dimension(1)); |
| 77 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Michele Di Giorgio | 9d3e7f9 | 2019-08-13 14:23:21 +0100 | [diff] [blame] | 80 | const bool window_changed = update_window_and_padding(win, input_access); |
| 81 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 82 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 83 | return std::make_pair(err, win); |
| 84 | } |
| 85 | } // namespace |
| 86 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 87 | void NEGEMMTranspose1xWKernel::configure(const ITensor *input, ITensor *output) |
| 88 | { |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 89 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | |
| 91 | // Output tensor auto inizialitation if not yet initialized |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 92 | auto_init_if_empty(*output->info(), get_output_shape(input->info()), 1, input->info()->data_type()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 94 | // Perform validate step |
| 95 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | |
| 97 | _input = input; |
| 98 | _output = output; |
| 99 | |
| 100 | // Configure kernel window |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 101 | auto win_config = validate_and_configure_window(input->info(), output->info()); |
| 102 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 103 | INEKernel::configure(win_config.second); |
| 104 | } |
Moritz Pflanzer | 0745a98 | 2017-07-05 16:34:28 +0100 | [diff] [blame] | 105 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 106 | Status NEGEMMTranspose1xWKernel::validate(const ITensorInfo *input, const ITensorInfo *output) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 107 | { |
| 108 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output)); |
| 109 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first); |
Moritz Pflanzer | 0745a98 | 2017-07-05 16:34:28 +0100 | [diff] [blame] | 110 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 111 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 114 | void NEGEMMTranspose1xWKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 115 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 116 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 117 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 118 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window); |
| 119 | |
| 120 | /* |
| 121 | * Following an example of how the transposition1xW works when the input data type is F32 |
| 122 | * |
| 123 | * |a00 a01 a02 a03| |
| 124 | * |a10 a11 a12 a13| |
| 125 | * |a20 a21 a22 a23| = | a00 a01 a02 a03 || a10 a11 a12 a13 || a20 a21 a22 a23 || a30 a31 a32 a33 | |
| 126 | * |a30 a31 a32 a33| |
| 127 | * |
| 128 | * The output matrix will have the following shape: [ height * W, ceil(width / W) ], where W = (16 / element size of the tensor) |
| 129 | */ |
| 130 | |
| 131 | // Set window for output tensor. Set to 0 the X and Y dimensions in order to allow multi-threading implementation and future batched matrix multiplications |
| 132 | Window win_out(window); |
| 133 | win_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 134 | win_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 135 | |
| 136 | Iterator in(_input, window); |
| 137 | Iterator out(_output, win_out); |
| 138 | |
| 139 | switch(_input->info()->element_size()) |
| 140 | { |
| 141 | case 1: |
| 142 | { |
| 143 | const size_t out_stride = _output->info()->strides_in_bytes()[1]; |
| 144 | execute_window_loop(window, [&](const Coordinates & id) |
| 145 | { |
| 146 | // Output address = base addr + (y * 16) + (x / 16 ) * stride |
| 147 | const uint8_t *in_ptr = in.ptr(); |
| 148 | uint8_t *const out_ptr = out.ptr() + (id.y() << 4) + (id.x() >> 4) * out_stride; |
| 149 | vst1q_u8(out_ptr, vld1q_u8(in_ptr)); |
| 150 | }, |
| 151 | in, out); |
| 152 | break; |
| 153 | } |
| 154 | case 2: |
| 155 | { |
| 156 | const size_t out_stride = _output->info()->strides_in_bytes()[1] / sizeof(int16_t); |
| 157 | execute_window_loop(window, [&](const Coordinates & id) |
| 158 | { |
| 159 | // Output address = base addr + (y * 8) + (x / 8 ) * stride |
| 160 | const auto in_ptr = reinterpret_cast<const uint16_t *>(in.ptr()); |
| 161 | const auto out_ptr = reinterpret_cast<uint16_t *>(out.ptr()) + (id.y() << 3) + (id.x() >> 3) * out_stride; |
| 162 | vst1q_u16(out_ptr, vld1q_u16(in_ptr)); |
| 163 | }, |
| 164 | in, out); |
| 165 | break; |
| 166 | } |
| 167 | case 4: |
| 168 | { |
| 169 | const size_t out_stride = _output->info()->strides_in_bytes()[1] / sizeof(float); |
| 170 | execute_window_loop(window, [&](const Coordinates & id) |
| 171 | { |
| 172 | // Output address = base addr + (y * 4) + (x / 4 ) * stride |
| 173 | const auto in_ptr = reinterpret_cast<const uint32_t *>(in.ptr()); |
| 174 | const auto out_ptr = reinterpret_cast<uint32_t *>(out.ptr()) + (id.y() << 2) + (id.x() >> 2) * out_stride; |
| 175 | vst1q_u32(out_ptr, vld1q_u32(in_ptr)); |
| 176 | }, |
| 177 | in, out); |
| 178 | break; |
| 179 | } |
| 180 | default: |
| 181 | { |
| 182 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | } |
Michele Di Giorgio | f9b595a | 2020-07-03 13:34:52 +0100 | [diff] [blame] | 187 | } // namespace arm_compute |