Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 5ce897f | 2020-04-29 11:44:10 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 ARM Limited. |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_NEWINOGRADCONVOLUTIONLAYER_H |
| 25 | #define ARM_COMPUTE_NEWINOGRADCONVOLUTIONLAYER_H |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/runtime/IFunction.h" |
| 28 | |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/NEON/INEKernel.h" |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/Types.h" |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 31 | #include "arm_compute/runtime/CPP/functions/CPPPermute.h" |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 32 | #include "arm_compute/runtime/MemoryGroup.h" |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 33 | #include "arm_compute/runtime/NEON/functions/NEActivationLayer.h" |
Pablo Tello | a518f30 | 2018-09-19 11:33:03 +0100 | [diff] [blame] | 34 | #include "arm_compute/runtime/NEON/functions/NEGEMM.h" |
| 35 | |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 36 | #include "arm_compute/runtime/Tensor.h" |
| 37 | |
| 38 | #include <memory> |
| 39 | |
| 40 | namespace arm_compute |
| 41 | { |
Georgios Pinitas | 5ce897f | 2020-04-29 11:44:10 +0100 | [diff] [blame] | 42 | // Forward declarations |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 43 | class ITensor; |
Georgios Pinitas | 5ce897f | 2020-04-29 11:44:10 +0100 | [diff] [blame] | 44 | |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 45 | /** Basic function to simulate a convolution layer. This function calls the following NEON kernels: |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 46 | * -# @ref NEWinogradLayerTransformWeightsKernel (executed only once in the first call to the run() method ) |
| 47 | * -# @ref NEWinogradLayerTransformInputKernel |
| 48 | * -# @ref NEWinogradLayerTransformOutputKernel |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 49 | * -# @ref NEGEMMAssemblyDispatch |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 50 | * -# @ref CPPPermute (three times: weights, input and output) |
Giorgio Arena | a3221e6 | 2018-05-03 15:57:48 +0100 | [diff] [blame] | 51 | * |
| 52 | * @note Some Winograd configurations (i.e. F(2x2, 5x5), F(4x4, 5x5)) are supported only with enable_fast_math = true |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 53 | */ |
Georgios Pinitas | 9fb1159 | 2018-04-26 20:34:58 +0100 | [diff] [blame] | 54 | class NEWinogradConvolutionLayer : public IFunction |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 55 | { |
| 56 | public: |
| 57 | /** Constructor */ |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 58 | NEWinogradConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager = nullptr); |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 59 | |
| 60 | /** Set the input and output tensors. |
| 61 | * |
Giorgio Arena | a3221e6 | 2018-05-03 15:57:48 +0100 | [diff] [blame] | 62 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 63 | * while every optional dimension from 4 and above represent a batch of inputs. |
Georgios Pinitas | 5ce897f | 2020-04-29 11:44:10 +0100 | [diff] [blame] | 64 | * Data types supported: F16/F32. |
Giorgio Arena | a3221e6 | 2018-05-03 15:57:48 +0100 | [diff] [blame] | 65 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input. |
| 66 | * Currently only 3x3 and 5x5 kernels are supported. |
| 67 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights. |
| 68 | * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 69 | * Data types supported: Same as @p input. |
| 70 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. Currently only unit strides are supported. |
| 71 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 72 | * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation |
| 73 | * available which may introduce a drop of accuracy as well. Default is false |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 74 | */ |
Giorgio Arena | a3221e6 | 2018-05-03 15:57:48 +0100 | [diff] [blame] | 75 | void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info = ActivationLayerInfo(), |
| 76 | bool enable_fast_math = false); |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 77 | |
| 78 | // Inherited methods overridden: |
| 79 | void run() override; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 80 | void prepare() override; |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 81 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 82 | /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMConvolutionLayer |
| 83 | * |
Giorgio Arena | a3221e6 | 2018-05-03 15:57:48 +0100 | [diff] [blame] | 84 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 85 | * while every optional dimension from 4 and above represent a batch of inputs. |
Georgios Pinitas | 5ce897f | 2020-04-29 11:44:10 +0100 | [diff] [blame] | 86 | * Data types supported: F16/F32. |
Giorgio Arena | a3221e6 | 2018-05-03 15:57:48 +0100 | [diff] [blame] | 87 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input. |
| 88 | * Currently only 3x3 and 5x5 kernels are supported. |
| 89 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights. |
| 90 | * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 91 | * Data types supported: Same as @p input. |
| 92 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. Currently only unit strides are supported. |
| 93 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 94 | * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation |
| 95 | * available which may introduce a drop of accuracy as well. Default is false |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 96 | * |
| 97 | * @return a status |
| 98 | */ |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 99 | static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Giorgio Arena | a3221e6 | 2018-05-03 15:57:48 +0100 | [diff] [blame] | 100 | const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 101 | |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 102 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
Georgios Pinitas | 9fb1159 | 2018-04-26 20:34:58 +0100 | [diff] [blame] | 103 | NEWinogradConvolutionLayer(const NEWinogradConvolutionLayer &) = delete; |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 104 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
Georgios Pinitas | 9fb1159 | 2018-04-26 20:34:58 +0100 | [diff] [blame] | 105 | NEWinogradConvolutionLayer &operator=(const NEWinogradConvolutionLayer &) = delete; |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 106 | |
| 107 | private: |
Anthony Barbier | 578225e | 2018-07-16 18:00:11 +0100 | [diff] [blame] | 108 | MemoryGroup _memory_group; |
Pablo Tello | a518f30 | 2018-09-19 11:33:03 +0100 | [diff] [blame] | 109 | NEGEMM _gemm_function; |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 110 | std::unique_ptr<INEKernel> _transform_input_kernel; |
| 111 | std::unique_ptr<INEKernel> _transform_output_kernel; |
| 112 | std::unique_ptr<INEKernel> _transform_weights_kernel; |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 113 | NEActivationLayer _activationlayer_function; |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 114 | |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 115 | CPPPermute _permute_input; |
| 116 | CPPPermute _permute_weights; |
| 117 | CPPPermute _permute_output; |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 118 | Tensor _input_transformed; |
| 119 | Tensor _output_transformed; |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 120 | Tensor _input_workspace; |
| 121 | Tensor _output_workspace; |
| 122 | Tensor _kernel_storage; |
| 123 | Tensor _input_nhwc; |
| 124 | Tensor _output_nhwc; |
| 125 | Tensor _weights_hwio; |
| 126 | const ITensor *_input; |
| 127 | const ITensor *_weights; |
| 128 | ITensor *_output; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 129 | bool _is_prepared; |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 130 | bool _is_activationlayer_enabled; |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 131 | }; |
Georgios Pinitas | 5ce897f | 2020-04-29 11:44:10 +0100 | [diff] [blame] | 132 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 133 | #endif /* ARM_COMPUTE_NEWINOGRADCONVOLUTIONLAYER_H */ |