blob: 1b8e5dcc1d5a9f8e7cf6acafd4e7690c6f52c96f [file] [log] [blame]
Gian Marco05288a22017-11-21 10:57:50 +00001/*
Matthew Benthamf1aeab92023-05-30 13:35:34 +00002 * Copyright (c) 2017-2021, 2023 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
SiCong Li91295492023-07-21 18:16:13 +010027#include "arm_compute/function_info/GEMMInfo.h"
Gian Marco05288a22017-11-21 10:57:50 +000028#include "arm_compute/runtime/CL/CLTensor.h"
29#include "arm_compute/runtime/IFunction.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010030#include "arm_compute/runtime/MemoryGroup.h"
Gian Marco05288a22017-11-21 10:57:50 +000031
Georgios Pinitasf4e84fb2021-07-08 15:36:07 +010032#include <memory>
33
Gian Marco05288a22017-11-21 10:57:50 +000034namespace arm_compute
35{
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010036class CLCompileContext;
Gian Marco05288a22017-11-21 10:57:50 +000037class IMemoryManager;
38class ICLTensor;
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010039class ITensorInfo;
Georgios Pinitas11d84152021-04-28 10:20:18 +010040
Gian Marco Iodiceeb65f6d2020-04-15 11:42:15 +010041/** Basic function to execute GEMMLowpMatrixMultiplyCore on OpenCL. */
Gian Marco05288a22017-11-21 10:57:50 +000042class CLGEMMLowpMatrixMultiplyCore : public IFunction
43{
44public:
45 /** Constructor */
46 CLGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Georgios Pinitas72219332018-06-05 14:56:06 +010047 /** Prevent instances of this class from being copied (As this class contains pointers) */
48 CLGEMMLowpMatrixMultiplyCore(const CLGEMMLowpMatrixMultiplyCore &) = delete;
49 /** Default move constructor */
50 CLGEMMLowpMatrixMultiplyCore(CLGEMMLowpMatrixMultiplyCore &&) = default;
51 /** Prevent instances of this class from being copied (As this class contains pointers) */
52 CLGEMMLowpMatrixMultiplyCore &operator=(const CLGEMMLowpMatrixMultiplyCore &) = delete;
53 /** Default move assignment operator */
54 CLGEMMLowpMatrixMultiplyCore &operator=(CLGEMMLowpMatrixMultiplyCore &&) = default;
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010055 /** Default destructor */
56 ~CLGEMMLowpMatrixMultiplyCore();
Gian Marco05288a22017-11-21 10:57:50 +000057 /** Initialise the kernel's inputs, output
58 *
Teresa Charlin62687422021-04-28 10:58:49 +010059 * Valid data layouts:
60 * - NHWC
61 * - NCHW
62 *
63 * Valid data type configurations:
64 * |src0 |src1 |src2 |dst |
65 * |:--------------|:------------------|:--------|:--------------|
66 * |QASYMM8 |QASYMM8 |S32 |QASYMM8 |
67 * |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |QASYMM8 |
68 * |QASYMM8 |QSYMM8 |S32 |QASYMM8 |
69 * |QASYMM8 |QASYMM8 |S32 |S32 |
70 * |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |S32 |
71 * |QASYMM8 |QSYMM8 |S32 |S32 |
72 * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED |
73 * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |QASYMM8_SIGNED |
74 * |QASYMM8_SIGNED |QSYMM8 |S32 |QASYMM8_SIGNED |
75 * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |S32 |
76 * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |S32 |
77 * |QASYMM8_SIGNED |QSYMM8 |S32 |S32 |
78 *
Gian Marco Iodice4b908652018-10-18 10:21:02 +010079 * @note GEMMLowp: low precision GEMM kernel. [A * B + C]
Gian Marco05288a22017-11-21 10:57:50 +000080 * This kernel performs the following computations:
81 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +010082 * -# Convert a values from 8-bit quantized to int32 and add a_offset to each of them.
83 * -# Convert b values from 8-bit quantized to int32 and add b_offset to each of them.
Gian Marco05288a22017-11-21 10:57:50 +000084 * -# Compute the matrix product of the resulting a * b in int32.
Gian Marco Iodice4b908652018-10-18 10:21:02 +010085 * -# Quantize to uint8 if gemm_info.gemmlowp_output_stage != NONE
Gian Marco05288a22017-11-21 10:57:50 +000086 *
Manuel Bottini959c26d2019-12-02 16:22:35 +000087 * @param[in] a First input tensor (Matrix A). Data type supported: QASYMM8/QASYMM8_SIGNED.
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +010088 * @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 +010089 * @param[in] c Third input tensor (Matrix C). It can be a nullptr. Data type supported: S32
Manuel Bottini959c26d2019-12-02 16:22:35 +000090 * @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 +070091 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
92 * if the reshape of matrix B should be executed only for the first run
Gian Marco05288a22017-11-21 10:57:50 +000093 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010094 void configure(const ICLTensor *a,
95 const ICLTensor *b,
96 const ICLTensor *c,
97 ICLTensor *output,
98 const GEMMInfo &gemm_info = GEMMInfo());
Manuel Bottini2b84be52020-04-08 10:15:51 +010099 /** Initialise the kernel's inputs, output
100 *
101 * @note GEMMLowp: low precision GEMM kernel. [A * B + C]
102 * This kernel performs the following computations:
103 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100104 * -# Convert a values from 8-bit quantized to int32 and add a_offset to each of them.
105 * -# Convert b values from 8-bit quantized to int32 and add b_offset to each of them.
Manuel Bottini2b84be52020-04-08 10:15:51 +0100106 * -# Compute the matrix product of the resulting a * b in int32.
107 * -# Quantize to uint8 if gemm_info.gemmlowp_output_stage != NONE
108 *
109 * @param[in] compile_context The compile context to be used.
110 * @param[in] a First input tensor (Matrix A). Data type supported: QASYMM8/QASYMM8_SIGNED.
111 * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a
112 * @param[in] c Third input tensor (Matrix C). It can be a nullptr. Data type supported: S32
113 * @param[out] output Output tensor. Data type supported: S32 or QASYMM8/QASYMM8_SIGNED if gemm_info.gemmlowp_output_stage != NONE
114 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
115 * if the reshape of matrix B should be executed only for the first run
116 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100117 void configure(const CLCompileContext &compile_context,
118 const ICLTensor *a,
119 const ICLTensor *b,
120 const ICLTensor *c,
121 ICLTensor *output,
122 const GEMMInfo &gemm_info = GEMMInfo());
Georgios Pinitas358ca202017-12-07 16:47:52 +0000123 /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMLowpMatrixMultiplyCore
124 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000125 * @param[in] a First input tensor info (Matrix A). Data type supported: QASYMM8.
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100126 * @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 +0000127 * @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 +0000128 * @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 +0000129 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
130 * if the reshape of matrix B should be executed only for the first run
131 *
132 * @return a status
133 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100134 static Status validate(const ITensorInfo *a,
135 const ITensorInfo *b,
136 const ITensorInfo *c,
137 const ITensorInfo *output,
138 const GEMMInfo &gemm_info = GEMMInfo());
Gian Marco05288a22017-11-21 10:57:50 +0000139
140 // Inherited methods overridden:
141 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +0100142 void prepare() override;
Gian Marco05288a22017-11-21 10:57:50 +0000143
144private:
Georgios Pinitasf4e84fb2021-07-08 15:36:07 +0100145 struct Impl;
146 std::unique_ptr<Impl> _impl;
Gian Marco05288a22017-11-21 10:57:50 +0000147};
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000148} // namespace arm_compute
Matthew Benthamf1aeab92023-05-30 13:35:34 +0000149#endif /*ARM_COMPUTE_CLGEMMLOWPMATRIXMULTIPLYCORE_H */