Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, 2017 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/NEGEMMTranspose1xWKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowTranspose.h" |
| 27 | #include "arm_compute/core/Coordinates.h" |
| 28 | #include "arm_compute/core/Error.h" |
| 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/ITensor.h" |
| 31 | #include "arm_compute/core/NEON/INEKernel.h" |
| 32 | #include "arm_compute/core/TensorInfo.h" |
| 33 | #include "arm_compute/core/TensorShape.h" |
| 34 | #include "arm_compute/core/Types.h" |
| 35 | #include "arm_compute/core/Validate.h" |
| 36 | #include "arm_compute/core/Window.h" |
| 37 | |
| 38 | #include <arm_neon.h> |
| 39 | #include <cstddef> |
| 40 | #include <cstring> |
| 41 | |
| 42 | using namespace arm_compute; |
| 43 | |
| 44 | void NEGEMMTranspose1xWKernel::configure(const ITensor *input, ITensor *output) |
| 45 | { |
Gian Marco Iodice | bdb6b0b | 2017-06-30 12:21:00 +0100 | [diff] [blame] | 46 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::U8, DataType::S8, DataType::U16, DataType::S16, DataType::U32, DataType::S32, DataType::F16, |
| 47 | DataType::F32); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 48 | ARM_COMPUTE_ERROR_ON_NULLPTR(output); |
| 49 | |
| 50 | TensorShape output_shape{ input->info()->tensor_shape() }; |
| 51 | const size_t transpose_w = 16 / input->info()->element_size(); |
| 52 | output_shape.set(0, input->info()->dimension(1) * transpose_w); |
| 53 | output_shape.set(1, static_cast<size_t>(std::ceil((input->info()->dimension(0) / static_cast<float>(transpose_w))))); |
| 54 | |
| 55 | // Output tensor auto inizialitation if not yet initialized |
| 56 | auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position()); |
| 57 | |
| 58 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 59 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); |
Gian Marco Iodice | 9f89bae | 2017-06-22 12:09:49 +0100 | [diff] [blame] | 60 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 61 | |
| 62 | const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size(); |
Moritz Pflanzer | 0745a98 | 2017-07-05 16:34:28 +0100 | [diff] [blame] | 63 | const int scale_x = num_elems_processed_per_iteration; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | |
| 65 | _input = input; |
| 66 | _output = output; |
| 67 | |
| 68 | // Configure kernel window |
Moritz Pflanzer | 0745a98 | 2017-07-05 16:34:28 +0100 | [diff] [blame] | 69 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 70 | |
| 71 | ARM_COMPUTE_ERROR_ON_MSG((win.x().end() / scale_x) == 0, "Transposed shape would be 0 in the second dimension"); |
| 72 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 73 | AccessWindowTranspose output_access(output->info(), 0, 0, num_elems_processed_per_iteration, 1, scale_x, 1.f / scale_x); |
| 74 | |
| 75 | update_window_and_padding(win, |
| 76 | AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration), |
| 77 | output_access); |
| 78 | |
Georgios Pinitas | 4cbee6e | 2017-06-19 13:02:56 +0100 | [diff] [blame] | 79 | output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), input->info()->tensor_shape())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 80 | |
| 81 | INEKernel::configure(win); |
| 82 | } |
| 83 | |
| 84 | void NEGEMMTranspose1xWKernel::run(const Window &window) |
| 85 | { |
| 86 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 87 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window); |
| 88 | |
| 89 | /* |
| 90 | * Following an example of how the transposition1xW works when the input data type is F32 |
| 91 | * |
| 92 | * |a00 a01 a02 a03| |
| 93 | * |a10 a11 a12 a13| |
| 94 | * |a20 a21 a22 a23| = | a00 a01 a02 a03 || a10 a11 a12 a13 || a20 a21 a22 a23 || a30 a31 a32 a33 | |
| 95 | * |a30 a31 a32 a33| |
| 96 | * |
| 97 | * The output matrix will have the following shape: [ height * W, ceil(width / W) ], where W = (16 / element size of the tensor) |
| 98 | */ |
| 99 | |
| 100 | // 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 |
| 101 | Window win_out(window); |
| 102 | win_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 103 | win_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 104 | |
| 105 | Iterator in(_input, window); |
| 106 | Iterator out(_output, win_out); |
| 107 | |
| 108 | switch(_input->info()->element_size()) |
| 109 | { |
| 110 | case 1: |
| 111 | { |
| 112 | const size_t out_stride = _output->info()->strides_in_bytes()[1]; |
| 113 | execute_window_loop(window, [&](const Coordinates & id) |
| 114 | { |
| 115 | // Output address = base addr + (y * 16) + (x / 16 ) * stride |
| 116 | const uint8_t *in_ptr = in.ptr(); |
| 117 | uint8_t *const out_ptr = out.ptr() + (id.y() << 4) + (id.x() >> 4) * out_stride; |
| 118 | vst1q_u8(out_ptr, vld1q_u8(in_ptr)); |
| 119 | }, |
| 120 | in, out); |
| 121 | break; |
| 122 | } |
| 123 | case 2: |
| 124 | { |
| 125 | const size_t out_stride = _output->info()->strides_in_bytes()[1] / sizeof(int16_t); |
| 126 | execute_window_loop(window, [&](const Coordinates & id) |
| 127 | { |
| 128 | // Output address = base addr + (y * 8) + (x / 8 ) * stride |
| 129 | const auto in_ptr = reinterpret_cast<const uint16_t *>(in.ptr()); |
| 130 | const auto out_ptr = reinterpret_cast<uint16_t *>(out.ptr()) + (id.y() << 3) + (id.x() >> 3) * out_stride; |
| 131 | vst1q_u16(out_ptr, vld1q_u16(in_ptr)); |
| 132 | }, |
| 133 | in, out); |
| 134 | break; |
| 135 | } |
| 136 | case 4: |
| 137 | { |
| 138 | const size_t out_stride = _output->info()->strides_in_bytes()[1] / sizeof(float); |
| 139 | execute_window_loop(window, [&](const Coordinates & id) |
| 140 | { |
| 141 | // Output address = base addr + (y * 4) + (x / 4 ) * stride |
| 142 | const auto in_ptr = reinterpret_cast<const uint32_t *>(in.ptr()); |
| 143 | const auto out_ptr = reinterpret_cast<uint32_t *>(out.ptr()) + (id.y() << 2) + (id.x() >> 2) * out_stride; |
| 144 | vst1q_u32(out_ptr, vld1q_u32(in_ptr)); |
| 145 | }, |
| 146 | in, out); |
| 147 | break; |
| 148 | } |
| 149 | default: |
| 150 | { |
| 151 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | } |