Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +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_ARM_COMPUTE_SRC_GPU_CL_OPERATORS_CLMATMUL |
| 25 | #define ACL_ARM_COMPUTE_SRC_GPU_CL_OPERATORS_CLMATMUL |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 26 | |
| 27 | #include "src/gpu/cl/IClOperator.h" |
Jakub Sujak | 1ed6a14 | 2023-04-13 21:14:42 +0100 | [diff] [blame] | 28 | #include "src/gpu/cl/kernels/ClMatMulNativeKernel.h" |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 29 | #include "src/gpu/cl/kernels/ClMatMulLowpNativeKernel.h" |
| 30 | |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 31 | #include <memory> |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | namespace opencl |
| 36 | { |
| 37 | /** Basic operator to execute BatchMatMul on OpenCL. This operator calls the following OpenCL kernels: |
| 38 | * |
Jakub Sujak | 1ed6a14 | 2023-04-13 21:14:42 +0100 | [diff] [blame] | 39 | * -# @ref kernels::ClMatMulNativeKernel |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 40 | */ |
| 41 | class ClMatMul : public IClOperator |
| 42 | { |
| 43 | public: |
| 44 | /** Constructor */ |
| 45 | ClMatMul(); |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 46 | /** Default destructor */ |
| 47 | ~ClMatMul() = default; |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 48 | /** Initialise the kernel's inputs and output |
| 49 | * |
| 50 | * Valid data layouts: |
| 51 | * - All |
| 52 | * |
| 53 | * Valid data type configurations: |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 54 | * |lhs |rhs |dst | |
| 55 | * |:--------------|:--------------|:--------------| |
| 56 | * |F32 |F32 |F32 | |
| 57 | * |F16 |F16 |F16 | |
| 58 | * |QASYMM8_SIGNED |QASYMM8_SIGNED |QASYMM8_SIGNED | |
| 59 | * |QASYMM8 |QASYMM8 |QASYMM8 | |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 60 | * |
| 61 | * @note BatchMatMul: Batched Matrix Multiply - [A * B], Multiplies all slices (slice is an element of a batch) of Tensors A and B |
| 62 | * and stores the result in the dst tensor of the same batch size. |
| 63 | * 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 |
| 64 | * For NHWC for example: the batch is the higher dimensions H * N, and in general it is H * all higher dimensions. |
| 65 | * @note All tensors must have the same data type. |
| 66 | * |
| 67 | * @param[in] compile_context The compile context to be used. |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 68 | * @param[in] lhs Left-hand side tensor info. Data types supported: F16/F32/QASYMM8_SIGNED/QASYMM8. |
| 69 | * @param[in] rhs Right-hand side tensor info. Data types supported: same as @p lhs. |
| 70 | * @param[out] dst Output tensor to store the result of the batched matrix multiplication. Data types supported: same as @p lhs. |
| 71 | * @param[in] matmul_info Contains MatMul operation information described in @ref MatMulInfo. |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 72 | */ |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 73 | void configure(const CLCompileContext &compile_context, ITensorInfo *lhs, ITensorInfo *rhs, ITensorInfo *dst, const MatMulInfo &matmul_info); |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 74 | /** Static function to check if given info will lead to a valid configuration |
| 75 | * |
| 76 | * Similar to @ref ClMatMul::configure() |
| 77 | * |
| 78 | * @return a status |
| 79 | */ |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 80 | static Status validate(const ITensorInfo *lhs, const ITensorInfo *rhs, const ITensorInfo *dst, const MatMulInfo &matmul_info); |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 81 | // Inherited methods overridden: |
| 82 | void run(ITensorPack &tensors) override; |
| 83 | |
| 84 | private: |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 85 | std::unique_ptr<kernels::ClMatMulNativeKernel> _matmul_native_kernel{nullptr}; |
| 86 | std::unique_ptr<kernels::ClMatMulLowpNativeKernel> _matmul_lowp_native_kernel{nullptr}; |
| 87 | |
| 88 | bool _is_quantized{ false }; |
Ramy Elgammal | f26ea2f | 2023-03-24 11:42:03 +0000 | [diff] [blame] | 89 | }; |
| 90 | } // namespace opencl |
| 91 | } // namespace arm_compute |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 92 | #endif /* ACL_ARM_COMPUTE_SRC_GPU_CL_OPERATORS_CLMATMUL */ |