blob: 713cdcd9d99b1e33f8f447ca27e05fce9230e362 [file] [log] [blame]
Giorgio Arena44f55722019-07-12 14:49:49 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 Arm Limited.
Giorgio Arena44f55722019-07-12 14:49:49 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_NEDEPTHWISECONVOLUTIONLAYERNATIVEKERNEL_H
25#define ARM_COMPUTE_NEDEPTHWISECONVOLUTIONLAYERNATIVEKERNEL_H
Giorgio Arena44f55722019-07-12 14:49:49 +010026
Sang-Hoon Parke4558b52020-10-01 10:13:07 +010027#include "arm_compute/core/utils/misc/Traits.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010028#include "src/core/NEON/INEKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010029#include "support/Requires.h"
Giorgio Arena44f55722019-07-12 14:49:49 +010030
Giorgio Arenad93e2632019-10-15 11:09:33 +010031#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
32#include <arm_neon.h>
33#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
34
Giorgio Arena44f55722019-07-12 14:49:49 +010035namespace arm_compute
36{
37// Forward declarations
38class ITensor;
39
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +010040/** Interface for the kernel to run a depthwise convolution native on a tensor. */
41class NEDepthwiseConvolutionLayerNativeKernel : public INEKernel
Giorgio Arena44f55722019-07-12 14:49:49 +010042{
43public:
44 const char *name() const override
45 {
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +010046 return "NEDepthwiseConvolutionLayerNativeKernel";
Giorgio Arena44f55722019-07-12 14:49:49 +010047 }
48 /** Default constructor */
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +010049 NEDepthwiseConvolutionLayerNativeKernel();
Giorgio Arena44f55722019-07-12 14:49:49 +010050 /** Prevent instances of this class from being copied (As this class contains pointers) */
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +010051 NEDepthwiseConvolutionLayerNativeKernel(const NEDepthwiseConvolutionLayerNativeKernel &) = delete;
Giorgio Arena44f55722019-07-12 14:49:49 +010052 /** Prevent instances of this class from being copied (As this class contains pointers) */
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +010053 NEDepthwiseConvolutionLayerNativeKernel &operator=(const NEDepthwiseConvolutionLayerNativeKernel &) = delete;
Giorgio Arena44f55722019-07-12 14:49:49 +010054 /** Default Move Constructor. */
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +010055 NEDepthwiseConvolutionLayerNativeKernel(NEDepthwiseConvolutionLayerNativeKernel &&) = default;
Giorgio Arena44f55722019-07-12 14:49:49 +010056 /** Default move assignment operator */
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +010057 NEDepthwiseConvolutionLayerNativeKernel &operator=(NEDepthwiseConvolutionLayerNativeKernel &&) = default;
Michalis Spyrouebcebf12020-10-21 00:04:14 +010058 /** Default destructor */
59 ~NEDepthwiseConvolutionLayerNativeKernel() = default;
Giorgio Arena44f55722019-07-12 14:49:49 +010060 /** Initialize the function's source, destination and parameters.
61 *
62 * @note Supported data layouts: NHWC
63 *
Michele Di Giorgio8c837ca2020-01-07 15:06:41 +000064 * @param[in] input Source tensor. DataType supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010065 * @param[in] weights Weights tensor. This is a 3D tensor with dimensions [IFM, W, H].
Michele Di Giorgio8c837ca2020-01-07 15:06:41 +000066 * Data type supported: Same as @p input or QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8/QASYMM8_SIGNED.
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010067 * @param[in] biases Biases tensor. A 1D tensor with dimensions [IFM]. Must be nullptr if not needed.
Michele Di Giorgio8c837ca2020-01-07 15:06:41 +000068 * Data type supported: Same as @p input, S32 when input is QASYMM8/QASYMM8_SIGNED.
Giorgio Arena44f55722019-07-12 14:49:49 +010069 * @param[out] output Destination tensor. Data type supported: Same as @p input.
70 * @param[in] conv_info Padding and stride information to use for the convolution.
71 * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
72 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
73 *
74 */
75 void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier = 1,
76 const Size2D &dilation = Size2D(1U, 1U));
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +010077 /** Static function to check if given info will lead to a valid configuration of @ref NEDepthwiseConvolutionLayerNativeKernel
Giorgio Arena44f55722019-07-12 14:49:49 +010078 *
79 * @note Supported data layouts: NHWC
80 *
Michele Di Giorgio8c837ca2020-01-07 15:06:41 +000081 * @param[in] input Source tensor info. DataType supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010082 * @param[in] weights Weights tensor info. This is a 3D tensor with dimensions [IFM, W, H].
Michele Di Giorgio8c837ca2020-01-07 15:06:41 +000083 * Data type supported: Same as @p input or QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8/QASYMM8_SIGNED.
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010084 * @param[in] biases Biases tensor info. A 1D tensor with dimensions [IFM]. Must be nullptr if not needed.
Michele Di Giorgio8c837ca2020-01-07 15:06:41 +000085 * Data type supported: Same as @p input, S32 when input is QASYMM8/QASYMM8_SIGNED.
Giorgio Arena44f55722019-07-12 14:49:49 +010086 * @param[in] output Destination tensor info. Data type supported: Same as @p input.
87 * @param[in] conv_info Padding and stride information to use for the convolution.
88 * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
89 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
90 *
91 * @return a status
92 */
93 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier = 1,
94 const Size2D &dilation = Size2D(1U, 1U));
95
96 // Inherited methods overridden:
97 void run(const Window &window, const ThreadInfo &info) override;
Giorgio Arena44f55722019-07-12 14:49:49 +010098
99private:
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100100 template <typename T>
101 using FloatEnalber = typename std::enable_if<arm_compute::utils::traits::is_floating_point<T>::value, int>::type;
102
103 template <typename T, typename TW, FloatEnalber<T> = 0>
Michalis Spyrouf401c742020-05-12 16:18:33 +0100104 void run_depthwise(const Window &window, bool has_biases);
Giorgio Arenad93e2632019-10-15 11:09:33 +0100105
Sang-Hoon Parke4558b52020-10-01 10:13:07 +0100106 template <typename T>
107 using Quantized8bitEnalber = typename std::enable_if < std::is_same<T, uint8_t>::value || std::is_same<T, int8_t>::value, int >::type;
108
109 template <typename T, typename TW, Quantized8bitEnalber<T> = 0>
Michalis Spyrouf401c742020-05-12 16:18:33 +0100110 void run_depthwise(const Window &window, bool has_biases);
Giorgio Arena44f55722019-07-12 14:49:49 +0100111
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +0100112 /** Common signature for all the specialised depthwise convolution native functions
Giorgio Arena44f55722019-07-12 14:49:49 +0100113 *
114 * @param[in] window Region on which to execute the kernel.
115 */
Michalis Spyrouf401c742020-05-12 16:18:33 +0100116 using DepthwiseFunctionPtr = void (NEDepthwiseConvolutionLayerNativeKernel::*)(const Window &window, bool has_biases);
Giorgio Arena44f55722019-07-12 14:49:49 +0100117
118 DepthwiseFunctionPtr _func;
Giorgio Arena44f55722019-07-12 14:49:49 +0100119 const ITensor *_input;
120 const ITensor *_weights;
121 const ITensor *_biases;
122 ITensor *_output;
123 PadStrideInfo _conv_info;
124 unsigned int _depth_multiplier;
125 Size2D _dilation;
Giorgio Arenad93e2632019-10-15 11:09:33 +0100126 std::vector<int> _output_multiplier;
127 std::vector<int> _output_shift;
Michalis Spyrouf401c742020-05-12 16:18:33 +0100128 bool _has_biases;
Giorgio Arena44f55722019-07-12 14:49:49 +0100129};
130} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000131#endif /* ARM_COMPUTE_NEDEPTHWISECONVOLUTIONLAYERNATIVEKERNEL_H */