blob: 12c120934ecea01a4c5893288a7370fc55efaee9 [file] [log] [blame]
Gian Marco Iodiceab182122017-10-09 15:05:40 +01001/*
George Wort2d7e6832019-02-22 16:37:41 +00002 * Copyright (c) 2017-2019 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 */
24#ifndef __ARM_COMPUTE_NEGEMMLOWPMATRIXMULTIPLYCORE_H__
25#define __ARM_COMPUTE_NEGEMMLOWPMATRIXMULTIPLYCORE_H__
26
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"
Gian Marcoe75a02b2017-11-08 12:24:09 +000029#include "arm_compute/core/NEON/kernels/NEGEMMLowpOffsetContributionKernel.h"
George Wort2d7e6832019-02-22 16:37:41 +000030#include "arm_compute/core/NEON/kernels/NEGEMMLowpOffsetContributionOutputStageKernel.h"
Gian Marcoe75a02b2017-11-08 12:24:09 +000031#include "arm_compute/core/NEON/kernels/NEGEMMLowpReductionKernel.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010032#include "arm_compute/runtime/IFunction.h"
33#include "arm_compute/runtime/IMemoryManager.h"
34#include "arm_compute/runtime/MemoryGroup.h"
Anthony Barbier71d9b572018-07-06 17:05:59 +010035#include "arm_compute/runtime/NEON/functions/NEGEMMAssemblyDispatch.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010036#include "arm_compute/runtime/Tensor.h"
37
38#include <memory>
39
40namespace arm_compute
41{
42class ITensor;
43
44/** Basic function to execute GEMMLowpMatrixMultiplyCore on NEON. This function calls the following NEON kernels if the DOT product instruction is not available:
45 *
46 * -# @ref NEGEMMInterleave4x4Kernel
47 * -# @ref NEGEMMTranspose1xWKernel
48 * -# @ref NEGEMMLowpMatrixMultiplyKernel
Gian Marcoe75a02b2017-11-08 12:24:09 +000049 * -# @ref NEGEMMLowpOffsetContributionKernel
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010050 * -# @ref NEActivationLayer
Gian Marco Iodiceab182122017-10-09 15:05:40 +010051 *
52 * otherwise if the DOT product instruction is available:
53 *
Gian Marcoe75a02b2017-11-08 12:24:09 +000054 * -# @ref NEGEMMLowpOffsetContributionKernel
Gian Marco Iodiceab182122017-10-09 15:05:40 +010055 *
56*/
57class NEGEMMLowpMatrixMultiplyCore : public IFunction
58{
59public:
60 /** Constructor */
61 NEGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Georgios Pinitas72219332018-06-05 14:56:06 +010062 /** Prevent instances of this class from being copied (As this class contains pointers) */
63 NEGEMMLowpMatrixMultiplyCore(const NEGEMMLowpMatrixMultiplyCore &) = delete;
64 /** Default move constructor */
65 NEGEMMLowpMatrixMultiplyCore(NEGEMMLowpMatrixMultiplyCore &&) = default;
66 /** Prevent instances of this class from being copied (As this class contains pointers) */
67 NEGEMMLowpMatrixMultiplyCore &operator=(const NEGEMMLowpMatrixMultiplyCore &) = delete;
68 /** Default move assignment operator */
69 NEGEMMLowpMatrixMultiplyCore &operator=(NEGEMMLowpMatrixMultiplyCore &&) = default;
Gian Marco Iodiceab182122017-10-09 15:05:40 +010070 /** Initialise the kernel's inputs, output
Anthony Barbierf202e502017-11-23 18:02:04 +000071 *
72 * @note GEMM_LOWP: low precision GEMM kernel
73 * This kernel performs the following computations:
74 *
75 * -# Convert a values from QASYMM8 to int32 and add a_offset to each of them.
76 * -# Convert b values from QASYMM8 to int32 add b_offset to each of them.
77 * -# Compute the matrix product of the resulting a * b in int32.
78 *
George Wort2d7e6832019-02-22 16:37:41 +000079 * @note The @p output type is S32 if @p gemm_info.type == GEMMLowpOutputStageType::NONE. It is QASYMM8 otherwise
80 *
Chunosov5124be52017-11-22 20:42:13 +070081 * @param[in] a First input tensor (Matrix A). Data type supported: QASYMM8.
82 * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a
Gian Marco Iodice4b908652018-10-18 10:21:02 +010083 * @param[in] c Third input tensor (Matrix C). It can be a nullptr. Data type supported: S32
George Wort2d7e6832019-02-22 16:37:41 +000084 * @param[out] output Output tensor. Data type supported: Data type supported: S32/QASYMM8
Chunosov5124be52017-11-22 20:42:13 +070085 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
86 * if the reshape of matrix B should be executed only for the first run
Anthony Barbierf202e502017-11-23 18:02:04 +000087 */
Gian Marco Iodice4b908652018-10-18 10:21:02 +010088 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 +000089 /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMLowpMatrixMultiplyCore
90 *
George Wort2d7e6832019-02-22 16:37:41 +000091 * @note The @p output type is S32 if @p gemm_info.type == GEMMLowpOutputStageType::NONE. It is QASYMM8 otherwise
92 *
93 * @param[in] a First input tensor info (Matrix A). Data type supported: QASYMM8.
94 * @param[in] b Second input tensor info (Matrix B). Data type supported: same as @p a
95 * @param[in] c Third input tensor info (Matrix C). It can be a nullptr. Data type supported: S32
96 * @param[in] output Output tensor info. Data type supported: Data type supported: S32/QASYMM8
Georgios Pinitas358ca202017-12-07 16:47:52 +000097 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
98 * if the reshape of matrix B should be executed only for the first run
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000099 *
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000100 * @return a status
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000101 */
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100102 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 +0100103
Pablo Tellofc004492018-03-23 11:40:05 +0000104 // Inherited methods overridden
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100105 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +0100106 void prepare() override;
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100107
108private:
George Wort2d7e6832019-02-22 16:37:41 +0000109 MemoryGroup _memory_group;
110 NEGEMMAssemblyDispatch _asm_glue;
111 std::unique_ptr<INEKernel> _mm_kernel;
112 std::unique_ptr<INEKernel> _mtx_a_reshape_kernel;
113 std::unique_ptr<INEKernel> _mtx_b_reshape_kernel;
114 NEGEMMLowpMatrixAReductionKernel _mtx_a_reduction_kernel;
115 NEGEMMLowpMatrixBReductionKernel _mtx_b_reduction_kernel;
116 NEGEMMLowpOffsetContributionKernel _offset_contribution_kernel;
117 NEGEMMLowpOffsetContributionOutputStageKernel _offset_contribution_output_stage_kernel;
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100118 NEActivationLayer _activation_func;
George Wort2d7e6832019-02-22 16:37:41 +0000119 Tensor _vector_sum_col;
120 Tensor _vector_sum_row;
121 Tensor _tmp_a;
122 Tensor _tmp_b;
123 Tensor _mm_result_s32;
124 const ITensor *_original_b;
125 int32_t _a_offset;
126 int32_t _b_offset;
127 bool _run_vector_matrix_multiplication;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100128 bool _assembly_path;
129 bool _fused_assembly_path;
George Wort2d7e6832019-02-22 16:37:41 +0000130 bool _reshape_b_only_on_first_run;
131 bool _is_prepared;
132 bool _fuse_output_stage;
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100133 bool _run_activation;
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100134};
George Wort2d7e6832019-02-22 16:37:41 +0000135} // namespace arm_compute
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100136#endif /*__ARM_COMPUTE_NEGEMMLOWPMATRIXMULTIPLYCORE_H__ */