Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 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 | #include "arm_compute/runtime/NEON/functions/NELocallyConnectedLayer.h" |
| 25 | |
| 26 | #include "arm_compute/core/PixelValue.h" |
| 27 | #include "arm_compute/core/Utils.h" |
| 28 | #include "arm_compute/core/Validate.h" |
| 29 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 30 | #include "src/core/NEON/kernels/NEIm2ColKernel.h" |
| 31 | #include "src/core/NEON/kernels/NELocallyConnectedMatrixMultiplyKernel.h" |
| 32 | #include "src/core/NEON/kernels/NEWeightsReshapeKernel.h" |
| 33 | #include "support/MemorySupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
| 35 | #include <cmath> |
| 36 | #include <tuple> |
| 37 | |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 38 | namespace arm_compute |
| 39 | { |
Alex Gilday | 27c08ab | 2018-02-22 11:36:16 +0000 | [diff] [blame] | 40 | namespace |
| 41 | { |
| 42 | void calculate_shapes(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
| 43 | TensorShape &shape_wr, TensorShape &shape_im2col, TensorShape &shape_gemm) |
| 44 | { |
| 45 | ARM_COMPUTE_UNUSED(output); |
| 46 | |
| 47 | const unsigned int kernel_width = weights->dimension(0); |
| 48 | const unsigned int kernel_height = weights->dimension(1); |
| 49 | |
| 50 | bool has_bias = (biases != nullptr); |
| 51 | |
| 52 | // Get convolved dimensions |
| 53 | unsigned int conv_w = 0; |
| 54 | unsigned int conv_h = 0; |
| 55 | std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(0), input->dimension(1), kernel_width, kernel_height, |
| 56 | conv_info); |
| 57 | |
| 58 | const size_t mat_weights_cols = weights->dimension(3); |
| 59 | const size_t mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + ((has_bias) ? 1 : 0); |
| 60 | const size_t mat_weights_num = weights->dimension(4); |
| 61 | |
| 62 | shape_wr = TensorShape(mat_weights_cols, mat_weights_rows, mat_weights_num); |
| 63 | |
| 64 | const size_t mat_input_cols = mat_weights_rows; |
| 65 | const size_t mat_input_rows = conv_w * conv_h; |
| 66 | |
| 67 | shape_im2col = input->tensor_shape(); |
| 68 | shape_im2col.set(0, mat_input_cols); |
| 69 | shape_im2col.set(1, mat_input_rows); |
| 70 | shape_im2col.set(2, 1); |
| 71 | |
| 72 | shape_gemm = shape_im2col; |
| 73 | shape_gemm.set(0, mat_weights_cols); |
| 74 | shape_gemm.set(1, mat_input_rows); |
| 75 | } |
| 76 | } // namespace |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 77 | NELocallyConnectedLayer::~NELocallyConnectedLayer() = default; |
Alex Gilday | 27c08ab | 2018-02-22 11:36:16 +0000 | [diff] [blame] | 78 | |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 79 | NELocallyConnectedLayer::NELocallyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 80 | : _memory_group(std::move(memory_manager)), _input_im2col(), _weights_reshape_kernel(), _mm_kernel(), _output_col2im(), _input_im2col_reshaped(), _weights_reshaped(), _gemm_output(), |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 81 | _is_prepared(false), _original_weights(nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 82 | { |
| 83 | } |
| 84 | |
Alex Gilday | 27c08ab | 2018-02-22 11:36:16 +0000 | [diff] [blame] | 85 | Status NELocallyConnectedLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info) |
| 86 | { |
| 87 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
| 88 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(2) != input->dimension(2)); |
| 89 | ARM_COMPUTE_RETURN_ERROR_ON(!conv_info.padding_is_symmetric()); |
| 90 | |
| 91 | bool has_bias = (biases != nullptr); |
| 92 | |
| 93 | if(has_bias) |
| 94 | { |
| 95 | ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3)); |
| 96 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 2); |
| 97 | } |
| 98 | |
| 99 | const unsigned int kernel_width = weights->dimension(0); |
| 100 | const unsigned int kernel_height = weights->dimension(1); |
| 101 | |
| 102 | // Get convolved dimensions |
| 103 | unsigned int conv_w = 0; |
| 104 | unsigned int conv_h = 0; |
| 105 | std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(0), input->dimension(1), kernel_width, kernel_height, |
| 106 | conv_info); |
| 107 | |
| 108 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((output->dimension(0) != conv_w) || (output->dimension(1) != conv_h), "Output shape does not match the expected one"); |
| 109 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(4) != (conv_w * conv_h), "Weights shape does not match the expected one"); |
| 110 | |
| 111 | // Calculate intermediate buffer shapes |
| 112 | TensorShape shape_wr; |
| 113 | TensorShape shape_im2col; |
| 114 | TensorShape shape_gemm; |
| 115 | calculate_shapes(input, weights, biases, output, conv_info, shape_wr, shape_im2col, shape_gemm); |
| 116 | |
| 117 | TensorInfo weights_reshaped_info(shape_wr, 1, weights->data_type()); |
| 118 | TensorInfo input_im2col_reshaped_info(shape_im2col, 1, input->data_type()); |
| 119 | TensorInfo gemm_output_info(shape_gemm, 1, input->data_type()); |
| 120 | |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 121 | ARM_COMPUTE_RETURN_ON_ERROR(NEIm2Col::validate(input, &input_im2col_reshaped_info, Size2D(kernel_width, kernel_height), conv_info, has_bias)); |
Alex Gilday | 27c08ab | 2018-02-22 11:36:16 +0000 | [diff] [blame] | 122 | ARM_COMPUTE_RETURN_ON_ERROR(NEWeightsReshapeKernel::validate(weights, biases, &weights_reshaped_info)); |
| 123 | ARM_COMPUTE_RETURN_ON_ERROR(NELocallyConnectedMatrixMultiplyKernel::validate(&input_im2col_reshaped_info, &weights_reshaped_info, &gemm_output_info)); |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 124 | ARM_COMPUTE_RETURN_ON_ERROR(NECol2Im::validate(&gemm_output_info, output, Size2D(conv_w, conv_h))); |
Alex Gilday | 27c08ab | 2018-02-22 11:36:16 +0000 | [diff] [blame] | 125 | |
| 126 | return Status{}; |
| 127 | } |
| 128 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 129 | void NELocallyConnectedLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info) |
| 130 | { |
Alex Gilday | 27c08ab | 2018-02-22 11:36:16 +0000 | [diff] [blame] | 131 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
| 132 | ARM_COMPUTE_ERROR_THROW_ON(NELocallyConnectedLayer::validate(input->info(), weights->info(), biases == nullptr ? nullptr : biases->info(), output->info(), conv_info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 133 | |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 134 | bool _has_bias = (biases != nullptr); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 135 | _is_prepared = false; |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 136 | _original_weights = weights; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 137 | |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 138 | const unsigned int kernel_width = weights->info()->dimension(0); |
| 139 | const unsigned int kernel_height = weights->info()->dimension(1); |
| 140 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 141 | // Get convolved dimensions |
| 142 | unsigned int conv_w = 0; |
| 143 | unsigned int conv_h = 0; |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 144 | std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width, kernel_height, |
Gian Marco Iodice | 4e28869 | 2017-06-27 11:41:59 +0100 | [diff] [blame] | 145 | conv_info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 146 | |
Alex Gilday | 27c08ab | 2018-02-22 11:36:16 +0000 | [diff] [blame] | 147 | // Calculate intermediate buffer shapes |
| 148 | TensorShape shape_wr; |
| 149 | TensorShape shape_im2col; |
| 150 | TensorShape shape_gemm; |
| 151 | calculate_shapes(input->info(), weights->info(), biases == nullptr ? nullptr : biases->info(), output->info(), conv_info, shape_wr, shape_im2col, shape_gemm); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 152 | |
| 153 | _weights_reshaped.allocator()->init(TensorInfo(shape_wr, 1, weights->info()->data_type())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 154 | _input_im2col_reshaped.allocator()->init(TensorInfo(shape_im2col, 1, input->info()->data_type())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 155 | _gemm_output.allocator()->init(TensorInfo(shape_gemm, 1, input->info()->data_type())); |
| 156 | |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 157 | // Manage intermediate buffers |
| 158 | _memory_group.manage(&_input_im2col_reshaped); |
| 159 | _memory_group.manage(&_gemm_output); |
| 160 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 161 | // Configure kernels |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 162 | _input_im2col.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, _has_bias); |
| 163 | _weights_reshape_kernel = arm_compute::support::cpp14::make_unique<NEWeightsReshapeKernel>(); |
| 164 | _weights_reshape_kernel->configure(weights, biases, &_weights_reshaped); |
| 165 | _mm_kernel = arm_compute::support::cpp14::make_unique<NELocallyConnectedMatrixMultiplyKernel>(); |
| 166 | _mm_kernel->configure(&_input_im2col_reshaped, &_weights_reshaped, &_gemm_output); |
| 167 | _output_col2im.configure(&_gemm_output, output, Size2D(conv_w, conv_h)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 168 | |
| 169 | // Allocate intermediate tensors |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 170 | _input_im2col_reshaped.allocator()->allocate(); |
| 171 | _gemm_output.allocator()->allocate(); |
| 172 | } |
| 173 | |
| 174 | void NELocallyConnectedLayer::run() |
| 175 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 176 | prepare(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 177 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 178 | MemoryGroupResourceScope scope_mg(_memory_group); |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 179 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 180 | // Run input reshaping |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 181 | _input_im2col.run(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 182 | |
| 183 | // Runs GEMM on reshaped matrices |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 184 | NEScheduler::get().schedule(_mm_kernel.get(), Window::DimX); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 185 | |
| 186 | // Reshape output matrix |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 187 | _output_col2im.run(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 188 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 189 | |
| 190 | void NELocallyConnectedLayer::prepare() |
| 191 | { |
| 192 | if(!_is_prepared) |
| 193 | { |
| 194 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
| 195 | |
| 196 | // Run weights reshaping and mark original weights tensor as unused |
| 197 | _weights_reshaped.allocator()->allocate(); |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 198 | NEScheduler::get().schedule(_weights_reshape_kernel.get(), 3); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 199 | _original_weights->mark_as_unused(); |
| 200 | |
| 201 | _is_prepared = true; |
| 202 | } |
| 203 | } |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 204 | } // namespace arm_compute |