Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Anthony Barbier | 21f67d6 | 2018-02-16 15:17:48 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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" |
| 30 | |
| 31 | #include <cmath> |
| 32 | #include <tuple> |
| 33 | |
| 34 | using namespace arm_compute; |
| 35 | |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 36 | NELocallyConnectedLayer::NELocallyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager) |
| 37 | : _memory_group(std::move(memory_manager)), _input_im2col_kernel(), _weights_reshape_kernel(), _mm_kernel(), _output_col2im_kernel(), _input_im2col_reshaped(), _weights_reshaped(), _gemm_output(), |
| 38 | _is_first_run(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
| 42 | void NELocallyConnectedLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info) |
| 43 | { |
| 44 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
| 45 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::F32); |
| 46 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F32); |
| 47 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output); |
| 48 | ARM_COMPUTE_ERROR_ON(weights->info()->dimension(2) != input->info()->dimension(2)); |
Anthony Barbier | 21f67d6 | 2018-02-16 15:17:48 +0000 | [diff] [blame] | 49 | ARM_COMPUTE_ERROR_ON(!conv_info.padding_is_symmetric()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 50 | |
| 51 | if(biases != nullptr) |
| 52 | { |
| 53 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::F32); |
| 54 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 55 | ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(3)); |
| 56 | ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 2); |
| 57 | } |
| 58 | |
| 59 | bool _has_bias = (biases != nullptr); |
| 60 | _is_first_run = true; |
| 61 | |
| 62 | // Get parameters for conv_info |
| 63 | unsigned int stride_x = 0; |
| 64 | unsigned int stride_y = 0; |
| 65 | unsigned int pad_x = 0; |
| 66 | unsigned int pad_y = 0; |
| 67 | std::tie(stride_x, stride_y) = conv_info.stride(); |
| 68 | std::tie(pad_x, pad_y) = conv_info.pad(); |
| 69 | |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 70 | const unsigned int kernel_width = weights->info()->dimension(0); |
| 71 | const unsigned int kernel_height = weights->info()->dimension(1); |
| 72 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 73 | // Get convolved dimensions |
| 74 | unsigned int conv_w = 0; |
| 75 | unsigned int conv_h = 0; |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 76 | 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] | 77 | conv_info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 78 | |
| 79 | ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(0) != conv_w) || (output->info()->dimension(1) != conv_h), "Output shape does not match the expected one"); |
| 80 | ARM_COMPUTE_ERROR_ON_MSG(weights->info()->dimension(4) != (conv_w * conv_h), "Weights shape does not match the expected one"); |
| 81 | |
| 82 | // Create tensor to store the reshaped weights |
| 83 | const size_t mat_weights_cols = weights->info()->dimension(3); |
| 84 | const size_t mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + ((_has_bias) ? 1 : 0); |
| 85 | const size_t mat_weights_num = weights->info()->dimension(4); |
| 86 | |
| 87 | const TensorShape shape_wr(mat_weights_cols, mat_weights_rows, mat_weights_num); |
| 88 | |
| 89 | _weights_reshaped.allocator()->init(TensorInfo(shape_wr, 1, weights->info()->data_type())); |
| 90 | |
| 91 | // Create tensor to store im2col reshaped inputs |
| 92 | const size_t mat_input_cols = mat_weights_rows; |
| 93 | const size_t mat_input_rows = conv_w * conv_h; |
| 94 | TensorShape shape_im2col = input->info()->tensor_shape(); |
| 95 | shape_im2col.set(0, mat_input_cols); |
| 96 | shape_im2col.set(1, mat_input_rows); |
| 97 | shape_im2col.set(2, 1); |
| 98 | |
| 99 | _input_im2col_reshaped.allocator()->init(TensorInfo(shape_im2col, 1, input->info()->data_type())); |
| 100 | |
| 101 | // Create locally connected layer output tensor |
| 102 | TensorShape shape_gemm = _input_im2col_reshaped.info()->tensor_shape(); |
| 103 | shape_gemm.set(0, mat_weights_cols); |
| 104 | shape_gemm.set(1, mat_input_rows); |
| 105 | _gemm_output.allocator()->init(TensorInfo(shape_gemm, 1, input->info()->data_type())); |
| 106 | |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 107 | // Manage intermediate buffers |
| 108 | _memory_group.manage(&_input_im2col_reshaped); |
| 109 | _memory_group.manage(&_gemm_output); |
| 110 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 111 | // Configure kernels |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 112 | _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, _has_bias); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped); |
| 114 | _mm_kernel.configure(&_input_im2col_reshaped, &_weights_reshaped, &_gemm_output); |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 115 | _output_col2im_kernel.configure(&_gemm_output, output, Size2D(conv_w, conv_h)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 116 | |
| 117 | // Allocate intermediate tensors |
| 118 | _weights_reshaped.allocator()->allocate(); |
| 119 | _input_im2col_reshaped.allocator()->allocate(); |
| 120 | _gemm_output.allocator()->allocate(); |
| 121 | } |
| 122 | |
| 123 | void NELocallyConnectedLayer::run() |
| 124 | { |
| 125 | // Run weights reshaping (Runs once for every configure) |
| 126 | if(_is_first_run) |
| 127 | { |
| 128 | _is_first_run = false; |
| 129 | NEScheduler::get().schedule(&_weights_reshape_kernel, 3); |
| 130 | } |
| 131 | |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 132 | _memory_group.acquire(); |
| 133 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 134 | // Run input reshaping |
| 135 | NEScheduler::get().schedule(&_input_im2col_kernel, Window::DimY); |
| 136 | |
| 137 | // Runs GEMM on reshaped matrices |
| 138 | NEScheduler::get().schedule(&_mm_kernel, Window::DimX); |
| 139 | |
| 140 | // Reshape output matrix |
| 141 | NEScheduler::get().schedule(&_output_col2im_kernel, Window::DimY); |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 142 | |
| 143 | _memory_group.release(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 144 | } |