From d93e263e70e3101422402c95946e520fef34c4c7 Mon Sep 17 00:00:00 2001 From: Giorgio Arena Date: Tue, 15 Oct 2019 11:09:33 +0100 Subject: COMPMID-2708 NEDepthwiseConvolution Generic: support for QUANT8_PER_CHANNEL_SYMM COMPMID-2470 Implement a new and generic depthwise convolution for NEON QASYMM8 NHWC COMPMID-2477 Enable FP16 data type for the new generic convolution on NEON for NHWC COMPMID-2625 Remove old implementation files for the generic NEDepthwiseConvolution Change-Id: I8f6deda4fc69dd7e472fba3228b1ed5dad172f3e Signed-off-by: Giorgio Arena Reviewed-on: https://review.mlplatform.org/c/2094 Comments-Addressed: Arm Jenkins Reviewed-by: Gian Marco Iodice Tested-by: Arm Jenkins --- arm_compute/core/NEON/NEKernels.h | 3 - .../NEDepthwiseConvolutionLayerNativeKernel.h | 20 +- .../core/NEON/kernels/NEDepthwiseIm2ColKernel.h | 114 ------- .../NEON/kernels/NEDepthwiseVectorToTensorKernel.h | 97 ------ .../NEON/kernels/NEDepthwiseWeightsReshapeKernel.h | 87 ------ .../NEON/functions/NEDepthwiseConvolutionLayer.h | 58 +--- docs/00_introduction.dox | 6 +- .../NEDepthwiseConvolutionLayerNativeKernel.cpp | 332 ++++++++++++++++++++- src/core/NEON/kernels/NEDepthwiseIm2ColKernel.cpp | 197 ------------ .../kernels/NEDepthwiseVectorToTensorKernel.cpp | 156 ---------- .../kernels/NEDepthwiseWeightsReshapeKernel.cpp | 165 ---------- src/core/NEON/kernels/NEPermuteKernel.cpp | 6 - .../NEON/functions/NEDepthwiseConvolutionLayer.cpp | 304 +++---------------- support/ToolchainSupport.h | 18 +- .../validation/NEON/DepthwiseConvolutionLayer.cpp | 68 ++++- .../fixtures/DepthwiseConvolutionLayerFixture.h | 16 +- .../reference/DepthwiseConvolutionLayer.cpp | 12 +- 17 files changed, 490 insertions(+), 1169 deletions(-) delete mode 100644 arm_compute/core/NEON/kernels/NEDepthwiseIm2ColKernel.h delete mode 100644 arm_compute/core/NEON/kernels/NEDepthwiseVectorToTensorKernel.h delete mode 100644 arm_compute/core/NEON/kernels/NEDepthwiseWeightsReshapeKernel.h delete mode 100644 src/core/NEON/kernels/NEDepthwiseIm2ColKernel.cpp delete mode 100644 src/core/NEON/kernels/NEDepthwiseVectorToTensorKernel.cpp delete mode 100644 src/core/NEON/kernels/NEDepthwiseWeightsReshapeKernel.cpp diff --git a/arm_compute/core/NEON/NEKernels.h b/arm_compute/core/NEON/NEKernels.h index 5eaf8ad445..33a640fa05 100644 --- a/arm_compute/core/NEON/NEKernels.h +++ b/arm_compute/core/NEON/NEKernels.h @@ -55,9 +55,6 @@ #include "arm_compute/core/NEON/kernels/NEDepthToSpaceLayerKernel.h" #include "arm_compute/core/NEON/kernels/NEDepthwiseConvolutionLayer3x3Kernel.h" #include "arm_compute/core/NEON/kernels/NEDepthwiseConvolutionLayerNativeKernel.h" -#include "arm_compute/core/NEON/kernels/NEDepthwiseIm2ColKernel.h" -#include "arm_compute/core/NEON/kernels/NEDepthwiseVectorToTensorKernel.h" -#include "arm_compute/core/NEON/kernels/NEDepthwiseWeightsReshapeKernel.h" #include "arm_compute/core/NEON/kernels/NEDequantizationLayerKernel.h" #include "arm_compute/core/NEON/kernels/NEDerivativeKernel.h" #include "arm_compute/core/NEON/kernels/NEDilateKernel.h" diff --git a/arm_compute/core/NEON/kernels/NEDepthwiseConvolutionLayerNativeKernel.h b/arm_compute/core/NEON/kernels/NEDepthwiseConvolutionLayerNativeKernel.h index 5db79f8bf7..a0205f1ea6 100644 --- a/arm_compute/core/NEON/kernels/NEDepthwiseConvolutionLayerNativeKernel.h +++ b/arm_compute/core/NEON/kernels/NEDepthwiseConvolutionLayerNativeKernel.h @@ -26,6 +26,10 @@ #include "arm_compute/core/NEON/INEKernel.h" +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC +#include +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + namespace arm_compute { // Forward declarations @@ -53,7 +57,7 @@ public: * * @note Supported data layouts: NHWC * - * @param[in] input Source tensor. DataType supported: F32. + * @param[in] input Source tensor. DataType supported: QASYMM8/F16/F32. * @param[in] weights Weights tensor. This is a 3D tensor with dimensions [IFM, W, H]. Data type supported: Same as @p input. * @param[in] biases Biases tensor. A 1D tensor with dimensions [IFM]. Must be nullptr if not needed. Data type supported: Same as @p input. * @param[out] output Destination tensor. Data type supported: Same as @p input. @@ -68,7 +72,7 @@ public: * * @note Supported data layouts: NHWC * - * @param[in] input Source tensor info. DataType supported: F32. + * @param[in] input Source tensor info. DataType supported: QASYMM8/F16/F32. * @param[in] weights Weights tensor info. This is a 3D tensor with dimensions [IFM, W, H]. Data type supported: Same as @p input. * @param[in] biases Biases tensor info. A 1D tensor with dimensions [IFM]. Must be nullptr if not needed. Data type supported: Same as @p input. * @param[in] output Destination tensor info. Data type supported: Same as @p input. @@ -86,7 +90,15 @@ public: BorderSize border_size() const override; private: - template + template < typename T, typename TW, int S, bool has_biases, bool is_per_channel, typename std::enable_if < std::is_same::value +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + || std::is_same::value +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + , + int >::type = 0 > + void run_depthwise(const Window &window); + + template ::value, int>::type = 0> void run_depthwise(const Window &window); /** Common signature for all the specialised depthwise convolution native functions @@ -104,6 +116,8 @@ private: PadStrideInfo _conv_info; unsigned int _depth_multiplier; Size2D _dilation; + std::vector _output_multiplier; + std::vector _output_shift; }; } // namespace arm_compute #endif /* __ARM_COMPUTE_NEDEPTHWISECONVOLUTIONLAYERNATIVEKERNEL_H__ */ diff --git a/arm_compute/core/NEON/kernels/NEDepthwiseIm2ColKernel.h b/arm_compute/core/NEON/kernels/NEDepthwiseIm2ColKernel.h deleted file mode 100644 index 3e123b4839..0000000000 --- a/arm_compute/core/NEON/kernels/NEDepthwiseIm2ColKernel.h +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (c) 2017-2019 ARM Limited. - * - * SPDX-License-Identifier: MIT - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -#ifndef __ARM_COMPUTE_NEDEPTHWISEIM2COLKERNEL_H__ -#define __ARM_COMPUTE_NEDEPTHWISEIM2COLKERNEL_H__ - -#include "arm_compute/core/NEON/INEKernel.h" -#include "arm_compute/core/Size2D.h" - -namespace arm_compute -{ -class ITensor; - -/** Interface for the depthwise im2col reshape kernel. - * This kernel reshape the input low 3 dimensions to a new 3D shape where the output's first dimension is - * the linear patch size (FILTER_WIDTH * FILTER_HEIGHT) and second dimension is number of patches in per image and third dimension unchanged . - **/ -class NEDepthwiseIm2ColKernel : public INEKernel -{ -public: - const char *name() const override - { - return "NEDepthwiseIm2ColKernel"; - } - /** Default constructor */ - NEDepthwiseIm2ColKernel(); - /** Prevent instances of this class from being copied (As this class contains pointers) */ - NEDepthwiseIm2ColKernel(const NEDepthwiseIm2ColKernel &) = delete; - /** Prevent instances of this class from being copied (As this class contains pointers) */ - NEDepthwiseIm2ColKernel &operator=(const NEDepthwiseIm2ColKernel &) = delete; - /** Allow instances of this class to be moved */ - NEDepthwiseIm2ColKernel(NEDepthwiseIm2ColKernel &&) = default; - /** Allow instances of this class to be moved */ - NEDepthwiseIm2ColKernel &operator=(NEDepthwiseIm2ColKernel &&) = default; - /** Set the input and output of the kernel. - * - * @param[in] input The input tensor to convert. 3 lower dimensions represent a single input [width, height, IFM], - * while every optional dimension from 4 and above represent a batch of inputs. Data types supported: QASYMM8/F16/F32 - * @param[out] output The output tensor. First 3 lower dimensions represent a transform of each 3D input, - * while every dimension above 3 represents a batch. Data types supported: Same as @p input - * @param[in] kernel_dims The kernel dimensions (width and height). - * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. - * @param[in] has_bias Boolean that specifies if the depthwise convolution has bias. - * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1. - * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). - */ - void configure(const ITensor *input, ITensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias = false, unsigned int depth_multiplier = 1, - const Size2D &dilation = Size2D(1U, 1U)); - - /** Static function to check if given info will lead to a valid configuration of @ref NEDepthwiseIm2ColKernel - * - * @param[in] input The input tensor info to convert. 3 lower dimensions represent a single input [width, height, IFM], - * while every optional dimension from 4 and above represent a batch of inputs. Data types supported: QASYMM8/F16/F32 - * @param[in] output The output tensor info. First 3 lower dimensions represent a transform of each 3D input, - * while every dimension above 3 represents a batch. Data types supported: Same as @p input - * @param[in] kernel_dims The kernel dimensions (width and height). - * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. - * @param[in] has_bias Boolean that specifies if the depthwise convolution has bias. - * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1. - * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). - * - * @return a status - */ - static Status validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias = false, unsigned int depth_multiplier = 1, - const Size2D &dilation = Size2D(1U, 1U)); - - // Inherited methods overridden: - void run(const Window &window, const ThreadInfo &info) override; - -private: - /** Template function to run the im2col used for the depthwise convolution layer case - * - * @param[in] window Region on which to execute the kernel. (Must be a valid region of the window returned by window()). - */ - template - void run_generic(const Window &window); - /** Common signature for all the specialised depthwise im2col functions - * - * @param[in] window Region on which to execute the kernel. - */ - using DepthwiseIm2ColFunctionPtr = void (NEDepthwiseIm2ColKernel::*)(const Window &window); - -private: - DepthwiseIm2ColFunctionPtr _func; - const ITensor *_input; - ITensor *_output; - Size2D _kernel_dims; - PadStrideInfo _conv_info; - bool _has_bias; - unsigned int _depth_multiplier; - Size2D _dilation; -}; -} // namespace arm_compute -#endif /*__ARM_COMPUTE_NEDEPTHWISEIM2COLKERNEL_H__ */ diff --git a/arm_compute/core/NEON/kernels/NEDepthwiseVectorToTensorKernel.h b/arm_compute/core/NEON/kernels/NEDepthwiseVectorToTensorKernel.h deleted file mode 100644 index 25af7a29cc..0000000000 --- a/arm_compute/core/NEON/kernels/NEDepthwiseVectorToTensorKernel.h +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (c) 2017-2018 ARM Limited. - * - * SPDX-License-Identifier: MIT - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -#ifndef __ARM_COMPUTE_NEDEPTHWISEVECTORTOTENSORKERNEL_H__ -#define __ARM_COMPUTE_NEDEPTHWISEVECTORTOTENSORKERNEL_H__ - -#include "arm_compute/core/NEON/INEKernel.h" - -namespace arm_compute -{ -class ITensor; - -/** Interface for the depthwise vector to tensor kernel. - * - * This kernel takes the 1D tensor that's been produced by the MatrixVectorMultiply - * kernel and reshapes it to given width and height (previously calculated, based - * on input/weights dimensions and convolution strides and padding). - * - **/ -class NEDepthwiseVectorToTensorKernel : public INEKernel -{ -public: - const char *name() const override - { - return "NEDepthwiseVectorToTensorKernel"; - } - /** Default constructor */ - NEDepthwiseVectorToTensorKernel(); - /** Prevent instances of this class from being copied (As this class contains pointers) */ - NEDepthwiseVectorToTensorKernel(const NEDepthwiseVectorToTensorKernel &) = delete; - /** Prevent instances of this class from being copied (As this class contains pointers) */ - NEDepthwiseVectorToTensorKernel &operator=(const NEDepthwiseVectorToTensorKernel &) = delete; - /** Allow instances of this class to be moved */ - NEDepthwiseVectorToTensorKernel(NEDepthwiseVectorToTensorKernel &&) = default; - /** Allow instances of this class to be moved */ - NEDepthwiseVectorToTensorKernel &operator=(NEDepthwiseVectorToTensorKernel &&) = default; - /** Set the input and output of the kernel. - * - * @param[in] input The input vector to convert. Data type supported: QASYMM8/S32/F16/F32. - * @param[out] output The output tensor. 3 lower dimensions represent a single input [width, height, IFM]. Data type supported: same as @p input. - * @param[in] conv_w The converted tensor's width. - * @param[in] conv_h The converted tensor's height. - */ - void configure(const ITensor *input, ITensor *output, size_t conv_w, size_t conv_h); - /** Static function to check if given info will lead to a valid configuration of @ref NEDepthwiseVectorToTensorKernel - * - * @param[in] input The input vector to convert. Data type supported: QASYMM8/S32/F16/F32. - * @param[in] output The output tensor. 3 lower dimensions represent a single input [width, height, IFM]. Data type supported: same as @p input. - * @param[in] conv_w The converted tensor's width. - * @param[in] conv_h The converted tensor's height. - */ - static Status validate(const ITensorInfo *input, const ITensorInfo *output, size_t conv_w, size_t conv_h); - - // Inherited methods overridden: - void run(const Window &window, const ThreadInfo &info) override; - -private: - /** Template function to run the vector to tensor reshape used for the depthwise convolution layer case - * - * @param[in] window Region on which to execute the kernel. (Must be a valid region of the window returned by window()). - */ - template - void vector_to_tensor(const Window &window); - /** Common signature for all the specialised depthwise vector to tensor functions - * - * @param[in] window Region on which to execute the kernel. - */ - using DepthwiseVectorToTensorFunctionPtr = void (NEDepthwiseVectorToTensorKernel::*)(const Window &window); - -private: - DepthwiseVectorToTensorFunctionPtr _func; - const ITensor *_input; - ITensor *_output; - std::pair _conv_dims; -}; -} // arm_compute -#endif /*__ARM_COMPUTE_NEDEPTHWISEVECTORTOTENSORKERNEL_H__ */ diff --git a/arm_compute/core/NEON/kernels/NEDepthwiseWeightsReshapeKernel.h b/arm_compute/core/NEON/kernels/NEDepthwiseWeightsReshapeKernel.h deleted file mode 100644 index dcf52442a9..0000000000 --- a/arm_compute/core/NEON/kernels/NEDepthwiseWeightsReshapeKernel.h +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (c) 2017-2018 ARM Limited. - * - * SPDX-License-Identifier: MIT - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -#ifndef __ARM_COMPUTE_NEDEPTHWISEWEIGHTSRESHAPEKERNEL_H__ -#define __ARM_COMPUTE_NEDEPTHWISEWEIGHTSRESHAPEKERNEL_H__ - -#include "arm_compute/core/NEON/INEKernel.h" - -namespace arm_compute -{ -class ITensor; - -/** Interface for the depthwise weights reshape kernel. - * This kernel reshape original weights' low 2D dimensions into a single col and - * have the second dimension as the original depth size. - **/ -class NEDepthwiseWeightsReshapeKernel : public INEKernel -{ -public: - const char *name() const override - { - return "NEDepthwiseWeightsReshapeKernel"; - } - /** Default constructor */ - NEDepthwiseWeightsReshapeKernel(); - /** Prevent instances of this class from being copied (As this class contains pointers) */ - NEDepthwiseWeightsReshapeKernel(const NEDepthwiseWeightsReshapeKernel &) = delete; - /** Prevent instances of this class from being copied (As this class contains pointers) */ - NEDepthwiseWeightsReshapeKernel &operator=(const NEDepthwiseWeightsReshapeKernel &) = delete; - /** Allow instances of this class to be moved */ - NEDepthwiseWeightsReshapeKernel(NEDepthwiseWeightsReshapeKernel &&) = default; - /** Allow instances of this class to be moved */ - NEDepthwiseWeightsReshapeKernel &operator=(NEDepthwiseWeightsReshapeKernel &&) = default; - /** Set the input and output of the kernel. - * - * @param[in] input The input tensor to convert. 3 lower dimensions represent a single input [width, height, IFM]. - * Data type supported: QASYMM8/F16/F32. - * @param[out] output The output tensor. Data type supported: same as @p input. - * @param[in] biases (Optional) The input biases to add. Shape [IFM]. Data type supported: same as @p input. - */ - void configure(const ITensor *input, ITensor *output, const ITensor *biases); - - /** Static function to check if given info will lead to a valid configuration of @ref NEDepthwiseWeightsReshapeKernel - * - * @param[in] input The input tensor to convert. 3 lower dimensions represent a single input [width, height, IFM]. - * Data type supported: QASYMM8/F16/F32. - * @param[in] output The output tensor. Data type supported: same as @p input. - * @param[in] biases (Optional) The input biases to add. Shape [IFM]. Data type supported: same as @p input. - * - * @return a status - */ - static Status validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *biases); - - // Inherited methods overridden: - void run(const Window &window, const ThreadInfo &info) override; - -private: - using DepthwiseWeightsReshapeFunction = void(const ITensor *input, const ITensor *bias, ITensor *output, const Window &window); - -private: - DepthwiseWeightsReshapeFunction *_func; - const ITensor *_input; - ITensor *_output; - const ITensor *_biases; -}; -} // namespace arm_compute -#endif /*__ARM_COMPUTE_NEDEPTHWISEWEIGHTSRESHAPEKERNEL_H__ */ diff --git a/arm_compute/runtime/NEON/functions/NEDepthwiseConvolutionLayer.h b/arm_compute/runtime/NEON/functions/NEDepthwiseConvolutionLayer.h index 87405fdb14..ea3ef9bf38 100644 --- a/arm_compute/runtime/NEON/functions/NEDepthwiseConvolutionLayer.h +++ b/arm_compute/runtime/NEON/functions/NEDepthwiseConvolutionLayer.h @@ -26,21 +26,11 @@ #include "arm_compute/core/NEON/kernels/NEDepthwiseConvolutionLayer3x3Kernel.h" #include "arm_compute/core/NEON/kernels/NEDepthwiseConvolutionLayerNativeKernel.h" -#include "arm_compute/core/NEON/kernels/NEDepthwiseIm2ColKernel.h" -#include "arm_compute/core/NEON/kernels/NEDepthwiseVectorToTensorKernel.h" -#include "arm_compute/core/NEON/kernels/NEDepthwiseWeightsReshapeKernel.h" #include "arm_compute/core/NEON/kernels/NEDirectConvolutionLayerOutputStageKernel.h" #include "arm_compute/core/NEON/kernels/NEFillBorderKernel.h" -#include "arm_compute/core/NEON/kernels/NEGEMMMatrixVectorMultiplyKernel.h" -#include "arm_compute/core/Types.h" -#include "arm_compute/core/utils/misc/Macros.h" -#include "arm_compute/runtime/IFunction.h" -#include "arm_compute/runtime/IMemoryManager.h" -#include "arm_compute/runtime/MemoryGroup.h" #include "arm_compute/runtime/NEON/functions/NEActivationLayer.h" #include "arm_compute/runtime/NEON/functions/NEPermute.h" #include "arm_compute/runtime/NEON/functions/assembly/NEDepthwiseConvolutionAssemblyDispatch.h" -#include "arm_compute/runtime/Tensor.h" namespace arm_compute { @@ -279,17 +269,10 @@ private: bool _is_prepared; }; -/** Basic function to execute a generic depthwise convolution. This function calls the following NEON kernels: +/** Basic function to execute a generic depthwise convolution. This function calls the following NEON kernel: * - * If data type is F32 and data layout is NHWC: * -# @ref NEDepthwiseConvolutionLayerNativeKernel * - * Otherwise: - * -# @ref NEDepthwiseIm2ColKernel - * -# @ref NEDepthwiseWeightsReshapeKernel - * -# @ref NEGEMMMatrixVectorMultiplyKernel - * -# @ref NEFillBorderKernel (if pad_x or pad_y > 0) - * */ class NEDepthwiseConvolutionLayer : public IFunction { @@ -341,32 +324,19 @@ public: void prepare() override; private: - NEDepthwiseIm2ColKernel _im2col_kernel; - NEDepthwiseWeightsReshapeKernel _weights_reshape_kernel; - NEGEMMMatrixVectorMultiplyKernel _v2mm_kernel; - NEDepthwiseConvolutionLayerNativeKernel _depthwise_conv_kernel; - NEDepthwiseVectorToTensorKernel _vector_to_tensor_kernel; - NEDirectConvolutionLayerOutputStageKernel _output_stage_kernel; - NEFillBorderKernel _fill_border; - NEFillBorderKernel _v2mm_input_fill_border; - NEFillBorderKernel _v2mm_weights_fill_border; - NEPermute _permute_input; - NEPermute _permute_weights; - NEPermute _permute_output; - NEActivationLayer _activationlayer_function; - Tensor _input_reshaped; - Tensor _weights_reshaped; - Tensor _v2mm_output; - Tensor _output_reshaped; - Tensor _permuted_input; - Tensor _permuted_weights; - Tensor _permuted_output; - bool _is_prepared; - bool _is_quantized; - bool _is_nhwc; - bool _is_activationlayer_enabled; - bool _is_optimized; - const ITensor *_original_weights; + NEDepthwiseConvolutionLayerNativeKernel _depthwise_conv_kernel; + NEFillBorderKernel _fill_border; + NEPermute _permute_input; + NEPermute _permute_weights; + NEPermute _permute_output; + NEActivationLayer _activationlayer_function; + Tensor _permuted_input; + Tensor _permuted_weights; + Tensor _permuted_output; + bool _is_prepared; + bool _is_nchw; + bool _is_activationlayer_enabled; + const ITensor *_original_weights; }; } // namespace arm_compute #endif /* __ARM_COMPUTE_NEDEPTHWISECONVOLUTION_H__ */ \ No newline at end of file diff --git a/docs/00_introduction.dox b/docs/00_introduction.dox index 1210b2bdec..6430411f5b 100644 --- a/docs/00_introduction.dox +++ b/docs/00_introduction.dox @@ -242,6 +242,10 @@ v19.11 Public major release - CLDepthwiseIm2ColKernel - CLDepthwiseVectorToTensorKernel - CLDirectConvolutionLayerOutputStageKernel + - Deprecated NEON kernels / functions + - NEDepthwiseWeightsReshapeKernel + - NEDepthwiseIm2ColKernel + - NEDepthwiseVectorToTensorKernel v19.08 Public major release - Various bug fixes. @@ -706,7 +710,7 @@ v17.12 Public major release - New NEON kernels / functions - arm_compute::NEGEMMLowpAArch64A53Kernel / arm_compute::NEGEMMLowpAArch64Kernel / arm_compute::NEGEMMLowpAArch64V8P4Kernel / arm_compute::NEGEMMInterleavedBlockedKernel / arm_compute::NEGEMMLowpAssemblyMatrixMultiplyCore - arm_compute::NEHGEMMAArch64FP16Kernel - - @ref NEDepthwiseConvolutionLayer3x3Kernel / @ref NEDepthwiseIm2ColKernel / @ref NEGEMMMatrixVectorMultiplyKernel / @ref NEDepthwiseVectorToTensorKernel / @ref NEDepthwiseConvolutionLayer + - @ref NEDepthwiseConvolutionLayer3x3Kernel / NEDepthwiseIm2ColKernel / @ref NEGEMMMatrixVectorMultiplyKernel / NEDepthwiseVectorToTensorKernel / @ref NEDepthwiseConvolutionLayer - @ref NEGEMMLowpOffsetContributionKernel / @ref NEGEMMLowpMatrixAReductionKernel / @ref NEGEMMLowpMatrixBReductionKernel / @ref NEGEMMLowpMatrixMultiplyCore - @ref NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel / @ref NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint - @ref NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel / @ref NEGEMMLowpQuantizeDownInt32ToUint8Scale diff --git a/src/core/NEON/kernels/NEDepthwiseConvolutionLayerNativeKernel.cpp b/src/core/NEON/kernels/NEDepthwiseConvolutionLayerNativeKernel.cpp index c9d4e9be50..a0d45afd2a 100644 --- a/src/core/NEON/kernels/NEDepthwiseConvolutionLayerNativeKernel.cpp +++ b/src/core/NEON/kernels/NEDepthwiseConvolutionLayerNativeKernel.cpp @@ -24,19 +24,30 @@ #include "arm_compute/core/NEON/kernels/NEDepthwiseConvolutionLayerNativeKernel.h" #include "arm_compute/core/AccessWindowStatic.h" +#include "arm_compute/core/CPP/Validate.h" #include "arm_compute/core/NEON/wrapper/traits.h" #include "arm_compute/core/NEON/wrapper/wrapper.h" #include "arm_compute/core/utils/misc/ShapeCalculator.h" - -#include "support/ToolchainSupport.h" +#include "arm_compute/core/utils/quantization/AsymmHelpers.h" +#include "src/core/NEON/kernels/convolution/depthwise/impl_qa8_qa8.hpp" namespace arm_compute { namespace { +void pad_vectors(std::vector &mult, std::vector &shift, int vec_size) +{ + ARM_COMPUTE_ERROR_ON(mult.size() != shift.size()); + while(mult.size() % vec_size != 0) + { + mult.push_back(0); + shift.push_back(0); + } +} + template -void depthwise_loop_multiplier1(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, - const Size2D &dilation, const Window &window) +void depthwise_loop_multiplier1_fp(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, + const Size2D &dilation, const Window &window) { using VectorType = typename wrapper::traits::neon_vector::type; using TagType = typename wrapper::traits::neon_vector::tag_type; @@ -108,8 +119,8 @@ void depthwise_loop_multiplier1(const ITensor *input, const ITensor *weights, co } template -void depthwise_loop_generic(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, - const Size2D &dilation, unsigned int depth_multiplier, const Window &window) +void depthwise_loop_generic_fp(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, + const Size2D &dilation, unsigned int depth_multiplier, const Window &window) { const size_t input_stride_y = input->info()->strides_in_bytes().y(); const size_t input_stride_z = input->info()->strides_in_bytes().z(); @@ -191,21 +202,243 @@ void depthwise_loop_generic(const ITensor *input, const ITensor *weights, const input_it, weights_it, biases_it, output_it); } +template +void depthwise_loop_multiplier1_quantized(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, + const Size2D &dilation, std::vector output_multiplier, std::vector output_shift, const Window &window) +{ + using VectorType = typename wrapper::traits::neon_vector::type; + using TagType = typename wrapper::traits::neon_vector::tag_type; + + const size_t input_stride_y = input->info()->strides_in_bytes().y(); + const size_t input_stride_z = input->info()->strides_in_bytes().z(); + const size_t input_max_offset = input->info()->strides_in_bytes().z() * input->info()->dimension(2) - (input->info()->padding().bottom + input->info()->padding().top) * + input->info()->strides_in_bytes().y(); + const size_t weights_width = weights->info()->dimension(1); + const size_t weights_height = weights->info()->dimension(2); + const size_t weights_stride_y = weights->info()->strides_in_bytes().y(); + const size_t weights_stride_z = weights->info()->strides_in_bytes().z(); + const size_t conv_stride_x = conv_info.stride().first; + const size_t conv_stride_y = conv_info.stride().second; + const size_t conv_pad_left = conv_info.pad_left(); + const size_t conv_pad_top = conv_info.pad_top(); + + const int32_t input_qoffset = input->info()->quantization_info().uniform().offset; + const int32_t weights_qoffset = weights->info()->quantization_info().uniform().offset; + const int32_t output_qoffset = output->info()->quantization_info().uniform().offset; + const int32_t k_offset = weights_width * weights_height * input_qoffset * weights_qoffset; + + Window win_input = window; + win_input.set(Window::DimY, Window::Dimension(0, 0, 0)); + win_input.set(Window::DimZ, Window::Dimension(0, 0, 0)); + + Window win_weights = win_input; + win_weights.set(3, Window::Dimension(0, 0, 0)); + + Iterator input_it(input, win_input); + Iterator weights_it(weights, win_weights); + Iterator output_it(output, window); + Iterator biases_it{}; + + if(has_biases) + { + biases_it = Iterator(biases, win_weights); + } + + execute_window_loop(window, [&](const Coordinates & id) + { + std::vector acc(S, 0); + std::vector in_sum(S, 0); + std::vector we_sum(S, 0); + + const int input_y = id.y() * conv_stride_x - conv_pad_left; + const int input_z = id.z() * conv_stride_y - conv_pad_top; + int input_offset = input_y * input_stride_y + input_z * input_stride_z; + + auto weights_ptr = weights_it.ptr(); + for(size_t h = 0; h < weights_height; ++h) + { + int offs = input_offset; + for(size_t w = 0; w < weights_width; ++w) + { + const auto input_vals = wrapper::vload(reinterpret_cast(input_it.ptr() + std::min(static_cast(offs), input_max_offset))); + const auto weights_vals = wrapper::vload(reinterpret_cast(weights_ptr + w * weights_stride_y)); + + for(int i = 0; i < S; ++i) + { + acc.at(i) += input_vals[i] * weights_vals[i]; + in_sum.at(i) += input_vals[i]; + we_sum.at(i) += weights_vals[i]; + } + + offs += dilation.x() * input_stride_y; + } + + weights_ptr += weights_stride_z; + input_offset += dilation.y() * input_stride_z; + } + + VectorType out_vals = wrapper::vdup_n(static_cast(0), TagType{}); + for(int i = 0; i < S; ++i) + { + acc.at(i) -= in_sum.at(i) * weights_qoffset; + acc.at(i) -= we_sum.at(i) * input_qoffset; + acc.at(i) += k_offset; + + if(has_biases) + { + acc.at(i) += *reinterpret_cast(biases_it.ptr() + i * sizeof(int32_t)); + } + + acc.at(i) = rounding_divide_by_exp2(saturating_doubling_high_mul(acc.at(i), output_multiplier.at(id.x() + i)), output_shift.at(id.x() + i)) + output_qoffset; + out_vals[i] = static_cast(utility::clamp(acc.at(i))); + } + + wrapper::vstore(reinterpret_cast(output_it.ptr()), out_vals); + }, + input_it, weights_it, biases_it, output_it); +} + +template +void depthwise_loop_generic_quantized(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, + const Size2D &dilation, unsigned int depth_multiplier, std::vector output_multiplier, std::vector output_shift, const Window &window) +{ + const size_t input_stride_y = input->info()->strides_in_bytes().y(); + const size_t input_stride_z = input->info()->strides_in_bytes().z(); + const size_t input_max_offset = input->info()->strides_in_bytes().z() * input->info()->dimension(2) - (input->info()->padding().bottom + input->info()->padding().top) * + input->info()->strides_in_bytes().y(); + const size_t weights_width = weights->info()->dimension(1); + const size_t weights_height = weights->info()->dimension(2); + const size_t weights_stride_y = weights->info()->strides_in_bytes().y(); + const size_t weights_stride_z = weights->info()->strides_in_bytes().z(); + const size_t conv_stride_x = conv_info.stride().first; + const size_t conv_stride_y = conv_info.stride().second; + const size_t conv_pad_left = conv_info.pad_left(); + const size_t conv_pad_top = conv_info.pad_top(); + + const int32_t input_qoffset = input->info()->quantization_info().uniform().offset; + const int32_t weights_qoffset = weights->info()->quantization_info().uniform().offset; + const int32_t output_qoffset = output->info()->quantization_info().uniform().offset; + const int32_t k_offset = weights_width * weights_height * input_qoffset * weights_qoffset; + + Window win_input = window; + win_input.set(Window::DimY, Window::Dimension(0, 0, 0)); + win_input.set(Window::DimZ, Window::Dimension(0, 0, 0)); + + Window win_weights = win_input; + win_weights.set(3, Window::Dimension(0, 0, 0)); + + win_input.set_dimension_step(Window::DimX, 1); + + Iterator input_it(input, win_input); + Iterator weights_it(weights, win_weights); + Iterator output_it(output, window); + Iterator biases_it{}; + + if(has_biases) + { + biases_it = Iterator(biases, win_weights); + } + + execute_window_loop(window, [&](const Coordinates & id) + { + std::vector acc(depth_multiplier, 0); + std::vector we_sum(depth_multiplier, 0); + int32_t in_sum = 0; + + const int input_y = id.y() * conv_stride_x - conv_pad_left; + const int input_z = id.z() * conv_stride_y - conv_pad_top; + int input_offset = input_y * input_stride_y + input_z * input_stride_z; + + auto weights_ptr = weights_it.ptr(); + for(size_t h = 0; h < weights_height; ++h) + { + int offs = input_offset; + for(size_t w = 0; w < weights_width; ++w) + { + const auto input_val = *(reinterpret_cast(input_it.ptr() + std::min(static_cast(offs), input_max_offset))); + + for(size_t m = 0; m < depth_multiplier; ++m) + { + const auto weights_val = *(reinterpret_cast(weights_ptr + m * sizeof(T) + w * weights_stride_y)); + acc.at(m) += input_val * weights_val; + + we_sum.at(m) += weights_val; + } + + offs += dilation.x() * input_stride_y; + in_sum += input_val; + } + + weights_ptr += weights_stride_z; + input_offset += dilation.y() * input_stride_z; + } + + for(size_t m = 0; m < depth_multiplier; ++m) + { + acc.at(m) -= in_sum * weights_qoffset; + acc.at(m) -= we_sum.at(m) * input_qoffset; + acc.at(m) += k_offset; + + if(has_biases) + { + const auto biases_val = *(reinterpret_cast(biases_it.ptr() + m * sizeof(int32_t))); + + int32_t out_val = acc.at(m) + biases_val; + out_val = rounding_divide_by_exp2(saturating_doubling_high_mul(out_val, output_multiplier.at(id.x() + m)), + output_shift.at(id.x() + m)) + + output_qoffset; + *(reinterpret_cast(output_it.ptr() + m * sizeof(T))) = static_cast(utility::clamp(out_val)); + } + else + { + int32_t out_val = rounding_divide_by_exp2(saturating_doubling_high_mul(acc.at(m), output_multiplier.at(id.x() + m)), + output_shift.at(id.x() + m)) + + output_qoffset; + *(reinterpret_cast(output_it.ptr() + m * sizeof(T))) = static_cast(utility::clamp(out_val)); + } + } + }, + input_it, weights_it, biases_it, output_it); +} + Status validate_arguments(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation) { ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); - ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output); + ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); + ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); ARM_COMPUTE_RETURN_ERROR_ON(depth_multiplier == 0); + ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(1) + (weights->dimension(1) - 1) * (dilation.x() - 1) > input->dimension(1) + conv_info.pad_left() + conv_info.pad_right()); + ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(2) + (weights->dimension(2) - 1) * (dilation.y() - 1) > input->dimension(2) + conv_info.pad_top() + conv_info.pad_bottom()); ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(0) * depth_multiplier) != weights->dimension(0)); ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || (dilation.y() < 1)); ARM_COMPUTE_RETURN_ERROR_ON((conv_info.stride().first < 1) || (conv_info.stride().second < 1)); + if(is_data_type_quantized_per_channel(weights->data_type())) + { + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QSYMM8_PER_CHANNEL); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(0) != weights->quantization_info().scale().size()); + } + else + { + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output); + } + if(biases != nullptr) { ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(0)); + + if(is_data_type_quantized_asymmetric(input->data_type())) + { + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); + } + else + { + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); + } } if(output->total_size() != 0) @@ -216,7 +449,6 @@ Status validate_arguments(const ITensorInfo *input, const ITensorInfo *weights, return Status{}; } -} // namespace std::pair validate_and_configure_window(ITensorInfo *input, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *output, const PadStrideInfo &conv_info, @@ -226,7 +458,7 @@ std::pair validate_and_configure_window(ITensorInfo *input, ITen const TensorShape output_shape = misc::shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation); // Output auto inizialitation if not yet initialized - auto_init_if_empty(*output, input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape)); + auto_init_if_empty(*output, input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape).set_quantization_info(output->quantization_info())); // Configure kernel window (generic) const unsigned int num_elems_read_per_iteration = (depth_multiplier == 1) ? 8 / element_size_from_data_type(input->data_type()) : 1; @@ -253,9 +485,10 @@ std::pair validate_and_configure_window(ITensorInfo *input, ITen Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; return std::make_pair(err, win); } +} // namespace NEDepthwiseConvolutionLayerNativeKernel::NEDepthwiseConvolutionLayerNativeKernel() - : _func(), _border_size(0), _input(), _weights(), _biases(), _output(), _conv_info(), _depth_multiplier(1), _dilation() + : _func(), _border_size(0), _input(), _weights(), _biases(), _output(), _conv_info(), _depth_multiplier(1), _dilation(), _output_multiplier(), _output_shift() { } @@ -279,10 +512,56 @@ void NEDepthwiseConvolutionLayerNativeKernel::configure(const ITensor *input, co _border_size = BorderSize(_conv_info.pad_left(), 0, std::max(std::max(conv_info.pad_right(), conv_info.pad_bottom()), conv_info.pad_top()), 0); _dilation = dilation; - switch(_input->info()->data_type()) + if(is_data_type_quantized(_input->info()->data_type())) + { + const auto input_scale = input->info()->quantization_info().uniform().scale; + const auto output_scale = output->info()->quantization_info().uniform().scale; + + auto weights_scale = weights->info()->quantization_info().scale(); + if(!is_data_type_quantized_per_channel(_weights->info()->data_type())) + { + for(size_t i = 1; i < _weights->info()->dimension(0); ++i) + { + weights_scale.push_back(weights_scale.front()); + } + } + + for(size_t i = 0; i < weights_scale.size(); ++i) + { + int out_mult = 0; + int out_shift = 0; + const float multiplier = input_scale * weights_scale.at(i) / output_scale; + ARM_COMPUTE_ERROR_ON(multiplier > 1.f); + arm_compute::quantization::calculate_quantized_multiplier_less_than_one(multiplier, &out_mult, &out_shift); + + _output_multiplier.push_back(out_mult); + _output_shift.push_back(out_shift); + } + } + + switch(_weights->info()->data_type()) { + case DataType::QASYMM8: + _func = (biases != nullptr) ? &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise : + &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise; + pad_vectors(_output_multiplier, _output_shift, 8); + break; + case DataType::QSYMM8_PER_CHANNEL: + _func = (biases != nullptr) ? &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise : + &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise; + pad_vectors(_output_multiplier, _output_shift, 8); + break; +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F16: + _func = (biases != nullptr) ? &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise : + &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise; + pad_vectors(_output_multiplier, _output_shift, 4); + break; +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC case DataType::F32: - _func = (biases != nullptr) ? &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise : &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise; + _func = (biases != nullptr) ? &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise : + &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise; + pad_vectors(_output_multiplier, _output_shift, 2); break; default: ARM_COMPUTE_ERROR("Data type not supported"); @@ -314,7 +593,28 @@ void NEDepthwiseConvolutionLayerNativeKernel::run(const Window &window, const Th (this->*_func)(window); } -template +template < typename T, typename TW, int S, bool has_biases, bool is_per_channel, typename std::enable_if < std::is_same::value +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + || std::is_same::value +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + , + int >::type > +void NEDepthwiseConvolutionLayerNativeKernel::run_depthwise(const Window &window) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); + + if(_depth_multiplier == 1) + { + depthwise_loop_multiplier1_fp(_input, _weights, _biases, _output, _conv_info, _dilation, window); + } + else + { + depthwise_loop_generic_fp(_input, _weights, _biases, _output, _conv_info, _dilation, _depth_multiplier, window); + } +} + +template ::value, int>::type> void NEDepthwiseConvolutionLayerNativeKernel::run_depthwise(const Window &window) { ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); @@ -322,11 +622,11 @@ void NEDepthwiseConvolutionLayerNativeKernel::run_depthwise(const Window &window if(_depth_multiplier == 1) { - depthwise_loop_multiplier1(_input, _weights, _biases, _output, _conv_info, _dilation, window); + depthwise_loop_multiplier1_quantized(_input, _weights, _biases, _output, _conv_info, _dilation, _output_multiplier, _output_shift, window); } else { - depthwise_loop_generic(_input, _weights, _biases, _output, _conv_info, _dilation, _depth_multiplier, window); + depthwise_loop_generic_quantized(_input, _weights, _biases, _output, _conv_info, _dilation, _depth_multiplier, _output_multiplier, _output_shift, window); } } } // namespace arm_compute diff --git a/src/core/NEON/kernels/NEDepthwiseIm2ColKernel.cpp b/src/core/NEON/kernels/NEDepthwiseIm2ColKernel.cpp deleted file mode 100644 index 53789e2472..0000000000 --- a/src/core/NEON/kernels/NEDepthwiseIm2ColKernel.cpp +++ /dev/null @@ -1,197 +0,0 @@ -/* - * Copyright (c) 2017-2019 ARM Limited. - * - * SPDX-License-Identifier: MIT - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -#include "arm_compute/core/NEON/kernels/NEDepthwiseIm2ColKernel.h" - -#include "arm_compute/core/Coordinates.h" -#include "arm_compute/core/Error.h" -#include "arm_compute/core/Helpers.h" -#include "arm_compute/core/ITensor.h" -#include "arm_compute/core/NEON/INEKernel.h" -#include "arm_compute/core/TensorInfo.h" -#include "arm_compute/core/TensorShape.h" -#include "arm_compute/core/Types.h" -#include "arm_compute/core/Validate.h" -#include "arm_compute/core/Window.h" - -using namespace arm_compute; - -namespace -{ -Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, - const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int depth_multiplier, const Size2D &dilation) -{ - ARM_COMPUTE_UNUSED(conv_info); - //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions. - ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); - ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(input->data_type()) && has_bias); - ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(2) * depth_multiplier) != output->dimension(2)); - ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(0) != (kernel_dims.width * kernel_dims.height + ((has_bias) ? 1 : 0))); - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); - ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || dilation.y() < 1); - - return Status{}; -} -} // namespace - -template -void NEDepthwiseIm2ColKernel::run_generic(const Window &window) -{ - const int input_w = _input->info()->dimension(0); - const int input_h = _input->info()->dimension(1); - const int input_stride_x = _input->info()->strides_in_bytes().x(); - const int input_stride_y = _input->info()->strides_in_bytes().y(); - const int input_stride_z = _input->info()->strides_in_bytes().z(); - const int stride_x = _conv_info.stride().first; - const int stride_y = _conv_info.stride().second; - - const int pad_left = _conv_info.pad_left(); - const int pad_right = _conv_info.pad_right(); - const int pad_top = _conv_info.pad_top(); - - Window window_in(window); - // The first three dimensions of the input are increased by the inner loops - window_in.set(Window::DimX, Window::Dimension(0, 0, 0)); - window_in.set(Window::DimY, Window::Dimension(0, 0, 0)); - window_in.set(Window::DimZ, Window::Dimension(0, 0, 0)); - - // Setup output window - Window window_out(window); - window_out.set(Window::DimX, Window::Dimension(0, _output->info()->dimension(0), _output->info()->dimension(0))); - window_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), 1)); - window_out.set(Window::DimZ, Window::Dimension(0, _output->info()->dimension(2), 1)); - - Iterator in(_input, window_in); - Iterator out(_output, window_out); - - const int full_length = input_w + pad_left + pad_right; - const int max_initial_x = stride_x * (((full_length - (_kernel_dims.width + (_kernel_dims.width - 1) * (_dilation.x() - 1))) / stride_x) + 1); - - // Define pad value - auto zero = static_cast(0); - if(std::is_same::value) - { - zero = _input->info()->quantization_info().uniform().offset; - } - - execute_window_loop(window_out, [&](const Coordinates & id) - { - const int src_pixel_linear = id.y() * stride_x; - - const int src_x = -pad_left + src_pixel_linear % max_initial_x; - const int src_y = -pad_top + src_pixel_linear / max_initial_x * stride_y; - - // Get pointers - const uint8_t *const input_ptr = in.ptr() + id.z() / _depth_multiplier * input_stride_z; - auto output_ptr = reinterpret_cast(out.ptr()); - const int height = src_y + (_kernel_dims.height + (_kernel_dims.height - 1) * (_dilation.y() - 1)); - const int width = src_x + (_kernel_dims.width + (_kernel_dims.width - 1) * (_dilation.x() - 1)); - - for(int y = src_y; y < height; y += _dilation.y()) - { - for(int x = src_x; x < width; x += _dilation.x(), ++output_ptr) - { - if(x < 0 || x >= input_w || y < 0 || y >= input_h) - { - *output_ptr = zero; - } - else - { - *output_ptr = *(reinterpret_cast(input_ptr + x * input_stride_x + y * input_stride_y)); - } - } - } - - if(_has_bias) - { - *output_ptr = static_cast(1); - } - }, - in, out); -} - -NEDepthwiseIm2ColKernel::NEDepthwiseIm2ColKernel() - : _func(nullptr), _input(nullptr), _output(nullptr), _kernel_dims(), _conv_info(), _has_bias(), _depth_multiplier(1), _dilation() -{ -} - -void NEDepthwiseIm2ColKernel::configure(const ITensor *input, ITensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int depth_multiplier, - const Size2D &dilation) -{ - ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); - - ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), kernel_dims, conv_info, has_bias, depth_multiplier, dilation)); - - _input = input; - _output = output; - _kernel_dims = kernel_dims; - _conv_info = conv_info; - _has_bias = has_bias; - _depth_multiplier = depth_multiplier; - _dilation = dilation; - - // Configure kernel window - Window win = calculate_max_window(*input->info(), Steps()); - - // Set appropriate function to run - switch(input->info()->data_type()) - { - case DataType::QASYMM8: - _func = &NEDepthwiseIm2ColKernel::run_generic; - break; - case DataType::F16: - _func = &NEDepthwiseIm2ColKernel::run_generic; - break; - case DataType::F32: - _func = &NEDepthwiseIm2ColKernel::run_generic; - break; - default: - ARM_COMPUTE_ERROR("Unsupported data type"); - } - - // The NEDepthwiseIm2ColKernel doesn't need padding so update_window_and_padding() can be skipped - output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape())); - - INEKernel::configure(win); -} - -Status NEDepthwiseIm2ColKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int depth_multiplier, - const Size2D &dilation) -{ - ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); - ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, kernel_dims, conv_info, has_bias, depth_multiplier, dilation)); - return Status{}; -} - -void NEDepthwiseIm2ColKernel::run(const Window &window, const ThreadInfo &info) -{ - ARM_COMPUTE_UNUSED(info); - ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); - ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); - - if(_func != nullptr) - { - (this->*_func)(window); - } -} diff --git a/src/core/NEON/kernels/NEDepthwiseVectorToTensorKernel.cpp b/src/core/NEON/kernels/NEDepthwiseVectorToTensorKernel.cpp deleted file mode 100644 index 37269cafaf..0000000000 --- a/src/core/NEON/kernels/NEDepthwiseVectorToTensorKernel.cpp +++ /dev/null @@ -1,156 +0,0 @@ -/* - * Copyright (c) 2017-2019 ARM Limited. - * - * SPDX-License-Identifier: MIT - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -#include "arm_compute/core/NEON/kernels/NEDepthwiseVectorToTensorKernel.h" - -#include "arm_compute/core/CPP/Validate.h" -#include "arm_compute/core/Coordinates.h" -#include "arm_compute/core/Error.h" -#include "arm_compute/core/Helpers.h" -#include "arm_compute/core/ITensor.h" -#include "arm_compute/core/NEON/INEKernel.h" -#include "arm_compute/core/TensorInfo.h" -#include "arm_compute/core/TensorShape.h" -#include "arm_compute/core/Types.h" -#include "arm_compute/core/Validate.h" -#include "arm_compute/core/Window.h" -#include "arm_compute/core/utils/misc/ShapeCalculator.h" - -using namespace arm_compute; -using namespace arm_compute::misc::shape_calculator; - -namespace -{ -Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, size_t conv_w, size_t conv_h) -{ - ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); - ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::S32, DataType::F16, DataType::F32); - - if(output->total_size() != 0) - { - TensorShape output_shape = compute_vector_to_tensor_output_shape(input->tensor_shape(), conv_w, conv_h, output->data_layout()); - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape); - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); - } - - return Status{}; -} -} // namespace - -template -void NEDepthwiseVectorToTensorKernel::vector_to_tensor(const Window &window) -{ - // const int input_w = _input->info()->dimension(0); - const int output_stride_x = _output->info()->strides_in_bytes().x(); - const int output_stride_y = _output->info()->strides_in_bytes().y(); - const int output_stride_z = _output->info()->strides_in_bytes().z(); - - // Setup output window - Window window_out(window); - window_out.set(Window::DimX, Window::Dimension(0, 0, 0)); - window_out.set(Window::DimY, Window::Dimension(0, 0, 0)); - window_out.set(Window::DimZ, Window::Dimension(0, 0, 0)); - - Iterator in(_input, window); - Iterator out(_output, window_out); - - const int patch_size = _conv_dims.first * _conv_dims.second; - - execute_window_loop(window, [&](const Coordinates & id) - { - const int z = id.x() / patch_size; - const int index2D = id.x() - z * patch_size; - - auto input_ptr = reinterpret_cast(in.ptr()); - auto output_ptr = reinterpret_cast(out.ptr() + index2D % _conv_dims.first * output_stride_x + index2D / _conv_dims.first * output_stride_y + z * output_stride_z); - - *output_ptr = *input_ptr; - }, - in, out); -} - -NEDepthwiseVectorToTensorKernel::NEDepthwiseVectorToTensorKernel() - : _func(nullptr), _input(nullptr), _output(nullptr), _conv_dims() -{ -} - -void NEDepthwiseVectorToTensorKernel::configure(const ITensor *input, ITensor *output, size_t conv_w, size_t conv_h) -{ - ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); - - // Output auto inizialitation if not yet initialized - TensorShape output_shape = compute_vector_to_tensor_output_shape(input->info()->tensor_shape(), conv_w, conv_h, output->info()->data_layout()); - auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape)); - - ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), conv_w, conv_h)); - - _input = input; - _output = output; - _conv_dims = std::pair(conv_w, conv_h); - - // Set appropriate function to run - switch(input->info()->data_type()) - { - case DataType::QASYMM8: - _func = &NEDepthwiseVectorToTensorKernel::vector_to_tensor; - break; - case DataType::S32: - _func = &NEDepthwiseVectorToTensorKernel::vector_to_tensor; - break; - case DataType::F16: - _func = &NEDepthwiseVectorToTensorKernel::vector_to_tensor; - break; - case DataType::F32: - _func = &NEDepthwiseVectorToTensorKernel::vector_to_tensor; - break; - default: - ARM_COMPUTE_ERROR("Unsupported data type"); - } - - // Configure kernel window - Window win = calculate_max_window(*input->info(), Steps()); - // The NEDepthwisevectorToTensorKernel doesn't need padding so update_window_and_padding() can be skipped - output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape())); - - INEKernel::configure(win); -} - -Status NEDepthwiseVectorToTensorKernel::validate(const ITensorInfo *input, const ITensorInfo *output, size_t conv_w, size_t conv_h) -{ - ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); - ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, conv_w, conv_h)); - return Status{}; -} - -void NEDepthwiseVectorToTensorKernel::run(const Window &window, const ThreadInfo &info) -{ - ARM_COMPUTE_UNUSED(info); - ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); - ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); - - if(_func != nullptr) - { - (this->*_func)(window); - } -} diff --git a/src/core/NEON/kernels/NEDepthwiseWeightsReshapeKernel.cpp b/src/core/NEON/kernels/NEDepthwiseWeightsReshapeKernel.cpp deleted file mode 100644 index b0e1fcb3f8..0000000000 --- a/src/core/NEON/kernels/NEDepthwiseWeightsReshapeKernel.cpp +++ /dev/null @@ -1,165 +0,0 @@ -/* - * Copyright (c) 2017-2019 ARM Limited. - * - * SPDX-License-Identifier: MIT - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -#include "arm_compute/core/NEON/kernels/NEDepthwiseWeightsReshapeKernel.h" - -#include "arm_compute/core/Coordinates.h" -#include "arm_compute/core/Error.h" -#include "arm_compute/core/Helpers.h" -#include "arm_compute/core/ITensor.h" -#include "arm_compute/core/NEON/INEKernel.h" -#include "arm_compute/core/TensorInfo.h" -#include "arm_compute/core/TensorShape.h" -#include "arm_compute/core/Types.h" -#include "arm_compute/core/Validate.h" -#include "arm_compute/core/Window.h" - -using namespace arm_compute; - -namespace -{ -template -void weights_reshape(const ITensor *input, const ITensor *bias, ITensor *output, const Window &window) -{ - const int input_w = input->info()->dimension(0); - const int output_stride_x = output->info()->strides_in_bytes().x(); - const int output_stride_y = output->info()->strides_in_bytes().y(); - - Window window_in(window); - // The first three dimensions of the input are increased by the inner loops - window_in.set(Window::DimX, Window::Dimension(0, input->info()->dimension(0), input->info()->dimension(0))); - window_in.set(Window::DimY, Window::Dimension(0, input->info()->dimension(1), 1)); - window_in.set(Window::DimZ, Window::Dimension(0, input->info()->dimension(2), 1)); - - // Setup output window - Window window_out; - window_out.set(Window::DimX, Window::Dimension(0, 0, 0)); - window_out.set(Window::DimY, Window::Dimension(0, 0, 0)); - - Iterator in(input, window_in); - Iterator out(output, window_out); - - execute_window_loop(window_in, [&](const Coordinates & id) - { - auto input_ptr = reinterpret_cast(in.ptr()); - auto output_ptr = reinterpret_cast(out.ptr() + id.y() * input_w * output_stride_x + id.z() * output_stride_y); - - for(int i = 0; i < input_w; ++i, ++input_ptr) - { - *(output_ptr + i) = *input_ptr; - } - - if(bias != nullptr) - { - *(output_ptr + input_w) = *(reinterpret_cast(bias->ptr_to_element(Coordinates(id.z())))); - } - }, - in, out); -} - -Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *biases) -{ - //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions. - ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); - ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(input->data_type()) && (biases != nullptr)); - ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) != output->dimension(1)); - ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(0) != (input->dimension(0) * input->dimension(1) + ((biases != nullptr) ? 1 : 0))); - - if(biases != nullptr) - { - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); - ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != input->dimension(2)); - ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); - } - - return Status{}; -} -} // namespace - -NEDepthwiseWeightsReshapeKernel::NEDepthwiseWeightsReshapeKernel() - : _func(nullptr), _input(nullptr), _output(nullptr), _biases(nullptr) -{ -} - -void NEDepthwiseWeightsReshapeKernel::configure(const ITensor *input, ITensor *output, const ITensor *biases) -{ - ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); - - ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), (biases != nullptr) ? biases->info() : nullptr)); - - _input = input; - _output = output; - _biases = biases; - - switch(_input->info()->element_size()) - { - case 4: - { - _func = &weights_reshape; - break; - } - case 2: - { - _func = &weights_reshape; - break; - } - case 1: - { - _func = &weights_reshape; - break; - } - default: - { - ARM_COMPUTE_ERROR_ON("Element size not supported"); - break; - } - } - - // Configure kernel window - Window win = calculate_max_window(*input->info(), Steps()); - // The NEDepthwiseWeightsReshapeKernel doesn't need padding so update_window_and_padding() can be skipped - output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape())); - - INEKernel::configure(win); -} - -Status NEDepthwiseWeightsReshapeKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *biases) -{ - ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); - ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, biases)); - return Status{}; -} - -void NEDepthwiseWeightsReshapeKernel::run(const Window &window, const ThreadInfo &info) -{ - ARM_COMPUTE_UNUSED(info); - ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); - ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); - - if(_func != nullptr) - { - (*_func)(_input, _biases, _output, window); - } -} diff --git a/src/core/NEON/kernels/NEPermuteKernel.cpp b/src/core/NEON/kernels/NEPermuteKernel.cpp index 1df94aef06..897b764b45 100644 --- a/src/core/NEON/kernels/NEPermuteKernel.cpp +++ b/src/core/NEON/kernels/NEPermuteKernel.cpp @@ -91,12 +91,6 @@ inline bool is_permutation_supported(const PermutationVector &v) Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm) { - //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions. - ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QASYMM8, - DataType::U16, DataType::S16, - DataType::U32, DataType::S32, - DataType::F16, DataType::F32); - ARM_COMPUTE_RETURN_ERROR_ON_MSG(!is_permutation_supported(perm), "PermutationVector not supported."); const TensorShape output_shape = misc::shape_calculator::compute_permutation_output_shape(*input, perm); diff --git a/src/runtime/NEON/functions/NEDepthwiseConvolutionLayer.cpp b/src/runtime/NEON/functions/NEDepthwiseConvolutionLayer.cpp index fbdee84474..76ae1fba3a 100644 --- a/src/runtime/NEON/functions/NEDepthwiseConvolutionLayer.cpp +++ b/src/runtime/NEON/functions/NEDepthwiseConvolutionLayer.cpp @@ -23,15 +23,10 @@ */ #include "arm_compute/runtime/NEON/functions/NEDepthwiseConvolutionLayer.h" -#include "arm_compute/core/Helpers.h" -#include "arm_compute/core/ITensor.h" -#include "arm_compute/core/PixelValue.h" +#include "arm_compute/core/utils/misc/InfoHelpers.h" #include "arm_compute/core/utils/misc/ShapeCalculator.h" #include "arm_compute/core/utils/quantization/AsymmHelpers.h" #include "arm_compute/runtime/NEON/NEScheduler.h" -#include "support/ToolchainSupport.h" - -#include "arm_compute/core/utils/misc/InfoHelpers.h" using namespace arm_compute::misc; using namespace arm_compute::misc::shape_calculator; @@ -701,10 +696,8 @@ void NEDepthwiseConvolutionLayerOptimized::prepare() } NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayer() - : _im2col_kernel(), _weights_reshape_kernel(), _v2mm_kernel(), _depthwise_conv_kernel(), _vector_to_tensor_kernel(), _output_stage_kernel(), _fill_border(), _v2mm_input_fill_border(), - _v2mm_weights_fill_border(), _permute_input(), _permute_weights(), _permute_output(), _activationlayer_function(), _input_reshaped(), _weights_reshaped(), _v2mm_output(), _output_reshaped(), - _permuted_input(), _permuted_weights(), _permuted_output(), _is_prepared(false), _is_quantized(false), _is_nhwc(false), _is_activationlayer_enabled(false), _is_optimized(false), - _original_weights(nullptr) + : _depthwise_conv_kernel(), _fill_border(), _permute_input(), _permute_weights(), _permute_output(), _activationlayer_function(), _permuted_input(), _permuted_weights(), _permuted_output(), + _is_prepared(false), _is_nchw(false), _is_activationlayer_enabled(false), _original_weights(nullptr) { } @@ -712,143 +705,45 @@ void NEDepthwiseConvolutionLayer::configure(ITensor *input, const ITensor *weigh unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation) { ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); - // Perform validation step ARM_COMPUTE_ERROR_THROW_ON(NEDepthwiseConvolutionLayer::validate(input->info(), weights->info(), (biases == nullptr) ? nullptr : biases->info(), output->info(), conv_info, depth_multiplier, act_info, dilation)); - _is_nhwc = input->info()->data_layout() == DataLayout::NHWC; - _is_optimized = _is_nhwc && input->info()->data_type() == DataType::F32; + _is_nchw = input->info()->data_layout() == DataLayout::NCHW; + _is_prepared = !_is_nchw; - if(!_is_optimized) + ITensor *input_to_use = input; + const ITensor *weights_to_use = weights; + ITensor *output_to_use = output; + if(_is_nchw) { - ITensor *input_to_use = input; - const ITensor *weights_to_use = weights; - ITensor *output_to_use = output; - - if(_is_nhwc) - { - _permute_input.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U)); - _permuted_input.info()->set_data_layout(DataLayout::NCHW); - input_to_use = &_permuted_input; - - _permute_weights.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U)); - _permuted_weights.info()->set_data_layout(DataLayout::NCHW); - weights_to_use = &_permuted_weights; - } - - const size_t weights_w = weights_to_use->info()->dimension(0); - const size_t weights_h = weights_to_use->info()->dimension(1); - const size_t weights_z = weights_to_use->info()->dimension(2); - - _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type()); - _is_prepared = false; - _original_weights = weights_to_use; - - // Should bias be appended ? - bool append_bias = (biases != nullptr) && !_is_quantized; - - // Calculate output shape - TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info, depth_multiplier, dilation); - - // Output auto inizialitation if not yet initialized - auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape)); - ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); - - if(_is_nhwc) - { - permute(output_shape, PermutationVector(1U, 2U, 0U)); - _permuted_output.allocator()->init(output->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape)); - _permuted_output.info()->set_data_layout(DataLayout::NCHW); - _permuted_output.info()->set_quantization_info(output->info()->quantization_info()); - output_to_use = &_permuted_output; - } - - // Output width and height - const unsigned int conv_w = output_shape.x(); - const unsigned int conv_h = output_shape.y(); - - // Set up intermediate tensors - const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0); - const size_t conv_size = conv_w * conv_h; - - // Im2Col configuration - TensorShape shape_im2col = input_to_use->info()->tensor_shape(); - shape_im2col.set(0, patch_size); - shape_im2col.set(1, conv_size); - shape_im2col.set(2, weights_z); - _input_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col).set_data_layout(DataLayout::NCHW)); - _im2col_kernel.configure(input_to_use, &_input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier, dilation); - - // Weights reshape configuration - const TensorShape shape_weights_reshape(patch_size, weights_z); - _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape).set_data_layout(DataLayout::NCHW)); - _weights_reshape_kernel.configure(weights_to_use, &_weights_reshaped, append_bias ? biases : nullptr); - - // GEMV configuration - DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type(); - TensorShape shape_v2mm_out = input_to_use->info()->tensor_shape(); - shape_v2mm_out.set(0, conv_size * weights_z); - shape_v2mm_out.set(1, 1); - shape_v2mm_out.set(2, 1); - _v2mm_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out).set_data_layout(DataLayout::NCHW)); - _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output); - _output_reshaped.allocator()->init(_v2mm_output.info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape)); - _vector_to_tensor_kernel.configure(&_v2mm_output, (_is_quantized) ? &_output_reshaped : output_to_use, conv_w, conv_h); - - // Output staged configuration - if(_is_quantized) - { - const UniformQuantizationInfo iq_info = input->info()->quantization_info().uniform(); - const UniformQuantizationInfo wq_info = weights->info()->quantization_info().uniform(); - const UniformQuantizationInfo oq_info = output->info()->quantization_info().uniform(); - - float multiplier = (iq_info.scale * wq_info.scale) / oq_info.scale; - int output_multiplier; - int output_shift; - quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); - _output_stage_kernel.configure(&_output_reshaped, biases, output_to_use, output_multiplier, output_shift, oq_info.offset); - _output_reshaped.allocator()->allocate(); - } - - if(_is_nhwc) - { - _permute_output.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U)); + _permute_input.configure(input, &_permuted_input, PermutationVector(2U, 0U, 1U)); + _permuted_input.info()->set_data_layout(DataLayout::NHWC); + input_to_use = &_permuted_input; - _permuted_input.allocator()->allocate(); - _permuted_weights.allocator()->allocate(); - _permuted_output.allocator()->allocate(); - } + _permute_weights.configure(weights, &_permuted_weights, PermutationVector(2U, 0U, 1U)); + _permuted_weights.info()->set_data_layout(DataLayout::NHWC); + weights_to_use = &_permuted_weights; - // Fill borders on inputs - PixelValue zero_in(static_cast(0)); - PixelValue zero_w(static_cast(0)); - if(_is_quantized) - { - zero_in = PixelValue(static_cast(input->info()->quantization_info().uniform().offset)); - zero_w = PixelValue(static_cast(weights->info()->quantization_info().uniform().offset)); - } - BorderSize border_size = _v2mm_kernel.border_size(); - _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in); + _permuted_output.allocator()->init(output->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(TensorShape())); + output_to_use = &_permuted_output; + } + _original_weights = weights_to_use; - border_size.bottom = 0; - _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w); + _depthwise_conv_kernel.configure(input_to_use, weights_to_use, biases, output_to_use, conv_info, depth_multiplier, dilation); + _fill_border.configure(input_to_use, _depthwise_conv_kernel.border_size(), BorderMode::CONSTANT, PixelValue(static_cast(0), input->info()->data_type(), input->info()->quantization_info())); - // Allocate intermediate tensors - _input_reshaped.allocator()->allocate(); - _v2mm_output.allocator()->allocate(); - } - else + if(_is_nchw) { - // Configure kernel - _depthwise_conv_kernel.configure(input, weights, biases, output, conv_info, depth_multiplier, dilation); + _permute_output.configure(&_permuted_output, output, PermutationVector(1U, 2U, 0U)); + _permuted_output.info()->set_data_layout(DataLayout::NHWC); - // Fill input borders - _fill_border.configure(input, _depthwise_conv_kernel.border_size(), BorderMode::CONSTANT, PixelValue(static_cast(0), input->info()->data_type())); + _permuted_input.allocator()->allocate(); + _permuted_weights.allocator()->allocate(); + _permuted_output.allocator()->allocate(); } //Configure Activation Layer _is_activationlayer_enabled = act_info.enabled(); - if(_is_activationlayer_enabled) { _activationlayer_function.configure(output, nullptr, act_info); @@ -859,103 +754,24 @@ Status NEDepthwiseConvolutionLayer::validate(const ITensorInfo *input, const ITe unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation) { ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); - ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN); - ARM_COMPUTE_RETURN_ERROR_ON(dilation.x() < 1 || dilation.y() < 1); - - const unsigned int width_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH); - const unsigned int height_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT); - const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL); - - ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(width_idx) + (weights->dimension(width_idx) - 1) * (dilation.x() - 1) > input->dimension(width_idx) + conv_info.pad_left() + conv_info.pad_right()); - ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(height_idx) + (weights->dimension(height_idx) - 1) * (dilation.y() - 1) > input->dimension(height_idx) + conv_info.pad_top() + conv_info.pad_bottom()); - ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(channel_idx) * depth_multiplier) != weights->dimension(channel_idx)); - - if(input->data_layout() != DataLayout::NHWC || input->data_type() != DataType::F32) + if(input->data_layout() == DataLayout::NCHW) { - // Clone output to use auto init - auto output_clone = output->clone(); - - const ITensorInfo *input_to_use = input; - const ITensorInfo *weights_to_use = weights; - const ITensorInfo *output_to_use = output_clone.get(); - TensorShape permuted_input_shape = input->tensor_shape(); TensorShape permuted_weights_shape = weights->tensor_shape(); - TensorInfo permuted_input; - TensorInfo permuted_weights; - - if(input->data_layout() == DataLayout::NHWC) - { - permute(permuted_input_shape, PermutationVector(1U, 2U, 0U)); - permute(permuted_weights_shape, PermutationVector(1U, 2U, 0U)); - - permuted_input = TensorInfo(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NCHW)); - permuted_weights = TensorInfo(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NCHW)); - - input_to_use = &permuted_input; - weights_to_use = &permuted_weights; - } + TensorShape permuted_output_shape = misc::shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation); + permute(permuted_input_shape, PermutationVector(2U, 0U, 1U)); + permute(permuted_weights_shape, PermutationVector(2U, 0U, 1U)); + permute(permuted_output_shape, PermutationVector(2U, 0U, 1U)); - const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type()); - const bool append_bias = (biases != nullptr) && !is_quantized; - TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation); - const size_t weights_w = weights_to_use->dimension(0); - const size_t weights_h = weights_to_use->dimension(1); - const size_t weights_z = weights_to_use->dimension(2); - const unsigned int conv_w = output_shape[width_idx]; - const unsigned int conv_h = output_shape[height_idx]; - const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0); - const size_t conv_size = conv_w * conv_h; - - // Output auto inizialitation if not yet initialized - auto_init_if_empty(*output_clone, input->clone()->set_tensor_shape(output_shape)); - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape); - - TensorInfo permuted_output; - if(input->data_layout() == DataLayout::NHWC) - { - permute(output_shape, PermutationVector(1U, 2U, 0U)); - permuted_output = TensorInfo(output_clone->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape).set_data_layout(DataLayout::NCHW)); - output_to_use = &permuted_output; - } + const TensorInfo permuted_input = TensorInfo(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NHWC)); + const TensorInfo permuted_weights = TensorInfo(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NHWC)); + const TensorInfo permuted_output = TensorInfo(output->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_output_shape).set_data_layout(DataLayout::NCHW)); - // Im2Col configuration - TensorShape shape_im2col = input_to_use->tensor_shape(); - shape_im2col.set(0, patch_size); - shape_im2col.set(1, conv_size); - shape_im2col.set(2, weights_z); - TensorInfo input_reshaped(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col).set_data_layout(DataLayout::NCHW)); - ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseIm2ColKernel::validate(input_to_use, &input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier, dilation)); - - // Weights reshape configuration - const TensorShape shape_weights_reshape(patch_size, weights_z); - TensorInfo weights_reshaped(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape).set_data_layout(DataLayout::NCHW)); - ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseWeightsReshapeKernel::validate(weights_to_use, &weights_reshaped, append_bias ? biases : nullptr)); - - // GEMV configuration - DataType v2mm_dt = (input->data_type() == DataType::QASYMM8) ? DataType::S32 : input->data_type(); - TensorShape shape_v2mm_out = input_to_use->tensor_shape(); - shape_v2mm_out.set(0, conv_size * weights_z); - shape_v2mm_out.set(1, 1); - shape_v2mm_out.set(2, 1); - TensorInfo v2mm_output(input->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out).set_data_layout(DataLayout::NCHW)); - ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixVectorMultiplyKernel::validate(&input_reshaped, &weights_reshaped, &v2mm_output)); - - TensorInfo output_reshaped(v2mm_output.clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_to_use->tensor_shape())); - ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseVectorToTensorKernel::validate(&v2mm_output, (is_quantized) ? &output_reshaped : output_to_use, conv_w, conv_h)); + ARM_COMPUTE_RETURN_ON_ERROR(NEPermute::validate(input, &permuted_input, PermutationVector(2U, 0U, 1U))); + ARM_COMPUTE_RETURN_ON_ERROR(NEPermute::validate(weights, &permuted_weights, PermutationVector(2U, 0U, 1U))); + ARM_COMPUTE_RETURN_ON_ERROR(NEPermute::validate(&permuted_output, output, PermutationVector(1U, 2U, 0U))); - if(is_quantized) - { - const UniformQuantizationInfo iq_info = input->quantization_info().uniform(); - const UniformQuantizationInfo wq_info = weights->quantization_info().uniform(); - const UniformQuantizationInfo oq_info = output->quantization_info().uniform(); - - float multiplier = (iq_info.scale * wq_info.scale) / oq_info.scale; - int output_multiplier; - int output_shift; - ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift)); - ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayerOutputStageKernel::validate(&output_reshaped, biases, output_to_use, output_multiplier, output_shift, oq_info.offset)); - } + ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseConvolutionLayerNativeKernel::validate(&permuted_input, &permuted_weights, biases, &permuted_output, conv_info, depth_multiplier, dilation)); } else { @@ -973,33 +789,18 @@ Status NEDepthwiseConvolutionLayer::validate(const ITensorInfo *input, const ITe void NEDepthwiseConvolutionLayer::run() { - if(!_is_optimized) + if(_is_nchw) { prepare(); + _permute_input.run(); + } - if(_is_nhwc) - { - _permute_input.run(); - } - - NEScheduler::get().schedule(&_im2col_kernel, Window::DimX); - NEScheduler::get().schedule(&_v2mm_input_fill_border, Window::DimX); - NEScheduler::get().schedule(&_v2mm_kernel, Window::DimX); - NEScheduler::get().schedule(&_vector_to_tensor_kernel, Window::DimX); - if(_is_quantized) - { - NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX); - } + NEScheduler::get().schedule(&_fill_border, Window::DimX); + NEScheduler::get().schedule(&_depthwise_conv_kernel, Window::DimY); - if(_is_nhwc) - { - _permute_output.run(); - } - } - else + if(_is_nchw) { - NEScheduler::get().schedule(&_fill_border, Window::DimX); - NEScheduler::get().schedule(&_depthwise_conv_kernel, Window::DimY); + _permute_output.run(); } if(_is_activationlayer_enabled) @@ -1010,21 +811,12 @@ void NEDepthwiseConvolutionLayer::run() void NEDepthwiseConvolutionLayer::prepare() { - if(!_is_prepared && !_is_optimized) + if(!_is_prepared) { ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); - if(_is_nhwc) - { - _permute_weights.run(); - } - - // Run reshape and mark original weights as unused - _weights_reshaped.allocator()->allocate(); - NEScheduler::get().schedule(&_weights_reshape_kernel, Window::DimX); - NEScheduler::get().schedule(&_v2mm_weights_fill_border, Window::DimX); + _permute_weights.run(); _original_weights->mark_as_unused(); - _is_prepared = true; } } diff --git a/support/ToolchainSupport.h b/support/ToolchainSupport.h index b4ed2fa9a4..deaded34f3 100644 --- a/support/ToolchainSupport.h +++ b/support/ToolchainSupport.h @@ -35,6 +35,10 @@ #include #include +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC +#include +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + #include "support/Half.h" namespace arm_compute @@ -228,7 +232,11 @@ inline T copysign(T x, T y) * * @return Result floating point value equal to (x*y) + z.c */ -template ::value>::type> +template < typename T, typename = typename std::enable_if < std::is_floating_point::value +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + || std::is_same::value +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + >::type > inline T fma(T x, T y, T z) { return ::fma(x, y, z); @@ -250,7 +258,7 @@ inline int snprintf(char *s, size_t n, const char *fmt, Ts &&... args) { return ::snprintf(s, n, fmt, std::forward(args)...); } -#else /* (__ANDROID__ || BARE_METAL) */ +#else /* (__ANDROID__ || BARE_METAL) */ /** Convert integer and float values to string. * * @note This function acts as a convenience wrapper around std::to_string. The @@ -354,7 +362,11 @@ inline T copysign(T x, T y) * * @return Result floating point value equal to (x*y) + z. */ -template ::value>::type> +template < typename T, typename = typename std::enable_if < std::is_floating_point::value +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + || std::is_same::value +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + >::type > inline T fma(T x, T y, T z) { return std::fma(x, y, z); diff --git a/tests/validation/NEON/DepthwiseConvolutionLayer.cpp b/tests/validation/NEON/DepthwiseConvolutionLayer.cpp index 6392906037..0b482ff9b3 100644 --- a/tests/validation/NEON/DepthwiseConvolutionLayer.cpp +++ b/tests/validation/NEON/DepthwiseConvolutionLayer.cpp @@ -163,16 +163,16 @@ DATA_TEST_CASE(Validate3x3, framework::DatasetMode::ALL, zip(zip(zip(zip(zip(zip } DATA_TEST_CASE(ValidateGeneric, framework::DatasetMode::ALL, zip(zip(zip(zip(zip(zip(zip( - framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data type input/weights - TensorInfo(TensorShape(27U, 13U, 3U), 1, DataType::F32), // Mismatching input feature maps - TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching depth multiplier - TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Invalid biases size - TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Invalid biases dimensions - TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Invalid output size - TensorInfo(TensorShape(27U, 13U, 8U), 1, DataType::F32), // patch size bigger than input width - TensorInfo(TensorShape(27U, 13U, 8U), 1, DataType::F32), // dilation < 1 - TensorInfo(TensorShape(27U, 13U, 8U), 1, DataType::F32), - TensorInfo(TensorShape(32U, 13U, 8U), 1, DataType::QASYMM8), + framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data type input/weights + TensorInfo(TensorShape(27U, 13U, 3U), 1, DataType::F32), // Mismatching input feature maps + TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching depth multiplier + TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Invalid biases size + TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Invalid biases dimensions + TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Invalid output size + TensorInfo(TensorShape(27U, 13U, 8U), 1, DataType::F32), // Patch size bigger than input width + TensorInfo(TensorShape(27U, 13U, 8U), 1, DataType::F32), // Dilation < 1 + TensorInfo(TensorShape(27U, 13U, 8U), 1, DataType::F32), // Window shrinking + TensorInfo(TensorShape(32U, 13U, 8U), 1, DataType::QASYMM8), // Window shrinking }), framework::dataset::make("WeightsInfo", { TensorInfo(TensorShape(3U, 3U, 2U), 1, DataType::F16), TensorInfo(TensorShape(3U, 3U, 2U), 1, DataType::F32), @@ -240,7 +240,7 @@ DATA_TEST_CASE(ValidateGeneric, framework::DatasetMode::ALL, zip(zip(zip(zip(zip Size2D(1U, 1U), Size2D(1U, 1U), })), - framework::dataset::make("Expected", { false, false, false, false, false, false,false, false, true, true })), + framework::dataset::make("Expected", { false, false, false, false, false, false,false, false, false, false })), input_info, weights_info, biases_info, output_info, conv_info, depth_multiplier,dilation, expected) { bool is_valid = bool(NEDepthwiseConvolutionLayer::validate(&input_info.clone()->set_is_resizable(false), &weights_info.clone()->set_is_resizable(false), &biases_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), conv_info, depth_multiplier, ActivationLayerInfo(), dilation)); @@ -512,7 +512,8 @@ TEST_SUITE_END() // Float template using NEDepthwiseConvolutionLayerQuantizedFixtureOptimized = DepthwiseConvolutionLayerValidationQuantizedFixture; template -using NEDepthwiseConvolutionLayerQuantizedFixture = DepthwiseConvolutionLayerValidationQuantizedFixture; +using NEDepthwiseConvolutionLayerQuantizedFixture = DepthwiseConvolutionLayerValidationQuantizedFixture; +using NEDepthwiseConvolutionLayerQuantizedSymmetricPerChannelFixture = DepthwiseConvolutionLayerValidationQuantizedPerChannelFixture; TEST_SUITE(Quantized) TEST_SUITE(QASYMM8) @@ -642,6 +643,49 @@ FIXTURE_DATA_TEST_CASE(RunLarge3x3, NEDepthwiseConvolutionLayerQuantizedFixtureO } TEST_SUITE_END() // Optimized TEST_SUITE_END() // QASYMM8 +TEST_SUITE(QSYMM8_PER_CHANNEL) +TEST_SUITE(Generic) +FIXTURE_DATA_TEST_CASE(RunSmall, NEDepthwiseConvolutionLayerQuantizedSymmetricPerChannelFixture, framework::DatasetMode::ALL, + combine(combine(combine(combine(combine(combine(combine(datasets::SmallDepthwiseConvolutionLayerDataset(), + depth_multipliers), + framework::dataset::make("InputDataType", DataType::QASYMM8)), + framework::dataset::make("WeightsDataType", DataType::QSYMM8_PER_CHANNEL)), + framework::dataset::make("SrcQuantizationInfo", { QuantizationInfo(0.3f, 10) })), + framework::dataset::make("DstQuantizationInfo", { QuantizationInfo(0.5f, 4) })), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })), + ActivationFunctionsDataset)) +{ + validate(Accessor(_target), _reference, tolerance_qasymm8); +} + +TEST_SUITE(Dilation) +FIXTURE_DATA_TEST_CASE(RunSmall, NEDepthwiseConvolutionLayerQuantizedSymmetricPerChannelFixture, framework::DatasetMode::ALL, + combine(combine(combine(combine(combine(combine(combine(datasets::SmallDepthwiseDilatedConvolutionLayerDataset(), + depth_multipliers), + framework::dataset::make("InputDataType", DataType::QASYMM8)), + framework::dataset::make("WeightsDataType", DataType::QSYMM8_PER_CHANNEL)), + framework::dataset::make("SrcQuantizationInfo", { QuantizationInfo(0.3f, 10) })), + framework::dataset::make("DstQuantizationInfo", { QuantizationInfo(0.5f, 4) })), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })), + ActivationFunctionsDataset)) +{ + validate(Accessor(_target), _reference, tolerance_qasymm8); +} +FIXTURE_DATA_TEST_CASE(RunLarge, NEDepthwiseConvolutionLayerQuantizedSymmetricPerChannelFixture, framework::DatasetMode::NIGHTLY, + combine(combine(combine(combine(combine(combine(combine(datasets::LargeDepthwiseDilatedConvolutionLayerDataset(), + depth_multipliers), + framework::dataset::make("InputDataType", DataType::QASYMM8)), + framework::dataset::make("WeightsDataType", DataType::QSYMM8_PER_CHANNEL)), + framework::dataset::make("SrcQuantizationInfo", { QuantizationInfo(0.3f, 10) })), + framework::dataset::make("DstQuantizationInfo", { QuantizationInfo(0.5f, 4) })), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })), + ActivationFunctionsDataset)) +{ + validate(Accessor(_target), _reference, tolerance_qasymm8); +} +TEST_SUITE_END() // Dilation +TEST_SUITE_END() // Generic +TEST_SUITE_END() // QSYMM8_PER_CHANNEL TEST_SUITE_END() // Quantized TEST_SUITE_END() // DepthwiseConvLayer diff --git a/tests/validation/fixtures/DepthwiseConvolutionLayerFixture.h b/tests/validation/fixtures/DepthwiseConvolutionLayerFixture.h index 85930eb95e..f909885245 100644 --- a/tests/validation/fixtures/DepthwiseConvolutionLayerFixture.h +++ b/tests/validation/fixtures/DepthwiseConvolutionLayerFixture.h @@ -449,12 +449,22 @@ class DepthwiseConvolutionLayerValidationQuantizedPerChannelFixture : public Dep public: template void setup(TensorShape in_shape, Size2D kernel_size, PadStrideInfo pad_stride_info, Size2D dilation, unsigned int depth_multiplier, DataType input_data_type, DataType weights_data_type, - QuantizationInfo input_quantization_info, QuantizationInfo weights_quantization_info, QuantizationInfo output_quantization_info, - DataLayout data_layout, ActivationLayerInfo act_info) + QuantizationInfo input_quantization_info, QuantizationInfo output_quantization_info, DataLayout data_layout, ActivationLayerInfo act_info) { + const float out_scale = output_quantization_info.uniform().scale; + const float in_scale = input_quantization_info.uniform().scale; + + std::vector weights_scales{}; + std::mt19937 gen(library->seed()); + std::uniform_real_distribution<> dis(0.01f, out_scale / in_scale); + for(size_t i = 0; i < in_shape.z() * depth_multiplier; ++i) + { + weights_scales.push_back(dis(gen)); + } + DepthwiseConvolutionLayerValidationGenericFixture::setup(in_shape, kernel_size, pad_stride_info, dilation, depth_multiplier, input_data_type, weights_data_type, - input_quantization_info, weights_quantization_info, output_quantization_info, + input_quantization_info, QuantizationInfo(weights_scales), output_quantization_info, data_layout, act_info); } }; diff --git a/tests/validation/reference/DepthwiseConvolutionLayer.cpp b/tests/validation/reference/DepthwiseConvolutionLayer.cpp index 7458f815b8..608093d381 100644 --- a/tests/validation/reference/DepthwiseConvolutionLayer.cpp +++ b/tests/validation/reference/DepthwiseConvolutionLayer.cpp @@ -188,17 +188,17 @@ SimpleTensor depthwise_convolution_quantized(const SimpleTensor &src, cons { for(int z = 0; z < input_depth; ++z) { - int output_multiplier = 0; - int output_shift = 0; - const float weights_scale = (is_quantized_per_channel) ? weights_scale_vec[z] : weights_scale_vec[0]; - const float multiplier = input_scale * weights_scale / output_scale; - arm_compute::quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); - for(unsigned int m = 0; m < depth_multiplier; ++m) { const int out_z = z * depth_multiplier + m; const int32_t bias_val = *static_cast(biases(Coordinates(out_z))); + int output_multiplier = 0; + int output_shift = 0; + const float weights_scale = (is_quantized_per_channel) ? weights_scale_vec[out_z] : weights_scale_vec[0]; + const float multiplier = input_scale * weights_scale / output_scale; + arm_compute::quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); + for(int y = minimum_y; y <= minimum_y + maximum_y; y += conv_info.stride().second) { for(int x = minimum_x; x <= minimum_x + maximum_x; x += conv_info.stride().first) -- cgit v1.2.1