blob: 9120aadf17c9fa14bedc22e09524ec328e748145 [file] [log] [blame]
Anthony Barbierdbdab852017-06-23 15:42:00 +01001/*
Anthony Barbier06ea0482018-02-22 15:45:35 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbierdbdab852017-06-23 15:42:00 +01003 *
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
Kaizen8938bd32017-09-28 14:38:23 +010036CLLocallyConnectedLayer::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 Barbierdbdab852017-06-23 15:42:00 +010039{
40}
41
42void 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));
Anthony Barbier06ea0482018-02-22 15:45:35 +000049 ARM_COMPUTE_ERROR_ON(!conv_info.padding_is_symmetric());
Anthony Barbierdbdab852017-06-23 15:42:00 +010050
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
70 // Get convolved dimensions
71 unsigned int conv_w = 0;
72 unsigned int conv_h = 0;
Kaizen8938bd32017-09-28 14:38:23 +010073 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), weights->info()->dimension(0), weights->info()->dimension(1),
74 conv_info);
Anthony Barbierdbdab852017-06-23 15:42:00 +010075
76 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");
77 ARM_COMPUTE_ERROR_ON_MSG(weights->info()->dimension(4) != (conv_w * conv_h), "Weights shape does not match the expected one");
78
79 // Create tensor to store the reshaped weights
80 const size_t mat_weights_cols = weights->info()->dimension(3);
81 const size_t mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + ((_has_bias) ? 1 : 0);
82 const size_t mat_weights_num = weights->info()->dimension(4);
83
84 const TensorShape shape_wr(mat_weights_cols, mat_weights_rows, mat_weights_num);
85
86 _weights_reshaped.allocator()->init(TensorInfo(shape_wr, 1, weights->info()->data_type()));
87
88 // Create tensor to store im2col reshaped inputs
89 const size_t mat_input_cols = mat_weights_rows;
90 const size_t mat_input_rows = conv_w * conv_h;
91 TensorShape shape_im2col = input->info()->tensor_shape();
92 shape_im2col.set(0, mat_input_cols);
93 shape_im2col.set(1, mat_input_rows);
94 shape_im2col.set(2, 1);
95
96 _input_im2col_reshaped.allocator()->init(TensorInfo(shape_im2col, 1, input->info()->data_type()));
97
98 // Create locally connected layer output tensor
99 TensorShape shape_gemm = _input_im2col_reshaped.info()->tensor_shape();
100 shape_gemm.set(0, mat_weights_cols);
101 shape_gemm.set(1, mat_input_rows);
102 _gemm_output.allocator()->init(TensorInfo(shape_gemm, 1, input->info()->data_type()));
103
Kaizen8938bd32017-09-28 14:38:23 +0100104 // Manage intermediate buffers
105 _memory_group.manage(&_input_im2col_reshaped);
106 _memory_group.manage(&_gemm_output);
107
Anthony Barbierdbdab852017-06-23 15:42:00 +0100108 // Configure kernels
Kaizen8938bd32017-09-28 14:38:23 +0100109 _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(conv_w, conv_h), conv_info, _has_bias);
Anthony Barbierdbdab852017-06-23 15:42:00 +0100110 _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped);
111 _mm_kernel.configure(&_input_im2col_reshaped, &_weights_reshaped, &_gemm_output);
112 _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h));
113
114 // Allocate intermediate tensors
115 _weights_reshaped.allocator()->allocate();
116 _input_im2col_reshaped.allocator()->allocate();
117 _gemm_output.allocator()->allocate();
118}
119
120void CLLocallyConnectedLayer::run()
121{
122 // Run weights reshaping (Runs once for every configure)
123 if(_is_first_run)
124 {
125 _is_first_run = false;
126 CLScheduler::get().enqueue(_weights_reshape_kernel);
127 }
128
Kaizen8938bd32017-09-28 14:38:23 +0100129 _memory_group.acquire();
130
Anthony Barbierdbdab852017-06-23 15:42:00 +0100131 // Run input reshaping
132 CLScheduler::get().enqueue(_input_im2col_kernel);
133
134 // Runs vector matrix multiply on reshaped matrices
135 CLScheduler::get().enqueue(_mm_kernel);
136
137 // Reshape output matrix
138 CLScheduler::get().enqueue(_output_col2im_kernel, false);
Kaizen8938bd32017-09-28 14:38:23 +0100139
140 _memory_group.release();
Anthony Barbierdbdab852017-06-23 15:42:00 +0100141}