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: |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 82 | const char *name() const override |
| 83 | { |
| 84 | return "NEWinogradLayerKernel"; |
| 85 | } |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 86 | /** Constructor */ |
| 87 | NEWinogradLayerKernel(); |
| 88 | |
| 89 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 90 | NEWinogradLayerKernel(const NEWinogradLayerKernel &) = delete; |
| 91 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 92 | NEWinogradLayerKernel &operator=(const NEWinogradLayerKernel &) = delete; |
| 93 | /** Allow instances of this class to be moved */ |
| 94 | NEWinogradLayerKernel(NEWinogradLayerKernel &&) = default; |
| 95 | /** Allow instances of this class to be moved */ |
| 96 | NEWinogradLayerKernel &operator=(NEWinogradLayerKernel &&) = default; |
| 97 | |
| 98 | virtual ~NEWinogradLayerKernel() = default; |
| 99 | |
| 100 | /** Initialise the kernel |
| 101 | * |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 102 | * @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] | 103 | */ |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 104 | void configure(Winograd3x3F32 *convolver); |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 105 | |
| 106 | // Inherited methods overridden: |
| 107 | void run(const Window &window, const ThreadInfo &info) override; |
| 108 | |
Pablo Tello | 6c6e77a | 2018-01-23 10:03:27 +0000 | [diff] [blame] | 109 | /** Determine how much memory (in units of TIn) to allocate for the |
| 110 | * transformed weights. |
| 111 | * |
| 112 | * @param[in] n_output_channels Number of output feature maps. |
| 113 | * @param[in] n_input_channels Number of input feature maps. |
| 114 | */ |
| 115 | static unsigned int get_weight_storage_size( |
| 116 | const int n_output_channels, |
| 117 | const int n_input_channels); |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 118 | |
Pablo Tello | 6c6e77a | 2018-01-23 10:03:27 +0000 | [diff] [blame] | 119 | /** Determine how much memory (in units of TIn) to allocate for the |
| 120 | * transformed input. |
| 121 | * |
| 122 | * @param[in] n_batches Number of batches in the input tensor. |
| 123 | * @param[in] n_channels Number of feature maps in the input tensor. |
| 124 | * @param[in] n_rows Number of rows in each feature map. |
| 125 | * @param[in] n_cols Number of columns in each feature map. |
| 126 | * @param[in] same_padding Use "SAME" padding, otherwise use "VALID". |
| 127 | */ |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 128 | static unsigned int get_input_storage_size( |
Pablo Tello | 6c6e77a | 2018-01-23 10:03:27 +0000 | [diff] [blame] | 129 | const int n_batches, |
| 130 | const int n_channels, |
| 131 | const int n_rows, |
| 132 | const int n_cols, |
| 133 | const bool same_padding); |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 134 | |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 135 | /** Determine how much memory (in units of TOut) to allocate for the |
| 136 | * (Winograd domain) output. |
Pablo Tello | 6c6e77a | 2018-01-23 10:03:27 +0000 | [diff] [blame] | 137 | * |
| 138 | * @param[in] n_batches Number of batches in the output tensor. |
| 139 | * @param[in] n_rows Number of rows in each feature map of the input tensor. |
| 140 | * @param[in] n_cols Number of columns in each feature map of the input tensor. |
| 141 | * @param[in] n_output_channels Number of feature maps in the output tensor. |
| 142 | * @param[in] same_padding Use "SAME" padding, otherwise use "VALID". |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 143 | */ |
| 144 | static unsigned int get_output_storage_size( |
Pablo Tello | 6c6e77a | 2018-01-23 10:03:27 +0000 | [diff] [blame] | 145 | const int n_batches, |
| 146 | const int n_rows, |
| 147 | const int n_cols, |
| 148 | const int n_output_channels, |
| 149 | const bool same_padding); |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 150 | |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 151 | protected: |
| 152 | Winograd3x3F32 *_convolver; |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | } // namespace arm_compute |
| 156 | #endif /*__ARM_COMPUTE_NEGEMMWINOGRADLAYERKERNEL_H__*/ |