Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1 | /* |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 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 | |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/CL/kernels/CLDepthConvertLayerKernel.h" |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/CL/kernels/CLGEMMLowpMatrixMultiplyKernel.h" |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/CL/kernels/CLGEMMLowpMatrixMultiplyNativeKernel.h" |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/CL/kernels/CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel.h" |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/CL/kernels/CLGEMMLowpOffsetContributionKernel.h" |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/CL/kernels/CLGEMMLowpOffsetContributionOutputStageKernel.h" |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 33 | #include "arm_compute/core/CL/kernels/CLGEMMLowpReductionKernel.h" |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 34 | #include "arm_compute/core/CL/kernels/CLGEMMReshapeRHSMatrixKernel.h" |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 35 | #include "arm_compute/runtime/CL/CLTensor.h" |
| 36 | #include "arm_compute/runtime/IFunction.h" |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 37 | #include "arm_compute/runtime/MemoryGroup.h" |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | class IMemoryManager; |
| 42 | class ICLTensor; |
| 43 | |
| 44 | /** Basic function to execute GEMMLowpMatrixMultiplyCore on OpenCL. This function calls the following OpenCL kernels: |
| 45 | * |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 46 | * -# @ref CLGEMMReshapeRHSMatrixKernel (if the output tensor is a matrix) |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 47 | * -# @ref CLGEMMLowpMatrixMultiplyKernel (if the parameter "reshape_b_only_on_first_run" of GEMMInfo is FALSE) |
| 48 | * -# @ref CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel (if the parameter "reshape_b_only_on_first_run" of GEMMInfo is TRUE) |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 49 | * -# @ref CLGEMMLowpMatrixAReductionKernel (if the offset of matrix B is not 0) |
| 50 | * -# @ref CLGEMMLowpMatrixBReductionKernel (if the offset of matrix A is not 0) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 51 | * -# @ref CLGEMMLowpOffsetContributionKernel (if gemm_info.gemmlowp_output_stage == NONE) |
| 52 | * -# @ref CLGEMMLowpOffsetContributionOutputStageKernel (if gemm_info.gemmlowp_output_stage != NONE) |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 53 | * -# @ref CLDepthConvertLayerKernel |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 54 | * |
| 55 | */ |
| 56 | class CLGEMMLowpMatrixMultiplyCore : public IFunction |
| 57 | { |
| 58 | public: |
| 59 | /** Constructor */ |
| 60 | CLGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 61 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 62 | CLGEMMLowpMatrixMultiplyCore(const CLGEMMLowpMatrixMultiplyCore &) = delete; |
| 63 | /** Default move constructor */ |
| 64 | CLGEMMLowpMatrixMultiplyCore(CLGEMMLowpMatrixMultiplyCore &&) = default; |
| 65 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 66 | CLGEMMLowpMatrixMultiplyCore &operator=(const CLGEMMLowpMatrixMultiplyCore &) = delete; |
| 67 | /** Default move assignment operator */ |
| 68 | CLGEMMLowpMatrixMultiplyCore &operator=(CLGEMMLowpMatrixMultiplyCore &&) = default; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 69 | /** Initialise the kernel's inputs, output |
| 70 | * |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 71 | * @note GEMMLowp: low precision GEMM kernel. [A * B + C] |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 72 | * This kernel performs the following computations: |
| 73 | * |
| 74 | * -# Convert a values from QASYMM8 to int32 and add a_offset to each of them. |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 75 | * -# Convert b values from QASYMM8 to int32 and add b_offset to each of them. |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 76 | * -# Compute the matrix product of the resulting a * b in int32. |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 77 | * -# Quantize to uint8 if gemm_info.gemmlowp_output_stage != NONE |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 78 | * |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 79 | * @param[in] a First input tensor (Matrix A). Data type supported: QASYMM8/QASYMM8_SIGNED. |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 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 |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 82 | * @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] | 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 |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 85 | */ |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 86 | void configure(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] | 87 | /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMLowpMatrixMultiplyCore |
| 88 | * |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 89 | * @param[in] a First input tensor info (Matrix A). Data type supported: QASYMM8. |
| 90 | * @param[in] b Second input tensor info (Matrix B). Data type supported: same as @p a |
| 91 | * @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] | 92 | * @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] | 93 | * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and |
| 94 | * if the reshape of matrix B should be executed only for the first run |
| 95 | * |
| 96 | * @return a status |
| 97 | */ |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 98 | 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] | 99 | |
| 100 | // Inherited methods overridden: |
| 101 | void run() override; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 102 | void prepare() override; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 103 | |
| 104 | private: |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 105 | MemoryGroup _memory_group; |
| 106 | |
| 107 | // Kernels used |
| 108 | CLDepthConvertLayerKernel _weights_to_qasymm8; |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 109 | CLGEMMLowpMatrixMultiplyKernel _mm_midgard_kernel; |
| 110 | CLGEMMLowpMatrixMultiplyNativeKernel _mm_native_kernel; |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 111 | CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel _mm_reshaped_only_rhs_kernel; |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 112 | CLGEMMReshapeRHSMatrixKernel _mtx_b_reshape_kernel; |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 113 | CLGEMMLowpMatrixAReductionKernel _mtx_a_reduction_kernel; |
| 114 | CLGEMMLowpMatrixBReductionKernel _mtx_b_reduction_kernel; |
| 115 | CLGEMMLowpOffsetContributionKernel _offset_contribution_kernel; |
| 116 | CLGEMMLowpOffsetContributionOutputStageKernel _offset_contribution_output_stage_kernel; |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 117 | |
| 118 | // Temporary tensors |
| 119 | CLTensor _qasymm8_weights; |
| 120 | CLTensor _vector_sum_col; |
| 121 | CLTensor _vector_sum_row; |
| 122 | CLTensor _tmp_b; |
| 123 | CLTensor _mm_result_s32; |
| 124 | CLTensor _gemm_output_stage_multipliers; |
| 125 | CLTensor _gemm_output_stage_shifts; |
| 126 | |
| 127 | // Tensor pointers |
| 128 | const ICLTensor *_matrix_a; |
| 129 | const ICLTensor *_original_b; |
| 130 | const ICLTensor *_output; |
| 131 | |
| 132 | int32_t _a_offset; |
| 133 | int32_t _b_offset; |
| 134 | bool _is_gemm_reshaped; |
| 135 | bool _is_midgard; |
| 136 | bool _reshape_b_only_on_first_run; |
| 137 | bool _is_prepared; |
| 138 | bool _fuse_output_stage; |
| 139 | bool _convert_to_qasymm8; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 140 | }; |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 141 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 142 | #endif /*ARM_COMPUTE_CLGEMMLOWPMATRIXMULTIPLYCORE_H */ |