Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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 | #ifndef __ARM_COMPUTE_NEGEMM_H__ |
| 25 | #define __ARM_COMPUTE_NEGEMM_H__ |
| 26 | |
| 27 | #include "arm_compute/core/NEON/kernels/NEFillBorderKernel.h" |
| 28 | #include "arm_compute/core/NEON/kernels/NEGEMMInterleave4x4Kernel.h" |
| 29 | #include "arm_compute/core/NEON/kernels/NEGEMMMatrixAdditionKernel.h" |
| 30 | #include "arm_compute/core/NEON/kernels/NEGEMMMatrixMultiplyKernel.h" |
| 31 | #include "arm_compute/core/NEON/kernels/NEGEMMTranspose1xWKernel.h" |
| 32 | #include "arm_compute/runtime/IFunction.h" |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 33 | #include "arm_compute/runtime/IMemoryManager.h" |
| 34 | #include "arm_compute/runtime/MemoryGroup.h" |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 35 | #include "arm_compute/runtime/NEON/functions/NEGEMMAssemblyDispatch.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | #include "arm_compute/runtime/Tensor.h" |
| 37 | |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 38 | #include <memory> |
| 39 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 40 | namespace arm_compute |
| 41 | { |
| 42 | /** Basic function to execute GEMM on NEON. This function calls the following NEON kernels: |
| 43 | * |
| 44 | * -# @ref NEGEMMInterleave4x4Kernel (if the output tensor is a matrix) |
| 45 | * -# @ref NEGEMMTranspose1xWKernel (if the output tensor is a matrix) |
| 46 | * -# @ref NEGEMMMatrixMultiplyKernel |
| 47 | * -# @ref NEGEMMMatrixAdditionKernel (if c != nullptr and beta != 0.0) |
| 48 | * |
| 49 | */ |
| 50 | class NEGEMM : public IFunction |
| 51 | { |
| 52 | public: |
| 53 | /** Constructor */ |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 54 | NEGEMM(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 55 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 56 | NEGEMM(const NEGEMM &) = delete; |
| 57 | /** Default move constructor */ |
| 58 | NEGEMM(NEGEMM &&) = default; |
| 59 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 60 | NEGEMM &operator=(const NEGEMM &) = delete; |
| 61 | /** Default move assignment operator */ |
| 62 | NEGEMM &operator=(NEGEMM &&) = default; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 63 | /** Initialise the kernel's inputs, output |
| 64 | * |
| 65 | * @note GEMM: General Matrix Multiply - [alpha * A * B + beta * C]. |
| 66 | * @note GEMM: The tensors a, b, c, d must have the same data type. You should not mix data types when calling this function. |
| 67 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 68 | * @param[in] a First input tensor (Matrix A or Vector A). Data type supported: F16/F32 |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 69 | * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a |
| 70 | * @param[in] c Third input tensor (Matrix C). It can be a nullptr if just the multiplication between @p a and @p b is needed. Data type supported: same as @p a |
| 71 | * @param[out] d Output tensor. Data type supported: same as @p a |
| 72 | * @param[in] alpha Weight of the matrix product |
| 73 | * @param[in] beta Weight of matrix C |
| 74 | * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and |
| 75 | * if the reshape of matrix B should happen only for the first run |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | */ |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 77 | void configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo()); |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 78 | /** Static function to check if given info will lead to a valid configuration of @ref NEGEMM. |
| 79 | * |
| 80 | * @param[in] a First input tensor info (Matrix or Vector A). Data types supported: F16/F32 |
| 81 | * @param[in] b Second input tensor info (Matrix B). Data type supported: same as @p a. |
| 82 | * @param[in] c Third input tensor info (Matrix C). It can be a nullptr if just the multiplication between @p a and @p b is needed. Data type supported: same as @p a. |
| 83 | * @param[out] output Output tensor info. Data type supported: same as @p a |
| 84 | * @param[in] alpha Weight of the matrix product |
| 85 | * @param[in] beta Weight of matrix C |
| 86 | * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and |
| 87 | * if the reshape of matrix B should happen only for the first run |
| 88 | * |
| 89 | * @return a status |
| 90 | */ |
| 91 | static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 92 | |
| 93 | // Inherited methods overridden: |
| 94 | void run() override; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 95 | void prepare() override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | |
| 97 | private: |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 98 | MemoryGroup _memory_group; |
| 99 | NEGEMMInterleave4x4Kernel _interleave_kernel; |
| 100 | NEGEMMTranspose1xWKernel _transpose_kernel; |
| 101 | NEGEMMMatrixMultiplyKernel _mm_kernel; |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 102 | NEGEMMAssemblyDispatch _asm_glue; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 103 | NEGEMMMatrixAdditionKernel _ma_kernel; |
| 104 | Tensor _tmp_a; |
| 105 | Tensor _tmp_b; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 106 | const ITensor *_original_b; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 107 | bool _run_vector_matrix_multiplication; |
| 108 | bool _run_addition; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 109 | bool _reshape_b_only_on_first_run; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 110 | bool _is_prepared; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 111 | }; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 112 | } // namespace arm_compute |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | #endif /*__ARM_COMPUTE_NEGEMM_H__ */ |