Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 1 | /* |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +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_NEGEMMLOWPMATRIXMULTIPLYCORE_H__ |
| 25 | #define __ARM_COMPUTE_NEGEMMLOWPMATRIXMULTIPLYCORE_H__ |
| 26 | |
| 27 | #include "arm_compute/core/NEON/INEKernel.h" |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/NEON/kernels/NEGEMMLowpOffsetContributionKernel.h" |
| 29 | #include "arm_compute/core/NEON/kernels/NEGEMMLowpReductionKernel.h" |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/IFunction.h" |
| 31 | #include "arm_compute/runtime/IMemoryManager.h" |
| 32 | #include "arm_compute/runtime/MemoryGroup.h" |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 33 | #include "arm_compute/runtime/NEON/functions/NEGEMMAssemblyDispatch.h" |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 34 | #include "arm_compute/runtime/Tensor.h" |
| 35 | |
| 36 | #include <memory> |
| 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | class ITensor; |
| 41 | |
| 42 | /** Basic function to execute GEMMLowpMatrixMultiplyCore on NEON. This function calls the following NEON kernels if the DOT product instruction is not available: |
| 43 | * |
| 44 | * -# @ref NEGEMMInterleave4x4Kernel |
| 45 | * -# @ref NEGEMMTranspose1xWKernel |
| 46 | * -# @ref NEGEMMLowpMatrixMultiplyKernel |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 47 | * -# @ref NEGEMMLowpOffsetContributionKernel |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 48 | * |
| 49 | * otherwise if the DOT product instruction is available: |
| 50 | * |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 51 | * -# @ref NEGEMMLowpOffsetContributionKernel |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 52 | * |
| 53 | */ |
| 54 | class NEGEMMLowpMatrixMultiplyCore : public IFunction |
| 55 | { |
| 56 | public: |
| 57 | /** Constructor */ |
| 58 | NEGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 59 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 60 | NEGEMMLowpMatrixMultiplyCore(const NEGEMMLowpMatrixMultiplyCore &) = delete; |
| 61 | /** Default move constructor */ |
| 62 | NEGEMMLowpMatrixMultiplyCore(NEGEMMLowpMatrixMultiplyCore &&) = default; |
| 63 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 64 | NEGEMMLowpMatrixMultiplyCore &operator=(const NEGEMMLowpMatrixMultiplyCore &) = delete; |
| 65 | /** Default move assignment operator */ |
| 66 | NEGEMMLowpMatrixMultiplyCore &operator=(NEGEMMLowpMatrixMultiplyCore &&) = default; |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 67 | /** Initialise the kernel's inputs, output |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 68 | * |
| 69 | * @note GEMM_LOWP: low precision GEMM kernel |
| 70 | * This kernel performs the following computations: |
| 71 | * |
| 72 | * -# Convert a values from QASYMM8 to int32 and add a_offset to each of them. |
| 73 | * -# Convert b values from QASYMM8 to int32 add b_offset to each of them. |
| 74 | * -# Compute the matrix product of the resulting a * b in int32. |
| 75 | * |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 76 | * @param[in] a First input tensor (Matrix A). Data type supported: QASYMM8. |
| 77 | * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a |
| 78 | * @param[out] output Output tensor. Data type supported: Data type supported: S32 |
| 79 | * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and |
| 80 | * if the reshape of matrix B should be executed only for the first run |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 81 | */ |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 82 | void configure(const ITensor *a, const ITensor *b, ITensor *output, const GEMMInfo &gemm_info = GEMMInfo()); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 83 | /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMLowpMatrixMultiplyCore |
| 84 | * |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 85 | * @param[in] a First input tensor (Matrix A). Data type supported: QASYMM8. |
| 86 | * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a |
| 87 | * @param[in] output Output tensor. Data type supported: Data type supported: S32 |
| 88 | * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and |
| 89 | * if the reshape of matrix B should be executed only for the first run |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 90 | * |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 91 | * @return a status |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 92 | */ |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 93 | static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *output, const GEMMInfo &gemm_info = GEMMInfo()); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 94 | |
Pablo Tello | fc00449 | 2018-03-23 11:40:05 +0000 | [diff] [blame] | 95 | // Inherited methods overridden |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 96 | void run() override; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 97 | void prepare() override; |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 98 | |
| 99 | private: |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 100 | MemoryGroup _memory_group; |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 101 | NEGEMMAssemblyDispatch _asm_glue; |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 102 | std::unique_ptr<INEKernel> _mm_kernel; |
| 103 | std::unique_ptr<INEKernel> _mtx_a_reshape_kernel; |
| 104 | std::unique_ptr<INEKernel> _mtx_b_reshape_kernel; |
| 105 | NEGEMMLowpMatrixAReductionKernel _mtx_a_reduction_kernel; |
| 106 | NEGEMMLowpMatrixBReductionKernel _mtx_b_reduction_kernel; |
| 107 | NEGEMMLowpOffsetContributionKernel _offset_contribution_kernel; |
| 108 | Tensor _vector_sum_col; |
| 109 | Tensor _vector_sum_row; |
| 110 | Tensor _tmp_a; |
| 111 | Tensor _tmp_b; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 112 | const ITensor *_original_b; |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 113 | int32_t _a_offset; |
| 114 | int32_t _b_offset; |
Gian Marco | c7f9b89 | 2017-11-30 14:31:13 +0000 | [diff] [blame] | 115 | bool _run_vector_matrix_multiplication; |
| 116 | bool _dot_product_path; |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 117 | bool _reshape_b_only_on_first_run; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 118 | bool _is_prepared; |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 119 | }; |
| 120 | } |
| 121 | #endif /*__ARM_COMPUTE_NEGEMMLOWPMATRIXMULTIPLYCORE_H__ */ |