Michele Di Giorgio | 4dfc553 | 2021-06-30 12:05:34 +0100 | [diff] [blame] | 1 | /* |
SiCong Li | 1b6377b | 2023-01-09 15:34:20 +0000 | [diff] [blame] | 2 | * Copyright (c) 2021-2023 Arm Limited. |
Michele Di Giorgio | 4dfc553 | 2021-06-30 12:05:34 +0100 | [diff] [blame] | 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 ARM_COMPUTE_CPU_GEMM_H |
| 25 | #define ARM_COMPUTE_CPU_GEMM_H |
| 26 | |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 27 | #include "src/cpu/ICpuOperator.h" |
Michele Di Giorgio | 4dfc553 | 2021-06-30 12:05:34 +0100 | [diff] [blame] | 28 | |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame^] | 29 | #include "arm_compute/core/GEMMInfo.h" |
Michele Di Giorgio | 4dfc553 | 2021-06-30 12:05:34 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/ITensorPack.h" |
| 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/Types.h" |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 33 | #include "src/cpu/kernels/CpuGemmInterleave4x4Kernel.h" |
| 34 | #include "src/cpu/kernels/CpuGemmMatrixAdditionKernel.h" |
| 35 | #include "src/cpu/kernels/CpuGemmMatrixMultiplyKernel.h" |
| 36 | #include "src/cpu/kernels/CpuGemmTranspose1xWKernel.h" |
| 37 | #include "src/cpu/operators/CpuActivation.h" |
| 38 | #include "src/cpu/operators/CpuAdd.h" |
| 39 | #include "src/cpu/operators/internal/CpuGemmAssemblyDispatch.h" |
Michele Di Giorgio | 4dfc553 | 2021-06-30 12:05:34 +0100 | [diff] [blame] | 40 | |
| 41 | #include <memory> |
| 42 | |
| 43 | namespace arm_compute |
| 44 | { |
| 45 | namespace cpu |
| 46 | { |
| 47 | /** Basic function to execute GEMM. This function calls the following kernels: |
| 48 | * |
| 49 | * If optimized assembly is available: |
| 50 | * -# @ref cpu::CpuGemmAssemblyDispatch |
| 51 | * -# @ref cpu::CpuActivation (if alpha != 1.0) |
| 52 | * Else: |
| 53 | * -# @ref cpu::kernels::CpuGemmInterleave4x4Kernel (if the output tensor is a matrix) |
| 54 | * -# @ref cpu::kernels::CpuGemmTranspose1xWKernel (if the output tensor is a matrix) |
| 55 | * -# @ref cpu::kernels::CpuGemmMatrixMultiplyKernel |
| 56 | * In both cases: |
| 57 | * -# @ref cpu::kernels::CpuGemmMatrixAdditionKernel (if c != nullptr and beta != 0.0 and is not reshaped once) |
| 58 | * Else: |
| 59 | * -# @ref cpu::CpuAdd (if c != nullptr and is reshaped once and not optimized assembly in place) |
| 60 | * |
| 61 | * -# @ref cpu::CpuActivation (if activation is specified in GEMMInfo) |
| 62 | */ |
| 63 | class CpuGemm : public ICpuOperator |
| 64 | { |
| 65 | public: |
| 66 | /** Default constructor */ |
| 67 | CpuGemm() = default; |
| 68 | /** Default destructor */ |
| 69 | ~CpuGemm() = default; |
| 70 | /** Configure operator for a given list of arguments |
| 71 | * |
| 72 | * Valid data layouts: |
| 73 | * - All |
| 74 | * |
| 75 | * Valid data type configurations: |
| 76 | * |src0 |src1 |src2 |dst | |
| 77 | * |:------------|:-----------|:---------|:--------------| |
| 78 | * |F32 |F32 |F32 |F32 | |
| 79 | * |F16 |F16 |F16 |F16 | |
Ramy Elgammal | c8cc024 | 2022-10-05 17:05:20 +0100 | [diff] [blame] | 80 | * |BFLOAT16 |BFLOAT16 |BFLOAT16 |FP32 | |
Michele Di Giorgio | 4dfc553 | 2021-06-30 12:05:34 +0100 | [diff] [blame] | 81 | * |
| 82 | * @note GEMM: General Matrix Multiply - [alpha * A * B + beta * C]. |
| 83 | * @note GEMM: The tensors a, b, c, d must have the same data type. You should not mix data types when calling this function. |
| 84 | * |
SiCong Li | 1b6377b | 2023-01-09 15:34:20 +0000 | [diff] [blame] | 85 | * @note Batched GEMM only supports broadcasting cases where RHS rank < LHS rank but not the other way around |
| 86 | * |
Michele Di Giorgio | 4dfc553 | 2021-06-30 12:05:34 +0100 | [diff] [blame] | 87 | * @param[in] a First input tensor info (Matrix A or Vector A). Data type supported: BFLOAT16/F16/F32 |
| 88 | * @param[in] b Second input tensor info (Matrix B). Data type supported: same as @p a |
| 89 | * @param[in] c Third input tensor info (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 |
| 90 | * @param[out] d Output tensor info. Data type supported: same as @p a |
| 91 | * @param[in] alpha Weight of the matrix product |
| 92 | * @param[in] beta Weight of matrix C |
| 93 | * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and |
| 94 | * if the reshape of matrix B should happen only for the first run |
| 95 | */ |
| 96 | void configure(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, ITensorInfo *d, |
| 97 | float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo()); |
| 98 | /** Static function to check if given info will lead to a valid configuration of @ref CpuGemm. |
| 99 | * |
| 100 | * Similar to @ref CpuGemm::configure() |
| 101 | * |
| 102 | * @return a status |
| 103 | */ |
| 104 | static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *d, |
| 105 | float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo()); |
| 106 | |
Francesco Petrogalli | 553f695 | 2022-06-30 10:22:01 +0000 | [diff] [blame] | 107 | /** Indicates whether or not there is an optimal assembly implementation that can be used to process the given parameters. |
| 108 | * |
| 109 | * This method has the same use of @ref |
| 110 | * NEGEMMConvolutionLayer::has_opt_impl, with the only caveat that |
Ramy Elgammal | 9178002 | 2022-07-20 14:57:37 +0100 | [diff] [blame] | 111 | * the value of arm_compute::WeightFormat need to be passed via the |
Francesco Petrogalli | 553f695 | 2022-06-30 10:22:01 +0000 | [diff] [blame] | 112 | * parameter gemm_info. |
| 113 | */ |
Ramy Elgammal | 9178002 | 2022-07-20 14:57:37 +0100 | [diff] [blame] | 114 | static Status has_opt_impl(arm_compute::WeightFormat &weight_format, const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *d, |
Francesco Petrogalli | 553f695 | 2022-06-30 10:22:01 +0000 | [diff] [blame] | 115 | const GEMMInfo &gemm_info = GEMMInfo()); |
| 116 | |
Michele Di Giorgio | 4dfc553 | 2021-06-30 12:05:34 +0100 | [diff] [blame] | 117 | // Inherited methods overridden: |
Ramy Elgammal | 9178002 | 2022-07-20 14:57:37 +0100 | [diff] [blame] | 118 | void run(ITensorPack &tensors) override; |
| 119 | void prepare(ITensorPack &constants) override; |
Michele Di Giorgio | 4dfc553 | 2021-06-30 12:05:34 +0100 | [diff] [blame] | 120 | experimental::MemoryRequirements workspace() const override; |
| 121 | |
Francesco Petrogalli | 553f695 | 2022-06-30 10:22:01 +0000 | [diff] [blame] | 122 | /** Indicates if the convolution executes in variable weights mode. |
| 123 | * |
| 124 | * When ACL executes convolution in variable weights mode, it does |
| 125 | * not perform any processing of the weights tensor. Instead, it |
| 126 | * utilizes the data as it is given by the user. |
| 127 | */ |
| 128 | bool isVarWeightsKernel() const; |
| 129 | |
Michele Di Giorgio | 4dfc553 | 2021-06-30 12:05:34 +0100 | [diff] [blame] | 130 | private: |
| 131 | enum AuxTensorIdx |
| 132 | { |
| 133 | AsmGemmWorkspace = 0, |
| 134 | Pretraspose, |
| 135 | InterleavedLHS, |
| 136 | TransposedRHS, |
| 137 | TempResult, |
| 138 | Count |
| 139 | }; |
| 140 | |
| 141 | std::unique_ptr<kernels::CpuGemmInterleave4x4Kernel> _interleave_kernel{ nullptr }; |
| 142 | std::unique_ptr<kernels::CpuGemmTranspose1xWKernel> _transpose_kernel{ nullptr }; |
| 143 | std::unique_ptr<kernels::CpuGemmMatrixMultiplyKernel> _mm_kernel{ nullptr }; |
| 144 | std::unique_ptr<CpuGemmAssemblyDispatch> _asm_glue{ nullptr }; |
| 145 | std::unique_ptr<kernels::CpuGemmMatrixAdditionKernel> _ma_kernel{ nullptr }; |
| 146 | std::unique_ptr<CpuActivation> _alpha_scale_func{ nullptr }; |
| 147 | std::unique_ptr<CpuAdd> _add_bias{ nullptr }; |
| 148 | std::unique_ptr<CpuActivation> _activation_func{ nullptr }; |
| 149 | |
| 150 | TensorInfo _tmp_a{}; |
| 151 | TensorInfo _tmp_b{}; |
| 152 | TensorInfo _tmp_d{}; |
| 153 | |
| 154 | bool _run_vector_matrix_multiplication{ false }; |
| 155 | bool _run_alpha_scale{ false }; |
| 156 | bool _run_addition{ false }; |
| 157 | bool _run_bias_addition{ false }; |
| 158 | bool _run_activation{ false }; |
| 159 | bool _reshape_b_only_on_first_run{ false }; |
| 160 | bool _is_prepared{ false }; |
| 161 | |
| 162 | experimental::MemoryRequirements _aux_mem{ Count }; |
| 163 | }; |
| 164 | } // namespace cpu |
| 165 | } // namespace arm_compute |
| 166 | #endif /*ARM_COMPUTE_CPU_GEMM_H */ |