Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Sheri Zhang | ac6499a | 2021-02-10 15:32:38 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2016-2021 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 | */ |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 24 | #include "src/core/NEON/kernels/NEGEMMTranspose1xWKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/ITensor.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/TensorInfo.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/Validate.h" |
| 29 | #include "arm_compute/core/Window.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 30 | #include "src/core/AccessWindowStatic.h" |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 31 | #include "src/core/NEON/INEKernel.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 32 | #include "src/core/helpers/AutoConfiguration.h" |
| 33 | #include "src/core/helpers/WindowHelpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
| 35 | #include <arm_neon.h> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | |
Michele Di Giorgio | f9b595a | 2020-07-03 13:34:52 +0100 | [diff] [blame] | 37 | namespace arm_compute |
| 38 | { |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 39 | namespace |
| 40 | { |
| 41 | TensorShape get_output_shape(const ITensorInfo *input) |
| 42 | { |
| 43 | TensorShape output_shape{ input->tensor_shape() }; |
| 44 | const size_t transpose_w = 16 / input->element_size(); |
| 45 | output_shape.set(0, input->dimension(1) * transpose_w); |
| 46 | output_shape.set(1, static_cast<size_t>(std::ceil((input->dimension(0) / static_cast<float>(transpose_w))))); |
| 47 | return output_shape; |
| 48 | } |
| 49 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 50 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 51 | { |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Sheri Zhang | ac6499a | 2021-02-10 15:32:38 +0000 | [diff] [blame^] | 54 | //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] | 55 | |
| 56 | if(output->total_size() != 0) |
| 57 | { |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), get_output_shape(input)); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 60 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 63 | return Status{}; |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 64 | } |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 65 | } // namespace |
| 66 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 67 | void NEGEMMTranspose1xWKernel::configure(const ITensor *input, ITensor *output) |
| 68 | { |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 69 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 70 | |
| 71 | // Output tensor auto inizialitation if not yet initialized |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 72 | 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] | 73 | |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 74 | // Perform validate step |
| 75 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | |
| 77 | _input = input; |
| 78 | _output = output; |
| 79 | |
Gian Marco Iodice | b3182b1 | 2020-09-04 08:44:52 +0100 | [diff] [blame] | 80 | const size_t vector_size = 16 / input->info()->element_size(); |
| 81 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 82 | // Configure kernel window |
Gian Marco Iodice | b3182b1 | 2020-09-04 08:44:52 +0100 | [diff] [blame] | 83 | Window win = calculate_max_window(*input->info(), Steps(vector_size)); |
| 84 | |
| 85 | Coordinates coord; |
| 86 | coord.set_num_dimensions(output->info()->num_dimensions()); |
| 87 | output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape())); |
| 88 | |
| 89 | INEKernel::configure(win); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 90 | } |
Moritz Pflanzer | 0745a98 | 2017-07-05 16:34:28 +0100 | [diff] [blame] | 91 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 92 | Status NEGEMMTranspose1xWKernel::validate(const ITensorInfo *input, const ITensorInfo *output) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 93 | { |
| 94 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output)); |
Moritz Pflanzer | 0745a98 | 2017-07-05 16:34:28 +0100 | [diff] [blame] | 95 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 96 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 99 | void NEGEMMTranspose1xWKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 101 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 102 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 103 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window); |
| 104 | |
| 105 | /* |
| 106 | * Following an example of how the transposition1xW works when the input data type is F32 |
| 107 | * |
| 108 | * |a00 a01 a02 a03| |
| 109 | * |a10 a11 a12 a13| |
| 110 | * |a20 a21 a22 a23| = | a00 a01 a02 a03 || a10 a11 a12 a13 || a20 a21 a22 a23 || a30 a31 a32 a33 | |
| 111 | * |a30 a31 a32 a33| |
| 112 | * |
| 113 | * The output matrix will have the following shape: [ height * W, ceil(width / W) ], where W = (16 / element size of the tensor) |
| 114 | */ |
| 115 | |
| 116 | // 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 |
| 117 | Window win_out(window); |
| 118 | win_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 119 | win_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 120 | |
| 121 | Iterator in(_input, window); |
| 122 | Iterator out(_output, win_out); |
| 123 | |
Gian Marco Iodice | b3182b1 | 2020-09-04 08:44:52 +0100 | [diff] [blame] | 124 | const size_t in_width = _input->info()->dimension(0); |
| 125 | const size_t element_size = _input->info()->element_size(); |
| 126 | const size_t out_stride = _output->info()->strides_in_bytes()[1]; |
| 127 | const size_t vector_size = 16 / element_size; |
| 128 | |
| 129 | execute_window_loop(window, [&](const Coordinates & id) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 130 | { |
Gian Marco Iodice | b3182b1 | 2020-09-04 08:44:52 +0100 | [diff] [blame] | 131 | const uint8_t *in_ptr = in.ptr(); |
| 132 | uint8_t *const out_ptr = out.ptr() + (id.y() * vector_size) * element_size + (id.x() / vector_size) * out_stride; |
| 133 | |
| 134 | for(size_t k = 0; k < vector_size; ++k) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 135 | { |
Gian Marco Iodice | b3182b1 | 2020-09-04 08:44:52 +0100 | [diff] [blame] | 136 | // If the input width is not multiple of W, we fill the reference with 0s |
| 137 | if((id.x() + k) >= in_width) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 138 | { |
Gian Marco Iodice | b3182b1 | 2020-09-04 08:44:52 +0100 | [diff] [blame] | 139 | std::memset(out_ptr + k * element_size, 0, element_size); |
| 140 | } |
| 141 | else |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 142 | { |
Gian Marco Iodice | b3182b1 | 2020-09-04 08:44:52 +0100 | [diff] [blame] | 143 | std::memcpy(out_ptr + k * element_size, in_ptr + k * element_size, element_size); |
| 144 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 145 | } |
Gian Marco Iodice | b3182b1 | 2020-09-04 08:44:52 +0100 | [diff] [blame] | 146 | }, |
| 147 | in, out); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 148 | } |
Michele Di Giorgio | f9b595a | 2020-07-03 13:34:52 +0100 | [diff] [blame] | 149 | } // namespace arm_compute |