1 /* 2 * Copyright (c) 2018-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_NEGEMMASSEMBLYDISPATCH_H 25 #define ARM_COMPUTE_NEGEMMASSEMBLYDISPATCH_H 26 27 #include "arm_compute/runtime/IFunction.h" 28 #include "arm_compute/runtime/IMemoryManager.h" 29 #include "arm_compute/runtime/IWeightsManager.h" 30 #include "arm_compute/runtime/MemoryGroup.h" 31 #include "arm_compute/runtime/Tensor.h" 32 33 namespace arm_compute 34 { 35 /* Convolution method supported by the assembly gemm interface */ 36 enum class AsmConvMethod 37 { 38 Im2Col, 39 Indirect, 40 Conv 41 }; 42 43 struct AsmGemmInfo 44 { 45 AsmConvMethod method{ AsmConvMethod::Im2Col }; 46 PadStrideInfo ps_info{}; 47 ActivationLayerInfo activation_info{}; 48 GEMMLowpOutputStageInfo output_stage{}; 49 bool negated_offsets{ true }; 50 bool reinterpret_input_as_3d{ false }; 51 bool depth_output_gemm3d{ false }; 52 int64_t padding_top{ 0 }; 53 int64_t padding_left{ 0 }; 54 float padding_value{ 0.f }; 55 }; 56 57 /** Assembly kernel glue */ 58 class NEGEMMAssemblyDispatch : public IFunction 59 { 60 public: 61 /** Constructor */ 62 NEGEMMAssemblyDispatch(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr); 63 /** Prevent instances of this class from being copy constructed */ 64 NEGEMMAssemblyDispatch(const NEGEMMAssemblyDispatch &) = delete; 65 /** Prevent instances of this class from being copied */ 66 NEGEMMAssemblyDispatch &operator=(const NEGEMMAssemblyDispatch &) = delete; 67 NEGEMMAssemblyDispatch(NEGEMMAssemblyDispatch &&) = default; 68 NEGEMMAssemblyDispatch &operator=(NEGEMMAssemblyDispatch &&) = default; 69 ~NEGEMMAssemblyDispatch() = default; 70 71 class IFallback 72 { 73 public: 74 virtual void run() = 0; 75 virtual void prepare() = 0; 76 virtual bool is_configured() const = 0; 77 virtual ~IFallback() = default; 78 }; 79 80 public: 81 /** If supported create a Compute Library function else fallback to the arm_gemm function. 82 * 83 * @param[in] a Input tensor (Matrix A) 84 * @param[in] b Input tensor (Matrix B) 85 * @param[in] c Input tensor (Matrix C) used to pass the bias for quantized calculations 86 * @param[out] d Output tensor to store the result of matrix multiplication. Data type supported: same as @p input0. 87 * @param[in] info GEMM meta-data 88 */ 89 void configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, const AsmGemmInfo &info); 90 91 /** Indicates whether or not this function can be used to process the given parameters. 92 * 93 * @param[in] a Input tensor info (Matrix A) 94 * @param[in] b Input tensor info (Matrix B) 95 * @param[in] c Input tensor info (Matrix C) used to pass the bias for quantized calculations 96 * @param[in] d Output tensor to store the result of matrix multiplication. Data type supported: same as @p input0. 97 * @param[in] info GEMM meta-data 98 * 99 * @return a status. 100 */ 101 static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *d, const AsmGemmInfo &info); 102 /** Checks if activation is supported by the gemm assembly dispatcher 103 * 104 * @param[in] activation Activation to check 105 * 106 * @return True if activation is supported else false 107 */ 108 static bool is_activation_supported(const ActivationLayerInfo &activation); 109 /** Was the function successfully configured ? 110 * 111 * @return True if the function is configured and ready to run 112 */ 113 bool is_configured() const; 114 115 // Inherited methods overridden: 116 void prepare() override; 117 void run() override; 118 119 private: 120 std::unique_ptr<IFallback> _arm_gemm; /** Interface for the arm_gemm fallback */ 121 MemoryGroup _memory_group; /**< Function memory group */ 122 IWeightsManager *_weights_manager; /**< Pointer to the weights manager */ 123 }; 124 } // namespace arm_compute 125 #endif /* ARM_COMPUTE_NEGEMMASSEMBLYDISPATCH_H */ 126