blob: 57b1e30df5294a60203a21d2271a10d8bd3ce8e8 [file] [log] [blame]
Gian Marco05288a22017-11-21 10:57:50 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Gian Marco05288a22017-11-21 10:57:50 +00003 *
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 Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CLGEMMLOWPMATRIXMULTIPLYCORE_H
25#define ARM_COMPUTE_CLGEMMLOWPMATRIXMULTIPLYCORE_H
Gian Marco05288a22017-11-21 10:57:50 +000026
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000027#include "arm_compute/core/CL/kernels/CLDepthConvertLayerKernel.h"
Gian Marco Iodice06be6f82019-06-24 17:47:51 +010028#include "arm_compute/core/CL/kernels/CLGEMMLowpMatrixMultiplyNativeKernel.h"
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +010029#include "arm_compute/core/CL/kernels/CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel.h"
Gian Marco05288a22017-11-21 10:57:50 +000030#include "arm_compute/core/CL/kernels/CLGEMMLowpOffsetContributionKernel.h"
Gian Marco Iodice4b908652018-10-18 10:21:02 +010031#include "arm_compute/core/CL/kernels/CLGEMMLowpOffsetContributionOutputStageKernel.h"
Gian Marco05288a22017-11-21 10:57:50 +000032#include "arm_compute/core/CL/kernels/CLGEMMLowpReductionKernel.h"
giuros018b6b4a92018-12-18 19:01:33 +000033#include "arm_compute/core/CL/kernels/CLGEMMReshapeRHSMatrixKernel.h"
Gian Marco05288a22017-11-21 10:57:50 +000034#include "arm_compute/runtime/CL/CLTensor.h"
35#include "arm_compute/runtime/IFunction.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010036#include "arm_compute/runtime/MemoryGroup.h"
Gian Marco05288a22017-11-21 10:57:50 +000037
38namespace arm_compute
39{
40class IMemoryManager;
41class ICLTensor;
42
Gian Marco Iodiceeb65f6d2020-04-15 11:42:15 +010043/** Basic function to execute GEMMLowpMatrixMultiplyCore on OpenCL. */
Gian Marco05288a22017-11-21 10:57:50 +000044class CLGEMMLowpMatrixMultiplyCore : public IFunction
45{
46public:
47 /** Constructor */
48 CLGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Georgios Pinitas72219332018-06-05 14:56:06 +010049 /** Prevent instances of this class from being copied (As this class contains pointers) */
50 CLGEMMLowpMatrixMultiplyCore(const CLGEMMLowpMatrixMultiplyCore &) = delete;
51 /** Default move constructor */
52 CLGEMMLowpMatrixMultiplyCore(CLGEMMLowpMatrixMultiplyCore &&) = default;
53 /** Prevent instances of this class from being copied (As this class contains pointers) */
54 CLGEMMLowpMatrixMultiplyCore &operator=(const CLGEMMLowpMatrixMultiplyCore &) = delete;
55 /** Default move assignment operator */
56 CLGEMMLowpMatrixMultiplyCore &operator=(CLGEMMLowpMatrixMultiplyCore &&) = default;
Gian Marco05288a22017-11-21 10:57:50 +000057 /** Initialise the kernel's inputs, output
58 *
Gian Marco Iodice4b908652018-10-18 10:21:02 +010059 * @note GEMMLowp: low precision GEMM kernel. [A * B + C]
Gian Marco05288a22017-11-21 10:57:50 +000060 * This kernel performs the following computations:
61 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +010062 * -# Convert a values from 8-bit quantized to int32 and add a_offset to each of them.
63 * -# Convert b values from 8-bit quantized to int32 and add b_offset to each of them.
Gian Marco05288a22017-11-21 10:57:50 +000064 * -# Compute the matrix product of the resulting a * b in int32.
Gian Marco Iodice4b908652018-10-18 10:21:02 +010065 * -# Quantize to uint8 if gemm_info.gemmlowp_output_stage != NONE
Gian Marco05288a22017-11-21 10:57:50 +000066 *
Manuel Bottini959c26d2019-12-02 16:22:35 +000067 * @param[in] a First input tensor (Matrix A). Data type supported: QASYMM8/QASYMM8_SIGNED.
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +010068 * @param[in] b Second input tensor (Matrix B). Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8/QSYMM8_PER_CHANNEL
Gian Marco Iodice4b908652018-10-18 10:21:02 +010069 * @param[in] c Third input tensor (Matrix C). It can be a nullptr. Data type supported: S32
Manuel Bottini959c26d2019-12-02 16:22:35 +000070 * @param[out] output Output tensor. Data type supported: S32 or QASYMM8/QASYMM8_SIGNED if gemm_info.gemmlowp_output_stage != NONE
Chunosov5124be52017-11-22 20:42:13 +070071 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
72 * if the reshape of matrix B should be executed only for the first run
Gian Marco05288a22017-11-21 10:57:50 +000073 */
Gian Marco Iodice4b908652018-10-18 10:21:02 +010074 void configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, const GEMMInfo &gemm_info = GEMMInfo());
Manuel Bottini2b84be52020-04-08 10:15:51 +010075 /** Initialise the kernel's inputs, output
76 *
77 * @note GEMMLowp: low precision GEMM kernel. [A * B + C]
78 * This kernel performs the following computations:
79 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +010080 * -# Convert a values from 8-bit quantized to int32 and add a_offset to each of them.
81 * -# Convert b values from 8-bit quantized to int32 and add b_offset to each of them.
Manuel Bottini2b84be52020-04-08 10:15:51 +010082 * -# Compute the matrix product of the resulting a * b in int32.
83 * -# Quantize to uint8 if gemm_info.gemmlowp_output_stage != NONE
84 *
85 * @param[in] compile_context The compile context to be used.
86 * @param[in] a First input tensor (Matrix A). Data type supported: QASYMM8/QASYMM8_SIGNED.
87 * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a
88 * @param[in] c Third input tensor (Matrix C). It can be a nullptr. Data type supported: S32
89 * @param[out] output Output tensor. Data type supported: S32 or QASYMM8/QASYMM8_SIGNED if gemm_info.gemmlowp_output_stage != NONE
90 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
91 * if the reshape of matrix B should be executed only for the first run
92 */
93 void configure(const CLCompileContext &compile_context, const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, const GEMMInfo &gemm_info = GEMMInfo());
Georgios Pinitas358ca202017-12-07 16:47:52 +000094 /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMLowpMatrixMultiplyCore
95 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000096 * @param[in] a First input tensor info (Matrix A). Data type supported: QASYMM8.
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +010097 * @param[in] b Second input tensor info (Matrix B). Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8/QSYMM8_PER_CHANNEL
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000098 * @param[in] c Third input tensor info (Matrix C). It can be a nullptr. Data type supported: S32
Manuel Bottini959c26d2019-12-02 16:22:35 +000099 * @param[in] output Output tensor info. Data type supported: S32 or QASYMM8/QASYMM8_SIGNED if gemm_info.gemmlowp_output_stage != NONE
Georgios Pinitas358ca202017-12-07 16:47:52 +0000100 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
101 * if the reshape of matrix B should be executed only for the first run
102 *
103 * @return a status
104 */
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100105 static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, const GEMMInfo &gemm_info = GEMMInfo());
Gian Marco05288a22017-11-21 10:57:50 +0000106
107 // Inherited methods overridden:
108 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +0100109 void prepare() override;
Gian Marco05288a22017-11-21 10:57:50 +0000110
111private:
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000112 MemoryGroup _memory_group;
113
114 // Kernels used
115 CLDepthConvertLayerKernel _weights_to_qasymm8;
Gian Marco Iodice06be6f82019-06-24 17:47:51 +0100116 CLGEMMLowpMatrixMultiplyNativeKernel _mm_native_kernel;
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100117 CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel _mm_reshaped_only_rhs_kernel;
giuros018b6b4a92018-12-18 19:01:33 +0000118 CLGEMMReshapeRHSMatrixKernel _mtx_b_reshape_kernel;
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100119 CLGEMMLowpMatrixAReductionKernel _mtx_a_reduction_kernel;
120 CLGEMMLowpMatrixBReductionKernel _mtx_b_reduction_kernel;
121 CLGEMMLowpOffsetContributionKernel _offset_contribution_kernel;
122 CLGEMMLowpOffsetContributionOutputStageKernel _offset_contribution_output_stage_kernel;
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000123
124 // Temporary tensors
125 CLTensor _qasymm8_weights;
126 CLTensor _vector_sum_col;
127 CLTensor _vector_sum_row;
128 CLTensor _tmp_b;
129 CLTensor _mm_result_s32;
130 CLTensor _gemm_output_stage_multipliers;
131 CLTensor _gemm_output_stage_shifts;
132
133 // Tensor pointers
134 const ICLTensor *_matrix_a;
135 const ICLTensor *_original_b;
136 const ICLTensor *_output;
137
138 int32_t _a_offset;
139 int32_t _b_offset;
140 bool _is_gemm_reshaped;
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000141 bool _reshape_b_only_on_first_run;
142 bool _is_prepared;
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000143 bool _run_output_stage;
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000144 bool _convert_to_qasymm8;
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000145 bool _run_offset_contribution;
Gian Marco05288a22017-11-21 10:57:50 +0000146};
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000147} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000148#endif /*ARM_COMPUTE_CLGEMMLOWPMATRIXMULTIPLYCORE_H */