blob: 9c1748bc637020a3537480689cf6907aa6f2b163 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2019 Arm Limited.
Anthony Barbier7068f992017-10-26 15:23:08 +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
Michalis Spyrouf4643372019-11-29 16:17:13 +000025#ifndef ARM_COMPUTE_GCGEMM_H
26#define ARM_COMPUTE_GCGEMM_H
Anthony Barbier7068f992017-10-26 15:23:08 +010027
28#include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMInterleave4x4Kernel.h"
29#include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMMatrixAdditionKernel.h"
30#include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMMatrixMultiplyKernel.h"
31#include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMTranspose1xWKernel.h"
32#include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h"
33#include "arm_compute/runtime/IFunction.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010034#include "arm_compute/runtime/MemoryGroup.h"
Anthony Barbier7068f992017-10-26 15:23:08 +010035
36namespace arm_compute
37{
38class IGCTensor;
39
40/** Basic function to execute GEMM on OpenGLES Compute. This function calls the following kernels:
41 *
42 * -# @ref GCGEMMInterleave4x4Kernel (if the output tensor is a matrix)
43 * -# @ref GCGEMMTranspose1xWKernel (if the output tensor is a matrix)
44 * -# @ref GCGEMMMatrixMultiplyKernel
45 * -# @ref GCGEMMMatrixAdditionKernel (if c != nullptr and beta != 0.0)
46 *
47 */
48class GCGEMM : public IFunction
49{
50public:
51 /** Default constructor. */
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000052 GCGEMM(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Georgios Pinitas72219332018-06-05 14:56:06 +010053 /** Prevent instances of this class from being copied (As this class contains pointers) */
54 GCGEMM(const GCGEMM &) = delete;
55 /** Default move constructor */
56 GCGEMM(GCGEMM &&) = default;
57 /** Prevent instances of this class from being copied (As this class contains pointers) */
58 GCGEMM &operator=(const GCGEMM &) = delete;
59 /** Default move assignment operator */
60 GCGEMM &operator=(GCGEMM &&) = default;
Anthony Barbier7068f992017-10-26 15:23:08 +010061 /** Initialise the kernel's inputs and output
62 *
63 * @note GEMM: General Matrix Multiply - [alpha * A * B + beta * C].
64 *
65 * @note All tensors must have the same data type.
66 *
67 * @note Whilst the first input tensor can be a vector, the second input tensor must be at least a matrix
68 *
Gian Marco1aede362018-02-05 15:07:36 +000069 * @param[in] a First input tensor (Matrix or Vector A). Data types supported: F32
70 * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a.
71 * @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.
72 * @param[out] output Output tensor. Data type supported: same as @p a
73 * @param[in] alpha Weight of the matrix product
74 * @param[in] beta Weight of matrix C
75 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
76 * if the reshape of matrix B should happen only for the first run
Anthony Barbier7068f992017-10-26 15:23:08 +010077 */
Gian Marco1aede362018-02-05 15:07:36 +000078 void configure(const IGCTensor *a, const IGCTensor *b, const IGCTensor *c, IGCTensor *output, float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo());
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010079 /** Static function to check if given info will lead to a valid configuration of @ref GCGEMM.
80 *
81 * @param[in] a First input tensor (Matrix or Vector A). Data types supported: F16/F32
82 * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a.
83 * @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.
84 * @param[out] output Output tensor. Data type supported: same as @p a
85 * @param[in] alpha Weight of the matrix product
86 * @param[in] beta Weight of matrix C
87 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
88 * if the reshape of matrix B should happen only for the first run
89 *
90 * @return a status
91 */
92 static Status validate(const ITensorInfo *a, const ITensorInfo *b, const IGCTensor *c, const ITensorInfo *output, const float alpha, const float beta, const GEMMInfo &gemm_info = GEMMInfo());
Anthony Barbier7068f992017-10-26 15:23:08 +010093
94 // Inherited methods overridden:
95 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +010096 void prepare() override;
Anthony Barbier7068f992017-10-26 15:23:08 +010097
98private:
Georgios Pinitas26014cf2019-09-09 19:00:57 +010099 MemoryGroup _memory_group;
Anthony Barbier7068f992017-10-26 15:23:08 +0100100 GCGEMMInterleave4x4Kernel _interleave_kernel;
101 GCGEMMTranspose1xWKernel _transpose_kernel;
102 GCGEMMMatrixMultiplyKernel _mm_kernel;
103 GCGEMMMatrixAdditionKernel _ma_kernel;
104 GCTensor _tmp_a;
105 GCTensor _tmp_b;
Georgios Pinitas72219332018-06-05 14:56:06 +0100106 const IGCTensor *_original_b;
Anthony Barbier7068f992017-10-26 15:23:08 +0100107 bool _is_interleaved_transposed;
108 bool _run_addition;
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100109 bool _reshape_b_only_on_first_run;
Georgios Pinitas72219332018-06-05 14:56:06 +0100110 bool _is_prepared;
Anthony Barbier7068f992017-10-26 15:23:08 +0100111};
112}
113
Michalis Spyrouf4643372019-11-29 16:17:13 +0000114#endif /* ARM_COMPUTE_GCGEMM_H */