blob: b267bf1cf2230abbab14cc4c3cbe039facc6eeaf [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
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/runtime/CL/CLTensor.h"
Gian Marco Iodice5a4fe192020-03-16 12:22:37 +000028#include "arm_compute/runtime/CL/CLTypes.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/runtime/IFunction.h"
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +010030#include "arm_compute/runtime/IMemoryManager.h"
Michalis Spyroub27e13a2019-09-27 11:04:27 +010031#include "arm_compute/runtime/IWeightsManager.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010032#include "arm_compute/runtime/MemoryGroup.h"
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +010033
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010034#include <memory>
35
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036namespace arm_compute
37{
Georgios Pinitas856f66e2021-04-22 21:13:21 +010038// Forward declarations
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010039class CLCompileContext;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040class ICLTensor;
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010041class ITensorInfo;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042
Georgios Pinitas856f66e2021-04-22 21:13:21 +010043/** Basic function to execute GEMM on OpenCL */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044class CLGEMM : public IFunction
45{
46public:
Alex Gildayc357c472018-03-21 13:54:09 +000047 /** Default constructor.
48 *
Michalis Spyroub27e13a2019-09-27 11:04:27 +010049 * @param[in] memory_manager (Optional) Memory manager.
50 * @param[in] weights_manager (Optional) Weights manager.
Alex Gildayc357c472018-03-21 13:54:09 +000051 */
Michalis Spyroub27e13a2019-09-27 11:04:27 +010052 CLGEMM(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
Georgios Pinitas856f66e2021-04-22 21:13:21 +010053 /** Default destructor */
54 ~CLGEMM();
Georgios Pinitas82b51482018-04-24 15:14:12 +010055 /** Prevent instances of this class from being copied (As this class contains pointers) */
56 CLGEMM(const CLGEMM &) = delete;
57 /** Default move constructor */
Georgios Pinitas856f66e2021-04-22 21:13:21 +010058 CLGEMM(CLGEMM &&);
Georgios Pinitas82b51482018-04-24 15:14:12 +010059 /** Prevent instances of this class from being copied (As this class contains pointers) */
60 CLGEMM &operator=(const CLGEMM &) = delete;
61 /** Default move assignment operator */
Georgios Pinitas856f66e2021-04-22 21:13:21 +010062 CLGEMM &operator=(CLGEMM &&);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063 /** Initialise the kernel's inputs and output
64 *
Teresa Charlin62687422021-04-28 10:58:49 +010065 * Valid data layouts:
66 * - All
67 *
68 * Valid data type configurations:
69 * |src0 |src1 |src2 |dst |
70 * |:------------|:-----------|:---------|:--------------|
71 * |F32 |F32 |F32 |F32 |
72 * |F16 |F16 |F16 |F16 |
73 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074 * @note GEMM: General Matrix Multiply - [alpha * A * B + beta * C].
75 *
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +010076 * @note All tensors must have the same data type.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077 *
78 * @note Whilst the first input tensor can be a vector, the second input tensor must be at least a matrix
79 *
SiCong Li1b6377b2023-01-09 15:34:20 +000080 * @note Batched GEMM only allows RHS tensor's rank to be <= 3
81 * @note Batched GEMM only supports broadcasting cases where RHS rank < LHS rank but not the other way around
82 *
Manuel Bottini2b84be52020-04-08 10:15:51 +010083 * @param[in] compile_context The compile context to be used.
84 * @param[in] a First input tensor (Matrix or Vector A). Data types supported: F16/F32
85 * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a.
86 * @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.
87 * @param[out] output Output tensor. Data type supported: same as @p a
88 * @param[in] alpha Weight of the matrix product
89 * @param[in] beta Weight of matrix C
90 * @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 +010091 * if the reshape of matrix B should happen only for the first run. GEMMInfo also contains information about the reshaping
92 * in case matrix A and matrix B have been already transformed.
Manuel Bottini2b84be52020-04-08 10:15:51 +010093 */
94 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 +010095
96 /** Initialise the kernel's inputs and output
97 *
98 * Similar to @ref CLGEMM::configure()
99 */
100 void configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo());
101
Georgios Pinitas78c00902018-01-09 17:33:11 +0000102 /** Static function to check if given info will lead to a valid configuration of @ref CLGEMM.
103 *
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100104 * Similar to @ref CLGEMM::configure()
Georgios Pinitas78c00902018-01-09 17:33:11 +0000105 *
106 * @return a status
107 */
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100108 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 +0100109
110 // Inherited methods overridden:
111 void run() override;
Georgios Pinitase0437672018-05-02 14:07:55 +0100112 void prepare() override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113
114private:
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100115 struct Impl;
116 std::unique_ptr<Impl> _impl;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117};
giuros011c9efeb2019-01-11 14:04:43 +0000118} // namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119
Michalis Spyrouf4643372019-11-29 16:17:13 +0000120#endif /* ARM_COMPUTE_CLGEMM_H */