blob: 9c9939b9d0455f365b58dbdefc0d2cb584f864f1 [file] [log] [blame]
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +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 */
Jakub Sujake9b3ee22023-04-17 12:08:48 +010024#ifndef ACL_ARM_COMPUTE_RUNTIME_CL_FUNCTIONS_CLMATMUL
25#define ACL_ARM_COMPUTE_RUNTIME_CL_FUNCTIONS_CLMATMUL
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +000026
Mohammed Suhail Munshi94abde42023-05-25 16:48:43 +010027#include "arm_compute/core/Types.h"
SiCong Li91295492023-07-21 18:16:13 +010028#include "arm_compute/function_info/ActivationLayerInfo.h"
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +000029#include "arm_compute/runtime/IFunction.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +000031#include <memory>
Jakub Sujake9b3ee22023-04-17 12:08:48 +010032
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +000033namespace arm_compute
34{
35// Forward declarations for used types instead of including their header, that could minimize compile time
36class CLCompileContext;
37class ICLTensor;
38class ITensorInfo;
39class MatMulInfo;
40class Status;
41
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +000042/** Settings for MatMul OpenCL implementation */
43class GpuMatMulSettings
44{
45public:
46 /* Placeholder for operator parity between CPU/GPU */
47};
48
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +000049/** Basic function to execute MatMul (Matrix Multiplication) on OpenCL */
50class CLMatMul : public IFunction
51{
52public:
53 /** Default constructor.*/
54 CLMatMul();
55 /** Default destructor */
56 ~CLMatMul();
57 /** Prevent instances of this class from being copied (As this class contains pointers) */
58 CLMatMul(const CLMatMul &) = delete;
59 /** Default move constructor */
60 CLMatMul(CLMatMul &&);
61 /** Prevent instances of this class from being copied (As this class contains pointers) */
62 CLMatMul &operator=(const CLMatMul &) = delete;
63 /** Default move assignment operator */
64 CLMatMul &operator=(CLMatMul &&);
65 /** Initialise the kernel's inputs and output
66 *
67 * Valid data layouts:
68 * - All
69 *
70 * Valid data type configurations:
Jakub Sujake9b3ee22023-04-17 12:08:48 +010071 * |lhs |rhs |dst |
72 * |:--------------|:--------------|:--------------|
73 * |F32 |F32 |F32 |
74 * |F16 |F16 |F16 |
75 * |QASYMM8_SIGNED |QASYMM8_SIGNED |QASYMM8_SIGNED |
76 * |QASYMM8 |QASYMM8 |QASYMM8 |
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +000077 *
78 * @note BatchMatMul: Batched Matrix Multiply - [A * B], Multiplies all slices (slice is an element of a batch) of Tensors A and B
79 * and stores the result in the dst tensor of the same batch size.
80 * Batch here is number of slices from A and B multiplied at a time, do not confuse with the batch dimension 'N' of NHWC/NCHW
81 * For NHWC for example: the batch is the higher dimensions H * N, and in general it is H * all higher dimensions.
82 * @note All tensors must have the same data type.
83 *
84 * @param[in] compile_context The compile context to be used.
Jakub Sujake9b3ee22023-04-17 12:08:48 +010085 * @param[in] lhs Left-hand side tensor info containing the input activations as Matrix A. Data types supported: F16/F32/QASYMM8_SIGNED/QASYMM8.
86 * @param[in] rhs Right-hand side tensor info containing the input weights as Matrix B. Data types supported: same as @p lhs.
87 * @param[out] dst Output tensor to store the result of the batched matrix multiplication. Data types supported: same as @p lhs.
88 * @param[in] matmul_info Contains MatMul operation information described in @ref MatMulInfo.
Mohammed Suhail Munshi94abde42023-05-25 16:48:43 +010089 * @param[in] settings Contains flags for function level settings
90 * @param[in] act_info (Optional) Contains activation function and lower and upper bound values for bounded activation functions.
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +000091 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010092 void configure(const CLCompileContext &compile_context,
93 ICLTensor *rhs,
94 ICLTensor *lhs,
95 ICLTensor *dst,
96 const MatMulInfo &matmul_info,
97 const GpuMatMulSettings &settings = GpuMatMulSettings{},
98 const ActivationLayerInfo &act_info = ActivationLayerInfo{});
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +000099 /** Initialise the kernel's inputs and output
100 *
101 * Similar to @ref CLMatMul::configure()
102 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103 void configure(ICLTensor *lhs,
104 ICLTensor *rhs,
105 ICLTensor *dst,
106 const MatMulInfo &matmul_info,
107 const GpuMatMulSettings &settings = GpuMatMulSettings{},
108 const ActivationLayerInfo &act_info = ActivationLayerInfo{});
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000109 /** Static function to check if given info will lead to a valid configuration of @ref CLMatMul.
110 *
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000111 *
Mohammed Suhail Munshi94abde42023-05-25 16:48:43 +0100112 * @note All tensors must have the same data type.
113 *
114 * @param[in] lhs Left-hand side (Matrix A) tensor info. Data types supported: F16/F32/QASYMM8_SIGNED/QASYMM8.
115 * @param[in] rhs Right-hand side (Matrix B) tensor info. Data types supported: same as @p lhs.
116 * @param[out] output Output tensor info to store the result of the batched matrix multiplication. Data types supported: same as @p lhs.
117 * @param[in] matmul_info Contains MatMul operation information described in @ref MatMulInfo.
118 * @param[in] act_info (Optional) Contains activation function and lower and upper bound values for bounded activation functions.
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000119 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100120 static Status validate(const ITensorInfo *lhs,
121 const ITensorInfo *rhs,
122 const ITensorInfo *output,
123 const MatMulInfo &matmul_info,
124 const ActivationLayerInfo &act_info = ActivationLayerInfo{});
Ramy Elgammalf26ea2f2023-03-24 11:42:03 +0000125 // Inherited methods overridden:
126 void run() override;
127
128private:
129 struct Impl;
130 std::unique_ptr<Impl> _impl;
131};
132} // namespace arm_compute
133
Jakub Sujake9b3ee22023-04-17 12:08:48 +0100134#endif /* ACL_ARM_COMPUTE_RUNTIME_CL_FUNCTIONS_CLMATMUL */