blob: c4accde23d417048f905b43aeee24d4458ba1ddb [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gian Marco36a0a462018-01-12 10:21:40 +00002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/CL/kernels/CLGEMMInterleave4x4Kernel.h"
28#include "arm_compute/core/CL/kernels/CLGEMMMatrixAdditionKernel.h"
29#include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyKernel.h"
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000030#include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyReshapedKernel.h"
31#include "arm_compute/core/CL/kernels/CLGEMMReshapeLHSMatrixKernel.h"
32#include "arm_compute/core/CL/kernels/CLGEMMReshapeRHSMatrixKernel.h"
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +010033#include "arm_compute/runtime/CL/CLMemoryGroup.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/runtime/CL/CLTensor.h"
35#include "arm_compute/runtime/IFunction.h"
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +010036#include "arm_compute/runtime/IMemoryManager.h"
37
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038namespace arm_compute
39{
40class ICLTensor;
41
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +010042/** Basic function to execute GEMM on OpenCL. This function calls the following OpenCL kernels:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043 *
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000044 * -# @ref CLGEMMInterleave4x4Kernel (only if the reshaped GEMM is selected by the heuristic model and the GPU target is NOT Mali-G76)
45 * -# @ref CLGEMMReshapeLHSMatrixKernel (only if the reshaped GEMM is selected by the heuristic model and the GPU target IS Mali-G76)
giuros018b6b4a92018-12-18 19:01:33 +000046 * -# @ref CLGEMMReshapeRHSMatrixKernel (only if the reshaped GEMM is selected by the heuristic model)
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000047 * -# @ref CLGEMMMatrixMultiplyKernel (if GPU target is NOT G76 or if the reshaped GEMM is NOT selected)
48 * -# @ref CLGEMMMatrixMultiplyReshapedKernel (only if the reshaped GEMM is selected by the heuristic model and the GPU target IS Mali-G76)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049 * -# @ref CLGEMMMatrixAdditionKernel (if c != nullptr and beta != 0.0)
50 *
51 */
52class CLGEMM : public IFunction
53{
54public:
Alex Gildayc357c472018-03-21 13:54:09 +000055 /** Default constructor.
56 *
57 * @param[in] memory_manager (Optional) Memory manager.
58 */
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +010059 CLGEMM(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Georgios Pinitas82b51482018-04-24 15:14:12 +010060 /** Prevent instances of this class from being copied (As this class contains pointers) */
61 CLGEMM(const CLGEMM &) = delete;
62 /** Default move constructor */
63 CLGEMM(CLGEMM &&) = default;
64 /** Prevent instances of this class from being copied (As this class contains pointers) */
65 CLGEMM &operator=(const CLGEMM &) = delete;
66 /** Default move assignment operator */
67 CLGEMM &operator=(CLGEMM &&) = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068 /** Initialise the kernel's inputs and output
69 *
70 * @note GEMM: General Matrix Multiply - [alpha * A * B + beta * C].
71 *
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +010072 * @note All tensors must have the same data type.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073 *
74 * @note Whilst the first input tensor can be a vector, the second input tensor must be at least a matrix
75 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010076 * @param[in] a First input tensor (Matrix or Vector A). Data types supported: F16/F32
Gian Marco1d25ed52017-12-16 19:33:50 +000077 * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a.
78 * @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.
79 * @param[out] output Output tensor. Data type supported: same as @p a
80 * @param[in] alpha Weight of the matrix product
81 * @param[in] beta Weight of matrix C
82 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
Gian Marco36a0a462018-01-12 10:21:40 +000083 * if the reshape of matrix B should happen only for the first run. GEMMInfo also contains information about the reshaping
84 * in case matrix A and matrix B have been already transformed.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010085 */
Gian Marco1d25ed52017-12-16 19:33:50 +000086 void configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo());
Georgios Pinitas78c00902018-01-09 17:33:11 +000087 /** Static function to check if given info will lead to a valid configuration of @ref CLGEMM.
88 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010089 * @param[in] a First input tensor info (Matrix or Vector A). Data types supported: F16/F32
Gian Marco Iodice750641d2018-05-08 12:01:57 +010090 * @param[in] b Second input tensor info (Matrix B). Data type supported: same as @p a.
91 * @param[in] c Third input tensor info (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.
92 * @param[out] output Output tensor info. Data type supported: same as @p a
Georgios Pinitas78c00902018-01-09 17:33:11 +000093 * @param[in] alpha Weight of the matrix product
94 * @param[in] beta Weight of matrix C
95 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
96 * if the reshape of matrix B should happen only for the first run
97 *
98 * @return a status
99 */
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100100 static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101
102 // Inherited methods overridden:
103 void run() override;
Georgios Pinitase0437672018-05-02 14:07:55 +0100104 void prepare() override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105
106private:
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000107 CLMemoryGroup _memory_group;
108 CLGEMMInterleave4x4Kernel _interleave_kernel; // TODO - COMPMID-1835: Remove this kernel and use CLGEMMReshapeLHSMatrixKernel
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000109 CLGEMMMatrixMultiplyKernel _mm_kernel;
110 CLGEMMMatrixAdditionKernel _ma_kernel;
111 CLGEMMReshapeLHSMatrixKernel _reshape_lhs_kernel;
112 CLGEMMReshapeRHSMatrixKernel _reshape_rhs_kernel;
113 CLGEMMMatrixMultiplyReshapedKernel _mm_reshaped_kernel;
114 CLTensor _tmp_a;
115 CLTensor _tmp_b;
116 const ICLTensor *_original_b;
117 bool _is_interleaved_transposed;
118 bool _run_addition;
119 bool _reshape_b_only_on_first_run;
120 bool _is_prepared;
121 bool _is_G76_path; // TODO: To be removed once completed COMPMID-1835 and COMPMID-1836
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122};
123}
124
125#endif /* __ARM_COMPUTE_CLGEMM_H__ */