Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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_CLGEMM_H__ |
| 25 | #define __ARM_COMPUTE_CLGEMM_H__ |
| 26 | |
| 27 | #include "arm_compute/core/CL/kernels/CLFillBorderKernel.h" |
| 28 | #include "arm_compute/core/CL/kernels/CLGEMMInterleave4x4Kernel.h" |
| 29 | #include "arm_compute/core/CL/kernels/CLGEMMMatrixAdditionKernel.h" |
| 30 | #include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyKernel.h" |
| 31 | #include "arm_compute/core/CL/kernels/CLGEMMTranspose1xWKernel.h" |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 32 | #include "arm_compute/runtime/CL/CLMemoryGroup.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | #include "arm_compute/runtime/CL/CLTensor.h" |
| 34 | #include "arm_compute/runtime/IFunction.h" |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 35 | #include "arm_compute/runtime/IMemoryManager.h" |
| 36 | |
| 37 | #include <memory> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | class ICLTensor; |
| 42 | |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 43 | /** Basic function to execute GEMM on OpenCL. This function calls the following OpenCL kernels: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | * |
| 45 | * -# @ref CLGEMMInterleave4x4Kernel (if the output tensor is a matrix) |
| 46 | * -# @ref CLGEMMTranspose1xWKernel (if the output tensor is a matrix) |
| 47 | * -# @ref CLGEMMMatrixMultiplyKernel |
| 48 | * -# @ref CLGEMMMatrixAdditionKernel (if c != nullptr and beta != 0.0) |
| 49 | * |
| 50 | */ |
| 51 | class CLGEMM : public IFunction |
| 52 | { |
| 53 | public: |
| 54 | /** Default constructor. */ |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 55 | CLGEMM(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 56 | /** Initialise the kernel's inputs and output |
| 57 | * |
| 58 | * @note GEMM: General Matrix Multiply - [alpha * A * B + beta * C]. |
| 59 | * |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 60 | * @note All tensors must have the same data type. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 61 | * |
| 62 | * @note Whilst the first input tensor can be a vector, the second input tensor must be at least a matrix |
| 63 | * |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 64 | * @param[in] a First input tensor (Matrix or Vector A). Data types supported: QS8/QS16/F16/F32 |
| 65 | * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a. |
| 66 | * @param[in] c Third input tensor (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. |
| 67 | * @param[out] output Output tensor. Data type supported: same as @p a |
| 68 | * @param[in] alpha Weight of the matrix product |
| 69 | * @param[in] beta Weight of matrix C |
| 70 | * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 71 | * if the reshape of matrix B should happen only for the first run. GEMMInfo also contains information about the reshaping |
| 72 | * in case matrix A and matrix B have been already transformed. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 73 | */ |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 74 | void configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 75 | |
| 76 | // Inherited methods overridden: |
| 77 | void run() override; |
| 78 | |
| 79 | private: |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 80 | CLMemoryGroup _memory_group; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | CLGEMMInterleave4x4Kernel _interleave_kernel; |
| 82 | CLGEMMTranspose1xWKernel _transpose_kernel; |
| 83 | CLGEMMMatrixMultiplyKernel _mm_kernel; |
| 84 | CLGEMMMatrixAdditionKernel _ma_kernel; |
| 85 | CLTensor _tmp_a; |
| 86 | CLTensor _tmp_b; |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 87 | bool _is_interleaved_transposed; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 88 | bool _run_addition; |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 89 | bool _is_first_run; |
| 90 | bool _reshape_b_only_on_first_run; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 91 | }; |
| 92 | } |
| 93 | |
| 94 | #endif /* __ARM_COMPUTE_CLGEMM_H__ */ |