| 1 |
/* |
| 2 |
* Copyright (c) 2017-2024 Arm Limited. |
| 3 |
* |
| 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 |
*/ |
| 24 |
#ifndef ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NEGEMM_H |
| 25 |
#define ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NEGEMM_H |
| 26 |
|
| 27 |
#include "arm_compute/function_info/GEMMInfo.h" |
| 28 |
#include "arm_compute/runtime/IFunction.h" |
| 29 |
#include "arm_compute/runtime/IMemoryManager.h" |
| 30 |
#include "arm_compute/runtime/IWeightsManager.h" |
| 31 |
#include "arm_compute/runtime/MemoryManagerOnDemand.h" |
| 32 |
|
| 33 |
#include <memory> |
| 34 |
|
| 35 |
namespace arm_compute |
| 36 |
{ |
| 37 |
/** Basic function to execute GEMM. This function calls the following kernels: |
| 38 |
* |
| 39 |
* -# cpu::CpuGemm |
| 40 |
*/ |
| 41 |
class NEGEMM : public IFunction |
| 42 |
{ |
| 43 |
public: |
| 44 |
/** Constructor */ |
| 45 |
NEGEMM(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager = nullptr); |
| 46 |
NEGEMM() : NEGEMM(MemoryManagerOnDemand::make_default()) |
| 47 |
{ |
| 48 |
} |
| 49 |
/** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 50 |
NEGEMM(const NEGEMM &) = delete; |
| 51 |
/** Default move constructor */ |
| 52 |
NEGEMM(NEGEMM &&) = default; |
| 53 |
/** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 54 |
NEGEMM &operator=(const NEGEMM &) = delete; |
| 55 |
/** Default move assignment operator */ |
| 56 |
NEGEMM &operator=(NEGEMM &&) = default; |
| 57 |
/** Default destructor */ |
| 58 |
~NEGEMM(); |
| 59 |
/** Initialise the kernel's inputs, output |
| 60 |
* |
| 61 |
* Valid data layouts: |
| 62 |
* - All |
| 63 |
* |
| 64 |
* Valid data type configurations: |
| 65 |
* |src0 |src1 |src2 |dst | |
| 66 |
* |:------------|:-----------|:---------|:--------------| |
| 67 |
* |F32 |F32 |F32 |F32 | |
| 68 |
* |F16 |F16 |F16 |F16 | |
| 69 |
* |BFLOAT16 |BFLOAT16 |BFLOAT16 |BFLOAT16 | |
| 70 |
* |
| 71 |
* @note GEMM: General Matrix Multiply - [alpha * A * B + beta * C]. |
| 72 |
* @note GEMM: The tensors a, b, c, d must have the same data type. You should not mix data types when calling this function. |
| 73 |
* |
| 74 |
* @note Batched GEMM only supports broadcasting cases where RHS rank < LHS rank but not the other way around |
| 75 |
* |
| 76 |
* @param[in] a First input tensor (Matrix A or Vector A). Data type supported: BFLOAT16/F16/F32 |
| 77 |
* @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a |
| 78 |
* @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 |
| 79 |
* @param[out] d Output tensor. Data type supported: same as @p a |
| 80 |
* @param[in] alpha Weight of the matrix product |
| 81 |
* @param[in] beta Weight of matrix C |
| 82 |
* @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and |
| 83 |
* if the reshape of matrix B should happen only for the first run |
| 84 |
*/ |
| 85 |
void configure(const ITensor *a, |
| 86 |
const ITensor *b, |
| 87 |
const ITensor *c, |
| 88 |
ITensor *d, |
| 89 |
float alpha, |
| 90 |
float beta, |
| 91 |
const GEMMInfo &gemm_info = GEMMInfo()); |
| 92 |
/** Static function to check if given info will lead to a valid configuration of @ref NEGEMM. |
| 93 |
* |
| 94 |
* Similar to @ref NEGEMM::configure() |
| 95 |
* |
| 96 |
* @return a status |
| 97 |
*/ |
| 98 |
static Status validate(const ITensorInfo *a, |
| 99 |
const ITensorInfo *b, |
| 100 |
const ITensorInfo *c, |
| 101 |
const ITensorInfo *output, |
| 102 |
float alpha, |
| 103 |
float beta, |
| 104 |
const GEMMInfo &gemm_info = GEMMInfo()); |
| 105 |
|
| 106 |
/** Static function that queries whether there exists fixed-format kernel and if it exists it will return in the first argument in what format |
| 107 |
* weights are expected to be reshaped as defined by WeightFormat class. Apart from the first argument the rest of the arguments are the same |
| 108 |
* as in @ref NEGEMM::validate() except that all arguments are required. |
| 109 |
* |
| 110 |
* @return a status |
| 111 |
*/ |
| 112 |
static Status has_opt_impl(arm_compute::WeightFormat &expected_weight_format, |
| 113 |
const ITensorInfo *a, |
| 114 |
const ITensorInfo *b, |
| 115 |
const ITensorInfo *c, |
| 116 |
const ITensorInfo *output, |
| 117 |
float alpha, |
| 118 |
float beta, |
| 119 |
const GEMMInfo &gemm_info = GEMMInfo()); |
| 120 |
|
| 121 |
// Inherited methods overridden: |
| 122 |
void run() override; |
| 123 |
void prepare() override; |
| 124 |
|
| 125 |
private: |
| 126 |
struct Impl; |
| 127 |
std::unique_ptr<Impl> _impl; |
| 128 |
}; |
| 129 |
} // namespace arm_compute |
| 130 |
#endif // ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NEGEMM_H |