Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Alex Gilday | 27c08ab | 2018-02-22 11:36:16 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [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 | */ |
| 24 | #ifndef __ARM_COMPUTE_NELOCALLYCONNECTEDLAYER_H__ |
| 25 | #define __ARM_COMPUTE_NELOCALLYCONNECTEDLAYER_H__ |
| 26 | |
| 27 | #include "arm_compute/runtime/IFunction.h" |
| 28 | |
| 29 | #include "arm_compute/core/NEON/kernels/NECol2ImKernel.h" |
| 30 | #include "arm_compute/core/NEON/kernels/NEIm2ColKernel.h" |
| 31 | #include "arm_compute/core/NEON/kernels/NELocallyConnectedMatrixMultiplyKernel.h" |
| 32 | #include "arm_compute/core/NEON/kernels/NEWeightsReshapeKernel.h" |
| 33 | #include "arm_compute/core/Types.h" |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 34 | #include "arm_compute/runtime/IMemoryManager.h" |
| 35 | #include "arm_compute/runtime/MemoryGroup.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | #include "arm_compute/runtime/Tensor.h" |
| 37 | |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 38 | #include <memory> |
| 39 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 40 | namespace arm_compute |
| 41 | { |
| 42 | class INETensor; |
| 43 | |
| 44 | /** Basic function to compute the locally connected layer. This function calls the following NEON kernels: |
| 45 | * |
| 46 | * -# @ref NEWeightsReshapeKernel (executed only once for each configuration) |
| 47 | * -# @ref NEIm2ColKernel |
| 48 | * -# @ref NELocallyConnectedMatrixMultiplyKernel |
| 49 | * -# @ref NECol2ImKernel |
| 50 | */ |
| 51 | class NELocallyConnectedLayer : public IFunction |
| 52 | { |
| 53 | public: |
| 54 | /** Default constructor */ |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 55 | NELocallyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 56 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 57 | NELocallyConnectedLayer(const NELocallyConnectedLayer &) = delete; |
| 58 | /** Default move constructor */ |
| 59 | NELocallyConnectedLayer(NELocallyConnectedLayer &&) = default; |
| 60 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 61 | NELocallyConnectedLayer &operator=(const NELocallyConnectedLayer &) = delete; |
| 62 | /** Default move assignment operator */ |
| 63 | NELocallyConnectedLayer &operator=(NELocallyConnectedLayer &&) = default; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | /** Set the input and output tensors. |
| 65 | * |
| 66 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 67 | * while every optional dimension from 4 and above represent a batch of inputs. |
Pablo Tello | afde732 | 2017-07-25 09:19:46 +0100 | [diff] [blame] | 68 | * Data types supported: F16, F32. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 69 | * @param[in] weights Weights tensor. Weights are 5D tensor with dimensions [kernel_x, kernel_y, IFM, OFM, num_patches]. Data type supported:Same as @p input. |
| 70 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 2D tensor with dimensions [OFM, num_patches]. Data type supported:Same as @p input. |
| 71 | * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 72 | * Data types supported: Same as @p input. |
| 73 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 74 | */ |
| 75 | void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info); |
Alex Gilday | 27c08ab | 2018-02-22 11:36:16 +0000 | [diff] [blame] | 76 | /** Static function to check if given info will lead to a valid configuration of @ref NELocallyConnectedLayer |
| 77 | * |
| 78 | * @param[in] input Input tensor info. 3 lower dimensions represent a single input [width, height, IFM], |
| 79 | * while every optional dimension from 4 and above represent a batch of inputs. |
| 80 | * Data types supported: F16, F32. |
| 81 | * @param[in] weights Weights tensor info. Weights are 5D tensor with dimensions [kernel_x, kernel_y, IFM, OFM, num_patches]. Data type supported:Same as @p input. |
| 82 | * @param[in] biases Biases tensor info. Shared biases supported. Biases are 2D tensor with dimensions [OFM, num_patches]. Data type supported:Same as @p input. |
| 83 | * @param[in] output Output tensor info. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 84 | * Data types supported: Same as @p input. |
| 85 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 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); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | |
| 91 | // Inherited methods overridden: |
| 92 | void run() override; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 93 | void prepare() override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 94 | |
| 95 | private: |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 96 | MemoryGroup _memory_group; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 97 | NEIm2ColKernel _input_im2col_kernel; |
| 98 | NEWeightsReshapeKernel _weights_reshape_kernel; |
| 99 | NELocallyConnectedMatrixMultiplyKernel _mm_kernel; |
| 100 | NECol2ImKernel _output_col2im_kernel; |
| 101 | Tensor _input_im2col_reshaped; |
| 102 | Tensor _weights_reshaped; |
| 103 | Tensor _gemm_output; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 104 | bool _is_prepared; |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 105 | const ITensor *_original_weights; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 106 | }; |
| 107 | } |
| 108 | #endif /* __ARM_COMPUTE_NELOCALLYCONNECTEDLAYER_H__ */ |