Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 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_NEGEMMASSEMBLYDISPATCH_H__ |
| 25 | #define __ARM_COMPUTE_NEGEMMASSEMBLYDISPATCH_H__ |
| 26 | |
| 27 | #include "arm_compute/core/NEON/kernels/assembly/NEGEMMAssemblyWrapperKernel.h" |
| 28 | #include "arm_compute/runtime/IFunction.h" |
| 29 | #include "arm_compute/runtime/IMemoryManager.h" |
| 30 | #include "arm_compute/runtime/MemoryGroup.h" |
| 31 | #include "arm_compute/runtime/Tensor.h" |
| 32 | |
| 33 | #include "arm_compute/core/NEON/kernels/assembly/arm_gemm.hpp" |
| 34 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | /** Assembly kernel glue */ |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 38 | class NEGEMMAssemblyDispatch : public IFunction |
| 39 | { |
| 40 | public: |
| 41 | /** Default constructor */ |
| 42 | NEGEMMAssemblyDispatch(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
| 43 | |
| 44 | /** Prevent instances of this class from being copy constructed */ |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 45 | NEGEMMAssemblyDispatch(const NEGEMMAssemblyDispatch &) = delete; |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 46 | /** Prevent instances of this class from being copied */ |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 47 | NEGEMMAssemblyDispatch &operator=(const NEGEMMAssemblyDispatch &) = delete; |
| 48 | NEGEMMAssemblyDispatch(NEGEMMAssemblyDispatch &&) = default; |
| 49 | NEGEMMAssemblyDispatch &operator=(NEGEMMAssemblyDispatch &&) = default; |
| 50 | ~NEGEMMAssemblyDispatch() = default; |
| 51 | |
| 52 | class IFallback |
| 53 | { |
| 54 | public: |
| 55 | virtual void run() = 0; |
| 56 | virtual void prepare() = 0; |
| 57 | virtual bool is_configured() const = 0; |
| 58 | virtual ~IFallback() = default; |
| 59 | }; |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 60 | |
| 61 | private: |
| 62 | /** ACL Function */ |
| 63 | std::unique_ptr<IFunction> _function; |
| 64 | |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 65 | /** If supported create the ACL function corresponding to the GemmMethod provided to process the other passed parameters |
| 66 | * |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 67 | * @param[in] method GemmMethod to use to perform the matrix multiplication. |
| 68 | * @param[in] a Input tensor (Matrix A). |
| 69 | * @param[in] b Input tensor (Matrix B). |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 70 | * @param[in] c Input tensor (Matrix C) used to pass the bias for quantized calculations |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 71 | * @param[out] d Output tensor to store the result of matrix multiplication. Data type supported: same as @p input0. |
| 72 | * @param[in] alpha Scalar multiplier to apply to AB matrix product. |
| 73 | * @param[in] beta Scalar multiplier to apply to input D matrix before adding product. |
| 74 | * @param[in] gemm_info GEMM meta-data |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 75 | * |
| 76 | * @return True if the method is supported and the function was successfully created, false otherwise. |
| 77 | */ |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 78 | bool create_function(arm_gemm::GemmMethod method, const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, float alpha, float beta, const GEMMInfo &gemm_info); |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 79 | |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 80 | /** Interface for the arm_gemm fallback */ |
Anthony Barbier | 3d677cc | 2018-07-23 16:42:59 +0100 | [diff] [blame] | 81 | std::unique_ptr<IFallback> _arm_gemm; |
| 82 | MemoryGroup _memory_group; /**< Function memory group */ |
| 83 | std::shared_ptr<IMemoryManager> _memory_manager; /**< Copy of the memory manager used to create the memory group to be used when instantiating new functions */ |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 84 | public: |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 85 | /** If supported create an ACL function else fallback to the arm_gemm function. |
| 86 | * |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 87 | * @param[in] a Input tensor (Matrix A) |
| 88 | * @param[in] b Input tensor (Matrix B) |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 89 | * @param[in] c Input tensor (Matrix C) used to pass the bias for quantized calculations |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 90 | * @param[out] d Output tensor to store the result of matrix multiplication. Data type supported: same as @p input0. |
| 91 | * @param[in] alpha Scalar multiplier to apply to AB matrix product. |
| 92 | * @param[in] beta Scalar multiplier to apply to input D matrix before adding product. |
| 93 | * @param[in] gemm_info GEMM meta-data |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 94 | */ |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 95 | void configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, float alpha, float beta, const GEMMInfo &gemm_info); |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 96 | |
| 97 | /** Indicates whether or not this function can be used to process the given parameters. |
| 98 | * |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 99 | * @param[in] a Input tensor info (Matrix A) |
| 100 | * @param[in] b Input tensor info (Matrix B) |
| 101 | * @param[in] c Input tensor info (Matrix C) used to pass the bias for quantized calculations |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 102 | * @param[in] d Output tensor to store the result of matrix multiplication. Data type supported: same as @p input0. |
| 103 | * @param[in] alpha Scalar multiplier to apply to AB matrix product. |
| 104 | * @param[in] beta Scalar multiplier to apply to input D matrix before adding product. |
| 105 | * @param[in] gemm_info GEMM meta-data |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 106 | * |
| 107 | * @return a status. |
| 108 | */ |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 109 | static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *d, float alpha, float beta, const GEMMInfo &gemm_info); |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 110 | /** Was the function successfully configured ? |
| 111 | * |
| 112 | * @return True if the function is configured and ready to run |
| 113 | */ |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 114 | bool is_configured() const; |
| 115 | // Inherited methods overridden: |
| 116 | /** Runs a preparation step, usually for pre-transposing matrix b */ |
| 117 | void prepare() override; |
| 118 | void run() override; |
| 119 | }; |
| 120 | |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 121 | } // namespace arm_compute |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 122 | #endif /* __ARM_COMPUTE_NEGEMMASSEMBLYDISPATCH_H__ */ |