1*c217d954SCole Faust /* 2*c217d954SCole Faust * Copyright (c) 2021-2022 Arm Limited. 3*c217d954SCole Faust * 4*c217d954SCole Faust * SPDX-License-Identifier: MIT 5*c217d954SCole Faust * 6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy 7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to 8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the 9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is 11*c217d954SCole Faust * furnished to do so, subject to the following conditions: 12*c217d954SCole Faust * 13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all 14*c217d954SCole Faust * copies or substantial portions of the Software. 15*c217d954SCole Faust * 16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22*c217d954SCole Faust * SOFTWARE. 23*c217d954SCole Faust */ 24*c217d954SCole Faust #ifndef ARM_COMPUTE_CPU_DEPTHWISE_CONV2D_H 25*c217d954SCole Faust #define ARM_COMPUTE_CPU_DEPTHWISE_CONV2D_H 26*c217d954SCole Faust 27*c217d954SCole Faust #include "arm_compute/core/ITensorInfo.h" 28*c217d954SCole Faust #include "arm_compute/core/experimental/Types.h" 29*c217d954SCole Faust #include "src/cpu/ICpuKernel.h" 30*c217d954SCole Faust #include "src/cpu/ICpuOperator.h" 31*c217d954SCole Faust #include "src/cpu/kernels/CpuDepthwiseConv2dNativeKernel.h" 32*c217d954SCole Faust #include "src/cpu/operators/CpuActivation.h" 33*c217d954SCole Faust #include "src/cpu/operators/CpuDepthwiseConv2dAssemblyDispatch.h" 34*c217d954SCole Faust #include "src/cpu/operators/CpuPermute.h" 35*c217d954SCole Faust 36*c217d954SCole Faust #include <memory> 37*c217d954SCole Faust 38*c217d954SCole Faust namespace arm_compute 39*c217d954SCole Faust { 40*c217d954SCole Faust namespace cpu 41*c217d954SCole Faust { 42*c217d954SCole Faust /** Function to execute a depthwise convolution. 43*c217d954SCole Faust */ 44*c217d954SCole Faust class CpuDepthwiseConv2d : public ICpuOperator 45*c217d954SCole Faust { 46*c217d954SCole Faust public: 47*c217d954SCole Faust /** Default constructor */ 48*c217d954SCole Faust CpuDepthwiseConv2d() = default; 49*c217d954SCole Faust /** Initialize the function's source, destination, weights and convolution information. 50*c217d954SCole Faust * 51*c217d954SCole Faust * @param[in, out] src Source tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32 52*c217d954SCole Faust * @param[out] dst Destination tensor info. Data type supported: same as @p src. 53*c217d954SCole Faust * @param[in] weights Weights tensor info. These are 3D tensor infos with shape [kernel_x, kernel_y, IFM]. 54*c217d954SCole Faust * Data type supported: Same as @p src or QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p src is QASYMM8/QASYMM8_SIGNED. 55*c217d954SCole Faust * @param[in] biases Biases tensor info. A 1D tensor with shape [IFM]. Must be nullptr if not needed. 56*c217d954SCole Faust * Data type supported: Same as @p src, S32 when src is QASYMM8/QASYMM8_SIGNED. 57*c217d954SCole Faust * @param[in] info Depthwise convolution meta-data. 58*c217d954SCole Faust */ 59*c217d954SCole Faust void configure(ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *dst, const ConvolutionInfo &info); 60*c217d954SCole Faust /** Static function to check if given info will lead to a valid configuration 61*c217d954SCole Faust * 62*c217d954SCole Faust * Similar to CpuDepthwiseConv2d::configure() 63*c217d954SCole Faust * 64*c217d954SCole Faust * @return a status 65*c217d954SCole Faust */ 66*c217d954SCole Faust static Status validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const ConvolutionInfo &info); 67*c217d954SCole Faust /** Static function to choose the best depthwise convolution function for @ref CpuDepthwiseConv2d 68*c217d954SCole Faust * 69*c217d954SCole Faust * @param[in] src Source tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32 70*c217d954SCole Faust * @param[in] weights Weights tensor info. These are 3D tensors with shape [kernel_x, kernel_y, IFM]. 71*c217d954SCole Faust * Data type supported: Same as @p src or QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p src is QASYMM8/QASYMM8_SIGNED. 72*c217d954SCole Faust * @param[in] biases Biases tensor info. A 1D tensor with shape [IFM]. Must be nullptr if not needed. 73*c217d954SCole Faust * Data type supported: Same as @p src, S32 when src is QASYMM8/QASYMM8_SIGNED. 74*c217d954SCole Faust * @param[in] dst Destination tensor. Data type supported: same as @p src. 75*c217d954SCole Faust * @param[in] info Depthwise convolution meta-data. 76*c217d954SCole Faust * 77*c217d954SCole Faust * @return a Depthwise Convolution Function 78*c217d954SCole Faust */ 79*c217d954SCole Faust static DepthwiseConvolutionFunction get_depthwiseconvolution_function(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, 80*c217d954SCole Faust const ConvolutionInfo &info); 81*c217d954SCole Faust 82*c217d954SCole Faust // Inherited methods overriden: 83*c217d954SCole Faust void run(ITensorPack &tensors) override; 84*c217d954SCole Faust void prepare(ITensorPack &tensors) override; 85*c217d954SCole Faust 86*c217d954SCole Faust private: 87*c217d954SCole Faust /** Basic function to execute optimized depthwise convolution routines. This function calls the following kernels: 88*c217d954SCole Faust * 89*c217d954SCole Faust * @note At the moment 3x3 and 5x5 convolution of stride 1, 2 are supported 90*c217d954SCole Faust * 91*c217d954SCole Faust * -# @ref NEFillBorderKernel (if pad_x or pad_y > 0) and no assembly kernel implementation is present 92*c217d954SCole Faust * -# @ref CpuDepthwiseConv2d3x3Kernel if 3x3 and no assembly kernel implementation is present 93*c217d954SCole Faust * -# @ref CpuDepthwiseConv2dAssemblyDispatch if assembly kernel implementation is present 94*c217d954SCole Faust * -# @ref CpuActivation if fused activation is required 95*c217d954SCole Faust * 96*c217d954SCole Faust */ 97*c217d954SCole Faust class CpuDepthwiseConv2dOptimizedInternal : public ICpuOperator 98*c217d954SCole Faust { 99*c217d954SCole Faust public: 100*c217d954SCole Faust /** Default constructor */ 101*c217d954SCole Faust CpuDepthwiseConv2dOptimizedInternal() = default; 102*c217d954SCole Faust /** Prevent instances of this class from being copied (As this class contains pointers) */ 103*c217d954SCole Faust CpuDepthwiseConv2dOptimizedInternal(const CpuDepthwiseConv2dOptimizedInternal &) = delete; 104*c217d954SCole Faust /** Default move constructor */ 105*c217d954SCole Faust CpuDepthwiseConv2dOptimizedInternal(CpuDepthwiseConv2dOptimizedInternal &&) = default; 106*c217d954SCole Faust /** Prevent instances of this class from being copied (As this class contains pointers) */ 107*c217d954SCole Faust CpuDepthwiseConv2dOptimizedInternal &operator=(const CpuDepthwiseConv2dOptimizedInternal &) = delete; 108*c217d954SCole Faust /** Default move assignment operator */ 109*c217d954SCole Faust CpuDepthwiseConv2dOptimizedInternal &operator=(CpuDepthwiseConv2dOptimizedInternal &&) = default; 110*c217d954SCole Faust /** Default destructor */ 111*c217d954SCole Faust ~CpuDepthwiseConv2dOptimizedInternal() = default; 112*c217d954SCole Faust /** Initialize the function's source, destination, kernels and border_size. 113*c217d954SCole Faust * 114*c217d954SCole Faust * @param[in, out] src Source tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32. (Written to only for border filling). 115*c217d954SCole Faust * @param[in] weights Weights tensor info. These are 3D tensors with shape [kernel_x, kernel_y, IFM]. Data type supported: Same as @p src. 116*c217d954SCole Faust * @param[in] biases Biases tensor info. A 1D tensor with shape [IFM]. Must be nullptr if not needed. 117*c217d954SCole Faust * Data type supported: Same as @p src, S32 when src is QASYMM8/QASYMM8_SIGNED. 118*c217d954SCole Faust * @param[out] dst Destination tensor info. Data type supported: same as @p src. 119*c217d954SCole Faust * @param[in] info Depthwise convolution meta-data. 120*c217d954SCole Faust */ 121*c217d954SCole Faust void configure(ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *dst, const ConvolutionInfo &info); 122*c217d954SCole Faust /** Static function to check if given info will lead to a valid configuration 123*c217d954SCole Faust * 124*c217d954SCole Faust * Similar to CpuDepthwiseConv2dOptimizedInternal::configure() 125*c217d954SCole Faust * 126*c217d954SCole Faust * @return a status 127*c217d954SCole Faust */ 128*c217d954SCole Faust static Status validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const ConvolutionInfo &info); 129*c217d954SCole Faust 130*c217d954SCole Faust // Inherited methods overriden: 131*c217d954SCole Faust void run(ITensorPack &tensors) override; 132*c217d954SCole Faust void prepare(ITensorPack &tensors) override; 133*c217d954SCole Faust 134*c217d954SCole Faust private: 135*c217d954SCole Faust std::unique_ptr<CpuDepthwiseConv2dAssemblyDispatch> _dwc_optimized_func{ nullptr }; 136*c217d954SCole Faust std::unique_ptr<CpuPermute> _permute_input{ nullptr }; 137*c217d954SCole Faust std::unique_ptr<CpuPermute> _permute_weights{ nullptr }; 138*c217d954SCole Faust std::unique_ptr<CpuPermute> _permute_output{ nullptr }; 139*c217d954SCole Faust std::unique_ptr<CpuActivation> _activationlayer_function{ nullptr }; 140*c217d954SCole Faust bool _has_bias{ false }; 141*c217d954SCole Faust bool _is_quantized{ false }; 142*c217d954SCole Faust bool _is_nchw{ true }; 143*c217d954SCole Faust bool _permute{ false }; 144*c217d954SCole Faust bool _is_activationlayer_enabled{ false }; 145*c217d954SCole Faust bool _is_prepared{ false }; 146*c217d954SCole Faust bool _are_weights_const{ true }; 147*c217d954SCole Faust }; 148*c217d954SCole Faust 149*c217d954SCole Faust /** Basic function to execute a generic depthwise convolution. This function calls the following kernel: 150*c217d954SCole Faust * 151*c217d954SCole Faust * -# @ref CpuDepthwiseConv2dNativeKernel 152*c217d954SCole Faust * 153*c217d954SCole Faust */ 154*c217d954SCole Faust class CpuDepthwiseConv2dGeneric : public ICpuOperator 155*c217d954SCole Faust { 156*c217d954SCole Faust public: 157*c217d954SCole Faust /** Default constructor */ 158*c217d954SCole Faust CpuDepthwiseConv2dGeneric() = default; 159*c217d954SCole Faust /** Prevent instances of this class from being copied (As this class contains pointers) */ 160*c217d954SCole Faust CpuDepthwiseConv2dGeneric(const CpuDepthwiseConv2dGeneric &) = delete; 161*c217d954SCole Faust /** Default move constructor */ 162*c217d954SCole Faust CpuDepthwiseConv2dGeneric(CpuDepthwiseConv2dGeneric &&) = default; 163*c217d954SCole Faust /** Prevent instances of this class from being copied (As this class contains pointers) */ 164*c217d954SCole Faust CpuDepthwiseConv2dGeneric &operator=(const CpuDepthwiseConv2dGeneric &) = delete; 165*c217d954SCole Faust /** Default move assignment operator */ 166*c217d954SCole Faust CpuDepthwiseConv2dGeneric &operator=(CpuDepthwiseConv2dGeneric &&) = default; 167*c217d954SCole Faust /** Default destructor */ 168*c217d954SCole Faust ~CpuDepthwiseConv2dGeneric() = default; 169*c217d954SCole Faust /** Initialize the function's source, destination, weights and convolution information. 170*c217d954SCole Faust * 171*c217d954SCole Faust * @param[in, out] src Source tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32. (Written to only for border filling). 172*c217d954SCole Faust * @param[out] dst Destination tensor info. Data type supported: same as @p src. 173*c217d954SCole Faust * @param[in] weights Weights tensor info. These are 3D tensors with shape [kernel_x, kernel_y, IFM]. 174*c217d954SCole Faust * Data type supported: Same as @p src or QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p src is QASYMM8/QASYMM8_SIGNED. 175*c217d954SCole Faust * @param[in] biases Biases tensor info. A 1D tensor with shape [IFM]. Must be nullptr if not needed. 176*c217d954SCole Faust * Data type supported: Same as @p src, S32 when src is QASYMM8/QASYMM8_SIGNED. 177*c217d954SCole Faust * @param[in] info Depthwise convolution meta-data. 178*c217d954SCole Faust */ 179*c217d954SCole Faust void configure(ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *dst, const ConvolutionInfo &info); 180*c217d954SCole Faust 181*c217d954SCole Faust /** Static function to check if given info will lead to a valid configuration 182*c217d954SCole Faust * 183*c217d954SCole Faust * Similar to CpuDepthwiseConv2dGeneric::configure() 184*c217d954SCole Faust * 185*c217d954SCole Faust * @return a status 186*c217d954SCole Faust */ 187*c217d954SCole Faust static Status validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const ConvolutionInfo &info); 188*c217d954SCole Faust 189*c217d954SCole Faust // Inherited methods overridden: 190*c217d954SCole Faust void run(ITensorPack &tensors) override; 191*c217d954SCole Faust void prepare(ITensorPack &tensors) override; 192*c217d954SCole Faust 193*c217d954SCole Faust private: 194*c217d954SCole Faust std::unique_ptr<kernels::CpuDepthwiseConv2dNativeKernel> _depthwise_conv_kernel{ nullptr }; 195*c217d954SCole Faust std::unique_ptr<CpuPermute> _permute_input{ nullptr }; 196*c217d954SCole Faust std::unique_ptr<CpuPermute> _permute_weights{ nullptr }; 197*c217d954SCole Faust std::unique_ptr<CpuPermute> _permute_output{ nullptr }; 198*c217d954SCole Faust std::unique_ptr<CpuActivation> _activationlayer_function{ nullptr }; 199*c217d954SCole Faust bool _is_nchw{ true }; 200*c217d954SCole Faust bool _is_prepared{ false }; 201*c217d954SCole Faust bool _is_activationlayer_enabled{ false }; 202*c217d954SCole Faust }; 203*c217d954SCole Faust 204*c217d954SCole Faust DepthwiseConvolutionFunction _depth_conv_func{ DepthwiseConvolutionFunction::GENERIC }; 205*c217d954SCole Faust CpuDepthwiseConv2dOptimizedInternal _func_optimized{}; 206*c217d954SCole Faust CpuDepthwiseConv2dGeneric _func_generic{}; 207*c217d954SCole Faust }; 208*c217d954SCole Faust } // namespace cpu 209*c217d954SCole Faust } // namespace arm_compute 210*c217d954SCole Faust #endif /* ARM_COMPUTE_CPU_DEPTHWISE_CONV2D_H */ 211