blob: 2b1b4cf0ffac0fca0d108b5df49663ec6b4ab762 [file] [log] [blame]
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +00001/*
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 */
SiCong Lic5ab4df2023-10-17 17:38:57 +010024#ifndef ACL_SRC_CPU_OPERATORS_CPUMATMUL_H
25#define ACL_SRC_CPU_OPERATORS_CPUMATMUL_H
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +000026
27#include "arm_compute/core/TensorInfo.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +000029#include "src/core/common/Macros.h"
30#include "src/cpu/ICpuOperator.h"
31#include "src/cpu/kernels/CpuTransposeKernel.h"
32#include "src/cpu/operators/internal/CpuGemmAssemblyDispatch.h"
33
34namespace arm_compute
35{
36// Forward Declarations
37class MatMulInfo;
38class CpuMatMulSettings;
39
40namespace cpu
41{
42/** Function to execute MatMul Operation. This function calls the following functions/kernels:
43 *
44 * If adjoint/adj flag is enabled for either input lhs or rhs (or both) :
45 * -# @ref cpu::kernels::CpuTransposeKernel
46 * Then :
47 * -# @ref cpu::CpuGemmAssemblyDispatch
48 */
49class CpuMatMul : public ICpuOperator
50{
51public:
52 /* Constructor */
53 CpuMatMul();
54 /* Destructor */
55 ~CpuMatMul() = default;
56
57 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(CpuMatMul);
58 /** Configure operator for a given list of arguments
59 *
60 * Note: Check documentation of @ref NEMatMul for a list of supported datatypes and layouts
61 *
62 *
Jakub Sujake9b3ee22023-04-17 12:08:48 +010063 * @param[in] lhs Left-hand side tensor info.
64 * @param[in] rhs Right-hand side tensor info.
65 * @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 Munshia1b1e412023-03-23 22:21:31 +000066 * @param[in] info Contains MatMul operation information described in @ref MatMulInfo.
67 * @param[in] settings The settings for matmul operation (i.e fast math)
Mohammed Suhail Munshi94abde42023-05-25 16:48:43 +010068 * @param[in] act_info Class containing information about fused activation function.
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +000069 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010070 void configure(ITensorInfo *lhs,
71 ITensorInfo *rhs,
72 ITensorInfo *dst,
73 const MatMulInfo &info,
74 const CpuMatMulSettings &settings,
75 const ActivationLayerInfo &act_info = ActivationLayerInfo());
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +000076 /** Static function to check if given info will lead to a valid configuration
77 *
78 * Similar to CpuMatMul::configure()
79 *
80 * @return a status
81 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010082 static Status validate(const ITensorInfo *lhs,
83 const ITensorInfo *rhs,
84 const ITensorInfo *dst,
85 const MatMulInfo &info,
86 const CpuMatMulSettings &settings,
Mohammed Suhail Munshi94abde42023-05-25 16:48:43 +010087 const ActivationLayerInfo &act_info = ActivationLayerInfo());
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +000088
89 // Inherited methods overridden:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010090 void run(ITensorPack &tensors) override;
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +000091 experimental::MemoryRequirements workspace() const override;
92
93private:
94 enum InternalTensorIdx
95 {
SiCong Lic5ab4df2023-10-17 17:38:57 +010096 /* Slots 0 - 2 reserved for CpuGemmAssemblyDispatch */
97 TransposeLHS = 3,
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +000098 TransposeRHS,
99 Count
100 };
101
102 // Define unique pointers to kernels/operators used by matmul
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103 std::unique_ptr<kernels::CpuTransposeKernel> _transpose_kernel_lhs{nullptr};
104 std::unique_ptr<kernels::CpuTransposeKernel> _transpose_kernel_rhs{nullptr};
105 std::unique_ptr<CpuGemmAssemblyDispatch> _asm_glue{nullptr};
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +0000106
107 // TensorInfo for tensors stored in auxillary memory
108 TensorInfo _lhs_transposed{};
109 TensorInfo _rhs_transposed{};
110
111 // Original tensor shapes prior to reshaping tensors and collapsing dimensions
112 TensorShape _original_lhs_shape{};
113 TensorShape _original_rhs_shape{};
114 TensorShape _original_dst_shape{};
115
116 // Note : adj_lhs means the same as transposing lhs
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100117 bool _adj_lhs{false};
118 bool _adj_rhs{false};
119 bool _fast_math{false};
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +0000120 AsmGemmInfo _gemm_info{};
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100121 experimental::MemoryRequirements _aux_mem{Count};
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +0000122};
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100123} // namespace cpu
124} // namespace arm_compute
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +0000125
SiCong Lic5ab4df2023-10-17 17:38:57 +0100126#endif // ACL_SRC_CPU_OPERATORS_CPUMATMUL_H