Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [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/NEON/functions/NEConvolutionLayer.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 | |
| 36 | NEConvolutionLayer::NEConvolutionLayer() |
| 37 | : _input_im2col_kernel(), _input_interleave_kernel(), _weights_reshape_kernel(), _weights_transposed_kernel(), _mm_kernel(), _output_col2im_kernel(), _input_im2col_reshaped(), |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 38 | _input_interleaved_reshaped(), _weights_reshaped(), _weights_transposed(), _gemm_output(), _is_first_run(false), _has_bias(false) |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
| 42 | void NEConvolutionLayer::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)); |
| 49 | ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4); |
| 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() > 1); |
| 57 | } |
| 58 | |
| 59 | _has_bias = (biases != nullptr); |
| 60 | _is_first_run = true; |
| 61 | |
| 62 | // Get parameters for conv_info |
Anthony Barbier | 46d5927 | 2017-05-04 09:15:15 +0100 | [diff] [blame] | 63 | unsigned int stride_x = 0; |
| 64 | unsigned int stride_y = 0; |
| 65 | unsigned int pad_x = 0; |
| 66 | unsigned int pad_y = 0; |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 67 | std::tie(stride_x, stride_y) = conv_info.stride(); |
| 68 | std::tie(pad_x, pad_y) = conv_info.pad(); |
| 69 | |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 70 | // Get convolved dimensions |
| 71 | unsigned int conv_w = 0; |
| 72 | unsigned int conv_h = 0; |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 73 | std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), weights->info()->dimension(0), |
| 74 | stride_x, stride_y, pad_x, pad_y, conv_info.round()); |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 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"); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 76 | |
| 77 | // Create tensor to store the reshaped weights |
Anthony Barbier | 46d5927 | 2017-05-04 09:15:15 +0100 | [diff] [blame] | 78 | const unsigned int mat_weights_cols = weights->info()->dimension(3); |
| 79 | const unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + (_has_bias ? 1 : 0); |
| 80 | TensorShape shape_wr(mat_weights_cols, mat_weights_rows); |
| 81 | TensorInfo info_wr(shape_wr, 1, weights->info()->data_type()); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 82 | _weights_reshaped.allocator()->init(info_wr); |
| 83 | |
| 84 | // Create tensor to store transposed weights |
Anthony Barbier | 46d5927 | 2017-05-04 09:15:15 +0100 | [diff] [blame] | 85 | TensorShape shape_wt(mat_weights_rows * 4, static_cast<unsigned int>(std::ceil(mat_weights_cols / 4.f))); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 86 | TensorInfo info_wt(shape_wt, 1, weights->info()->data_type()); |
| 87 | _weights_transposed.allocator()->init(info_wt); |
| 88 | |
| 89 | // Create tensor to store im2col reshaped inputs |
Anthony Barbier | 46d5927 | 2017-05-04 09:15:15 +0100 | [diff] [blame] | 90 | const unsigned int mat_input_cols = mat_weights_rows; |
| 91 | const unsigned int mat_input_rows = conv_w * conv_h; |
| 92 | TensorShape shape_im2col = input->info()->tensor_shape(); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 93 | shape_im2col.set(0, mat_input_cols); |
| 94 | shape_im2col.set(1, mat_input_rows); |
| 95 | shape_im2col.set(2, 1); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 96 | TensorInfo info_im2col(shape_im2col, 1, input->info()->data_type()); |
| 97 | _input_im2col_reshaped.allocator()->init(info_im2col); |
| 98 | |
| 99 | // Create tensor to prepare input tensor for GEMM |
| 100 | TensorShape shape_interleaved = shape_im2col; |
| 101 | shape_interleaved.set(0, shape_interleaved.x() * 4); |
Anthony Barbier | 46d5927 | 2017-05-04 09:15:15 +0100 | [diff] [blame] | 102 | shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f)); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 103 | TensorInfo info_interleaved(shape_interleaved, 1, input->info()->data_type()); |
| 104 | _input_interleaved_reshaped.allocator()->init(info_interleaved); |
| 105 | |
| 106 | // Create GEMM output tensor |
| 107 | TensorShape shape_gemm = _input_im2col_reshaped.info()->tensor_shape(); |
| 108 | shape_gemm.set(0, mat_weights_cols); |
| 109 | shape_gemm.set(1, mat_input_rows); |
| 110 | TensorInfo info_gemm(shape_gemm, 1, input->info()->data_type()); |
| 111 | _gemm_output.allocator()->init(info_gemm); |
| 112 | |
| 113 | // Configure kernels |
| 114 | _input_im2col_kernel.configure(input, &_input_im2col_reshaped, std::make_pair(conv_w, conv_h), conv_info, _has_bias); |
| 115 | _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped); |
| 116 | _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped); |
| 117 | _weights_transposed_kernel.configure(&_weights_reshaped, &_weights_transposed); |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 118 | _mm_kernel.configure(&_input_interleaved_reshaped, &_weights_transposed, &_gemm_output, 1.0f); |
| 119 | _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h)); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 120 | |
| 121 | // Allocate the tensors once the all configure methods have been called |
| 122 | _weights_reshaped.allocator()->allocate(); |
| 123 | _weights_transposed.allocator()->allocate(); |
| 124 | _input_im2col_reshaped.allocator()->allocate(); |
| 125 | _input_interleaved_reshaped.allocator()->allocate(); |
| 126 | _gemm_output.allocator()->allocate(); |
| 127 | } |
| 128 | |
| 129 | void NEConvolutionLayer::run() |
| 130 | { |
| 131 | // Run weights reshaping (Runs once for every configure) |
| 132 | if(_is_first_run) |
| 133 | { |
| 134 | _is_first_run = false; |
| 135 | NEScheduler::get().multithread(&_weights_reshape_kernel, 3); |
| 136 | NEScheduler::get().multithread(&_weights_transposed_kernel); |
| 137 | } |
| 138 | |
| 139 | // Run input reshaping |
| 140 | NEScheduler::get().multithread(&_input_im2col_kernel); |
| 141 | |
| 142 | // Run interleave |
| 143 | NEScheduler::get().multithread(&_input_interleave_kernel); |
| 144 | |
| 145 | // Runs GEMM on reshaped matrices |
| 146 | NEScheduler::get().multithread(&_mm_kernel); |
| 147 | |
| 148 | // Reshape output matrix |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 149 | NEScheduler::get().multithread(&_output_col2im_kernel); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 150 | } |