| 1 |
/* |
| 2 |
* Copyright (c) 2017-2024 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 ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NEGEMMCONVOLUTIONLAYER_H |
| 25 |
#define ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NEGEMMCONVOLUTIONLAYER_H |
| 26 |
|
| 27 |
#include "arm_compute/core/Types.h" |
| 28 |
#include "arm_compute/function_info/ActivationLayerInfo.h" |
| 29 |
#include "arm_compute/runtime/IFunction.h" |
| 30 |
#include "arm_compute/runtime/IMemoryManager.h" |
| 31 |
#include "arm_compute/runtime/IWeightsManager.h" |
| 32 |
#include "arm_compute/runtime/MemoryGroup.h" |
| 33 |
#include "arm_compute/runtime/MemoryManagerOnDemand.h" |
| 34 |
|
| 35 |
#include <memory> |
| 36 |
|
| 37 |
namespace arm_compute |
| 38 |
{ |
| 39 |
class ITensor; |
| 40 |
class ITensorInfo; |
| 41 |
|
| 42 |
/** Basic function to compute the convolution layer. This function calls the following kernels/functions: |
| 43 |
* |
| 44 |
* -# cpu::CpuGemmConv2d |
| 45 |
* |
| 46 |
*/ |
| 47 |
class NEGEMMConvolutionLayer : public IFunction |
| 48 |
{ |
| 49 |
public: |
| 50 |
/** Constructor */ |
| 51 |
NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager, |
| 52 |
IWeightsManager *weights_manager = nullptr); |
| 53 |
NEGEMMConvolutionLayer() : NEGEMMConvolutionLayer(MemoryManagerOnDemand::make_default()) |
| 54 |
{ |
| 55 |
} |
| 56 |
/** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 57 |
NEGEMMConvolutionLayer(const NEGEMMConvolutionLayer &) = delete; |
| 58 |
/** Prevent instances of this class from being moved (As this class contains non movable objects) */ |
| 59 |
NEGEMMConvolutionLayer(NEGEMMConvolutionLayer &&) = delete; |
| 60 |
/** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 61 |
NEGEMMConvolutionLayer &operator=(const NEGEMMConvolutionLayer &) = delete; |
| 62 |
/** Prevent instances of this class from being moved (As this class contains non movable objects) */ |
| 63 |
NEGEMMConvolutionLayer &operator=(NEGEMMConvolutionLayer &&) = delete; |
| 64 |
/** Default destructor */ |
| 65 |
~NEGEMMConvolutionLayer(); |
| 66 |
/** Set the input and output tensors. |
| 67 |
* |
| 68 |
* Valid data layouts: |
| 69 |
* - NHWC |
| 70 |
* - NCHW |
| 71 |
* |
| 72 |
* Valid data type configurations: |
| 73 |
* |src0 |src1 |src2 |dst | |
| 74 |
* |:--------------|:------------------|:--------|:--------------| |
| 75 |
* |F16 |F16 |F16 |F16 | |
| 76 |
* |F32 |F32 |F32 |F32 | |
| 77 |
* |BFLOAT16 |BFLOAT16 |BFLOAT16 |BFLOAT16 | |
| 78 |
* |QASYMM8 |QASYMM8 |S32 |QASYMM8 | |
| 79 |
* |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |QASYMM8 | |
| 80 |
* |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED | |
| 81 |
* |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |QASYMM8_SIGNED | |
| 82 |
* |
| 83 |
* @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 84 |
* while every optional dimension from 4 and above represent a batch of inputs. |
| 85 |
* Data types supported: QASYMM8/QASYMM8_SIGNED/BFLOAT16/F16/F32. |
| 86 |
* @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. |
| 87 |
* Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/BFLOAT16/F16/F32. |
| 88 |
* @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
| 89 |
* Data type supported: Should match @p input data type, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type. |
| 90 |
* @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 91 |
* Data types supported: Same as @p input. |
| 92 |
* @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 93 |
* @param[in] weights_info Specifies if the weights tensor has been reshaped with NEWeightsReshapeKernel. If this is not part of the fully connected layer the weights |
| 94 |
* tensor has also been transposed with cpu::kernels::CpuGemmTranspose1xWKernel. Data type supported: Same as @p input. |
| 95 |
* @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
| 96 |
* @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported. |
| 97 |
* @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation |
| 98 |
* available which may introduce a drop of accuracy as well. Default is false |
| 99 |
* @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is not supported |
| 100 |
*/ |
| 101 |
void configure(const ITensor *input, |
| 102 |
const ITensor *weights, |
| 103 |
const ITensor *biases, |
| 104 |
ITensor *output, |
| 105 |
const PadStrideInfo &conv_info, |
| 106 |
const WeightsInfo &weights_info = WeightsInfo(), |
| 107 |
const Size2D &dilation = Size2D(1U, 1U), |
| 108 |
const ActivationLayerInfo &act_info = ActivationLayerInfo(), |
| 109 |
bool enable_fast_math = false, |
| 110 |
unsigned int num_groups = 1); |
| 111 |
/** Static function to check if given info will lead to a valid configuration of @ref NEGEMMConvolutionLayer |
| 112 |
* |
| 113 |
* @param[in] input Source tensor info. 3 lower dimensions represent a single input [width, height, IFM], |
| 114 |
* while every optional dimension from 4 and above represent a batch of inputs. |
| 115 |
* Data types supported: QASYMM8/QASYMM8_SIGNED/BFLOAT16/F16/F32. |
| 116 |
* @param[in] weights Weights tensor info. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. |
| 117 |
* Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/BFLOAT16/F16/F32. |
| 118 |
* @param[in] biases Biases tensor info. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
| 119 |
* Data type supported: Should match @p input data type, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type. |
| 120 |
* @param[in] output Destination tensor info. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 121 |
* Data types supported: Same as @p input. |
| 122 |
* @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 123 |
* @param[in] weights_info Specifies if the weights tensor has been reshaped with NEWeightsReshapeKernel. If this is not part of the fully connected layer the weights |
| 124 |
* tensor has also been transposed with cpu::kernels::CpuGemmTranspose1xWKernel. Data type supported: Same as @p input. |
| 125 |
* @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
| 126 |
* @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported. |
| 127 |
* @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation |
| 128 |
* available which may introduce a drop of accuracy as well. Default is false |
| 129 |
* @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is not supported |
| 130 |
* |
| 131 |
* @return a status |
| 132 |
*/ |
| 133 |
static Status validate(const ITensorInfo *input, |
| 134 |
const ITensorInfo *weights, |
| 135 |
const ITensorInfo *biases, |
| 136 |
const ITensorInfo *output, |
| 137 |
const PadStrideInfo &conv_info, |
| 138 |
const WeightsInfo &weights_info = WeightsInfo(), |
| 139 |
const Size2D &dilation = Size2D(1U, 1U), |
| 140 |
const ActivationLayerInfo &act_info = ActivationLayerInfo(), |
| 141 |
bool enable_fast_math = false, |
| 142 |
unsigned int num_groups = 1); |
| 143 |
|
| 144 |
/** Static function to check if there is an optimized version of |
| 145 |
* GEMM available for the input parameters. |
| 146 |
* |
| 147 |
* The method is intended to be used to find out the optimal |
| 148 |
* memory layout to be used for the weights tensor when running |
| 149 |
* variable weights execution. |
| 150 |
* |
| 151 |
* The user can query the database of optimised kernels in |
| 152 |
* arm_gemm by specifying one of the enumerations of |
| 153 |
* arm_compute::WeightFormat in the weight_format field of the input |
| 154 |
* parameter weights_info. In case of success, the method |
| 155 |
* writes the expected format in the output parameter |
| 156 |
* expected_weight_format. The expected_weight_format can than be |
| 157 |
* used in the configure method of the class for retrieving the |
| 158 |
* best optimal kernel. |
| 159 |
* |
| 160 |
* Use case one - query for a specific format: |
| 161 |
* |
| 162 |
* WeightInfo weights_info(..., arm_compute::WeightFormat::OHWIo4, ...); // Set the value of the input query. |
| 163 |
* if (NEGEMMConvolutionlayer::has_opt_impl(WeightFormat(), ...., weights_info, ...)) |
| 164 |
* { |
| 165 |
* auto conv = std::unique_ptr<NEGEMMConvolutionlayer>(); |
| 166 |
* conv->configure(..., weights_info, ...); // uses the same WeightFormat the user wanted originally, OHWYo4. |
| 167 |
* conv->run(...); |
| 168 |
* } |
| 169 |
* |
| 170 |
* Use case two - query for any format that would be optimal for the GEMM to execute: |
| 171 |
* |
| 172 |
* WeightInfo weights_info(..., arm_compute::WeightFormat::ANY, ...); // Set the value of the input query. |
| 173 |
* arm_compute::WeightFormat expected_wf; |
| 174 |
* if (NEGEMMConvolutionlayer::has_opt_impl(expected_wf, ...., weights_info, ...)) |
| 175 |
* { |
| 176 |
* auto conv = std::unique_ptr<NEGEMMConvolutionlayer>(); |
| 177 |
* // ... code to convert the layout of the weights tensor to the layout returned by has_opt_impl |
| 178 |
* WeightInfo new_weights_info(..., expected_wf, ...); // Set the value of the WeightFormat returned by has_opt_impl. |
| 179 |
* conv->configure(..., new_weights_info, ...); |
| 180 |
* conv->run(...); |
| 181 |
* } |
| 182 |
* |
| 183 |
* Notice that a GEMM configured with a WeightFormat other than |
| 184 |
* UNSPECIFIED will run GEMM with variable weights mode. |
| 185 |
* |
| 186 |
* @param[out] expected_weight_format The arm_compute::WeightFormat expected by the kernel. |
| 187 |
* @param[in] src Source tensor info. |
| 188 |
* @param[in] weights Weights tensor info. |
| 189 |
* @param[in] biases Biases tensor info. Shared biases supported. |
| 190 |
* @param[in] dst Destination tensor info. |
| 191 |
* @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 192 |
* @param[in] weights_info (optional) Specifies additional configuration parameters for the weights of the GEMM computation. |
| 193 |
* @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
| 194 |
* @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported. And no activation (i.e. Linear) which is the default value. |
| 195 |
* @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation |
| 196 |
* |
| 197 |
* @return a Status |
| 198 |
*/ |
| 199 |
static Status has_opt_impl(arm_compute::WeightFormat &expected_weight_format, |
| 200 |
const ITensorInfo *src, |
| 201 |
const ITensorInfo *weights, |
| 202 |
const ITensorInfo *biases, |
| 203 |
const ITensorInfo *dst, |
| 204 |
const PadStrideInfo &conv_info, |
| 205 |
const WeightsInfo &weights_info = WeightsInfo(), |
| 206 |
const Size2D &dilation = Size2D(1U, 1U), |
| 207 |
const ActivationLayerInfo &act_info = ActivationLayerInfo(), |
| 208 |
bool enable_fast_math = false); |
| 209 |
|
| 210 |
/** Update of quantization information at the run stage for convolution so that the quantization multipliers can be properly calculated. |
| 211 |
* |
| 212 |
* Quantization information is usually required at configure time, that is, when we call configure(). However, this information is not always available at this stage. |
| 213 |
* Quantization parameters are sometimes only available in the run() stage. But at this stage it is too late, as the operator was already configured everything based on the |
| 214 |
* wrong quantization parameters. One could call configure() again when the correct information is available, but that is a huge overhead. Therefore, this solution |
| 215 |
* that correctly and efficiently propagates the quantization information (multipliers and offsets) to the kernel. |
| 216 |
* |
| 217 |
* Example: |
| 218 |
* |
| 219 |
* op->configure(...) |
| 220 |
* make sure to set the correct parameters in the tensors by updating QuantizationInfo before calling update_quantization_parameters. |
| 221 |
* op->update_quantization_parameters() |
| 222 |
* op->run(...) |
| 223 |
* |
| 224 |
* Please have a look at examples/neon_gemm_qasymm8_signed.cpp for an example realization. |
| 225 |
* |
| 226 |
* This API is experimental and it can be changed in the future. |
| 227 |
*/ |
| 228 |
void update_quantization_parameters(); |
| 229 |
|
| 230 |
// Inherited methods overridden: |
| 231 |
void run() override; |
| 232 |
void prepare() override; |
| 233 |
|
| 234 |
private: |
| 235 |
struct Impl; |
| 236 |
std::unique_ptr<Impl> _impl; |
| 237 |
}; |
| 238 |
} // namespace arm_compute |
| 239 |
#endif // ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NEGEMMCONVOLUTIONLAYER_H |