blob: 541985b50c30c6de0f6cb2518217b58f37e2cc5d [file] [log] [blame]
Gian Marco05288a22017-11-21 10:57:50 +00001/*
giuros011c9efeb2019-01-11 14:04:43 +00002 * Copyright (c) 2017-2019 ARM Limited.
Gian Marco05288a22017-11-21 10:57:50 +00003 *
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_CLGEMMLOWPMATRIXMULTIPLYCORE_H__
25#define __ARM_COMPUTE_CLGEMMLOWPMATRIXMULTIPLYCORE_H__
26
Gian Marco05288a22017-11-21 10:57:50 +000027#include "arm_compute/core/CL/kernels/CLGEMMLowpMatrixMultiplyKernel.h"
Gian Marco Iodice06be6f82019-06-24 17:47:51 +010028#include "arm_compute/core/CL/kernels/CLGEMMLowpMatrixMultiplyNativeKernel.h"
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +010029#include "arm_compute/core/CL/kernels/CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel.h"
Gian Marco05288a22017-11-21 10:57:50 +000030#include "arm_compute/core/CL/kernels/CLGEMMLowpOffsetContributionKernel.h"
Gian Marco Iodice4b908652018-10-18 10:21:02 +010031#include "arm_compute/core/CL/kernels/CLGEMMLowpOffsetContributionOutputStageKernel.h"
Gian Marco05288a22017-11-21 10:57:50 +000032#include "arm_compute/core/CL/kernels/CLGEMMLowpReductionKernel.h"
giuros018b6b4a92018-12-18 19:01:33 +000033#include "arm_compute/core/CL/kernels/CLGEMMReshapeRHSMatrixKernel.h"
Gian Marco05288a22017-11-21 10:57:50 +000034#include "arm_compute/runtime/CL/CLMemoryGroup.h"
35#include "arm_compute/runtime/CL/CLTensor.h"
36#include "arm_compute/runtime/IFunction.h"
37
38namespace arm_compute
39{
40class IMemoryManager;
41class ICLTensor;
42
43/** Basic function to execute GEMMLowpMatrixMultiplyCore on OpenCL. This function calls the following OpenCL kernels:
44 *
giuros018b6b4a92018-12-18 19:01:33 +000045 * -# @ref CLGEMMReshapeRHSMatrixKernel (if the output tensor is a matrix)
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +010046 * -# @ref CLGEMMLowpMatrixMultiplyKernel (if the parameter "reshape_b_only_on_first_run" of GEMMInfo is FALSE)
47 * -# @ref CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel (if the parameter "reshape_b_only_on_first_run" of GEMMInfo is TRUE)
Gian Marco05288a22017-11-21 10:57:50 +000048 * -# @ref CLGEMMLowpMatrixAReductionKernel (if the offset of matrix B is not 0)
49 * -# @ref CLGEMMLowpMatrixBReductionKernel (if the offset of matrix A is not 0)
Gian Marco Iodice4b908652018-10-18 10:21:02 +010050 * -# @ref CLGEMMLowpOffsetContributionKernel (if gemm_info.gemmlowp_output_stage == NONE)
51 * -# @ref CLGEMMLowpOffsetContributionOutputStageKernel (if gemm_info.gemmlowp_output_stage != NONE)
Gian Marco05288a22017-11-21 10:57:50 +000052 *
53*/
54class CLGEMMLowpMatrixMultiplyCore : public IFunction
55{
56public:
57 /** Constructor */
58 CLGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Georgios Pinitas72219332018-06-05 14:56:06 +010059 /** Prevent instances of this class from being copied (As this class contains pointers) */
60 CLGEMMLowpMatrixMultiplyCore(const CLGEMMLowpMatrixMultiplyCore &) = delete;
61 /** Default move constructor */
62 CLGEMMLowpMatrixMultiplyCore(CLGEMMLowpMatrixMultiplyCore &&) = default;
63 /** Prevent instances of this class from being copied (As this class contains pointers) */
64 CLGEMMLowpMatrixMultiplyCore &operator=(const CLGEMMLowpMatrixMultiplyCore &) = delete;
65 /** Default move assignment operator */
66 CLGEMMLowpMatrixMultiplyCore &operator=(CLGEMMLowpMatrixMultiplyCore &&) = default;
Gian Marco05288a22017-11-21 10:57:50 +000067 /** Initialise the kernel's inputs, output
68 *
Gian Marco Iodice4b908652018-10-18 10:21:02 +010069 * @note GEMMLowp: low precision GEMM kernel. [A * B + C]
Gian Marco05288a22017-11-21 10:57:50 +000070 * This kernel performs the following computations:
71 *
72 * -# Convert a values from QASYMM8 to int32 and add a_offset to each of them.
George Wort2d7e6832019-02-22 16:37:41 +000073 * -# Convert b values from QASYMM8 to int32 and add b_offset to each of them.
Gian Marco05288a22017-11-21 10:57:50 +000074 * -# Compute the matrix product of the resulting a * b in int32.
Gian Marco Iodice4b908652018-10-18 10:21:02 +010075 * -# Quantize to uint8 if gemm_info.gemmlowp_output_stage != NONE
Gian Marco05288a22017-11-21 10:57:50 +000076 *
Chunosov5124be52017-11-22 20:42:13 +070077 * @param[in] a First input tensor (Matrix A). Data type supported: QASYMM8.
78 * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a
Gian Marco Iodice4b908652018-10-18 10:21:02 +010079 * @param[in] c Third input tensor (Matrix C). It can be a nullptr. Data type supported: S32
80 * @param[out] output Output tensor. Data type supported: S32 or QASYMM8 if gemm_info.gemmlowp_output_stage != NONE
Chunosov5124be52017-11-22 20:42:13 +070081 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
82 * if the reshape of matrix B should be executed only for the first run
Gian Marco05288a22017-11-21 10:57:50 +000083 */
Gian Marco Iodice4b908652018-10-18 10:21:02 +010084 void configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, const GEMMInfo &gemm_info = GEMMInfo());
Georgios Pinitas358ca202017-12-07 16:47:52 +000085 /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMLowpMatrixMultiplyCore
86 *
87 * @param[in] a First input tensor (Matrix A). Data type supported: QASYMM8.
88 * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a
Gian Marco Iodice4b908652018-10-18 10:21:02 +010089 * @param[in] c Third input tensor (Matrix C). It can be a nullptr. Data type supported: S32
90 * @param[in] output Output tensor. Data type supported: S32 or QASYMM8 if gemm_info.gemmlowp_output_stage != NONE
Georgios Pinitas358ca202017-12-07 16:47:52 +000091 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
92 * if the reshape of matrix B should be executed only for the first run
93 *
94 * @return a status
95 */
Gian Marco Iodice4b908652018-10-18 10:21:02 +010096 static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, const GEMMInfo &gemm_info = GEMMInfo());
Gian Marco05288a22017-11-21 10:57:50 +000097
98 // Inherited methods overridden:
99 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +0100100 void prepare() override;
Gian Marco05288a22017-11-21 10:57:50 +0000101
102private:
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100103 CLMemoryGroup _memory_group;
Gian Marco Iodice06be6f82019-06-24 17:47:51 +0100104 CLGEMMLowpMatrixMultiplyKernel _mm_midgard_kernel;
105 CLGEMMLowpMatrixMultiplyNativeKernel _mm_native_kernel;
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100106 CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel _mm_reshaped_only_rhs_kernel;
giuros018b6b4a92018-12-18 19:01:33 +0000107 CLGEMMReshapeRHSMatrixKernel _mtx_b_reshape_kernel;
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100108 CLGEMMLowpMatrixAReductionKernel _mtx_a_reduction_kernel;
109 CLGEMMLowpMatrixBReductionKernel _mtx_b_reduction_kernel;
110 CLGEMMLowpOffsetContributionKernel _offset_contribution_kernel;
111 CLGEMMLowpOffsetContributionOutputStageKernel _offset_contribution_output_stage_kernel;
112 CLTensor _vector_sum_col;
113 CLTensor _vector_sum_row;
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100114 CLTensor _tmp_b;
115 CLTensor _mm_result_s32;
116 const ICLTensor *_original_b;
117 int32_t _a_offset;
118 int32_t _b_offset;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000119 bool _is_gemm_reshaped;
Gian Marco Iodice06be6f82019-06-24 17:47:51 +0100120 bool _is_midgard;
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100121 bool _reshape_b_only_on_first_run;
122 bool _is_prepared;
123 bool _fuse_output_stage;
Gian Marco05288a22017-11-21 10:57:50 +0000124};
125}
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000126#endif /*__ARM_COMPUTE_CLGEMMLOWPMATRIXMULTIPLYCORE_H__ */