Mohammed Suhail Munshi | a1b1e41 | 2023-03-23 22:21:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023 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 | */ |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 24 | #ifndef ACL_SRC_CPU_OPERATORS_CPUMATMUL |
| 25 | #define ACL_SRC_CPU_OPERATORS_CPUMATMUL |
Mohammed Suhail Munshi | a1b1e41 | 2023-03-23 22:21:31 +0000 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/TensorInfo.h" |
| 28 | #include "src/core/common/Macros.h" |
| 29 | #include "src/cpu/ICpuOperator.h" |
| 30 | #include "src/cpu/kernels/CpuTransposeKernel.h" |
| 31 | #include "src/cpu/operators/internal/CpuGemmAssemblyDispatch.h" |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | // Forward Declarations |
| 36 | class MatMulInfo; |
| 37 | class CpuMatMulSettings; |
| 38 | |
| 39 | namespace cpu |
| 40 | { |
| 41 | /** Function to execute MatMul Operation. This function calls the following functions/kernels: |
| 42 | * |
| 43 | * If adjoint/adj flag is enabled for either input lhs or rhs (or both) : |
| 44 | * -# @ref cpu::kernels::CpuTransposeKernel |
| 45 | * Then : |
| 46 | * -# @ref cpu::CpuGemmAssemblyDispatch |
| 47 | */ |
| 48 | class CpuMatMul : public ICpuOperator |
| 49 | { |
| 50 | public: |
| 51 | /* Constructor */ |
| 52 | CpuMatMul(); |
| 53 | /* Destructor */ |
| 54 | ~CpuMatMul() = default; |
| 55 | |
| 56 | ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(CpuMatMul); |
| 57 | /** Configure operator for a given list of arguments |
| 58 | * |
| 59 | * Note: Check documentation of @ref NEMatMul for a list of supported datatypes and layouts |
| 60 | * |
| 61 | * |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 62 | * @param[in] lhs Left-hand side tensor info. |
| 63 | * @param[in] rhs Right-hand side tensor info. |
| 64 | * @param[out] dst Output tensor to store the result of the batched matrix multiplication. Data types supported: same as @p lhs / @p rhs. |
Mohammed Suhail Munshi | a1b1e41 | 2023-03-23 22:21:31 +0000 | [diff] [blame] | 65 | * @param[in] info Contains MatMul operation information described in @ref MatMulInfo. |
| 66 | * @param[in] settings The settings for matmul operation (i.e fast math) |
Mohammed Suhail Munshi | 94abde4 | 2023-05-25 16:48:43 +0100 | [diff] [blame] | 67 | * @param[in] act_info Class containing information about fused activation function. |
Mohammed Suhail Munshi | a1b1e41 | 2023-03-23 22:21:31 +0000 | [diff] [blame] | 68 | */ |
Mohammed Suhail Munshi | 94abde4 | 2023-05-25 16:48:43 +0100 | [diff] [blame] | 69 | void configure(ITensorInfo *lhs, ITensorInfo *rhs, ITensorInfo *dst, const MatMulInfo &info, const CpuMatMulSettings &settings, const ActivationLayerInfo &act_info = ActivationLayerInfo()); |
Mohammed Suhail Munshi | a1b1e41 | 2023-03-23 22:21:31 +0000 | [diff] [blame] | 70 | /** Static function to check if given info will lead to a valid configuration |
| 71 | * |
| 72 | * Similar to CpuMatMul::configure() |
| 73 | * |
| 74 | * @return a status |
| 75 | */ |
Mohammed Suhail Munshi | 94abde4 | 2023-05-25 16:48:43 +0100 | [diff] [blame] | 76 | static Status validate(const ITensorInfo *lhs, const ITensorInfo *rhs, const ITensorInfo *dst, const MatMulInfo &info, const CpuMatMulSettings &settings, |
| 77 | const ActivationLayerInfo &act_info = ActivationLayerInfo()); |
Mohammed Suhail Munshi | a1b1e41 | 2023-03-23 22:21:31 +0000 | [diff] [blame] | 78 | |
| 79 | // Inherited methods overridden: |
| 80 | void run(ITensorPack &tensors) override; |
| 81 | experimental::MemoryRequirements workspace() const override; |
| 82 | |
| 83 | private: |
| 84 | enum InternalTensorIdx |
| 85 | { |
| 86 | AsmGemmWorkspace = 0, // Pre-allocate workspace tensors for CpuGemmAssemblyDispatch |
| 87 | PretransposeRHS, // Pre-allocate workspace tensors for CpuGemmAssemblyDispatch |
| 88 | TransposeLHS, |
| 89 | TransposeRHS, |
| 90 | Count |
| 91 | }; |
| 92 | |
| 93 | // Define unique pointers to kernels/operators used by matmul |
| 94 | std::unique_ptr<kernels::CpuTransposeKernel> _transpose_kernel_lhs{ nullptr }; |
| 95 | std::unique_ptr<kernels::CpuTransposeKernel> _transpose_kernel_rhs{ nullptr }; |
| 96 | std::unique_ptr<CpuGemmAssemblyDispatch> _asm_glue{ nullptr }; |
| 97 | |
| 98 | // TensorInfo for tensors stored in auxillary memory |
| 99 | TensorInfo _lhs_transposed{}; |
| 100 | TensorInfo _rhs_transposed{}; |
| 101 | |
| 102 | // Original tensor shapes prior to reshaping tensors and collapsing dimensions |
| 103 | TensorShape _original_lhs_shape{}; |
| 104 | TensorShape _original_rhs_shape{}; |
| 105 | TensorShape _original_dst_shape{}; |
| 106 | |
| 107 | // Note : adj_lhs means the same as transposing lhs |
| 108 | bool _adj_lhs{ false }; |
| 109 | bool _adj_rhs{ false }; |
| 110 | bool _fast_math{ false }; |
| 111 | AsmGemmInfo _gemm_info{}; |
| 112 | experimental::MemoryRequirements _aux_mem{ Count }; |
| 113 | }; |
| 114 | } |
| 115 | } |
| 116 | |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 117 | #endif /* ACL_SRC_CPU_OPERATORS_CPUMATMUL */ |