Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
Gian Marco Iodice | bd9097d | 2019-07-26 15:31:02 +0100 | [diff] [blame] | 24 | #ifndef __ARM_COMPUTE_NEDEPTHWISECONVOLUTIONLAYERNATIVEKERNEL_H__ |
| 25 | #define __ARM_COMPUTE_NEDEPTHWISECONVOLUTIONLAYERNATIVEKERNEL_H__ |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/NEON/INEKernel.h" |
| 28 | |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 29 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 30 | #include <arm_neon.h> |
| 31 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 32 | |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 33 | namespace arm_compute |
| 34 | { |
| 35 | // Forward declarations |
| 36 | class ITensor; |
| 37 | |
Gian Marco Iodice | bd9097d | 2019-07-26 15:31:02 +0100 | [diff] [blame] | 38 | /** Interface for the kernel to run a depthwise convolution native on a tensor. */ |
| 39 | class NEDepthwiseConvolutionLayerNativeKernel : public INEKernel |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 40 | { |
| 41 | public: |
| 42 | const char *name() const override |
| 43 | { |
Gian Marco Iodice | bd9097d | 2019-07-26 15:31:02 +0100 | [diff] [blame] | 44 | return "NEDepthwiseConvolutionLayerNativeKernel"; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 45 | } |
| 46 | /** Default constructor */ |
Gian Marco Iodice | bd9097d | 2019-07-26 15:31:02 +0100 | [diff] [blame] | 47 | NEDepthwiseConvolutionLayerNativeKernel(); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 48 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
Gian Marco Iodice | bd9097d | 2019-07-26 15:31:02 +0100 | [diff] [blame] | 49 | NEDepthwiseConvolutionLayerNativeKernel(const NEDepthwiseConvolutionLayerNativeKernel &) = delete; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 50 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
Gian Marco Iodice | bd9097d | 2019-07-26 15:31:02 +0100 | [diff] [blame] | 51 | NEDepthwiseConvolutionLayerNativeKernel &operator=(const NEDepthwiseConvolutionLayerNativeKernel &) = delete; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 52 | /** Default Move Constructor. */ |
Gian Marco Iodice | bd9097d | 2019-07-26 15:31:02 +0100 | [diff] [blame] | 53 | NEDepthwiseConvolutionLayerNativeKernel(NEDepthwiseConvolutionLayerNativeKernel &&) = default; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 54 | /** Default move assignment operator */ |
Gian Marco Iodice | bd9097d | 2019-07-26 15:31:02 +0100 | [diff] [blame] | 55 | NEDepthwiseConvolutionLayerNativeKernel &operator=(NEDepthwiseConvolutionLayerNativeKernel &&) = default; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 56 | /** Initialize the function's source, destination and parameters. |
| 57 | * |
| 58 | * @note Supported data layouts: NHWC |
| 59 | * |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 60 | * @param[in] input Source tensor. DataType supported: QASYMM8/F16/F32. |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 61 | * @param[in] weights Weights tensor. This is a 3D tensor with dimensions [IFM, W, H]. |
| 62 | * Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8. |
| 63 | * @param[in] biases Biases tensor. A 1D tensor with dimensions [IFM]. Must be nullptr if not needed. |
| 64 | * Data type supported: Same as @p input, S32 when input is QASYMM8. |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 65 | * @param[out] output Destination tensor. Data type supported: Same as @p input. |
| 66 | * @param[in] conv_info Padding and stride information to use for the convolution. |
| 67 | * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1. |
| 68 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
| 69 | * |
| 70 | */ |
| 71 | void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier = 1, |
| 72 | const Size2D &dilation = Size2D(1U, 1U)); |
Gian Marco Iodice | bd9097d | 2019-07-26 15:31:02 +0100 | [diff] [blame] | 73 | /** Static function to check if given info will lead to a valid configuration of @ref NEDepthwiseConvolutionLayerNativeKernel |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 74 | * |
| 75 | * @note Supported data layouts: NHWC |
| 76 | * |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 77 | * @param[in] input Source tensor info. DataType supported: QASYMM8/F16/F32. |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 78 | * @param[in] weights Weights tensor info. This is a 3D tensor with dimensions [IFM, W, H]. |
| 79 | * Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8. |
| 80 | * @param[in] biases Biases tensor info. A 1D tensor with dimensions [IFM]. Must be nullptr if not needed. |
| 81 | * Data type supported: Same as @p input, S32 when input is QASYMM8. |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 82 | * @param[in] output Destination tensor info. Data type supported: Same as @p input. |
| 83 | * @param[in] conv_info Padding and stride information to use for the convolution. |
| 84 | * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1. |
| 85 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
| 86 | * |
| 87 | * @return a status |
| 88 | */ |
| 89 | static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier = 1, |
| 90 | const Size2D &dilation = Size2D(1U, 1U)); |
| 91 | |
| 92 | // Inherited methods overridden: |
| 93 | void run(const Window &window, const ThreadInfo &info) override; |
| 94 | BorderSize border_size() const override; |
| 95 | |
| 96 | private: |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 97 | template < typename T, typename TW, int S, bool has_biases, bool is_per_channel, typename std::enable_if < std::is_same<T, float>::value |
| 98 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 99 | || std::is_same<T, float16_t>::value |
| 100 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 101 | , |
| 102 | int >::type = 0 > |
| 103 | void run_depthwise(const Window &window); |
| 104 | |
| 105 | template <typename T, typename TW, int S, bool has_biases, bool is_per_channel, typename std::enable_if<std::is_same<T, uint8_t>::value, int>::type = 0> |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 106 | void run_depthwise(const Window &window); |
| 107 | |
Gian Marco Iodice | bd9097d | 2019-07-26 15:31:02 +0100 | [diff] [blame] | 108 | /** Common signature for all the specialised depthwise convolution native functions |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 109 | * |
| 110 | * @param[in] window Region on which to execute the kernel. |
| 111 | */ |
Gian Marco Iodice | bd9097d | 2019-07-26 15:31:02 +0100 | [diff] [blame] | 112 | using DepthwiseFunctionPtr = void (NEDepthwiseConvolutionLayerNativeKernel::*)(const Window &window); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 113 | |
| 114 | DepthwiseFunctionPtr _func; |
| 115 | BorderSize _border_size; |
| 116 | const ITensor *_input; |
| 117 | const ITensor *_weights; |
| 118 | const ITensor *_biases; |
| 119 | ITensor *_output; |
| 120 | PadStrideInfo _conv_info; |
| 121 | unsigned int _depth_multiplier; |
| 122 | Size2D _dilation; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 123 | std::vector<int> _output_multiplier; |
| 124 | std::vector<int> _output_shift; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 125 | }; |
| 126 | } // namespace arm_compute |
Gian Marco Iodice | bd9097d | 2019-07-26 15:31:02 +0100 | [diff] [blame] | 127 | #endif /* __ARM_COMPUTE_NEDEPTHWISECONVOLUTIONLAYERNATIVEKERNEL_H__ */ |