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 | 6681d24 | 2017-11-13 16:44:08 +0000 | [diff] [blame] | 41 | #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] | 42 | } // namespace arm_compute |
| 43 | |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 44 | #include <arm_neon.h> |
| 45 | #include <cstddef> |
| 46 | #include <cstdint> |
| 47 | |
Ioan-Cristian Szabo | 33fd07b | 2017-10-26 15:42:24 +0100 | [diff] [blame] | 48 | // Enable only if compiled for AArch64-V8.2-A targets |
| 49 | #ifdef ARM_COMPUTE_AARCH64_V8_2 |
| 50 | |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame^] | 51 | namespace |
| 52 | { |
| 53 | using namespace arm_compute; |
| 54 | |
| 55 | Error validate_arguments(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output) |
| 56 | { |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::QASYMM8); |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1); |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, input1); |
| 61 | |
| 62 | return Error{}; |
| 63 | } |
| 64 | |
| 65 | std::pair<Error, Window> validate_and_configure_window(ITensorInfo *input0, ITensorInfo *input1, ITensorInfo *output) |
| 66 | { |
| 67 | // Configure kernel window |
| 68 | Window win = calculate_max_window(*output); |
| 69 | |
| 70 | AccessWindowRectangle output_access(output, 0, 0, 12, 8); |
| 71 | |
| 72 | const int input0_access_end = ceil_to_multiple(input0->tensor_shape().x(), 8); |
| 73 | const int input1_access_end = ceil_to_multiple(input1->tensor_shape().x(), 12); |
| 74 | |
| 75 | bool window_changed = update_window_and_padding(win, |
| 76 | AccessWindowStatic(input0, 0, 0, input0_access_end, input0->tensor_shape().y()), |
| 77 | AccessWindowStatic(input1, 0, 0, input1_access_end, input1->tensor_shape().y()), |
| 78 | output_access); |
| 79 | |
| 80 | Error err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Error{}; |
| 81 | return std::make_pair(err, win); |
| 82 | } |
| 83 | } // namespace |
| 84 | |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 85 | namespace arm_compute |
| 86 | { |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 87 | void NEGEMMLowpAArch64V8P4Kernel::internal_configure(const ITensor *input0, const ITensor *input1, ITensor *output, ITensor *workspace, float alpha, float beta, bool transform_0, bool transform_1) |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 88 | { |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame^] | 89 | // Perform validate step |
| 90 | ARM_COMPUTE_ERROR_ON_NULLPTR(input0, input1, output); |
| 91 | 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] | 92 | |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 93 | _input0 = input0; |
| 94 | _input1 = input1; |
| 95 | _output = output; |
| 96 | _workspace = workspace; |
| 97 | _alpha = alpha; |
| 98 | _beta = beta; |
| 99 | _transform_0 = transform_0; |
| 100 | _transform_1 = transform_1; |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 101 | |
| 102 | // Configure kernel window |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame^] | 103 | auto win_config = validate_and_configure_window(input0->info(), input1->info(), output->info()); |
| 104 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 105 | INEKernel::configure(win_config.second); |
| 106 | } |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 107 | |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame^] | 108 | Error NEGEMMLowpAArch64V8P4Kernel::validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output) |
| 109 | { |
| 110 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input0, input1, output)); |
| 111 | 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] | 112 | |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame^] | 113 | return Error{}; |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 116 | void NEGEMMLowpAArch64V8P4Kernel::run(const Window &window, const ThreadInfo &info) |
| 117 | { |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 118 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 119 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 120 | |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 121 | const int lda = _input0->info()->strides_in_bytes().y(); |
| 122 | const int ldb = _input1->info()->strides_in_bytes().y(); |
Pablo Tello | 6681d24 | 2017-11-13 16:44:08 +0000 | [diff] [blame] | 123 | const int ldc = _output->info()->strides_in_bytes().y() / sizeof(uint32_t); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 124 | |
Pablo Tello | 6681d24 | 2017-11-13 16:44:08 +0000 | [diff] [blame] | 125 | const auto in1_ptr = reinterpret_cast<const gemm_u8_12x8::operand_type *>(_input1->buffer()); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 126 | |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 127 | const int M = std::min(_output->info()->tensor_shape().y(), static_cast<size_t>(window.y().end())) - window.y().start(); |
| 128 | const int N = _output->info()->tensor_shape().x(); |
| 129 | const int K = _input0->info()->tensor_shape().x(); |
| 130 | |
| 131 | // Only iterate over batches |
| 132 | Window win(window); |
| 133 | win.set(0, Window::Dimension(0, 1, 1)); |
| 134 | win.set(1, Window::Dimension(0, 1, 1)); |
| 135 | |
| 136 | Iterator in0(_input0, window); |
| 137 | Iterator out(_output, window); |
| 138 | |
Pablo Tello | 6681d24 | 2017-11-13 16:44:08 +0000 | [diff] [blame] | 139 | GemmInterleaved<gemm_u8_12x8, gemm_u8_12x8::operand_type, gemm_u8_12x8::result_type> gemm(&info.cpu_info, M, N, K, !_transform_1, !_transform_1); |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 140 | |
| 141 | constexpr size_t alignment = 4096; |
| 142 | const size_t offset = (gemm.get_working_size() + alignment - 1) * info.thread_id; |
| 143 | void *workspace = _workspace->buffer() + offset; |
| 144 | size_t workspace_size = _workspace->info()->total_size(); |
| 145 | |
| 146 | if(support::cpp11::align(alignment, gemm.get_working_size(), workspace, workspace_size) == nullptr) |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 147 | { |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 148 | ARM_COMPUTE_ERROR("Not enough space to align buffer!"); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 149 | } |
| 150 | |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 151 | execute_window_loop(win, [&](const Coordinates & id) |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 152 | { |
Pablo Tello | 6681d24 | 2017-11-13 16:44:08 +0000 | [diff] [blame] | 153 | gemm.execute(reinterpret_cast<const gemm_u8_12x8::operand_type *>(in0.ptr()), lda, |
| 154 | reinterpret_cast<const gemm_u8_12x8::operand_type *>(in1_ptr), ldb, |
| 155 | reinterpret_cast<gemm_u8_12x8::result_type *>(out.ptr()), ldc, |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 156 | _alpha, _beta, workspace); |
| 157 | }, |
| 158 | in0, out); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 159 | } |
| 160 | } // namespace arm_compute |
Ioan-Cristian Szabo | 33fd07b | 2017-10-26 15:42:24 +0100 | [diff] [blame] | 161 | #endif /* ARM_COMPUTE_AARCH64_V8_2 */ |