blob: 263fb519876f5209e9042c53b082b24e6cd515b8 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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
34using namespace arm_compute;
35
36CLLocallyConnectedLayer::CLLocallyConnectedLayer()
37 : _input_im2col_kernel(), _weights_reshape_kernel(), _mm_kernel(), _output_col2im_kernel(), _input_im2col_reshaped(), _weights_reshaped(), _gemm_output(), _is_first_run(false)
38{
39}
40
41void CLLocallyConnectedLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info)
42{
43 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
44 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::F32);
45 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F32);
46 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output);
47 ARM_COMPUTE_ERROR_ON(weights->info()->dimension(2) != input->info()->dimension(2));
48
49 if(biases != nullptr)
50 {
51 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::F32);
52 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
53 ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(3));
54 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 2);
55 }
56
57 bool _has_bias = (biases != nullptr);
58 _is_first_run = true;
59
60 // Get parameters for conv_info
61 unsigned int stride_x = 0;
62 unsigned int stride_y = 0;
63 unsigned int pad_x = 0;
64 unsigned int pad_y = 0;
65 std::tie(stride_x, stride_y) = conv_info.stride();
66 std::tie(pad_x, pad_y) = conv_info.pad();
67
68 // Get convolved dimensions
69 unsigned int conv_w = 0;
70 unsigned int conv_h = 0;
71 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), weights->info()->dimension(0),
72 stride_x, stride_y, pad_x, pad_y, conv_info.round());
73
74 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");
75 ARM_COMPUTE_ERROR_ON_MSG(weights->info()->dimension(4) != (conv_w * conv_h), "Weights shape does not match the expected one");
76
77 // Create tensor to store the reshaped weights
78 const size_t mat_weights_cols = weights->info()->dimension(3);
79 const size_t mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + ((_has_bias) ? 1 : 0);
80 const size_t mat_weights_num = weights->info()->dimension(4);
81
82 const TensorShape shape_wr(mat_weights_cols, mat_weights_rows, mat_weights_num);
83
84 _weights_reshaped.allocator()->init(TensorInfo(shape_wr, 1, weights->info()->data_type()));
85
86 // Create tensor to store im2col reshaped inputs
87 const size_t mat_input_cols = mat_weights_rows;
88 const size_t mat_input_rows = conv_w * conv_h;
89 TensorShape shape_im2col = input->info()->tensor_shape();
90 shape_im2col.set(0, mat_input_cols);
91 shape_im2col.set(1, mat_input_rows);
92 shape_im2col.set(2, 1);
93
94 _input_im2col_reshaped.allocator()->init(TensorInfo(shape_im2col, 1, input->info()->data_type()));
95
96 // Create locally connected layer output tensor
97 TensorShape shape_gemm = _input_im2col_reshaped.info()->tensor_shape();
98 shape_gemm.set(0, mat_weights_cols);
99 shape_gemm.set(1, mat_input_rows);
100 _gemm_output.allocator()->init(TensorInfo(shape_gemm, 1, input->info()->data_type()));
101
102 // Configure kernels
103 _input_im2col_kernel.configure(input, &_input_im2col_reshaped, std::make_pair(conv_w, conv_h), conv_info, _has_bias);
104 _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped);
105 _mm_kernel.configure(&_input_im2col_reshaped, &_weights_reshaped, &_gemm_output);
106 _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h));
107
108 // Allocate intermediate tensors
109 _weights_reshaped.allocator()->allocate();
110 _input_im2col_reshaped.allocator()->allocate();
111 _gemm_output.allocator()->allocate();
112}
113
114void CLLocallyConnectedLayer::run()
115{
116 // Run weights reshaping (Runs once for every configure)
117 if(_is_first_run)
118 {
119 _is_first_run = false;
120 CLScheduler::get().enqueue(_weights_reshape_kernel);
121 }
122
123 // Run input reshaping
124 CLScheduler::get().enqueue(_input_im2col_kernel);
125
126 // Runs vector matrix multiply on reshaped matrices
127 CLScheduler::get().enqueue(_mm_kernel);
128
129 // Reshape output matrix
130 CLScheduler::get().enqueue(_output_col2im_kernel, false);
131}