Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 1 | /* |
Sheri Zhang | ac6499a | 2021-02-10 15:32:38 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_NEGEMMLOWPMATRIXMULTIPLYCORE_H |
| 25 | #define ARM_COMPUTE_NEGEMMLOWPMATRIXMULTIPLYCORE_H |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 26 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 27 | #include "NEActivationLayer.h" |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 28 | #include "arm_compute/runtime/IFunction.h" |
| 29 | #include "arm_compute/runtime/IMemoryManager.h" |
Georgios Pinitas | ec2256b | 2020-12-03 18:51:58 +0000 | [diff] [blame] | 30 | #include "arm_compute/runtime/IWeightsManager.h" |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 31 | #include "arm_compute/runtime/MemoryGroup.h" |
| 32 | #include "arm_compute/runtime/Tensor.h" |
| 33 | |
| 34 | #include <memory> |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | class ITensor; |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 39 | class NEConvertQuantizedSignednessKernel; |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 40 | class NEGEMMInterleave4x4Kernel; |
| 41 | class NEGEMMLowpMatrixMultiplyKernel; |
| 42 | class NEGEMMLowpOffsetContributionKernel; |
| 43 | class NEGEMMLowpOffsetContributionOutputStageKernel; |
| 44 | class NEGEMMLowpMatrixAReductionKernel; |
| 45 | class NEGEMMLowpMatrixBReductionKernel; |
| 46 | class NEGEMMTranspose1xWKernel; |
Georgios Pinitas | ec2256b | 2020-12-03 18:51:58 +0000 | [diff] [blame] | 47 | class NEGEMMAssemblyDispatch; |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 48 | |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 49 | /** Basic function to execute GEMMLowpMatrixMultiplyCore. This function calls the following kernels if the DOT product instruction is not available: |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 50 | * |
| 51 | * -# @ref NEGEMMInterleave4x4Kernel |
| 52 | * -# @ref NEGEMMTranspose1xWKernel |
| 53 | * -# @ref NEGEMMLowpMatrixMultiplyKernel |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 54 | * -# @ref NEGEMMLowpOffsetContributionKernel |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 55 | * -# @ref NEActivationLayer |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 56 | * |
| 57 | * otherwise if the DOT product instruction is available: |
| 58 | * |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 59 | * -# @ref NEGEMMLowpOffsetContributionKernel |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 60 | * |
| 61 | */ |
| 62 | class NEGEMMLowpMatrixMultiplyCore : public IFunction |
| 63 | { |
| 64 | public: |
| 65 | /** Constructor */ |
SiCongLi | 2e5fd63 | 2020-03-02 15:39:15 +0000 | [diff] [blame] | 66 | NEGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 67 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 68 | NEGEMMLowpMatrixMultiplyCore(const NEGEMMLowpMatrixMultiplyCore &) = delete; |
| 69 | /** Default move constructor */ |
| 70 | NEGEMMLowpMatrixMultiplyCore(NEGEMMLowpMatrixMultiplyCore &&) = default; |
| 71 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 72 | NEGEMMLowpMatrixMultiplyCore &operator=(const NEGEMMLowpMatrixMultiplyCore &) = delete; |
| 73 | /** Default move assignment operator */ |
| 74 | NEGEMMLowpMatrixMultiplyCore &operator=(NEGEMMLowpMatrixMultiplyCore &&) = default; |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 75 | /** Default destructor */ |
| 76 | ~NEGEMMLowpMatrixMultiplyCore(); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 77 | /** Initialise the kernel's inputs, output |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 78 | * |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 79 | * Valid data layouts: |
| 80 | * - NHWC |
| 81 | * - NCHW |
| 82 | * |
| 83 | * Valid data type configurations: |
| 84 | * |src0 |src1 |src2 |dst | |
| 85 | * |:--------------|:------------------|:--------|:--------------| |
| 86 | * |QASYMM8 |QASYMM8 |S32 |QASYMM8 | |
| 87 | * |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |QASYMM8 | |
| 88 | * |QASYMM8 |QSYMM8 |S32 |QASYMM8 | |
| 89 | * |QASYMM8 |QASYMM8 |S32 |S32 | |
| 90 | * |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |S32 | |
| 91 | * |QASYMM8 |QSYMM8 |S32 |S32 | |
| 92 | * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED | |
| 93 | * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |QASYMM8_SIGNED | |
| 94 | * |QASYMM8_SIGNED |QSYMM8 |S32 |QASYMM8_SIGNED | |
| 95 | * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |S32 | |
| 96 | * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |S32 | |
| 97 | * |QASYMM8_SIGNED |QSYMM8 |S32 |S32 | |
| 98 | * |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 99 | * @note GEMM_LOWP: low precision GEMM kernel |
| 100 | * This kernel performs the following computations: |
| 101 | * |
| 102 | * -# Convert a values from QASYMM8 to int32 and add a_offset to each of them. |
| 103 | * -# Convert b values from QASYMM8 to int32 add b_offset to each of them. |
| 104 | * -# Compute the matrix product of the resulting a * b in int32. |
| 105 | * |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 106 | * @note The @p output type is S32 if @p gemm_info.type == GEMMLowpOutputStageType::NONE. It is QASYMM8/QASYMM8_SIGNED otherwise |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 107 | * |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 108 | * @param[in] a First input tensor (Matrix A). Data type supported: QASYMM8/QASYMM8_SIGNED. |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 109 | * @param[in] b Second input tensor (Matrix B). Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8/QSYMM8_PER_CHANNEL. |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 110 | * @param[in] c Third input tensor (Matrix C). It can be a nullptr. Data type supported: S32 |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 111 | * @param[out] output Output tensor. Data type supported: Data type supported: S32/QASYMM8/QASYMM8_SIGNED |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 112 | * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and |
| 113 | * 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] | 114 | */ |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 115 | void configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *output, const GEMMInfo &gemm_info = GEMMInfo()); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 116 | /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMLowpMatrixMultiplyCore |
| 117 | * |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 118 | * @note The @p output type is S32 if @p gemm_info.type == GEMMLowpOutputStageType::NONE. It is QASYMM8/QASYMM8_SIGNED otherwise |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 119 | * |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 120 | * @param[in] a First input tensor info (Matrix A). Data type supported: QASYMM8/QASYMM8_SIGNED. |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 121 | * @param[in] b Second input tensor info (Matrix B). Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8/QSYMM8_PER_CHANNEL. |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 122 | * @param[in] c Third input tensor info (Matrix C). It can be a nullptr. Data type supported: S32 |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 123 | * @param[in] output Output tensor info. Data type supported: Data type supported: S32/QASYMM8/QASYMM8_SIGNED |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 124 | * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and |
| 125 | * 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] | 126 | * |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 127 | * @return a status |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 128 | */ |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 129 | static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, const GEMMInfo &gemm_info = GEMMInfo()); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 130 | |
Pablo Tello | fc00449 | 2018-03-23 11:40:05 +0000 | [diff] [blame] | 131 | // Inherited methods overridden |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 132 | void run() override; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 133 | void prepare() override; |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 134 | |
| 135 | private: |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 136 | MemoryGroup _memory_group; |
| 137 | IWeightsManager *_weights_manager; |
Georgios Pinitas | ec2256b | 2020-12-03 18:51:58 +0000 | [diff] [blame] | 138 | std::unique_ptr<NEGEMMAssemblyDispatch> _asm_glue; |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 139 | std::unique_ptr<NEGEMMLowpMatrixMultiplyKernel> _mm_kernel; |
| 140 | std::unique_ptr<NEGEMMInterleave4x4Kernel> _mtx_a_reshape_kernel; |
| 141 | std::unique_ptr<NEGEMMTranspose1xWKernel> _mtx_b_reshape_kernel; |
| 142 | std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _mtx_a_reduction_kernel; |
| 143 | std::unique_ptr<NEGEMMLowpMatrixBReductionKernel> _mtx_b_reduction_kernel; |
| 144 | std::unique_ptr<NEGEMMLowpOffsetContributionKernel> _offset_contribution_kernel; |
| 145 | std::unique_ptr<NEGEMMLowpOffsetContributionOutputStageKernel> _offset_contribution_output_stage_kernel; |
| 146 | NEActivationLayer _activation_func; |
| 147 | std::unique_ptr<NEConvertQuantizedSignednessKernel> _convert_to_signed_asymm; |
| 148 | std::unique_ptr<NEConvertQuantizedSignednessKernel> _convert_from_signed_asymm; |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 149 | |
| 150 | Tensor _vector_sum_col; |
| 151 | Tensor _vector_sum_row; |
| 152 | Tensor _tmp_a; |
| 153 | Tensor _tmp_b; |
| 154 | Tensor _mm_result_s32; |
| 155 | Tensor _signed_a; |
| 156 | Tensor _signed_output; |
| 157 | const ITensor *_original_b; |
| 158 | int32_t _a_offset; |
| 159 | int32_t _b_offset; |
| 160 | |
| 161 | bool _run_vector_matrix_multiplication; |
| 162 | bool _assembly_path; |
| 163 | bool _fused_assembly_path; |
| 164 | bool _reshape_b_only_on_first_run; |
| 165 | bool _is_prepared; |
| 166 | bool _fuse_output_stage; |
| 167 | bool _run_activation; |
| 168 | bool _flip_signedness; |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 169 | }; |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 170 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 171 | #endif /*ARM_COMPUTE_NEGEMMLOWPMATRIXMULTIPLYCORE_H */ |