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/NEGEMMInterleave4x4Kernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
| 29 | #include "arm_compute/core/NEON/INEKernel.h" |
| 30 | #include "arm_compute/core/Types.h" |
| 31 | #include "arm_compute/core/Validate.h" |
| 32 | #include "arm_compute/core/Window.h" |
| 33 | |
| 34 | #include <arm_neon.h> |
| 35 | #include <cstddef> |
| 36 | #include <cstdint> |
| 37 | #include <tuple> |
| 38 | |
| 39 | using namespace arm_compute; |
| 40 | |
| 41 | namespace |
| 42 | { |
| 43 | void gemm_interleave_8bit_elements(const ITensor *input, ITensor *output, const Window &window) |
| 44 | { |
| 45 | const size_t in_stride = input->info()->strides_in_bytes()[1]; |
| 46 | |
| 47 | // Set window for output tensor |
| 48 | Window win_out(window); |
| 49 | win_out.scale(Window::DimY, 0.25f); |
| 50 | Iterator in(input, window); |
| 51 | |
| 52 | win_out.set_dimension_step(Window::DimX, 32); |
| 53 | Iterator out(output, win_out); |
| 54 | |
| 55 | execute_window_loop(window, [&](const Coordinates &) |
| 56 | { |
| 57 | const uint8x8x4_t data = |
| 58 | { |
| 59 | { |
| 60 | vld1_u8(in.ptr() + 0 * in_stride), |
| 61 | vld1_u8(in.ptr() + 1 * in_stride), |
| 62 | vld1_u8(in.ptr() + 2 * in_stride), |
| 63 | vld1_u8(in.ptr() + 3 * in_stride), |
| 64 | } |
| 65 | }; |
| 66 | vst4_u8(out.ptr(), data); |
| 67 | }, |
| 68 | in, out); |
| 69 | } |
| 70 | |
| 71 | void gemm_interleave_16bit_elements(const ITensor *input, ITensor *output, const Window &window) |
| 72 | { |
| 73 | const size_t in_stride = input->info()->strides_in_bytes()[1]; |
| 74 | |
| 75 | // Set window for output tensor |
| 76 | Window win_out(window); |
| 77 | win_out.scale(Window::DimY, 0.25f); |
| 78 | Iterator in(input, window); |
| 79 | |
| 80 | win_out.set_dimension_step(Window::DimX, 16); |
| 81 | Iterator out(output, win_out); |
| 82 | |
| 83 | execute_window_loop(window, [&](const Coordinates & id) |
| 84 | { |
| 85 | const uint16x4x4_t data = |
| 86 | { |
| 87 | { |
| 88 | vld1_u16(reinterpret_cast<uint16_t *>(in.ptr() + 0 * in_stride)), |
| 89 | vld1_u16(reinterpret_cast<uint16_t *>(in.ptr() + 1 * in_stride)), |
| 90 | vld1_u16(reinterpret_cast<uint16_t *>(in.ptr() + 2 * in_stride)), |
| 91 | vld1_u16(reinterpret_cast<uint16_t *>(in.ptr() + 3 * in_stride)), |
| 92 | } |
| 93 | }; |
| 94 | vst4_u16(reinterpret_cast<uint16_t *>(out.ptr()), data); |
| 95 | }, |
| 96 | in, out); |
| 97 | } |
| 98 | |
| 99 | void gemm_interleave_32bit_elements(const ITensor *input, ITensor *output, const Window &window) |
| 100 | { |
| 101 | const size_t in_stride = input->info()->strides_in_bytes()[1]; |
| 102 | |
| 103 | // Set window for output tensor |
| 104 | Window win_out(window); |
| 105 | win_out.scale(Window::DimY, 0.25f); |
| 106 | Iterator in(input, window); |
| 107 | |
| 108 | win_out.set_dimension_step(Window::DimX, 16); |
| 109 | Iterator out(output, win_out); |
| 110 | |
| 111 | execute_window_loop(window, [&](const Coordinates & id) |
| 112 | { |
| 113 | const uint32x4x4_t data = |
| 114 | { |
| 115 | { |
| 116 | vld1q_u32(reinterpret_cast<uint32_t *>(in.ptr() + 0 * in_stride)), |
| 117 | vld1q_u32(reinterpret_cast<uint32_t *>(in.ptr() + 1 * in_stride)), |
| 118 | vld1q_u32(reinterpret_cast<uint32_t *>(in.ptr() + 2 * in_stride)), |
| 119 | vld1q_u32(reinterpret_cast<uint32_t *>(in.ptr() + 3 * in_stride)) |
| 120 | } |
| 121 | }; |
| 122 | vst4q_u32(reinterpret_cast<uint32_t *>(out.ptr()), data); |
| 123 | }, |
| 124 | in, out); |
| 125 | } |
| 126 | } // namespace |
| 127 | |
| 128 | NEGEMMInterleave4x4Kernel::NEGEMMInterleave4x4Kernel() |
| 129 | : _func(nullptr) |
| 130 | { |
| 131 | } |
| 132 | |
| 133 | void NEGEMMInterleave4x4Kernel::configure(const ITensor *input, ITensor *output) |
| 134 | { |
Gian Marco Iodice | bdb6b0b | 2017-06-30 12:21:00 +0100 | [diff] [blame] | 135 | 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, |
| 136 | DataType::F32); |
Gian Marco Iodice | ec8b45e | 2017-06-22 13:00:39 +0100 | [diff] [blame] | 137 | ARM_COMPUTE_ERROR_ON_NULLPTR(output); |
| 138 | |
| 139 | TensorShape output_shape = input->info()->tensor_shape(); |
| 140 | output_shape.set(0, input->info()->dimension(0) * 4); |
| 141 | output_shape.set(1, std::ceil(input->info()->dimension(1) / 4.0f)); |
| 142 | |
| 143 | // Output auto inizialitation if not yet initialized |
| 144 | auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position()); |
| 145 | |
| 146 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 147 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Gian Marco Iodice | ec8b45e | 2017-06-22 13:00:39 +0100 | [diff] [blame] | 148 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 149 | |
| 150 | _input = input; |
| 151 | _output = output; |
| 152 | |
| 153 | unsigned int num_elems_processed_per_iteration_x = 4; |
| 154 | constexpr unsigned int num_elems_processed_per_iteration_y = 4; |
| 155 | |
| 156 | switch(input->info()->element_size()) |
| 157 | { |
| 158 | case 1: |
| 159 | num_elems_processed_per_iteration_x = 8; |
| 160 | _func = &gemm_interleave_8bit_elements; |
| 161 | break; |
| 162 | case 2: |
| 163 | _func = &gemm_interleave_16bit_elements; |
| 164 | break; |
| 165 | case 4: |
| 166 | _func = &gemm_interleave_32bit_elements; |
| 167 | break; |
| 168 | default: |
| 169 | ARM_COMPUTE_ERROR_ON("Element size not supported"); |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | // Configure kernel window |
| 174 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
| 175 | |
| 176 | AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_processed_per_iteration_x * num_elems_processed_per_iteration_y, 1, 4.0f, 0.25f); |
| 177 | AccessWindowRectangle input_access(input->info(), 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y); |
| 178 | update_window_and_padding(win, output_access, input_access); |
| 179 | |
| 180 | output_access.set_valid_region(win, input->info()->valid_region()); |
| 181 | |
| 182 | INEKernel::configure(win); |
| 183 | } |
| 184 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame^] | 185 | void NEGEMMInterleave4x4Kernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 186 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame^] | 187 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 188 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 189 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 190 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 191 | /* |
| 192 | * This kernel puts the values in a 4x4 block of Matrix A on the same row (Interleaved values) |
| 193 | * |a00 a01 a02 a03| |
| 194 | * |a10 a11 a12 a13| |
| 195 | * |a20 a21 a22 a23| = | a00 a10 a20 a30 || a01 a11 a21 a31 || a02 a12 a22 a32 || a03 a13 a23 a33 | |
| 196 | * |a30 a31 a32 a33| |
| 197 | * |
| 198 | * After this operation, the output matrix will have the following shape: [ height * 4, ceil(width / 4.0f) ] |
| 199 | */ |
| 200 | (*_func)(_input, _output, window); |
| 201 | } |