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