Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 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/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 | { |
| 105 | GemmInterleaved<strategy, typename strategy::operand_type, typename strategy::result_type> gemm(&info.cpu_info, M, N, K, is_transposed_0, is_transposed_1); |
| 106 | void *workspace = align_workspace(gemm, info, ws); |
| 107 | execute_window_loop(win, [&](const Coordinates & id) |
| 108 | { |
| 109 | gemm.execute(reinterpret_cast<const typename strategy::operand_type *>(in0.ptr()), lda, |
| 110 | reinterpret_cast<const typename strategy::operand_type *>(in1.ptr()), ldb, |
| 111 | reinterpret_cast<typename strategy::result_type *>(out.ptr()), ldc, |
| 112 | alpha, beta, workspace); |
| 113 | }, |
| 114 | in0, out); |
| 115 | } |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 116 | } // namespace |
| 117 | |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 118 | namespace arm_compute |
| 119 | { |
Georgios Pinitas | 08c5a06 | 2017-12-14 17:53:39 +0000 | [diff] [blame] | 120 | void NEGEMMLowpAArch64V8P4Kernel::internal_configure(const ITensor *input0, const ITensor *input1, ITensor *output, ITensor *workspace, float alpha, float beta, bool is_transposed_0, |
| 121 | bool is_transposed_1) |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 122 | { |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 123 | // Perform validate step |
| 124 | ARM_COMPUTE_ERROR_ON_NULLPTR(input0, input1, output); |
| 125 | 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] | 126 | |
Georgios Pinitas | 08c5a06 | 2017-12-14 17:53:39 +0000 | [diff] [blame] | 127 | _input0 = input0; |
| 128 | _input1 = input1; |
| 129 | _output = output; |
| 130 | _workspace = workspace; |
| 131 | _alpha = alpha; |
| 132 | _beta = beta; |
| 133 | _is_transposed_0 = is_transposed_0; |
| 134 | _is_transposed_1 = is_transposed_1; |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 135 | |
| 136 | // Configure kernel window |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 137 | auto win_config = validate_and_configure_window(input0->info(), input1->info(), output->info()); |
| 138 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 139 | INEKernel::configure(win_config.second); |
| 140 | } |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 141 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 142 | Status NEGEMMLowpAArch64V8P4Kernel::validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 143 | { |
| 144 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input0, input1, output)); |
| 145 | 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] | 146 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 147 | return Status{}; |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 150 | void NEGEMMLowpAArch64V8P4Kernel::run(const Window &window, const ThreadInfo &info) |
| 151 | { |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 152 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 153 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 154 | |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 155 | const int lda = _input0->info()->strides_in_bytes().y(); |
| 156 | const int ldb = _input1->info()->strides_in_bytes().y(); |
Pablo Tello | 6681d24 | 2017-11-13 16:44:08 +0000 | [diff] [blame] | 157 | const int ldc = _output->info()->strides_in_bytes().y() / sizeof(uint32_t); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 158 | |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 159 | const int M = std::min(_output->info()->tensor_shape().y(), static_cast<size_t>(window.y().end())) - window.y().start(); |
| 160 | const int N = _output->info()->tensor_shape().x(); |
| 161 | const int K = _input0->info()->tensor_shape().x(); |
| 162 | |
| 163 | // Only iterate over batches |
| 164 | Window win(window); |
| 165 | win.set(0, Window::Dimension(0, 1, 1)); |
| 166 | win.set(1, Window::Dimension(0, 1, 1)); |
| 167 | |
| 168 | Iterator in0(_input0, window); |
Pablo Tello | fef6dae | 2017-12-15 10:36:21 +0000 | [diff] [blame^] | 169 | Iterator in1(_input1, window); |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 170 | Iterator out(_output, window); |
| 171 | |
Pablo Tello | fef6dae | 2017-12-15 10:36:21 +0000 | [diff] [blame^] | 172 | switch(_input0->info()->data_type()) |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 173 | { |
Pablo Tello | fef6dae | 2017-12-15 10:36:21 +0000 | [diff] [blame^] | 174 | case DataType::QASYMM8: |
| 175 | case DataType::U8: |
| 176 | { |
| 177 | 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); |
| 178 | break; |
| 179 | } |
| 180 | case DataType::S8: |
| 181 | { |
| 182 | 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); |
| 183 | break; |
| 184 | } |
| 185 | default: |
| 186 | { |
| 187 | ARM_COMPUTE_ERROR("Not supported."); |
| 188 | break; |
| 189 | } |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 190 | } |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 191 | } |
| 192 | } // namespace arm_compute |
Ioan-Cristian Szabo | 33fd07b | 2017-10-26 15:42:24 +0100 | [diff] [blame] | 193 | #endif /* ARM_COMPUTE_AARCH64_V8_2 */ |