1 /* 2 * Copyright (c) 2017-2020 Arm Limited. 3 * 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_NEGEMMMATRIXMULTIPLYKERNEL_H 25 #define ARM_COMPUTE_NEGEMMMATRIXMULTIPLYKERNEL_H 26 27 #include "src/core/NEON/INEKernel.h" 28 29 namespace arm_compute 30 { 31 class ITensor; 32 33 /** NEON kernel to multiply two input matrices "A" and "B". All elements of the output matrix/vector will be multiplied by alpha after the matrix multiplication 34 * 35 * @note If the output tensor is a matrix, the implementation assumes that the input tensors @p input0 and @p input1 are both matrices and reshaped respectively with @ref NEGEMMInterleave4x4Kernel" and @ref NEGEMMTranspose1xWKernel 36 * @note If the output tensor is a vector and the data type is F32, the implementation assumes that the first input tensor @p input0 is a vector and the second input tensor @p input1 a matrix. The implementation also assumes that both tensors have not been reshaped 37 * 38 */ 39 class NEGEMMMatrixMultiplyKernel : public INEKernel 40 { 41 public: name()42 const char *name() const override 43 { 44 return "NEGEMMMatrixMultiplyKernel"; 45 } 46 /** Constructor */ 47 NEGEMMMatrixMultiplyKernel(); 48 /** Prevent instances of this class from being copied (As this class contains pointers) */ 49 NEGEMMMatrixMultiplyKernel(const NEGEMMMatrixMultiplyKernel &) = delete; 50 /** Prevent instances of this class from being copied (As this class contains pointers) */ 51 NEGEMMMatrixMultiplyKernel &operator=(const NEGEMMMatrixMultiplyKernel &) = delete; 52 /** Allow instances of this class to be moved */ 53 NEGEMMMatrixMultiplyKernel(NEGEMMMatrixMultiplyKernel &&) = default; 54 /** Allow instances of this class to be moved */ 55 NEGEMMMatrixMultiplyKernel &operator=(NEGEMMMatrixMultiplyKernel &&) = default; 56 /** Initialise the kernel's input and output. 57 * 58 * @note If the output tensor is a matrix, the input matrices @p input0 and @p input1 should be the output of the kernels: @ref NEGEMMInterleave4x4Kernel and @ref NEGEMMTranspose1xWKernel 59 * These two kernels change the layout of the original matrices to be more cache-friendly. 60 * 61 * @param[in] input0 Input tensor containing the interleaved Matrix A or the vector A. Data types supported: F16/F32 62 * @param[in] input1 Input tensor containing the transposed Matrix B if the first input tensor A is not a vector. 63 * If the output tensor is a vector, input1 must contain the matrix B not reshaped. Data type supported: same as @p input0 64 * @param[out] output Output tensor to store the result of matrix multiplication. Data type supported: same as @p input0. 65 * @param[in] alpha Weight of the matrix product 66 * @param[in] is_interleaved (Optional) True if input0 and input1 have been reshaped respectively using @ref NEGEMMInterleave4x4Kernel and @ref NEGEMMTranspose1xWKernel 67 * @param[in] reshape_info (Optional) GEMM reshape info. If is_interleaved_transposed = true, this object must contain the information to understand how the matrix A and matrix B have been reshaped 68 */ 69 void configure(const ITensor *input0, const ITensor *input1, ITensor *output, float alpha, bool is_interleaved, const GEMMReshapeInfo &reshape_info = GEMMReshapeInfo()); 70 /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMMatrixMultiplyKernel 71 * 72 * @param[in] input0 Input tensor containing the interleaved Matrix A or the vector A. Data types supported: F16/F32 73 * @param[in] input1 Input tensor containing the transposed Matrix B if the first input tensor A is not a vector. 74 * If the output tensor is a vector, input1 must contain the matrix B not reshaped. Data type supported: same as @p input0 75 * @param[in] output Output tensor to store the result of matrix multiplication. Data type supported: same as @p input0. 76 * @param[in] alpha Weight of the matrix product 77 * @param[in] is_interleaved (Optional) True if input0 and input1 have been reshaped respectively using @ref NEGEMMInterleave4x4Kernel and @ref NEGEMMTranspose1xWKernel 78 * @param[in] reshape_info (Optional) GEMM reshape info. If is_interleaved_transposed = true, this object must contain the information to understand how the matrix A and matrix B have been reshaped 79 * 80 * @return a status 81 */ 82 static Status validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output, float alpha, bool is_interleaved, const GEMMReshapeInfo &reshape_info); 83 84 // Inherited methods overridden: 85 void run(const Window &window, const ThreadInfo &info) override; 86 87 private: 88 const ITensor *_input0; 89 const ITensor *_input1; 90 ITensor *_output; 91 float _alpha; 92 }; 93 } // namespace arm_compute 94 #endif /*ARM_COMPUTE_NEGEMMMATRIXMULTIPLYKERNEL_H*/ 95