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 | */ |
Georgios Pinitas | 9fb1159 | 2018-04-26 20:34:58 +0100 | [diff] [blame] | 24 | #include "arm_compute/runtime/NEON/functions/NEWinogradConvolutionLayer.h" |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 25 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/Error.h" |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/Utils.h" |
| 28 | #include "arm_compute/core/Validate.h" |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Validate.h" |
| 30 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Michalis Spyrou | 2b3129e | 2018-04-25 18:10:13 +0100 | [diff] [blame] | 31 | #include "arm_compute/runtime/NEON/AssemblyHelper.h" |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 32 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
| 33 | #include "support/ToolchainSupport.h" |
| 34 | |
Georgios Pinitas | 9fb1159 | 2018-04-26 20:34:58 +0100 | [diff] [blame] | 35 | #include "arm_compute/core/NEON/kernels/NEWinogradConvolutionLayerKernel.h" |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 36 | |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 37 | #include "arm_compute/core/NEON/kernels/convolution/winograd/winograd_gemm.hpp" |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 38 | |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 39 | namespace arm_compute |
| 40 | { |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 41 | namespace |
| 42 | { |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame^] | 43 | inline Tensor4DShape internal_get_input_shape(const arm_compute::ITensor *input) |
| 44 | { |
| 45 | const DataLayout data_layout = input->info()->data_layout(); |
| 46 | const int in_width = input->info()->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH)); |
| 47 | const int in_height = input->info()->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT)); |
| 48 | const int in_channels = input->info()->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL)); |
| 49 | const int in_batches = input->info()->dimension(3); |
| 50 | |
| 51 | return Tensor4DShape({ in_batches, in_height, in_width, in_channels }); |
| 52 | } |
| 53 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 54 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info) |
| 55 | { |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame^] | 56 | const DataLayout data_layout = input->data_layout(); |
| 57 | const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 58 | const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 59 | |
| 60 | ARM_COMPUTE_UNUSED(output); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 61 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
Andrew Mundy | 4d9379a | 2018-03-15 16:47:03 +0000 | [diff] [blame] | 62 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame^] | 63 | ARM_COMPUTE_RETURN_ERROR_ON(data_layout != DataLayout::NCHW); // COMPMID-1162 |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(width_idx) != 3 && weights->dimension(height_idx) != 5, "Only 3 and 5 kernels are supported"); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 65 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4); |
| 66 | |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame^] | 67 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.stride().first != 1 || conv_info.stride().second != 1, "Winograd layer only supports unit strides."); |
| 68 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 69 | if(biases != nullptr) |
| 70 | { |
| 71 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 72 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
| 73 | } |
| 74 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 75 | return Status{}; |
| 76 | } |
| 77 | } //namespace |
| 78 | |
Georgios Pinitas | 9fb1159 | 2018-04-26 20:34:58 +0100 | [diff] [blame] | 79 | NEWinogradConvolutionLayer::NEWinogradConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Michalis Spyrou | 2b3129e | 2018-04-25 18:10:13 +0100 | [diff] [blame] | 80 | : _memory_group(std::move(memory_manager)), _arm_gemm(nullptr), _gemm_kernel(nullptr), _transform_input_kernel(nullptr), _transform_output_kernel(nullptr), _transform_weights_kernel(nullptr), |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 81 | _activationlayer_function(), _permute_input(), _permute_weights(), _permute_output(), _input_workspace(), _output_workspace(), _kernel_storage(), _input_nhwc(), _output_nhwc(), _weights_hwio(), |
Michalis Spyrou | 2b3129e | 2018-04-25 18:10:13 +0100 | [diff] [blame] | 82 | _workspace(), _input(), _weights(), _output(), _reshaped_kernel(false), _is_activationlayer_enabled(false) |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 83 | { |
| 84 | } /* arm_compute */ |
| 85 | |
Georgios Pinitas | 9fb1159 | 2018-04-26 20:34:58 +0100 | [diff] [blame] | 86 | void NEWinogradConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info) |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 87 | { |
Andrew Mundy | 4d9379a | 2018-03-15 16:47:03 +0000 | [diff] [blame] | 88 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
Andrew Mundy | 4d9379a | 2018-03-15 16:47:03 +0000 | [diff] [blame] | 89 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), weights->info(), (biases != nullptr) ? biases->info() : nullptr, output->info(), conv_info)); |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 90 | |
| 91 | _weights = weights; |
| 92 | _input = input; |
| 93 | _output = output; |
| 94 | |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame^] | 95 | // Get indices for the width and height |
| 96 | const DataLayout data_layout = input->info()->data_layout(); |
| 97 | const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 98 | const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 99 | const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 100 | |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 101 | std::unique_ptr<INEWinogradLayerTransformInputKernel<float>> transform_input_kernel; |
| 102 | std::unique_ptr<INEWinogradLayerTransformWeightsKernel<float>> transform_weights_kernel; |
| 103 | std::unique_ptr<INEWinogradLayerTransformOutputKernel<float>> transform_output_kernel; |
| 104 | |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame^] | 105 | const int weights_width = weights->info()->dimension(width_idx); |
| 106 | const int weights_height = weights->info()->dimension(height_idx); |
Michalis Spyrou | 2b3129e | 2018-04-25 18:10:13 +0100 | [diff] [blame] | 107 | |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame^] | 108 | Size2D output_tile{}; |
| 109 | int n_gemms = 0; |
| 110 | int N_BLOCK = 0; // Size of block used by GEMM. |
Michalis Spyrou | 2b3129e | 2018-04-25 18:10:13 +0100 | [diff] [blame] | 111 | |
| 112 | switch(weights_width) |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 113 | { |
| 114 | case 3: |
| 115 | { |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame^] | 116 | if(input->info()->dimension(width_idx) > 4 && input->info()->dimension(height_idx) > 4) |
| 117 | { |
| 118 | transform_input_kernel = support::cpp14::make_unique<NEWinogradLayerTransformInputKernel<float, 4, 4, 3, 3>>(); |
| 119 | transform_weights_kernel = support::cpp14::make_unique<NEWinogradLayerTransformWeightsKernel<float, 4, 4, 3, 3>>(); |
| 120 | transform_output_kernel = support::cpp14::make_unique<NEWinogradLayerTransformOutputKernel<float, 4, 4, 3, 3>>(); |
| 121 | output_tile = Size2D(4U, 4U); |
| 122 | n_gemms = NEWinogradLayerBatchedGEMMKernel<float, float, 4, 4, 3, 3>::WinogradBase::N_GEMMS; |
| 123 | N_BLOCK = NEWinogradLayerBatchedGEMMKernel<float, float, 4, 4, 3, 3>::WinogradConv::N_BLOCK; |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | transform_input_kernel = support::cpp14::make_unique<NEWinogradLayerTransformInputKernel<float, 2, 2, 3, 3>>(); |
| 128 | transform_weights_kernel = support::cpp14::make_unique<NEWinogradLayerTransformWeightsKernel<float, 2, 2, 3, 3>>(); |
| 129 | transform_output_kernel = support::cpp14::make_unique<NEWinogradLayerTransformOutputKernel<float, 2, 2, 3, 3>>(); |
| 130 | output_tile = Size2D(2U, 2U); |
| 131 | n_gemms = NEWinogradLayerBatchedGEMMKernel<float, float, 2, 2, 3, 3>::WinogradBase::N_GEMMS; |
| 132 | N_BLOCK = NEWinogradLayerBatchedGEMMKernel<float, float, 2, 2, 3, 3>::WinogradConv::N_BLOCK; |
| 133 | } |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 134 | break; |
| 135 | } |
| 136 | case 5: |
| 137 | { |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 138 | transform_input_kernel = support::cpp14::make_unique<NEWinogradLayerTransformInputKernel<float, 2, 2, 5, 5>>(); |
| 139 | transform_weights_kernel = support::cpp14::make_unique<NEWinogradLayerTransformWeightsKernel<float, 2, 2, 5, 5>>(); |
| 140 | transform_output_kernel = support::cpp14::make_unique<NEWinogradLayerTransformOutputKernel<float, 2, 2, 5, 5>>(); |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame^] | 141 | output_tile = Size2D(2U, 2U); |
Michalis Spyrou | 2b3129e | 2018-04-25 18:10:13 +0100 | [diff] [blame] | 142 | n_gemms = NEWinogradLayerBatchedGEMMKernel<float, float, 2, 2, 5, 5>::WinogradBase::N_GEMMS; |
| 143 | N_BLOCK = NEWinogradLayerBatchedGEMMKernel<float, float, 2, 2, 5, 5>::WinogradConv::N_BLOCK; |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 144 | break; |
| 145 | } |
| 146 | default: |
| 147 | { |
| 148 | ARM_COMPUTE_ERROR("Not supported."); |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | |
Pablo Tello | 679463a | 2018-02-06 11:47:59 +0000 | [diff] [blame] | 153 | const PaddingType use_padding_type = (conv_info.pad_left() != 0u) ? PADDING_SAME : PADDING_VALID; |
| 154 | const bool use_same_padding = use_padding_type == PADDING_SAME; |
| 155 | |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 156 | // Get convolved dimensions |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame^] | 157 | const int in_channels = input->info()->dimension(channel_idx); |
| 158 | const int out_channels = output->info()->dimension(channel_idx); |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 159 | |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 160 | const Tensor4DShape in_shape(internal_get_input_shape(input)); |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 161 | const size_t data_type_size = input->info()->element_size(); |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 162 | // Get the memory required to instantiate a new Winograd operator. |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 163 | constexpr size_t storage_alignment = 64; |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 164 | const size_t kernel_storage_size = transform_weights_kernel->get_weight_storage_size(out_channels, in_channels) * data_type_size; |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 165 | _kernel_storage.allocator()->init(TensorInfo(TensorShape{ (kernel_storage_size + storage_alignment - 1) }, 1, DataType::U8)); |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 166 | _kernel_storage.allocator()->allocate(); |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 167 | // Input storage |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 168 | const size_t input_storage_size = transform_input_kernel->get_input_storage_size(in_shape.n_batches, in_shape.n_channels, in_shape.n_rows, in_shape.n_cols, use_same_padding) * data_type_size; |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 169 | _input_workspace.allocator()->init(TensorInfo(TensorShape{ (input_storage_size + storage_alignment - 1) }, 1, DataType::U8)); |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 170 | _input_workspace.allocator()->allocate(); |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 171 | |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 172 | // Output storage |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 173 | const size_t output_storage_size = transform_output_kernel->get_output_storage_size(in_shape.n_batches, in_shape.n_rows, in_shape.n_cols, out_channels, use_same_padding) * data_type_size; |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 174 | _output_workspace.allocator()->init(TensorInfo(TensorShape{ (output_storage_size + storage_alignment - 1) }, 1, DataType::U8)); |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 175 | _output_workspace.allocator()->allocate(); |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 176 | |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 177 | // configure and allocate dst tensor to be used to convert from winograd domain to spatial domain when calling to reshape_output() |
| 178 | TensorInfo info(TensorShape(_output->info()->dimension(2), _output->info()->dimension(0), |
| 179 | _output->info()->dimension(1), _output->info()->dimension(3)), |
| 180 | 1, _output->info()->data_type()); |
| 181 | _output_nhwc.allocator()->init(info); |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 182 | _output_nhwc.allocator()->allocate(); |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 183 | |
| 184 | // Re-order a weight tensor from [Output feature map x Input feature map x Height x Width] to [Height x Width x Input feature map x Output feature map] |
Georgios Pinitas | 02ee429 | 2018-02-15 17:22:36 +0000 | [diff] [blame] | 185 | _permute_weights.configure(weights, &_weights_hwio, PermutationVector(3U, 2U, 0U, 1U)); |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 186 | _weights_hwio.allocator()->allocate(); |
| 187 | |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 188 | // configure the kernel to transform the input tensor from NCHW -> NHWC |
| 189 | _permute_input.configure(input, &_input_nhwc, PermutationVector(2U, 0U, 1U)); |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 190 | _input_nhwc.allocator()->allocate(); |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 191 | |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 192 | const KernelShape kernel_shape({ out_channels, weights_height, weights_width, in_channels }); |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 193 | |
| 194 | // Configure the InputTransform |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 195 | const int input_matrix_stride = transform_input_kernel->get_matrix_stride(kernel_shape, in_shape, use_padding_type); |
| 196 | transform_input_kernel->configure(reinterpret_cast<float *>(_input_nhwc.buffer()), in_shape.n_batches, in_shape.n_rows, in_shape.n_cols, in_shape.n_channels, use_padding_type, |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 197 | reinterpret_cast<float *>(_input_workspace.buffer()), input_matrix_stride); |
| 198 | |
| 199 | // Configure WeightsTransform |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 200 | const int kernel_matrix_stride = transform_weights_kernel->get_matrix_stride(kernel_shape); |
| 201 | transform_weights_kernel->configure(&_weights_hwio, reinterpret_cast<float *>(_kernel_storage.buffer()), kernel_matrix_stride, out_channels, in_channels); |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 202 | |
| 203 | // Configure OutputTransform |
| 204 | //The biases tensor has not been allocated at this point in time, the output transform will add the biases to the final result in the run() method |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 205 | const int output_matrix_stride = transform_output_kernel->get_matrix_stride(kernel_shape, in_shape, use_padding_type); |
| 206 | const auto output_shape(transform_output_kernel->get_output_shape(kernel_shape, in_shape, use_padding_type)); |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 207 | |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 208 | transform_output_kernel->configure(biases, reinterpret_cast<float *>(_output_workspace.buffer()), |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 209 | output_matrix_stride, reinterpret_cast<float *>(_output_nhwc.buffer()), |
| 210 | in_shape.n_batches, output_shape.n_rows, output_shape.n_cols, out_channels); |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 211 | |
Michalis Spyrou | 2b3129e | 2018-04-25 18:10:13 +0100 | [diff] [blame] | 212 | // Configure GEMM |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame^] | 213 | const int tile_rows = iceildiv(output_shape.n_rows, output_tile.height); |
| 214 | const int tile_cols = iceildiv(output_shape.n_cols, output_tile.width); |
Michalis Spyrou | 2b3129e | 2018-04-25 18:10:13 +0100 | [diff] [blame] | 215 | const int m = in_shape.n_batches * tile_rows * tile_cols; |
| 216 | const int k = in_shape.n_channels; |
| 217 | const int n = out_channels; |
| 218 | const int input_matrix_row_stride = in_shape.n_channels; |
| 219 | const int kernel_matrix_row_stride = roundup(out_channels, N_BLOCK); |
| 220 | const int output_matrix_row_stride = kernel_matrix_row_stride; |
| 221 | unsigned int num_threads = NEScheduler::get().num_threads(); |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 222 | |
Michalis Spyrou | 2b3129e | 2018-04-25 18:10:13 +0100 | [diff] [blame] | 223 | _arm_gemm = arm_gemm::gemm<float, float>(NEScheduler::get().cpu_info(), m, n, k, 1, n_gemms, false, false, 1.f, 0.f, num_threads, false); |
| 224 | _arm_gemm->set_arrays(reinterpret_cast<float *>(_input_workspace.buffer()), input_matrix_row_stride, 0, input_matrix_stride, reinterpret_cast<float *>(_kernel_storage.buffer()), |
| 225 | kernel_matrix_row_stride, kernel_matrix_stride, reinterpret_cast<float *>(_output_workspace.buffer()), output_matrix_row_stride, 0, output_matrix_stride); |
| 226 | |
| 227 | auto acl_gemm_wrapper = support::cpp14::make_unique<NEGEMMAssemblyWrapper<arm_gemm::GemmCommon<float, float>>>(); |
| 228 | acl_gemm_wrapper->configure(_arm_gemm.get()); |
| 229 | const size_t workspace_size = _arm_gemm->get_working_size(); |
| 230 | |
| 231 | // Allocate workspace |
| 232 | if(workspace_size > 0) |
| 233 | { |
| 234 | const unsigned int alignment = 4096; |
Georgios Pinitas | 932b561 | 2018-05-03 13:44:35 +0100 | [diff] [blame] | 235 | allocate_workspace(workspace_size, _workspace, &_memory_group, alignment, 1); |
Michalis Spyrou | 2b3129e | 2018-04-25 18:10:13 +0100 | [diff] [blame] | 236 | _arm_gemm->set_working_space(reinterpret_cast<float *>(_workspace.buffer())); |
| 237 | } |
| 238 | |
| 239 | const unsigned int window_size = _arm_gemm->get_window_size(); |
| 240 | if(window_size < num_threads) |
| 241 | { |
| 242 | num_threads = window_size; |
| 243 | _arm_gemm->set_nthreads(num_threads); |
| 244 | } |
| 245 | |
| 246 | _gemm_kernel = std::move(acl_gemm_wrapper); |
Pablo Tello | 52140b4 | 2018-01-30 14:48:11 +0000 | [diff] [blame] | 247 | |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 248 | // Reorder the convoluted output to ACL's ordering NCHW |
| 249 | _permute_output.configure(&_output_nhwc, _output, PermutationVector(1U, 2U, 0U)); |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 250 | |
| 251 | _transform_input_kernel = std::move(transform_input_kernel); |
| 252 | _transform_weights_kernel = std::move(transform_weights_kernel); |
| 253 | _transform_output_kernel = std::move(transform_output_kernel); |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 254 | |
| 255 | //Configure Activation Layer |
| 256 | _is_activationlayer_enabled = act_info.enabled(); |
| 257 | if(_is_activationlayer_enabled) |
| 258 | { |
| 259 | _activationlayer_function.configure(output, nullptr, act_info); |
| 260 | } |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Georgios Pinitas | 9fb1159 | 2018-04-26 20:34:58 +0100 | [diff] [blame] | 263 | void NEWinogradConvolutionLayer::run() |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 264 | { |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 265 | _memory_group.acquire(); |
| 266 | if(!_reshaped_kernel) |
| 267 | { |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 268 | _reshaped_kernel = true; |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 269 | _permute_weights.run(); |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 270 | NEScheduler::get().schedule(_transform_weights_kernel.get(), Window::DimX); |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 271 | } |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 272 | //Bring channels to the front as Winograd code expects the tensor to be in the format NHWC |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 273 | _permute_input.run(); |
Pablo Tello | 679463a | 2018-02-06 11:47:59 +0000 | [diff] [blame] | 274 | |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 275 | // Transform input tensor to the winograd domain |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 276 | NEScheduler::get().schedule(_transform_input_kernel.get(), Window::DimX); |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 277 | |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 278 | //Run 16 GEMMs in multiple threads, each kernel runs one or more GEMMs |
Michalis Spyrou | 2b3129e | 2018-04-25 18:10:13 +0100 | [diff] [blame] | 279 | NEScheduler::get().schedule(_gemm_kernel.get(), Window::DimX); |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 280 | |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 281 | // Transform output tensor to the spatial domain |
Pablo Tello | f6c572c | 2018-02-14 12:47:30 +0000 | [diff] [blame] | 282 | NEScheduler::get().schedule(_transform_output_kernel.get(), Window::DimX); |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 283 | |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 284 | // Reorder the convoluted output to ACL's ordering NCHW |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 285 | _permute_output.run(); |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 286 | |
| 287 | if(_is_activationlayer_enabled) |
| 288 | { |
| 289 | _activationlayer_function.run(); |
| 290 | } |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 291 | _memory_group.release(); |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 292 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 293 | |
Georgios Pinitas | 9fb1159 | 2018-04-26 20:34:58 +0100 | [diff] [blame] | 294 | Status NEWinogradConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
| 295 | const ActivationLayerInfo &act_info) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 296 | { |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame^] | 297 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 298 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, weights, biases, output, conv_info)); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 299 | |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 300 | // Get indices for the width and height |
| 301 | const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH); |
| 302 | const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT); |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 303 | // Input shape |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame^] | 304 | const TensorShape input_shape = input->tensor_shape(); |
| 305 | const unsigned int input_w = input_shape[idx_width]; |
| 306 | const unsigned int input_h = input_shape[idx_height]; |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 307 | |
| 308 | // Kernel size |
| 309 | const unsigned int kernel_w = weights->tensor_shape()[idx_width]; |
| 310 | const unsigned int kernel_h = weights->tensor_shape()[idx_height]; |
| 311 | |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame^] | 312 | const Size2D output_tile = (Size2D(kernel_w, kernel_h) == Size2D(3U, 3U) && input_w > 4 && input_h > 4) ? Size2D(4U, 4U) : Size2D(2U, 2U); |
| 313 | |
| 314 | const WinogradInfo winograd_info = WinogradInfo(output_tile, |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 315 | Size2D(kernel_w, kernel_h), |
| 316 | Size2D(input_shape[idx_width], input_shape[idx_height]), |
| 317 | conv_info, |
| 318 | input->data_layout()); |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 319 | |
| 320 | // Validate input transform |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 321 | const TensorShape input0_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input, winograd_info); |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 322 | const TensorInfo input0 = input->clone()->set_tensor_shape(input0_shape); |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame^] | 323 | switch(weights->dimension(idx_width)) |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 324 | { |
| 325 | case 3: |
| 326 | { |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame^] | 327 | if(input_w > 4 && input_h > 4) |
| 328 | { |
| 329 | ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 4, 4, 3, 3>::validate(input, &input0, winograd_info))); |
| 330 | } |
| 331 | else |
| 332 | { |
| 333 | ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 2, 2, 3, 3>::validate(input, &input0, winograd_info))); |
| 334 | } |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 335 | break; |
| 336 | } |
| 337 | case 5: |
| 338 | { |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 339 | ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 2, 2, 5, 5>::validate(input, &input0, winograd_info))); |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 340 | break; |
| 341 | } |
| 342 | default: |
| 343 | { |
| 344 | ARM_COMPUTE_RETURN_ERROR_MSG("Only 3x3 and 5x5 kernels supported."); |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | // Validate filter transform |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 349 | const TensorShape input1_shape = misc::shape_calculator::compute_winograd_filter_transform_shape(*weights, winograd_info); |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 350 | const TensorInfo input1 = weights->clone()->set_tensor_shape(input1_shape); |
| 351 | |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame^] | 352 | switch(weights->dimension(idx_width)) |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 353 | { |
| 354 | case 3: |
| 355 | { |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame^] | 356 | if(input_w > 4 && input_h > 4) |
| 357 | { |
| 358 | ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 4, 4, 3, 3>::validate(weights, &input1, winograd_info))); |
| 359 | } |
| 360 | else |
| 361 | { |
| 362 | ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 2, 2, 3, 3>::validate(weights, &input1, winograd_info))); |
| 363 | } |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 364 | break; |
| 365 | } |
| 366 | case 5: |
| 367 | { |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 368 | ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 2, 2, 5, 5>::validate(weights, &input1, winograd_info))); |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 369 | break; |
| 370 | } |
| 371 | default: |
| 372 | { |
| 373 | ARM_COMPUTE_RETURN_ERROR_MSG("Only 3x3 and 5x5 kernels supported."); |
| 374 | break; |
| 375 | } |
| 376 | } |
| 377 | // Validate batched matrix multiply |
| 378 | TensorShape batched_mm_output_shape = input0.tensor_shape(); |
| 379 | batched_mm_output_shape[0] = input1.tensor_shape()[0]; |
| 380 | const TensorInfo batched_mm_output = input0.clone()->set_tensor_shape(batched_mm_output_shape); |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame^] | 381 | switch(weights->dimension(idx_width)) |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 382 | { |
| 383 | case 3: |
| 384 | { |
Vidhya Sudhan Loganathan | cb0010b | 2018-05-11 16:23:53 +0100 | [diff] [blame^] | 385 | if(input_w > 4 && input_h > 4) |
| 386 | { |
| 387 | // Validate output transform |
| 388 | ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 4, 4, 3, 3>::validate(&batched_mm_output, biases, output, winograd_info))); |
| 389 | } |
| 390 | else |
| 391 | { |
| 392 | // Validate output transform |
| 393 | ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 2, 2, 3, 3>::validate(&batched_mm_output, biases, output, winograd_info))); |
| 394 | } |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 395 | break; |
| 396 | } |
| 397 | case 5: |
| 398 | { |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 399 | // Validate output transform |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 400 | ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 2, 2, 5, 5>::validate(&batched_mm_output, biases, output, winograd_info))); |
Vidhya Sudhan Loganathan | 3ca9786 | 2018-04-23 08:20:04 +0100 | [diff] [blame] | 401 | break; |
| 402 | } |
| 403 | default: |
| 404 | { |
| 405 | ARM_COMPUTE_RETURN_ERROR_MSG("Only 3x3 and 5x5 kernels supported."); |
| 406 | break; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | // Validate Activation Layer |
| 411 | if(act_info.enabled()) |
| 412 | { |
| 413 | NEActivationLayer::validate(output, nullptr, act_info); |
| 414 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 415 | return Status{}; |
| 416 | } |
| 417 | |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 418 | } // namespace arm_compute |