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