Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 1 | /* |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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 | */ |
| 24 | #ifndef __ARM_COMPUTE_NEGEMMWINOGRADLAYERKERNEL_H__ |
| 25 | #define __ARM_COMPUTE_NEGEMMWINOGRADLAYERKERNEL_H__ |
| 26 | |
| 27 | #include "arm_compute/core/NEON/INEKernel.h" |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/NEON/kernels/winograd/convolution.hpp" |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/NEON/kernels/winograd/tensor.hpp" |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 30 | |
| 31 | namespace arm_compute |
| 32 | { |
| 33 | class ITensor; |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 34 | class NEWinogradLayerKernel; |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 35 | |
Pablo Tello | 4e2c139 | 2018-01-09 10:30:27 +0000 | [diff] [blame] | 36 | class Winograd3x3F32 final |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 37 | { |
| 38 | public: |
Pablo Tello | 6c6e77a | 2018-01-23 10:03:27 +0000 | [diff] [blame] | 39 | /** Create a new Winograd convolution layer. |
| 40 | * |
| 41 | * @param[in] n_batches Number of batches in the input and output tensors. |
| 42 | * @param[in] n_input_channels Number of feature maps in a batch of the input tensor. |
| 43 | * @param[in] n_input_rows Number of rows in a feature map of the input tensor. |
| 44 | * @param[in] n_input_cols Number of columns in a feature map of the input tensor. |
| 45 | * @param[in] n_output_channels Number of feature maps in the output tensor. |
| 46 | * @param[in] same_padding Use "SAME" padding, otherwise use "VALID". |
| 47 | * @param[in] weights Pointer to weight tensor in spatial domain. Must be ordered as "Height x Rows x Input Feature Maps x Output Feature Maps. |
| 48 | * @param[out] weights_storage Pointer to storage for weight tensor in the Winograd domain. Must be at least the size returned by `get_weight_storage_size |
| 49 | * @param[in] input Pointer to NHWC ordered input tensor, in the spatial domain. |
| 50 | * @param[out] winograd_input Pointer to working space for the input tensor in the Winograd domain. Must be at least the size returned by `get_input_storage_size`. |
| 51 | * @param[out] output Pointer to NHWC ordered output tensor, in the spatial domain. |
| 52 | * @param[out] winograd_output Pointer to working space for the output tensor in the Winograd domain. Must be at least the size returned by `get_output_storage_size`. |
| 53 | */ |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 54 | friend class NEWinogradLayerKernel; |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 55 | Winograd3x3F32( |
Pablo Tello | 6c6e77a | 2018-01-23 10:03:27 +0000 | [diff] [blame] | 56 | const int n_batches, |
| 57 | const int n_input_channels, |
| 58 | const int n_input_rows, |
| 59 | const int n_input_cols, |
| 60 | const int n_output_channels, |
| 61 | const bool same_padding, |
| 62 | const float *const weights, |
| 63 | float *const weights_storage, |
| 64 | const float *const input, |
| 65 | float *const winograd_input, |
| 66 | float *const output, |
| 67 | float *const winograd_output); |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 68 | |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 69 | ~Winograd3x3F32(); |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 70 | void transform_weights(); |
| 71 | void transform_input(); |
| 72 | void transform_output(); |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 73 | |
| 74 | private: |
| 75 | class Private; |
| 76 | std::unique_ptr<Private> _pimpl; |
| 77 | }; |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 78 | |
| 79 | class NEWinogradLayerKernel : public INEKernel |
| 80 | { |
| 81 | public: |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 82 | /** Constructor */ |
| 83 | NEWinogradLayerKernel(); |
| 84 | |
| 85 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 86 | NEWinogradLayerKernel(const NEWinogradLayerKernel &) = delete; |
| 87 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 88 | NEWinogradLayerKernel &operator=(const NEWinogradLayerKernel &) = delete; |
| 89 | /** Allow instances of this class to be moved */ |
| 90 | NEWinogradLayerKernel(NEWinogradLayerKernel &&) = default; |
| 91 | /** Allow instances of this class to be moved */ |
| 92 | NEWinogradLayerKernel &operator=(NEWinogradLayerKernel &&) = default; |
| 93 | |
| 94 | virtual ~NEWinogradLayerKernel() = default; |
| 95 | |
| 96 | /** Initialise the kernel |
| 97 | * |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 98 | * @param[in] convolver A pointer to the winograd convolver, this object must have been configured and is ready to execute 16 GEMMS . |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 99 | */ |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 100 | void configure(Winograd3x3F32 *convolver); |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 101 | |
| 102 | // Inherited methods overridden: |
| 103 | void run(const Window &window, const ThreadInfo &info) override; |
| 104 | |
Pablo Tello | 6c6e77a | 2018-01-23 10:03:27 +0000 | [diff] [blame] | 105 | /** Determine how much memory (in units of TIn) to allocate for the |
| 106 | * transformed weights. |
| 107 | * |
| 108 | * @param[in] n_output_channels Number of output feature maps. |
| 109 | * @param[in] n_input_channels Number of input feature maps. |
| 110 | */ |
| 111 | static unsigned int get_weight_storage_size( |
| 112 | const int n_output_channels, |
| 113 | const int n_input_channels); |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 114 | |
Pablo Tello | 6c6e77a | 2018-01-23 10:03:27 +0000 | [diff] [blame] | 115 | /** Determine how much memory (in units of TIn) to allocate for the |
| 116 | * transformed input. |
| 117 | * |
| 118 | * @param[in] n_batches Number of batches in the input tensor. |
| 119 | * @param[in] n_channels Number of feature maps in the input tensor. |
| 120 | * @param[in] n_rows Number of rows in each feature map. |
| 121 | * @param[in] n_cols Number of columns in each feature map. |
| 122 | * @param[in] same_padding Use "SAME" padding, otherwise use "VALID". |
| 123 | */ |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 124 | static unsigned int get_input_storage_size( |
Pablo Tello | 6c6e77a | 2018-01-23 10:03:27 +0000 | [diff] [blame] | 125 | const int n_batches, |
| 126 | const int n_channels, |
| 127 | const int n_rows, |
| 128 | const int n_cols, |
| 129 | const bool same_padding); |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 130 | |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 131 | /** Determine how much memory (in units of TOut) to allocate for the |
| 132 | * (Winograd domain) output. |
Pablo Tello | 6c6e77a | 2018-01-23 10:03:27 +0000 | [diff] [blame] | 133 | * |
| 134 | * @param[in] n_batches Number of batches in the output tensor. |
| 135 | * @param[in] n_rows Number of rows in each feature map of the input tensor. |
| 136 | * @param[in] n_cols Number of columns in each feature map of the input tensor. |
| 137 | * @param[in] n_output_channels Number of feature maps in the output tensor. |
| 138 | * @param[in] same_padding Use "SAME" padding, otherwise use "VALID". |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 139 | */ |
| 140 | static unsigned int get_output_storage_size( |
Pablo Tello | 6c6e77a | 2018-01-23 10:03:27 +0000 | [diff] [blame] | 141 | const int n_batches, |
| 142 | const int n_rows, |
| 143 | const int n_cols, |
| 144 | const int n_output_channels, |
| 145 | const bool same_padding); |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 146 | |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 147 | protected: |
| 148 | Winograd3x3F32 *_convolver; |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | } // namespace arm_compute |
| 152 | #endif /*__ARM_COMPUTE_NEGEMMWINOGRADLAYERKERNEL_H__*/ |