Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 1 | /* |
Pablo Tello | f6169d8 | 2018-02-02 10:05:50 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +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/arm64/NEGEMMLowpAArch64V8P4Kernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/IAccessWindow.h" |
| 30 | #include "arm_compute/core/ITensor.h" |
| 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/Types.h" |
| 33 | #include "arm_compute/core/Utils.h" |
| 34 | #include "arm_compute/core/Validate.h" |
| 35 | #include "arm_compute/core/Window.h" |
| 36 | #include "support/ToolchainSupport.h" |
| 37 | |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 38 | namespace arm_compute |
| 39 | { |
| 40 | #include "arm_compute/core/NEON/kernels/assembly/gemm_interleaved.hpp" |
Pablo Tello | fef6dae | 2017-12-15 10:36:21 +0000 | [diff] [blame] | 41 | #include "arm_compute/core/NEON/kernels/assembly/kernels/a64_gemm_s8_12x8.hpp" |
Pablo Tello | 6681d24 | 2017-11-13 16:44:08 +0000 | [diff] [blame] | 42 | #include "arm_compute/core/NEON/kernels/assembly/kernels/a64_gemm_u8_12x8.hpp" |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 43 | } // namespace arm_compute |
| 44 | |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 45 | #include <arm_neon.h> |
| 46 | #include <cstddef> |
| 47 | #include <cstdint> |
| 48 | |
Ioan-Cristian Szabo | 33fd07b | 2017-10-26 15:42:24 +0100 | [diff] [blame] | 49 | // Enable only if compiled for AArch64-V8.2-A targets |
| 50 | #ifdef ARM_COMPUTE_AARCH64_V8_2 |
| 51 | |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 52 | namespace |
| 53 | { |
| 54 | using namespace arm_compute; |
| 55 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 56 | Status validate_arguments(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 57 | { |
Pablo Tello | fef6dae | 2017-12-15 10:36:21 +0000 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::QASYMM8, DataType::U8, DataType::S8); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 59 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32); |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1); |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, input1); |
| 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 | } |
| 65 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 66 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input0, ITensorInfo *input1, ITensorInfo *output) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 67 | { |
| 68 | // Configure kernel window |
| 69 | Window win = calculate_max_window(*output); |
| 70 | |
| 71 | AccessWindowRectangle output_access(output, 0, 0, 12, 8); |
| 72 | |
| 73 | const int input0_access_end = ceil_to_multiple(input0->tensor_shape().x(), 8); |
| 74 | const int input1_access_end = ceil_to_multiple(input1->tensor_shape().x(), 12); |
| 75 | |
| 76 | bool window_changed = update_window_and_padding(win, |
| 77 | AccessWindowStatic(input0, 0, 0, input0_access_end, input0->tensor_shape().y()), |
| 78 | AccessWindowStatic(input1, 0, 0, input1_access_end, input1->tensor_shape().y()), |
| 79 | output_access); |
| 80 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 81 | 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] | 82 | return std::make_pair(err, win); |
| 83 | } |
Pablo Tello | fef6dae | 2017-12-15 10:36:21 +0000 | [diff] [blame] | 84 | |
| 85 | template <typename strategy, typename To, typename Tr> |
| 86 | void *align_workspace(GemmInterleaved<strategy, To, Tr> &gemm, const ThreadInfo &info, ITensor *ws) |
| 87 | { |
| 88 | constexpr size_t alignment = 4096; |
| 89 | const size_t offset = (gemm.get_working_size() + alignment - 1) * info.thread_id; |
| 90 | void *workspace = ws->buffer() + offset; |
| 91 | size_t workspace_size = ws->info()->total_size(); |
| 92 | |
| 93 | if(support::cpp11::align(alignment, gemm.get_working_size(), workspace, workspace_size) == nullptr) |
| 94 | { |
| 95 | ARM_COMPUTE_ERROR("Not enough space to align buffer!"); |
| 96 | } |
| 97 | return workspace; |
| 98 | } |
| 99 | |
| 100 | template <typename strategy> |
| 101 | void execute_gemm(const Window &win, Iterator &in0, Iterator &in1, Iterator &out, |
| 102 | const ThreadInfo &info, ITensor *ws, int M, int N, int K, bool is_transposed_0, bool is_transposed_1, |
| 103 | int lda, int ldb, int ldc, float alpha, float beta) |
| 104 | { |
Pablo Tello | f6169d8 | 2018-02-02 10:05:50 +0000 | [diff] [blame] | 105 | ARM_COMPUTE_UNUSED(M); |
| 106 | ARM_COMPUTE_UNUSED(N); |
| 107 | ARM_COMPUTE_UNUSED(K); |
| 108 | ARM_COMPUTE_UNUSED(is_transposed_0); |
| 109 | ARM_COMPUTE_UNUSED(is_transposed_1); |
Pablo Tello | fef6dae | 2017-12-15 10:36:21 +0000 | [diff] [blame] | 110 | GemmInterleaved<strategy, typename strategy::operand_type, typename strategy::result_type> gemm(&info.cpu_info, M, N, K, is_transposed_0, is_transposed_1); |
| 111 | void *workspace = align_workspace(gemm, info, ws); |
| 112 | execute_window_loop(win, [&](const Coordinates & id) |
| 113 | { |
| 114 | gemm.execute(reinterpret_cast<const typename strategy::operand_type *>(in0.ptr()), lda, |
| 115 | reinterpret_cast<const typename strategy::operand_type *>(in1.ptr()), ldb, |
| 116 | reinterpret_cast<typename strategy::result_type *>(out.ptr()), ldc, |
| 117 | alpha, beta, workspace); |
| 118 | }, |
| 119 | in0, out); |
| 120 | } |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 121 | } // namespace |
| 122 | |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 123 | namespace arm_compute |
| 124 | { |
Georgios Pinitas | 08c5a06 | 2017-12-14 17:53:39 +0000 | [diff] [blame] | 125 | void NEGEMMLowpAArch64V8P4Kernel::internal_configure(const ITensor *input0, const ITensor *input1, ITensor *output, ITensor *workspace, float alpha, float beta, bool is_transposed_0, |
| 126 | bool is_transposed_1) |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 127 | { |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 128 | // Perform validate step |
| 129 | ARM_COMPUTE_ERROR_ON_NULLPTR(input0, input1, output); |
| 130 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input0->info(), input1->info(), output->info())); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 131 | |
Georgios Pinitas | 08c5a06 | 2017-12-14 17:53:39 +0000 | [diff] [blame] | 132 | _input0 = input0; |
| 133 | _input1 = input1; |
| 134 | _output = output; |
| 135 | _workspace = workspace; |
| 136 | _alpha = alpha; |
| 137 | _beta = beta; |
| 138 | _is_transposed_0 = is_transposed_0; |
| 139 | _is_transposed_1 = is_transposed_1; |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 140 | |
| 141 | // Configure kernel window |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 142 | auto win_config = validate_and_configure_window(input0->info(), input1->info(), output->info()); |
| 143 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 144 | INEKernel::configure(win_config.second); |
| 145 | } |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 146 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 147 | Status NEGEMMLowpAArch64V8P4Kernel::validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 148 | { |
| 149 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input0, input1, output)); |
| 150 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input0->clone().get(), input1->clone().get(), output->clone().get()).first); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 151 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 152 | return Status{}; |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 155 | void NEGEMMLowpAArch64V8P4Kernel::run(const Window &window, const ThreadInfo &info) |
| 156 | { |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 157 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 158 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 159 | |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 160 | const int lda = _input0->info()->strides_in_bytes().y(); |
| 161 | const int ldb = _input1->info()->strides_in_bytes().y(); |
Pablo Tello | 6681d24 | 2017-11-13 16:44:08 +0000 | [diff] [blame] | 162 | const int ldc = _output->info()->strides_in_bytes().y() / sizeof(uint32_t); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 163 | |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 164 | const int M = std::min(_output->info()->tensor_shape().y(), static_cast<size_t>(window.y().end())) - window.y().start(); |
| 165 | const int N = _output->info()->tensor_shape().x(); |
| 166 | const int K = _input0->info()->tensor_shape().x(); |
| 167 | |
| 168 | // Only iterate over batches |
| 169 | Window win(window); |
| 170 | win.set(0, Window::Dimension(0, 1, 1)); |
| 171 | win.set(1, Window::Dimension(0, 1, 1)); |
| 172 | |
| 173 | Iterator in0(_input0, window); |
Pablo Tello | fef6dae | 2017-12-15 10:36:21 +0000 | [diff] [blame] | 174 | Iterator in1(_input1, window); |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 175 | Iterator out(_output, window); |
| 176 | |
Pablo Tello | fef6dae | 2017-12-15 10:36:21 +0000 | [diff] [blame] | 177 | switch(_input0->info()->data_type()) |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 178 | { |
Pablo Tello | fef6dae | 2017-12-15 10:36:21 +0000 | [diff] [blame] | 179 | case DataType::QASYMM8: |
| 180 | case DataType::U8: |
| 181 | { |
| 182 | execute_gemm<gemm_u8_12x8>(win, in0, in1, out, info, _workspace, M, N, K, _is_transposed_0, _is_transposed_1, lda, ldb, ldc, _alpha, _beta); |
| 183 | break; |
| 184 | } |
| 185 | case DataType::S8: |
| 186 | { |
| 187 | execute_gemm<gemm_s8_12x8>(win, in0, in1, out, info, _workspace, M, N, K, _is_transposed_0, _is_transposed_1, lda, ldb, ldc, _alpha, _beta); |
| 188 | break; |
| 189 | } |
| 190 | default: |
| 191 | { |
| 192 | ARM_COMPUTE_ERROR("Not supported."); |
| 193 | break; |
| 194 | } |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 195 | } |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 196 | } |
| 197 | } // namespace arm_compute |
Ioan-Cristian Szabo | 33fd07b | 2017-10-26 15:42:24 +0100 | [diff] [blame] | 198 | #endif /* ARM_COMPUTE_AARCH64_V8_2 */ |