blob: db1592316526330c1923ebdfbf1997523cb91070 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
SiCong Li1b6377b2023-01-09 15:34:20 +00002 * Copyright (c) 2017-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_NEGEMM_H
25#define ARM_COMPUTE_NEGEMM_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/runtime/IFunction.h"
Georgios Pinitas658039b2017-09-15 16:30:50 +010028#include "arm_compute/runtime/IMemoryManager.h"
Michalis Spyrou1a569a32019-09-10 17:20:34 +010029#include "arm_compute/runtime/IWeightsManager.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030
Michalis Spyrouebcebf12020-10-21 00:04:14 +010031#include <memory>
32
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033namespace arm_compute
34{
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +000035/** Basic function to execute GEMM. This function calls the following kernels:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036 *
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +010037 * -# @ref cpu::CpuGemm
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038 */
39class NEGEMM : public IFunction
40{
41public:
42 /** Constructor */
Michalis Spyrou1a569a32019-09-10 17:20:34 +010043 NEGEMM(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
Georgios Pinitas72219332018-06-05 14:56:06 +010044 /** Prevent instances of this class from being copied (As this class contains pointers) */
45 NEGEMM(const NEGEMM &) = delete;
46 /** Default move constructor */
47 NEGEMM(NEGEMM &&) = default;
48 /** Prevent instances of this class from being copied (As this class contains pointers) */
49 NEGEMM &operator=(const NEGEMM &) = delete;
50 /** Default move assignment operator */
51 NEGEMM &operator=(NEGEMM &&) = default;
Michalis Spyrouebcebf12020-10-21 00:04:14 +010052 /** Default destructor */
53 ~NEGEMM();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054 /** Initialise the kernel's inputs, output
55 *
Teresa Charlin62687422021-04-28 10:58:49 +010056 * Valid data layouts:
57 * - All
58 *
59 * Valid data type configurations:
60 * |src0 |src1 |src2 |dst |
61 * |:------------|:-----------|:---------|:--------------|
62 * |F32 |F32 |F32 |F32 |
63 * |F16 |F16 |F16 |F16 |
64 * |BFLOAT16 |BFLOAT16 |BFLOAT16 |BFLOAT16 |
65 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066 * @note GEMM: General Matrix Multiply - [alpha * A * B + beta * C].
67 * @note GEMM: The tensors a, b, c, d must have the same data type. You should not mix data types when calling this function.
68 *
SiCong Li1b6377b2023-01-09 15:34:20 +000069 * @note Batched GEMM only supports broadcasting cases where RHS rank < LHS rank but not the other way around
70 *
Michele Di Giorgioa602f032020-03-12 19:34:33 +000071 * @param[in] a First input tensor (Matrix A or Vector A). Data type supported: BFLOAT16/F16/F32
Gian Marco1d25ed52017-12-16 19:33:50 +000072 * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a
73 * @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
74 * @param[out] d Output tensor. Data type supported: same as @p a
75 * @param[in] alpha Weight of the matrix product
76 * @param[in] beta Weight of matrix C
77 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
78 * if the reshape of matrix B should happen only for the first run
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079 */
Gian Marco1d25ed52017-12-16 19:33:50 +000080 void configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo());
Giorgio Arenaa855af12018-07-16 17:20:38 +010081 /** Static function to check if given info will lead to a valid configuration of @ref NEGEMM.
82 *
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010083 * Similar to @ref NEGEMM::configure()
Giorgio Arenaa855af12018-07-16 17:20:38 +010084 *
85 * @return a status
86 */
87 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 +010088
Milos Puzovic13b623e2022-07-27 17:53:21 +000089 /** Static function that queries whether there exists fixed-format kernel and if it exists it will return in the first argument in what format
90 * weights are expected to be reshaped as defined by WeightFormat class. Apart from the first argument the rest of the arguments are the same
91 * as in @ref NEGEMM::validate() except that all arguments are required.
92 *
93 * @return a status
94 */
95 static Status has_opt_impl(arm_compute::WeightFormat &expected_weight_format, const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output,
96 float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo());
97
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098 // Inherited methods overridden:
99 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +0100100 void prepare() override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101
102private:
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100103 struct Impl;
104 std::unique_ptr<Impl> _impl;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105};
Georgios Pinitas72219332018-06-05 14:56:06 +0100106} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000107#endif /*ARM_COMPUTE_NEGEMM_H */