Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1 | /* |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [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_CLGEMMLOWPMATRIXMULTIPLYCORE_H |
| 25 | #define ARM_COMPUTE_CLGEMMLOWPMATRIXMULTIPLYCORE_H |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 26 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 27 | #include "arm_compute/runtime/CL/CLTensor.h" |
| 28 | #include "arm_compute/runtime/IFunction.h" |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/MemoryGroup.h" |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 30 | |
| 31 | namespace arm_compute |
| 32 | { |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 33 | class CLCompileContext; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 34 | class IMemoryManager; |
| 35 | class ICLTensor; |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 36 | class ITensorInfo; |
| 37 | class CLDepthConvertLayerKernel; |
| 38 | class CLGEMMLowpMatrixMultiplyNativeKernel; |
| 39 | class CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel; |
| 40 | class CLGEMMLowpOffsetContributionKernel; |
| 41 | class CLGEMMLowpOffsetContributionOutputStageKernel; |
| 42 | class CLGEMMLowpMatrixAReductionKernel; |
| 43 | class CLGEMMLowpMatrixBReductionKernel; |
| 44 | class CLGEMMReshapeRHSMatrixKernel; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 45 | |
Gian Marco Iodice | eb65f6d | 2020-04-15 11:42:15 +0100 | [diff] [blame] | 46 | /** Basic function to execute GEMMLowpMatrixMultiplyCore on OpenCL. */ |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 47 | class CLGEMMLowpMatrixMultiplyCore : public IFunction |
| 48 | { |
| 49 | public: |
| 50 | /** Constructor */ |
| 51 | CLGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 52 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 53 | CLGEMMLowpMatrixMultiplyCore(const CLGEMMLowpMatrixMultiplyCore &) = delete; |
| 54 | /** Default move constructor */ |
| 55 | CLGEMMLowpMatrixMultiplyCore(CLGEMMLowpMatrixMultiplyCore &&) = default; |
| 56 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 57 | CLGEMMLowpMatrixMultiplyCore &operator=(const CLGEMMLowpMatrixMultiplyCore &) = delete; |
| 58 | /** Default move assignment operator */ |
| 59 | CLGEMMLowpMatrixMultiplyCore &operator=(CLGEMMLowpMatrixMultiplyCore &&) = default; |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 60 | /** Default destructor */ |
| 61 | ~CLGEMMLowpMatrixMultiplyCore(); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 62 | /** Initialise the kernel's inputs, output |
| 63 | * |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame^] | 64 | * Valid data layouts: |
| 65 | * - NHWC |
| 66 | * - NCHW |
| 67 | * |
| 68 | * Valid data type configurations: |
| 69 | * |src0 |src1 |src2 |dst | |
| 70 | * |:--------------|:------------------|:--------|:--------------| |
| 71 | * |QASYMM8 |QASYMM8 |S32 |QASYMM8 | |
| 72 | * |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |QASYMM8 | |
| 73 | * |QASYMM8 |QSYMM8 |S32 |QASYMM8 | |
| 74 | * |QASYMM8 |QASYMM8 |S32 |S32 | |
| 75 | * |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |S32 | |
| 76 | * |QASYMM8 |QSYMM8 |S32 |S32 | |
| 77 | * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED | |
| 78 | * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |QASYMM8_SIGNED | |
| 79 | * |QASYMM8_SIGNED |QSYMM8 |S32 |QASYMM8_SIGNED | |
| 80 | * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |S32 | |
| 81 | * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |S32 | |
| 82 | * |QASYMM8_SIGNED |QSYMM8 |S32 |S32 | |
| 83 | * |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 84 | * @note GEMMLowp: low precision GEMM kernel. [A * B + C] |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 85 | * This kernel performs the following computations: |
| 86 | * |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 87 | * -# Convert a values from 8-bit quantized to int32 and add a_offset to each of them. |
| 88 | * -# Convert b values from 8-bit quantized to int32 and add b_offset to each of them. |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 89 | * -# Compute the matrix product of the resulting a * b in int32. |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 90 | * -# Quantize to uint8 if gemm_info.gemmlowp_output_stage != NONE |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 91 | * |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 92 | * @param[in] a First input tensor (Matrix A). Data type supported: QASYMM8/QASYMM8_SIGNED. |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 93 | * @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] | 94 | * @param[in] c Third input tensor (Matrix C). It can be a nullptr. Data type supported: S32 |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 95 | * @param[out] output Output tensor. Data type supported: S32 or QASYMM8/QASYMM8_SIGNED if gemm_info.gemmlowp_output_stage != NONE |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 96 | * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and |
| 97 | * if the reshape of matrix B should be executed only for the first run |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 98 | */ |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 99 | void configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, const GEMMInfo &gemm_info = GEMMInfo()); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 100 | /** Initialise the kernel's inputs, output |
| 101 | * |
| 102 | * @note GEMMLowp: low precision GEMM kernel. [A * B + C] |
| 103 | * This kernel performs the following computations: |
| 104 | * |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 105 | * -# Convert a values from 8-bit quantized to int32 and add a_offset to each of them. |
| 106 | * -# Convert b values from 8-bit quantized to int32 and add b_offset to each of them. |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 107 | * -# Compute the matrix product of the resulting a * b in int32. |
| 108 | * -# Quantize to uint8 if gemm_info.gemmlowp_output_stage != NONE |
| 109 | * |
| 110 | * @param[in] compile_context The compile context to be used. |
| 111 | * @param[in] a First input tensor (Matrix A). Data type supported: QASYMM8/QASYMM8_SIGNED. |
| 112 | * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a |
| 113 | * @param[in] c Third input tensor (Matrix C). It can be a nullptr. Data type supported: S32 |
| 114 | * @param[out] output Output tensor. Data type supported: S32 or QASYMM8/QASYMM8_SIGNED if gemm_info.gemmlowp_output_stage != NONE |
| 115 | * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and |
| 116 | * if the reshape of matrix B should be executed only for the first run |
| 117 | */ |
| 118 | void configure(const CLCompileContext &compile_context, const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, const GEMMInfo &gemm_info = GEMMInfo()); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 119 | /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMLowpMatrixMultiplyCore |
| 120 | * |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 121 | * @param[in] a First input tensor info (Matrix A). Data type supported: QASYMM8. |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 122 | * @param[in] b Second input tensor info (Matrix B). Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8/QSYMM8_PER_CHANNEL |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 123 | * @param[in] c Third input tensor info (Matrix C). It can be a nullptr. Data type supported: S32 |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 124 | * @param[in] output Output tensor info. Data type supported: S32 or QASYMM8/QASYMM8_SIGNED if gemm_info.gemmlowp_output_stage != NONE |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 125 | * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and |
| 126 | * if the reshape of matrix B should be executed only for the first run |
| 127 | * |
| 128 | * @return a status |
| 129 | */ |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 130 | static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, const GEMMInfo &gemm_info = GEMMInfo()); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 131 | |
| 132 | // Inherited methods overridden: |
| 133 | void run() override; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 134 | void prepare() override; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 135 | |
| 136 | private: |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 137 | MemoryGroup _memory_group; |
| 138 | |
| 139 | // Kernels used |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 140 | std::unique_ptr<CLDepthConvertLayerKernel> _weights_to_qasymm8; |
| 141 | std::unique_ptr<CLGEMMLowpMatrixMultiplyNativeKernel> _mm_native_kernel; |
| 142 | std::unique_ptr<CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel> _mm_reshaped_only_rhs_kernel; |
| 143 | std::unique_ptr<CLGEMMReshapeRHSMatrixKernel> _mtx_b_reshape_kernel; |
| 144 | std::unique_ptr<CLGEMMLowpMatrixAReductionKernel> _mtx_a_reduction_kernel; |
| 145 | std::unique_ptr<CLGEMMLowpMatrixBReductionKernel> _mtx_b_reduction_kernel; |
| 146 | std::unique_ptr<CLGEMMLowpOffsetContributionKernel> _offset_contribution_kernel; |
| 147 | std::unique_ptr<CLGEMMLowpOffsetContributionOutputStageKernel> _offset_contribution_output_stage_kernel; |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 148 | |
| 149 | // Temporary tensors |
| 150 | CLTensor _qasymm8_weights; |
| 151 | CLTensor _vector_sum_col; |
| 152 | CLTensor _vector_sum_row; |
| 153 | CLTensor _tmp_b; |
| 154 | CLTensor _mm_result_s32; |
| 155 | CLTensor _gemm_output_stage_multipliers; |
| 156 | CLTensor _gemm_output_stage_shifts; |
| 157 | |
| 158 | // Tensor pointers |
| 159 | const ICLTensor *_matrix_a; |
| 160 | const ICLTensor *_original_b; |
| 161 | const ICLTensor *_output; |
| 162 | |
| 163 | int32_t _a_offset; |
| 164 | int32_t _b_offset; |
| 165 | bool _is_gemm_reshaped; |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 166 | bool _reshape_b_only_on_first_run; |
| 167 | bool _is_prepared; |
Michele Di Giorgio | b54ba28 | 2020-01-14 15:31:55 +0000 | [diff] [blame] | 168 | bool _run_output_stage; |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 169 | bool _convert_to_qasymm8; |
Michele Di Giorgio | b54ba28 | 2020-01-14 15:31:55 +0000 | [diff] [blame] | 170 | bool _run_offset_contribution; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 171 | }; |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 172 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 173 | #endif /*ARM_COMPUTE_CLGEMMLOWPMATRIXMULTIPLYCORE_H */ |