blob: 01720f05fac2a5792e0b677fe626adb92d50259d [file] [log] [blame]
Gian Marco Iodiceab182122017-10-09 15:05:40 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Gian Marco Iodiceab182122017-10-09 15:05:40 +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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_NEGEMMLOWPMATRIXMULTIPLYCORE_H
25#define ARM_COMPUTE_NEGEMMLOWPMATRIXMULTIPLYCORE_H
Gian Marco Iodiceab182122017-10-09 15:05:40 +010026
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010027#include "NEActivationLayer.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010028#include "arm_compute/core/NEON/INEKernel.h"
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010029#include "arm_compute/core/NEON/kernels/NEConvertQuantizedSignednessKernel.h"
30#include "arm_compute/core/NEON/kernels/NEConvertQuantizedSignednessKernel.h"
SiCongLi2e5fd632020-03-02 15:39:15 +000031#include "arm_compute/core/NEON/kernels/NEGEMMInterleave4x4Kernel.h"
32#include "arm_compute/core/NEON/kernels/NEGEMMLowpMatrixMultiplyKernel.h"
Gian Marcoe75a02b2017-11-08 12:24:09 +000033#include "arm_compute/core/NEON/kernels/NEGEMMLowpOffsetContributionKernel.h"
George Wort2d7e6832019-02-22 16:37:41 +000034#include "arm_compute/core/NEON/kernels/NEGEMMLowpOffsetContributionOutputStageKernel.h"
Gian Marcoe75a02b2017-11-08 12:24:09 +000035#include "arm_compute/core/NEON/kernels/NEGEMMLowpReductionKernel.h"
SiCongLi2e5fd632020-03-02 15:39:15 +000036#include "arm_compute/core/NEON/kernels/NEGEMMTranspose1xWKernel.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010037#include "arm_compute/runtime/IFunction.h"
38#include "arm_compute/runtime/IMemoryManager.h"
39#include "arm_compute/runtime/MemoryGroup.h"
Anthony Barbier71d9b572018-07-06 17:05:59 +010040#include "arm_compute/runtime/NEON/functions/NEGEMMAssemblyDispatch.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010041#include "arm_compute/runtime/Tensor.h"
42
43#include <memory>
44
45namespace arm_compute
46{
47class ITensor;
48
49/** Basic function to execute GEMMLowpMatrixMultiplyCore on NEON. This function calls the following NEON kernels if the DOT product instruction is not available:
50 *
51 * -# @ref NEGEMMInterleave4x4Kernel
52 * -# @ref NEGEMMTranspose1xWKernel
53 * -# @ref NEGEMMLowpMatrixMultiplyKernel
Gian Marcoe75a02b2017-11-08 12:24:09 +000054 * -# @ref NEGEMMLowpOffsetContributionKernel
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010055 * -# @ref NEActivationLayer
Gian Marco Iodiceab182122017-10-09 15:05:40 +010056 *
57 * otherwise if the DOT product instruction is available:
58 *
Gian Marcoe75a02b2017-11-08 12:24:09 +000059 * -# @ref NEGEMMLowpOffsetContributionKernel
Gian Marco Iodiceab182122017-10-09 15:05:40 +010060 *
61*/
62class NEGEMMLowpMatrixMultiplyCore : public IFunction
63{
64public:
65 /** Constructor */
SiCongLi2e5fd632020-03-02 15:39:15 +000066 NEGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
Georgios Pinitas72219332018-06-05 14:56:06 +010067 /** Prevent instances of this class from being copied (As this class contains pointers) */
68 NEGEMMLowpMatrixMultiplyCore(const NEGEMMLowpMatrixMultiplyCore &) = delete;
69 /** Default move constructor */
70 NEGEMMLowpMatrixMultiplyCore(NEGEMMLowpMatrixMultiplyCore &&) = default;
71 /** Prevent instances of this class from being copied (As this class contains pointers) */
72 NEGEMMLowpMatrixMultiplyCore &operator=(const NEGEMMLowpMatrixMultiplyCore &) = delete;
73 /** Default move assignment operator */
74 NEGEMMLowpMatrixMultiplyCore &operator=(NEGEMMLowpMatrixMultiplyCore &&) = default;
Gian Marco Iodiceab182122017-10-09 15:05:40 +010075 /** Initialise the kernel's inputs, output
Anthony Barbierf202e502017-11-23 18:02:04 +000076 *
77 * @note GEMM_LOWP: low precision GEMM kernel
78 * This kernel performs the following computations:
79 *
80 * -# Convert a values from QASYMM8 to int32 and add a_offset to each of them.
81 * -# Convert b values from QASYMM8 to int32 add b_offset to each of them.
82 * -# Compute the matrix product of the resulting a * b in int32.
83 *
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010084 * @note The @p output type is S32 if @p gemm_info.type == GEMMLowpOutputStageType::NONE. It is QASYMM8/QASYMM8_SIGNED otherwise
George Wort2d7e6832019-02-22 16:37:41 +000085 *
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010086 * @param[in] a First input tensor (Matrix A). Data type supported: QASYMM8/QASYMM8_SIGNED.
Michele Di Giorgio47a89902020-03-09 19:32:33 +000087 * @param[in] b Second input tensor (Matrix B). Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8/QSYMM8_PER_CHANNEL.
Gian Marco Iodice4b908652018-10-18 10:21:02 +010088 * @param[in] c Third input tensor (Matrix C). It can be a nullptr. Data type supported: S32
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010089 * @param[out] output Output tensor. Data type supported: Data type supported: S32/QASYMM8/QASYMM8_SIGNED
Chunosov5124be52017-11-22 20:42:13 +070090 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
91 * if the reshape of matrix B should be executed only for the first run
Anthony Barbierf202e502017-11-23 18:02:04 +000092 */
Gian Marco Iodice4b908652018-10-18 10:21:02 +010093 void configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *output, const GEMMInfo &gemm_info = GEMMInfo());
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000094 /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMLowpMatrixMultiplyCore
95 *
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010096 * @note The @p output type is S32 if @p gemm_info.type == GEMMLowpOutputStageType::NONE. It is QASYMM8/QASYMM8_SIGNED otherwise
George Wort2d7e6832019-02-22 16:37:41 +000097 *
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010098 * @param[in] a First input tensor info (Matrix A). Data type supported: QASYMM8/QASYMM8_SIGNED.
Michele Di Giorgio47a89902020-03-09 19:32:33 +000099 * @param[in] b Second input tensor info (Matrix B). Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8/QSYMM8_PER_CHANNEL.
George Wort2d7e6832019-02-22 16:37:41 +0000100 * @param[in] c Third input tensor info (Matrix C). It can be a nullptr. Data type supported: S32
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100101 * @param[in] output Output tensor info. Data type supported: Data type supported: S32/QASYMM8/QASYMM8_SIGNED
Georgios Pinitas358ca202017-12-07 16:47:52 +0000102 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
103 * if the reshape of matrix B should be executed only for the first run
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000104 *
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000105 * @return a status
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000106 */
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100107 static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, const GEMMInfo &gemm_info = GEMMInfo());
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100108
Pablo Tellofc004492018-03-23 11:40:05 +0000109 // Inherited methods overridden
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100110 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +0100111 void prepare() override;
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100112
113private:
George Wort2d7e6832019-02-22 16:37:41 +0000114 MemoryGroup _memory_group;
SiCongLi2e5fd632020-03-02 15:39:15 +0000115 IWeightsManager *_weights_manager;
George Wort2d7e6832019-02-22 16:37:41 +0000116 NEGEMMAssemblyDispatch _asm_glue;
SiCongLi2e5fd632020-03-02 15:39:15 +0000117 NEGEMMLowpMatrixMultiplyKernel _mm_kernel;
118 NEGEMMInterleave4x4Kernel _mtx_a_reshape_kernel;
119 NEGEMMTranspose1xWKernel _mtx_b_reshape_kernel;
George Wort2d7e6832019-02-22 16:37:41 +0000120 NEGEMMLowpMatrixAReductionKernel _mtx_a_reduction_kernel;
121 NEGEMMLowpMatrixBReductionKernel _mtx_b_reduction_kernel;
122 NEGEMMLowpOffsetContributionKernel _offset_contribution_kernel;
123 NEGEMMLowpOffsetContributionOutputStageKernel _offset_contribution_output_stage_kernel;
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100124 NEActivationLayer _activation_func;
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100125 NEConvertQuantizedSignednessKernel _convert_to_signed_asymm;
126 NEConvertQuantizedSignednessKernel _convert_from_signed_asymm;
127
128 Tensor _vector_sum_col;
129 Tensor _vector_sum_row;
130 Tensor _tmp_a;
131 Tensor _tmp_b;
132 Tensor _mm_result_s32;
133 Tensor _signed_a;
134 Tensor _signed_output;
135 const ITensor *_original_b;
136 int32_t _a_offset;
137 int32_t _b_offset;
138
139 bool _run_vector_matrix_multiplication;
140 bool _assembly_path;
141 bool _fused_assembly_path;
142 bool _reshape_b_only_on_first_run;
143 bool _is_prepared;
144 bool _fuse_output_stage;
145 bool _run_activation;
146 bool _flip_signedness;
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100147};
George Wort2d7e6832019-02-22 16:37:41 +0000148} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000149#endif /*ARM_COMPUTE_NEGEMMLOWPMATRIXMULTIPLYCORE_H */