blob: ce9f0967194ebf99b1eef24f8c017fb7558e10a9 [file] [log] [blame]
Anthony Barbier2a07e182017-08-04 18:20:27 +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/graph/nodes/ConvolutionLayer.h"
25
26#include "arm_compute/runtime/CL/functions/CLConvolutionLayer.h"
Georgios Pinitas6f669f02017-09-26 12:32:57 +010027#include "arm_compute/runtime/CL/functions/CLDirectConvolutionLayer.h"
28#include "arm_compute/runtime/IFunction.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010029#include "arm_compute/runtime/NEON/functions/NEConvolutionLayer.h"
Georgios Pinitas6f669f02017-09-26 12:32:57 +010030#include "arm_compute/runtime/NEON/functions/NEDirectConvolutionLayer.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010031#include "support/ToolchainSupport.h"
Georgios Pinitas6f669f02017-09-26 12:32:57 +010032#include "utils/GraphTypePrinter.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010033#include "utils/TypePrinter.h"
34
Georgios Pinitas6f669f02017-09-26 12:32:57 +010035#include <tuple>
36#include <vector>
37
Anthony Barbier2a07e182017-08-04 18:20:27 +010038using namespace arm_compute::graph;
39
40namespace
41{
Georgios Pinitas6f669f02017-09-26 12:32:57 +010042/** Calculates the output shaped of the convolution layer
43 *
44 * @param[in] input_shape Input tensor shape
45 * @param[in] weights_shape Weights shape
46 * @param[in] conv_info Convolution information (padding, stride, etc.)
47 *
48 * @return The expected output tensor shape
49 */
50TensorShape calculate_convolution_layer_output_shape(const TensorShape &input_shape, const TensorShape &weights_shape, const PadStrideInfo &conv_info)
Anthony Barbier2a07e182017-08-04 18:20:27 +010051{
Georgios Pinitas6f669f02017-09-26 12:32:57 +010052 unsigned int output_width = 0;
53 unsigned int output_height = 0;
Anthony Barbier2a07e182017-08-04 18:20:27 +010054
Georgios Pinitas6f669f02017-09-26 12:32:57 +010055 // Get output width and height
56 std::tie(output_width, output_height) = arm_compute::scaled_dimensions(input_shape.x(), input_shape.y(), weights_shape.x(), weights_shape.y(), conv_info);
57
58 // Create output shape
59 TensorShape output_shape = input_shape;
60 output_shape.set(0, output_width);
61 output_shape.set(1, output_height);
62 output_shape.set(2, weights_shape[3]);
63
64 return output_shape;
65}
66
67// Instantiate GEMM based convolution layer
68template <typename ConvolutionType, typename TensorType, Hint hint>
69std::unique_ptr<arm_compute::IFunction> instantiate_function(ITensor *input, ITensor *weights, ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info)
70{
Anthony Barbier2a07e182017-08-04 18:20:27 +010071 auto conv = arm_compute::support::cpp14::make_unique<ConvolutionType>();
72 conv->configure(
73 dynamic_cast<TensorType *>(input),
Georgios Pinitas6f669f02017-09-26 12:32:57 +010074 dynamic_cast<TensorType *>(weights),
75 dynamic_cast<TensorType *>(biases),
Anthony Barbier2a07e182017-08-04 18:20:27 +010076 dynamic_cast<TensorType *>(output),
77 conv_info, weights_info);
Georgios Pinitas6f669f02017-09-26 12:32:57 +010078 return std::move(conv);
79}
Anthony Barbier2a07e182017-08-04 18:20:27 +010080
Georgios Pinitas6f669f02017-09-26 12:32:57 +010081// Instantiate direct convolution layer
82template <typename ConvolutionType, typename TensorType, Hint hint>
83std::unique_ptr<arm_compute::IFunction> instantiate_direct_function(ITensor *input, ITensor *weights, ITensor *biases, ITensor *output, const PadStrideInfo &conv_info)
84{
85 auto conv = arm_compute::support::cpp14::make_unique<ConvolutionType>();
86 conv->configure(
87 dynamic_cast<TensorType *>(input),
88 dynamic_cast<TensorType *>(weights),
89 dynamic_cast<TensorType *>(biases),
90 dynamic_cast<TensorType *>(output),
91 conv_info);
Anthony Barbier2a07e182017-08-04 18:20:27 +010092 return std::move(conv);
93}
94
95template <Hint hint>
Georgios Pinitas6f669f02017-09-26 12:32:57 +010096std::unique_ptr<arm_compute::IFunction> instantiate(ITensor *input, ITensor *weights, ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
97 ConvolutionMethodHint conv_method);
Anthony Barbier2a07e182017-08-04 18:20:27 +010098
99template <>
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100100std::unique_ptr<arm_compute::IFunction> instantiate<Hint::OPENCL>(ITensor *input, ITensor *weights, ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
101 ConvolutionMethodHint conv_method)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100102{
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100103 if(conv_method == ConvolutionMethodHint::GEMM)
104 {
105 return instantiate_function<arm_compute::CLConvolutionLayer, arm_compute::ICLTensor, Hint::OPENCL>(input, weights, biases, output, conv_info, weights_info);
106 }
107 else
108 {
109 return instantiate_direct_function<arm_compute::CLDirectConvolutionLayer, arm_compute::ICLTensor, Hint::OPENCL>(input, weights, biases, output, conv_info);
110 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100111}
112
113template <>
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100114std::unique_ptr<arm_compute::IFunction> instantiate<Hint::NEON>(ITensor *input, ITensor *weights, ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
115 ConvolutionMethodHint conv_method)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100116{
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100117 if(conv_method == ConvolutionMethodHint::GEMM)
118 {
119 return instantiate_function<arm_compute::NEConvolutionLayer, arm_compute::ITensor, Hint::NEON>(input, weights, biases, output, conv_info, weights_info);
120 }
121 else
122 {
123 return instantiate_direct_function<arm_compute::NEDirectConvolutionLayer, arm_compute::ITensor, Hint::NEON>(input, weights, biases, output, conv_info);
124 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100125}
126} // namespace
127
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100128/** Grouped Convolution function */
129class GroupedConvolutionFunction final : public arm_compute::IFunction
130{
131public:
132 /** Default Constructor */
133 GroupedConvolutionFunction()
134 : _convolutions()
135 {
136 }
137 /** Default Destructor */
138 ~GroupedConvolutionFunction() final = default;
139 /** Prevent instances from being copy constructed */
140 GroupedConvolutionFunction(const GroupedConvolutionFunction &) = delete;
141 /** Prevent instances from being copy assigned */
142 GroupedConvolutionFunction &operator=(const GroupedConvolutionFunction &) = delete;
143 /** Allow instances to be move constructed */
144 GroupedConvolutionFunction(GroupedConvolutionFunction &&) noexcept = default;
145 /** Allow instances to be move assigned */
146 GroupedConvolutionFunction &operator=(GroupedConvolutionFunction &&) noexcept = default;
147 /** Adds a convolution
148 *
149 * @param convolution Convolution function to add
150 */
151 void add_convolution_function(std::unique_ptr<IFunction> convolution)
152 {
153 _convolutions.emplace_back(std::move(convolution));
154 }
155
156 // Inherited methods overriden:
157 void run() override
158 {
159 for(auto &c : _convolutions)
160 {
161 c->run();
162 }
163 }
164
165private:
166 std::vector<std::unique_ptr<IFunction>> _convolutions;
167};
168
Anthony Barbier2a07e182017-08-04 18:20:27 +0100169std::unique_ptr<arm_compute::IFunction> ConvolutionLayer::instantiate_node(Hint hint, ITensor *input, ITensor *output)
170{
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100171 // Set weights and biases info
Anthony Barbier2a07e182017-08-04 18:20:27 +0100172 if(_weights.tensor() == nullptr)
173 {
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100174 _weights.set_info(TensorInfo(TensorShape(_conv_width, _conv_height, input->info()->dimension(2) / _num_groups, _ofm),
175 input->info()->num_channels(), input->info()->data_type(),
Anthony Barbier2a07e182017-08-04 18:20:27 +0100176 input->info()->fixed_point_position()));
177 }
178 if(_biases.tensor() == nullptr)
179 {
180 _biases.set_info(TensorInfo(TensorShape(_ofm), input->info()->num_channels(), input->info()->data_type(), input->info()->fixed_point_position()));
181 }
182
183 std::unique_ptr<arm_compute::IFunction> func;
184 _hint = hint;
185 _input = input;
186 _output = output;
187
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100188 // Check if the weights and biases are loaded
189 bool weights_are_loaded = _weights.tensor() != nullptr;
190 bool biases_are_loaded = _weights.tensor() != nullptr;
191
192 // Set bias and weights target
193 _weights.set_target(_hint);
194 _biases.set_target(_hint);
195
196 // Calculate output shape
197 TensorShape output_shape = calculate_convolution_layer_output_shape(_input->info()->tensor_shape(), _weights.info().tensor_shape(), _conv_info);
198
199 // Output auto inizialitation if not yet initialized
200 arm_compute::auto_init_if_empty(*_output->info(), output_shape, 1, _input->info()->data_type(), _input->info()->fixed_point_position());
201
202 // Create appropriate convolution function
203 // TODO(geopin01): Fix convolution layer hints once the GraphContext has been added
204 if(_num_groups == 1)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100205 {
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100206 func = instantiate_convolution(ConvolutionMethodHint::GEMM);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100207 }
208 else
209 {
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100210 func = instantiate_grouped_convolution(ConvolutionMethodHint::GEMM);
211 }
212
213 // Fill weights
214 if(!weights_are_loaded)
215 {
216 _weights.allocate_and_fill_if_needed();
217 }
218 // Fill biases
219 if(!biases_are_loaded)
220 {
221 _biases.allocate_and_fill_if_needed();
Anthony Barbier2a07e182017-08-04 18:20:27 +0100222 }
223
224 return func;
225}
226
227void ConvolutionLayer::print_info()
228{
229 if(_hint == Hint::OPENCL)
230 {
231 std::cout << "Instantiating CLConvolutionLayer";
232 }
233 else
234 {
235 std::cout << "Instantiating NEConvolutionLayer";
236 }
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100237 std::cout << " Data Type: " << _input->info()->data_type()
238 << " Input Shape: " << _input->info()->tensor_shape()
239 << " Weights shape: " << _weights.info().tensor_shape()
240 << " Biases Shape: " << _biases.info().tensor_shape()
241 << " Output Shape: " << _output->info()->tensor_shape()
242 << " PadStrideInfo: " << _conv_info
243 << " Groups: " << _num_groups
244 << " WeightsInfo: " << _weights_info
245 << std::endl;
246}
247
248std::unique_ptr<arm_compute::IFunction> ConvolutionLayer::instantiate_convolution(ConvolutionMethodHint conv_method_hint)
249{
250 std::unique_ptr<arm_compute::IFunction> func;
251 if(_hint == Hint::OPENCL)
252 {
253 func = instantiate<Hint::OPENCL>(_input, _weights.tensor(), _biases.tensor(), _output, _conv_info, _weights_info, conv_method_hint);
254 }
255 else
256 {
257 func = instantiate<Hint::NEON>(_input, _weights.tensor(), _biases.tensor(), _output, _conv_info, _weights_info, conv_method_hint);
258 }
259 return func;
260}
261
262std::unique_ptr<arm_compute::IFunction> ConvolutionLayer::instantiate_grouped_convolution(ConvolutionMethodHint conv_method_hint)
263{
264 // Get tensor shapes
265 TensorShape input_shape = _input->info()->tensor_shape();
266 TensorShape output_shape = _output->info()->tensor_shape();
267 TensorShape weights_shape = _weights.info().tensor_shape();
268 TensorShape biases_shape = _biases.info().tensor_shape();
269
270 ARM_COMPUTE_ERROR_ON_MSG((input_shape.z() % _num_groups) != 0, "Input depth not multiple of the number of groups!");
271 ARM_COMPUTE_ERROR_ON_MSG((output_shape.z() % _num_groups) != 0, "Output depth not multiple of the number of groups!");
272 ARM_COMPUTE_ERROR_ON_MSG((weights_shape[3] % _num_groups) != 0, "Number of kernels not multiple of the number of groups!");
273 ARM_COMPUTE_ERROR_ON_MSG((biases_shape.x() % _num_groups) != 0, "Biases not multiple of the number of groups!");
274
275 // Create a grouped convolution function
276 auto grouped_conv = arm_compute::support::cpp14::make_unique<GroupedConvolutionFunction>();
277
278 // Create sub-tensors vectors
279 _is = arm_compute::support::cpp14::make_unique<SubTensor[]>(_num_groups);
280 _os = arm_compute::support::cpp14::make_unique<SubTensor[]>(_num_groups);
281 _ws = arm_compute::support::cpp14::make_unique<SubTensor[]>(_num_groups);
282 _bs = arm_compute::support::cpp14::make_unique<SubTensor[]>(_num_groups);
283
284 // Calculate sub-tensor splits
285 const int input_split = input_shape.z() / _num_groups;
286 const int output_split = output_shape.z() / _num_groups;
287 const int weights_split = weights_shape[3] / _num_groups;
288 const int biases_split = biases_shape.x() / _num_groups;
289
290 // Calculate sub-tensor shapes
291 input_shape.set(2, input_split);
292 output_shape.set(2, output_split);
293 weights_shape.set(3, weights_split);
294 biases_shape.set(0, biases_split);
295
296 // Configure sub-tensors
297 for(int i = 0; i < static_cast<int>(_num_groups); ++i)
298 {
299 // Create convolution function
300 std::unique_ptr<arm_compute::IFunction> func;
301
302 // Calculate sub-tensors starting coordinates
303 Coordinates input_coord(0, 0, input_split * i);
304 Coordinates output_coord(0, 0, output_split * i);
305 Coordinates weights_coord(0, 0, 0, weights_split * i);
306 Coordinates biases_coord(biases_split * i);
307
308 // Create sub-tensors for input, output, weights and bias
309 auto hint_to_use = (_hint == Hint::OPENCL) ? Hint::OPENCL : Hint::NEON;
310 _is[i] = SubTensor(_input, input_shape, input_coord, hint_to_use);
311 _os[i] = SubTensor(_output, output_shape, output_coord, hint_to_use);
312 _ws[i] = SubTensor(_weights.tensor(), weights_shape, weights_coord, hint_to_use);
313 _bs[i] = SubTensor(_biases.tensor(), biases_shape, biases_coord, hint_to_use);
314
315 // Instantiate convolution function
316 if(_hint == Hint::OPENCL)
317 {
318 func = instantiate<Hint::OPENCL>(_is[i].tensor(), _ws[i].tensor(), _bs[i].tensor(), _os[i].tensor(), _conv_info, _weights_info, conv_method_hint);
319 }
320 else
321 {
322 func = instantiate<Hint::NEON>(_is[i].tensor(), _ws[i].tensor(), _bs[i].tensor(), _os[i].tensor(), _conv_info, _weights_info, conv_method_hint);
323 }
324
325 // Add convolution function to the list of convolutions for the grouped convolution
326 grouped_conv->add_convolution_function(std::move(func));
327 }
328
329 return std::move(grouped_conv);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100330}