Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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_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 */ |
| 38 | template <typename TypeInput, typename TypeOutput> |
| 39 | class NEGEMMAssemblyDispatch : public IFunction |
| 40 | { |
| 41 | public: |
| 42 | /** Default constructor */ |
| 43 | NEGEMMAssemblyDispatch(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
| 44 | |
| 45 | /** Prevent instances of this class from being copy constructed */ |
| 46 | NEGEMMAssemblyDispatch(const NEGEMMAssemblyDispatch<TypeInput, TypeOutput> &) = delete; |
| 47 | /** Prevent instances of this class from being copied */ |
| 48 | NEGEMMAssemblyDispatch<TypeInput, TypeOutput> &operator=(const NEGEMMAssemblyDispatch<TypeInput, TypeOutput> &) = delete; |
| 49 | NEGEMMAssemblyDispatch(NEGEMMAssemblyDispatch<TypeInput, TypeOutput> &&) = default; |
| 50 | NEGEMMAssemblyDispatch<TypeInput, TypeOutput> &operator=(NEGEMMAssemblyDispatch<TypeInput, TypeOutput> &&) = default; |
| 51 | ~NEGEMMAssemblyDispatch() = default; |
| 52 | |
| 53 | private: |
| 54 | /** ACL Function */ |
| 55 | std::unique_ptr<IFunction> _function; |
| 56 | |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame^] | 57 | /** If supported create the ACL function corresponding to the GemmMethod provided to process the other passed parameters |
| 58 | * |
| 59 | * @param[in] method GemmMethod to use to perform the matrix multiplication. |
| 60 | * @param[in] a Input tensor (Matrix A). |
| 61 | * @param[in] b Input tensor (Matrix B). |
| 62 | * @param[out] d Output tensor to store the result of matrix multiplication. Data type supported: same as @p input0. |
| 63 | * @param[in] alpha Scalar multiplier to apply to AB matrix product. |
| 64 | * @param[in] beta Scalar multiplier to apply to input D matrix before adding product. |
| 65 | * @param[in] pretransposed_hint Can the B tensor can be pretransposed (ie shared across invocations)? |
| 66 | * |
| 67 | * @return True if the method is supported and the function was successfully created, false otherwise. |
| 68 | */ |
| 69 | bool create_function(arm_gemm::GemmMethod method, const ITensor *a, const ITensor *b, ITensor *d, float alpha, float beta, bool pretranspose_hint); |
| 70 | |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 71 | //Fallback: use arm_gemm's AssemblyGemm: |
| 72 | class Fallback |
| 73 | { |
| 74 | #ifndef DOXYGEN_SKIP_THIS |
| 75 | public: |
| 76 | /** Configures the arrays pointers and strides in the assembly kernel and executes the assembly kernel. |
| 77 | * The call to set_arrays is needed to deal with the input sizes containing batches (dims > 2) |
| 78 | */ |
| 79 | void run(); |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame^] | 80 | void configure(const ITensor *a, const ITensor *b, ITensor *d, arm_gemm::GemmArgs<TypeOutput> &args, MemoryGroup &memory_group); |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 81 | void prepare(); |
| 82 | bool is_configured() const; |
| 83 | #endif /* DOXYGEN_SKIP_THIS */ |
| 84 | |
| 85 | private: |
| 86 | /** Allocate a workspace tensor. |
| 87 | * |
| 88 | * @param[in] workspace_size Size to allocate. |
| 89 | * @param[in] memory_group Tensor memory group. |
| 90 | * @param[in] alignment Workspace memory alignment. |
| 91 | */ |
| 92 | void allocate_workspace(size_t workspace_size, MemoryGroup *memory_group, size_t alignment); |
| 93 | |
| 94 | /** Assembly Gemm kernel */ |
| 95 | std::unique_ptr<arm_gemm::GemmCommon<TypeInput, TypeOutput>> _gemm_kernel_asm{ nullptr }; |
| 96 | /** Optimised NEON kernel */ |
| 97 | std::unique_ptr<INEKernel> _optimised_kernel{ nullptr }; |
| 98 | /** Input A */ |
| 99 | const ITensor *_a |
| 100 | { |
| 101 | nullptr |
| 102 | }; |
| 103 | /** Input B */ |
| 104 | const ITensor *_b |
| 105 | { |
| 106 | nullptr |
| 107 | }; |
| 108 | /** Output */ |
| 109 | ITensor *_d{ nullptr }; |
| 110 | /** GEMM workspace */ |
| 111 | Tensor _workspace{}; |
| 112 | /** Pre-transpose tensor */ |
| 113 | Tensor _pretranspose{}; |
| 114 | /** Prepared flag */ |
| 115 | bool _is_prepared{ false }; |
| 116 | } _arm_gemm; /**< Fallback in case ACL doesn't have a function */ |
| 117 | MemoryGroup _memory_group; /**< Function memory group */ |
| 118 | public: |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame^] | 119 | /** If supported create an ACL function else fallback to the arm_gemm function. |
| 120 | * |
| 121 | * @param[in] a Input tensor (Matrix A) |
| 122 | * @param[in] b Input tensor (Matrix B) |
| 123 | * @param[out] d Output tensor to store the result of matrix multiplication. Data type supported: same as @p input0. |
| 124 | * @param[in] alpha Scalar multiplier to apply to AB matrix product. |
| 125 | * @param[in] beta Scalar multiplier to apply to input D matrix before adding product. |
| 126 | * @param[in] pretranspose_hint Can the B tensor can be pretransposed (ie shared across invocations)? |
| 127 | */ |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 128 | void configure(const ITensor *a, const ITensor *b, ITensor *d, float alpha, float beta, bool pretranspose_hint); |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame^] | 129 | /** Was the function successfully configured ? |
| 130 | * |
| 131 | * @return True if the function is configured and ready to run |
| 132 | */ |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 133 | bool is_configured() const; |
| 134 | // Inherited methods overridden: |
| 135 | /** Runs a preparation step, usually for pre-transposing matrix b */ |
| 136 | void prepare() override; |
| 137 | void run() override; |
| 138 | }; |
| 139 | |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame^] | 140 | /** Float 32 assembly dispatch kernel */ |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 141 | using NEGEMMAssemblyDispatchF32 = NEGEMMAssemblyDispatch<float, float>; |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame^] | 142 | /** Uint 8 to Uint 32 assembly dispatch kernel */ |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 143 | using NEGEMMAssemblyDispatchU8U32 = NEGEMMAssemblyDispatch<uint8_t, uint32_t>; |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame^] | 144 | /** Int 8 to Int 32 assembly dispatch kernel */ |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 145 | using NEGEMMAssemblyDispatchS8S32 = NEGEMMAssemblyDispatch<int8_t, int32_t>; |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame^] | 146 | } // namespace arm_compute |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 147 | #endif /* __ARM_COMPUTE_NEGEMMASSEMBLYDISPATCH_H__ */ |