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 | #include "arm_compute/core/NEON/kernels/NEWinogradLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
| 29 | #include "arm_compute/core/TensorInfo.h" |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 30 | #include "support/ToolchainSupport.h" |
| 31 | |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame^] | 32 | #include "arm_compute/core/NEON/kernels/winograd/winograd_layer.hpp" |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 33 | |
Pablo Tello | 4e2c139 | 2018-01-09 10:30:27 +0000 | [diff] [blame] | 34 | namespace |
| 35 | { |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame^] | 36 | using T = WinogradConvolutionLayer<2, 2, 3, 3, float, float>; |
Pablo Tello | 4e2c139 | 2018-01-09 10:30:27 +0000 | [diff] [blame] | 37 | } // namespace |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 38 | |
| 39 | namespace arm_compute |
| 40 | { |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 41 | class Winograd3x3F32::Private |
| 42 | { |
| 43 | public: |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame^] | 44 | Private( |
| 45 | const int n_batches, /** Number of batches in the input and output tensors. */ |
| 46 | const int n_input_channels, /** Number of feature maps in a batch of the input tensor. */ |
| 47 | const int n_input_rows, /** Number of rows in a feature map of the input tensor. */ |
| 48 | const int n_input_cols, /** Number of columns in a feature map of the input tensor. */ |
| 49 | const int n_output_channels, /** Number of feature maps in the output tensor. */ |
| 50 | const bool same_padding, /** Use "SAME" padding, otherwise use "VALID". */ |
| 51 | const float *const weights, /** Pointer to weight tensor in spatial domain. Must be ordered as "Height x Rows x Input Feature Maps x Output Feature Maps. */ |
| 52 | float *const weights_storage, /** Pointer to storage for weight tensor in the Winograd domain. Must be at least the size returned by `get_weight_storage_size`. */ |
| 53 | const float *const input, /** Pointer to NHWC ordered input tensor, in the spatial domain. */ |
| 54 | float *const 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`. */ |
| 55 | float *const output, /** Pointer to NHWC ordered output tensor, in the spatial domain. */ |
| 56 | float *const 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`. */ |
| 57 | ) |
| 58 | : convolver(n_batches, n_input_channels, n_input_rows, n_input_cols, n_output_channels, same_padding, weights, weights_storage, input, winograd_input, output, winograd_output) |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 59 | { |
| 60 | } |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 61 | T convolver; |
| 62 | }; |
| 63 | |
| 64 | Winograd3x3F32::~Winograd3x3F32() |
| 65 | { |
| 66 | } |
| 67 | |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame^] | 68 | void Winograd3x3F32::transform_output() |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 69 | { |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame^] | 70 | auto win = _pimpl->convolver.output_transform.get_window(); |
| 71 | _pimpl->convolver.output_transform.run(0, win); |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame^] | 74 | void Winograd3x3F32::transform_input() |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 75 | { |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame^] | 76 | auto win = _pimpl->convolver.input_transform.get_window(); |
| 77 | _pimpl->convolver.input_transform.run(0, win); |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame^] | 80 | void Winograd3x3F32::transform_weights() |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 81 | { |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame^] | 82 | auto win = _pimpl->convolver.weights_transform.get_window(); |
| 83 | _pimpl->convolver.weights_transform.run(0, win); |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame^] | 86 | Winograd3x3F32::Winograd3x3F32( |
| 87 | const int n_batches, /** Number of batches in the input and output tensors. */ |
| 88 | const int n_input_channels, /** Number of feature maps in a batch of the input tensor. */ |
| 89 | const int n_input_rows, /** Number of rows in a feature map of the input tensor. */ |
| 90 | const int n_input_cols, /** Number of columns in a feature map of the input tensor. */ |
| 91 | const int n_output_channels, /** Number of feature maps in the output tensor. */ |
| 92 | const bool same_padding, /** Use "SAME" padding, otherwise use "VALID". */ |
| 93 | const float *const weights, /** Pointer to weight tensor in spatial domain. Must be ordered as "Height x Rows x Input Feature Maps x Output Feature Maps. */ |
| 94 | float *const weights_storage, /** Pointer to storage for weight tensor in the Winograd domain. Must be at least the size returned by `get_weight_storage_size`. */ |
| 95 | const float *const input, /** Pointer to NHWC ordered input tensor, in the spatial domain. */ |
| 96 | float *const 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`. */ |
| 97 | float *const output, /** Pointer to NHWC ordered output tensor, in the spatial domain. */ |
| 98 | float *const 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`. */ |
| 99 | ) |
| 100 | : _pimpl(support::cpp14::make_unique<Private>(n_batches, n_input_channels, n_input_rows, n_input_cols, n_output_channels, same_padding, weights, weights_storage, input, winograd_input, output, |
| 101 | winograd_output)) |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 102 | { |
| 103 | } |
| 104 | |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame^] | 105 | unsigned int NEWinogradLayerKernel::get_input_storage_size(const int n_batches, const int n_channels, const int n_rows, const int n_cols, const bool same_padding) |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 106 | { |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame^] | 107 | return T::get_input_storage_size(n_batches, n_channels, n_rows, n_cols, same_padding); |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame^] | 110 | unsigned int NEWinogradLayerKernel::get_output_storage_size( |
| 111 | const int n_batches, /** Number of batches in the output tensor. */ |
| 112 | const int n_rows, /** Number of rows in each feature map of the input tensor. */ |
| 113 | const int n_cols, /** Number of columns in each feature map of the input tensor. */ |
| 114 | const int n_output_channels, /** Number of feature maps in the output tensor. */ |
| 115 | const bool same_padding /** Use "SAME" padding, otherwise use "VALID". */ |
| 116 | ) |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 117 | { |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame^] | 118 | return T::get_output_storage_size(n_batches, n_rows, n_cols, n_output_channels, same_padding); |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame^] | 121 | size_t NEWinogradLayerKernel::get_weight_storage_size(const int n_output_channels, const int n_input_channels) |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 122 | { |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame^] | 123 | return T::get_weight_storage_size(n_output_channels, n_input_channels); |
Pablo Tello | 3d4968a | 2017-12-04 15:03:35 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 126 | NEWinogradLayerKernel::NEWinogradLayerKernel() |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 127 | : _convolver(nullptr) |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 128 | { |
| 129 | } |
| 130 | |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 131 | void NEWinogradLayerKernel::configure(Winograd3x3F32 *convolver) |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 132 | { |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 133 | ARM_COMPUTE_ERROR_ON_NULLPTR(convolver); |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 134 | _convolver = convolver; |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 135 | Window win; |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame^] | 136 | auto win_last = _convolver->_pimpl->convolver.gemms.get_window(); |
| 137 | win.set(Window::DimX, Window::Dimension(0, win_last, 1)); |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 138 | INEKernel::configure(win); |
| 139 | } |
| 140 | |
| 141 | void NEWinogradLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 142 | { |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 143 | ARM_COMPUTE_UNUSED(info); |
| 144 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 145 | const size_t first_gemm = window.x().start(); |
| 146 | const size_t last_gemm = window.x().end(); |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame^] | 147 | _convolver->_pimpl->convolver.gemms.run(first_gemm, last_gemm); |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 148 | } |
| 149 | } // namespace arm_compute |