blob: 3a39aca6924b33738499e411ef0d5128af559110 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
SiCong Li1b6377b2023-01-09 15:34:20 +00002 * Copyright (c) 2016-2021, 2023 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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_CLGEMM_H
25#define ARM_COMPUTE_CLGEMM_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
SiCong Li91295492023-07-21 18:16:13 +010027#include "arm_compute/function_info/GEMMInfo.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/runtime/CL/CLTensor.h"
Gian Marco Iodice5a4fe192020-03-16 12:22:37 +000029#include "arm_compute/runtime/CL/CLTypes.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/runtime/IFunction.h"
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +010031#include "arm_compute/runtime/IMemoryManager.h"
Michalis Spyroub27e13a2019-09-27 11:04:27 +010032#include "arm_compute/runtime/IWeightsManager.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010033#include "arm_compute/runtime/MemoryGroup.h"
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +010034
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010035#include <memory>
36
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037namespace arm_compute
38{
Georgios Pinitas856f66e2021-04-22 21:13:21 +010039// Forward declarations
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010040class CLCompileContext;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041class ICLTensor;
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010042class ITensorInfo;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043
Georgios Pinitas856f66e2021-04-22 21:13:21 +010044/** Basic function to execute GEMM on OpenCL */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045class CLGEMM : public IFunction
46{
47public:
Alex Gildayc357c472018-03-21 13:54:09 +000048 /** Default constructor.
49 *
Michalis Spyroub27e13a2019-09-27 11:04:27 +010050 * @param[in] memory_manager (Optional) Memory manager.
51 * @param[in] weights_manager (Optional) Weights manager.
Alex Gildayc357c472018-03-21 13:54:09 +000052 */
Michalis Spyroub27e13a2019-09-27 11:04:27 +010053 CLGEMM(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
Georgios Pinitas856f66e2021-04-22 21:13:21 +010054 /** Default destructor */
55 ~CLGEMM();
Georgios Pinitas82b51482018-04-24 15:14:12 +010056 /** Prevent instances of this class from being copied (As this class contains pointers) */
57 CLGEMM(const CLGEMM &) = delete;
58 /** Default move constructor */
Georgios Pinitas856f66e2021-04-22 21:13:21 +010059 CLGEMM(CLGEMM &&);
Georgios Pinitas82b51482018-04-24 15:14:12 +010060 /** Prevent instances of this class from being copied (As this class contains pointers) */
61 CLGEMM &operator=(const CLGEMM &) = delete;
62 /** Default move assignment operator */
Georgios Pinitas856f66e2021-04-22 21:13:21 +010063 CLGEMM &operator=(CLGEMM &&);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064 /** Initialise the kernel's inputs and output
65 *
Teresa Charlin62687422021-04-28 10:58:49 +010066 * Valid data layouts:
67 * - All
68 *
69 * Valid data type configurations:
70 * |src0 |src1 |src2 |dst |
71 * |:------------|:-----------|:---------|:--------------|
72 * |F32 |F32 |F32 |F32 |
73 * |F16 |F16 |F16 |F16 |
74 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075 * @note GEMM: General Matrix Multiply - [alpha * A * B + beta * C].
76 *
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +010077 * @note All tensors must have the same data type.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078 *
79 * @note Whilst the first input tensor can be a vector, the second input tensor must be at least a matrix
80 *
SiCong Li1b6377b2023-01-09 15:34:20 +000081 * @note Batched GEMM only allows RHS tensor's rank to be <= 3
82 * @note Batched GEMM only supports broadcasting cases where RHS rank < LHS rank but not the other way around
83 *
Manuel Bottini2b84be52020-04-08 10:15:51 +010084 * @param[in] compile_context The compile context to be used.
85 * @param[in] a First input tensor (Matrix or Vector A). Data types supported: F16/F32
86 * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a.
87 * @param[in] c Third input tensor (Matrix C). It can be a nullptr if just the multiplication between @p a and @p b is needed. Data type supported: same as @p a.
88 * @param[out] output Output tensor. Data type supported: same as @p a
89 * @param[in] alpha Weight of the matrix product
90 * @param[in] beta Weight of matrix C
91 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
Georgios Pinitas856f66e2021-04-22 21:13:21 +010092 * if the reshape of matrix B should happen only for the first run. GEMMInfo also contains information about the reshaping
93 * in case matrix A and matrix B have been already transformed.
Manuel Bottini2b84be52020-04-08 10:15:51 +010094 */
95 void configure(const CLCompileContext &compile_context, const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo());
Georgios Pinitas856f66e2021-04-22 21:13:21 +010096
97 /** Initialise the kernel's inputs and output
98 *
99 * Similar to @ref CLGEMM::configure()
100 */
101 void configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo());
102
Georgios Pinitas78c00902018-01-09 17:33:11 +0000103 /** Static function to check if given info will lead to a valid configuration of @ref CLGEMM.
104 *
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100105 * Similar to @ref CLGEMM::configure()
Georgios Pinitas78c00902018-01-09 17:33:11 +0000106 *
107 * @return a status
108 */
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100109 static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110
111 // Inherited methods overridden:
112 void run() override;
Georgios Pinitase0437672018-05-02 14:07:55 +0100113 void prepare() override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114
115private:
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100116 struct Impl;
117 std::unique_ptr<Impl> _impl;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118};
giuros011c9efeb2019-01-11 14:04:43 +0000119} // namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120
Michalis Spyrouf4643372019-11-29 16:17:13 +0000121#endif /* ARM_COMPUTE_CLGEMM_H */