blob: 53d06ea75feed9d4d08585d2a8005baf0c033ef5 [file] [log] [blame]
Anthony Barbier2a07e182017-08-04 18:20:27 +01001/*
Giorgio Arenaa66eaa22017-12-21 19:50:06 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier2a07e182017-08-04 18:20:27 +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/graph/nodes/ConvolutionLayer.h"
25
Michalis Spyroued194b12017-10-31 15:04:34 +000026#include "arm_compute/graph/Error.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010027#include "arm_compute/runtime/CL/functions/CLConvolutionLayer.h"
Georgios Pinitas6f669f02017-09-26 12:32:57 +010028#include "arm_compute/runtime/CL/functions/CLDirectConvolutionLayer.h"
29#include "arm_compute/runtime/IFunction.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010030#include "arm_compute/runtime/NEON/functions/NEConvolutionLayer.h"
Georgios Pinitas6f669f02017-09-26 12:32:57 +010031#include "arm_compute/runtime/NEON/functions/NEDirectConvolutionLayer.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010032#include "support/ToolchainSupport.h"
Georgios Pinitas6f669f02017-09-26 12:32:57 +010033#include "utils/GraphTypePrinter.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010034#include "utils/TypePrinter.h"
35
Georgios Pinitas6f669f02017-09-26 12:32:57 +010036#include <tuple>
37#include <vector>
38
Anthony Barbier2a07e182017-08-04 18:20:27 +010039using namespace arm_compute::graph;
40
41namespace
42{
Georgios Pinitas6f669f02017-09-26 12:32:57 +010043/** Calculates the output shaped of the convolution layer
44 *
45 * @param[in] input_shape Input tensor shape
46 * @param[in] weights_shape Weights shape
47 * @param[in] conv_info Convolution information (padding, stride, etc.)
48 *
49 * @return The expected output tensor shape
50 */
51TensorShape calculate_convolution_layer_output_shape(const TensorShape &input_shape, const TensorShape &weights_shape, const PadStrideInfo &conv_info)
Anthony Barbier2a07e182017-08-04 18:20:27 +010052{
Georgios Pinitas6f669f02017-09-26 12:32:57 +010053 unsigned int output_width = 0;
54 unsigned int output_height = 0;
Anthony Barbier2a07e182017-08-04 18:20:27 +010055
Georgios Pinitas6f669f02017-09-26 12:32:57 +010056 // Get output width and height
57 std::tie(output_width, output_height) = arm_compute::scaled_dimensions(input_shape.x(), input_shape.y(), weights_shape.x(), weights_shape.y(), conv_info);
58
59 // Create output shape
60 TensorShape output_shape = input_shape;
61 output_shape.set(0, output_width);
62 output_shape.set(1, output_height);
63 output_shape.set(2, weights_shape[3]);
64
65 return output_shape;
66}
67
68// Instantiate GEMM based convolution layer
Georgios Pinitasff421f22017-10-04 16:53:58 +010069template <typename ConvolutionType, typename TensorType, TargetHint target_hint>
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010070std::unique_ptr<arm_compute::IFunction> instantiate_function(arm_compute::ITensor *input, arm_compute::ITensor *weights, arm_compute::ITensor *biases, arm_compute::ITensor *output,
71 const PadStrideInfo &conv_info, const WeightsInfo &weights_info)
Georgios Pinitas6f669f02017-09-26 12:32:57 +010072{
Anthony Barbier2a07e182017-08-04 18:20:27 +010073 auto conv = arm_compute::support::cpp14::make_unique<ConvolutionType>();
74 conv->configure(
75 dynamic_cast<TensorType *>(input),
Georgios Pinitas6f669f02017-09-26 12:32:57 +010076 dynamic_cast<TensorType *>(weights),
77 dynamic_cast<TensorType *>(biases),
Anthony Barbier2a07e182017-08-04 18:20:27 +010078 dynamic_cast<TensorType *>(output),
79 conv_info, weights_info);
Georgios Pinitas6f669f02017-09-26 12:32:57 +010080 return std::move(conv);
81}
Anthony Barbier2a07e182017-08-04 18:20:27 +010082
Georgios Pinitas6f669f02017-09-26 12:32:57 +010083// Instantiate direct convolution layer
Georgios Pinitasff421f22017-10-04 16:53:58 +010084template <typename ConvolutionType, typename TensorType, TargetHint target_hint>
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010085std::unique_ptr<arm_compute::IFunction> instantiate_direct_function(arm_compute::ITensor *input, arm_compute::ITensor *weights, arm_compute::ITensor *biases, arm_compute::ITensor *output,
86 const PadStrideInfo &conv_info)
Georgios Pinitas6f669f02017-09-26 12:32:57 +010087{
88 auto conv = arm_compute::support::cpp14::make_unique<ConvolutionType>();
89 conv->configure(
90 dynamic_cast<TensorType *>(input),
91 dynamic_cast<TensorType *>(weights),
92 dynamic_cast<TensorType *>(biases),
93 dynamic_cast<TensorType *>(output),
94 conv_info);
Anthony Barbier2a07e182017-08-04 18:20:27 +010095 return std::move(conv);
96}
97
Georgios Pinitasff421f22017-10-04 16:53:58 +010098template <TargetHint target_hint>
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010099std::unique_ptr<arm_compute::IFunction> instantiate(arm_compute::ITensor *input, arm_compute::ITensor *weights, arm_compute::ITensor *biases, arm_compute::ITensor *output,
100 const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100101 ConvolutionMethodHint conv_method);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100102
103template <>
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100104std::unique_ptr<arm_compute::IFunction> instantiate<TargetHint::OPENCL>(arm_compute::ITensor *input, arm_compute::ITensor *weights, arm_compute::ITensor *biases, arm_compute::ITensor *output,
105 const PadStrideInfo &conv_info,
Georgios Pinitasff421f22017-10-04 16:53:58 +0100106 const WeightsInfo &weights_info,
107 ConvolutionMethodHint conv_method)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100108{
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100109 if(conv_method == ConvolutionMethodHint::GEMM)
110 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100111 return instantiate_function<arm_compute::CLConvolutionLayer, arm_compute::ICLTensor, TargetHint::OPENCL>(input, weights, biases, output, conv_info, weights_info);
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100112 }
113 else
114 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100115 return instantiate_direct_function<arm_compute::CLDirectConvolutionLayer, arm_compute::ICLTensor, TargetHint::OPENCL>(input, weights, biases, output, conv_info);
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100116 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100117}
118
119template <>
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100120std::unique_ptr<arm_compute::IFunction> instantiate<TargetHint::NEON>(arm_compute::ITensor *input, arm_compute::ITensor *weights, arm_compute::ITensor *biases, arm_compute::ITensor *output,
121 const PadStrideInfo &conv_info,
Georgios Pinitasff421f22017-10-04 16:53:58 +0100122 const WeightsInfo &weights_info,
123 ConvolutionMethodHint conv_method)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100124{
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100125 if(conv_method == ConvolutionMethodHint::GEMM)
126 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100127 return instantiate_function<arm_compute::NEConvolutionLayer, arm_compute::ITensor, TargetHint::NEON>(input, weights, biases, output, conv_info, weights_info);
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100128 }
129 else
130 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100131 return instantiate_direct_function<arm_compute::NEDirectConvolutionLayer, arm_compute::ITensor, TargetHint::NEON>(input, weights, biases, output, conv_info);
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100132 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100133}
134} // namespace
135
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100136/** Grouped Convolution function */
137class GroupedConvolutionFunction final : public arm_compute::IFunction
138{
139public:
140 /** Default Constructor */
141 GroupedConvolutionFunction()
142 : _convolutions()
143 {
144 }
145 /** Default Destructor */
146 ~GroupedConvolutionFunction() final = default;
147 /** Prevent instances from being copy constructed */
148 GroupedConvolutionFunction(const GroupedConvolutionFunction &) = delete;
149 /** Prevent instances from being copy assigned */
150 GroupedConvolutionFunction &operator=(const GroupedConvolutionFunction &) = delete;
151 /** Allow instances to be move constructed */
152 GroupedConvolutionFunction(GroupedConvolutionFunction &&) noexcept = default;
153 /** Allow instances to be move assigned */
154 GroupedConvolutionFunction &operator=(GroupedConvolutionFunction &&) noexcept = default;
155 /** Adds a convolution
156 *
157 * @param convolution Convolution function to add
158 */
159 void add_convolution_function(std::unique_ptr<IFunction> convolution)
160 {
161 _convolutions.emplace_back(std::move(convolution));
162 }
163
164 // Inherited methods overriden:
165 void run() override
166 {
167 for(auto &c : _convolutions)
168 {
169 c->run();
170 }
171 }
172
173private:
174 std::vector<std::unique_ptr<IFunction>> _convolutions;
175};
176
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100177std::unique_ptr<arm_compute::IFunction> ConvolutionLayer::instantiate_node(GraphContext &ctx, ITensorObject *input, ITensorObject *output)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100178{
Michalis Spyroued194b12017-10-31 15:04:34 +0000179 ARM_COMPUTE_ERROR_ON_UNALLOCATED_TENSOR_OBJECT(input, output);
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100180
181 arm_compute::ITensor *in = input->tensor();
182 arm_compute::ITensor *out = output->tensor();
183
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100184 // Set weights and biases info
Anthony Barbier2a07e182017-08-04 18:20:27 +0100185 if(_weights.tensor() == nullptr)
186 {
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000187 TensorInfo info = TensorInfo(TensorShape(_conv_width, _conv_height, in->info()->dimension(2) / _num_groups, _ofm),
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100188 in->info()->num_channels(),
189 in->info()->data_type(),
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000190 in->info()->fixed_point_position());
191 info.set_quantization_info(_weights_quant_info);
192 _weights.set_info(std::move(info));
Anthony Barbier2a07e182017-08-04 18:20:27 +0100193 }
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000194 if(_biases.has_accessor() && _biases.tensor() == nullptr)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100195 {
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000196 DataType dt = in->info()->data_type();
197 _biases.set_info(TensorInfo(TensorShape(_ofm), in->info()->num_channels(), is_data_type_quantized_asymmetric(dt) ? DataType::S32 : dt, in->info()->fixed_point_position()));
Anthony Barbier2a07e182017-08-04 18:20:27 +0100198 }
199
200 std::unique_ptr<arm_compute::IFunction> func;
Georgios Pinitasff421f22017-10-04 16:53:58 +0100201 _target_hint = ctx.hints().target_hint();
Georgios Pinitasff421f22017-10-04 16:53:58 +0100202 const ConvolutionMethodHint conv_method_hint = ctx.hints().convolution_method_hint();
Anthony Barbier2a07e182017-08-04 18:20:27 +0100203
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100204 // Check if the weights and biases are loaded
205 bool weights_are_loaded = _weights.tensor() != nullptr;
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000206 bool biases_are_loaded = _biases.has_accessor() ? _biases.tensor() != nullptr : true;
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100207
208 // Set bias and weights target
Georgios Pinitasff421f22017-10-04 16:53:58 +0100209 _weights.set_target(_target_hint);
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000210 if(_biases.has_accessor())
211 {
212 _biases.set_target(_target_hint);
213 }
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100214
215 // Calculate output shape
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100216 TensorShape output_shape = calculate_convolution_layer_output_shape(in->info()->tensor_shape(), _weights.info().tensor_shape(), _conv_info);
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100217
218 // Output auto inizialitation if not yet initialized
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000219 arm_compute::auto_init_if_empty(*out->info(), output_shape, 1, in->info()->data_type(), in->info()->fixed_point_position(),
220 (_out_quant_info.empty()) ? in->info()->quantization_info() : _out_quant_info);
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100221
222 // Create appropriate convolution function
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100223 if(_num_groups == 1)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100224 {
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100225 func = instantiate_convolution(in, out, conv_method_hint);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100226 }
227 else
228 {
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100229 func = instantiate_grouped_convolution(in, out, conv_method_hint);
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100230 }
231
232 // Fill weights
233 if(!weights_are_loaded)
234 {
235 _weights.allocate_and_fill_if_needed();
236 }
237 // Fill biases
238 if(!biases_are_loaded)
239 {
240 _biases.allocate_and_fill_if_needed();
Anthony Barbier2a07e182017-08-04 18:20:27 +0100241 }
242
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100243 ARM_COMPUTE_LOG_GRAPH_INFO(" Data Type: " << in->info()->data_type()
244 << " Input Shape: " << in->info()->tensor_shape()
245 << " Weights shape: " << _weights.info().tensor_shape()
246 << " Biases Shape: " << _biases.info().tensor_shape()
247 << " Output Shape: " << out->info()->tensor_shape()
248 << " PadStrideInfo: " << _conv_info
249 << " Groups: " << _num_groups
250 << " WeightsInfo: " << _weights_info
251 << std::endl);
Michalis Spyroue4720822017-10-02 17:44:52 +0100252
Anthony Barbier2a07e182017-08-04 18:20:27 +0100253 return func;
254}
255
Michalis Spyroue4720822017-10-02 17:44:52 +0100256std::unique_ptr<arm_compute::IFunction> ConvolutionLayer::instantiate_convolution(ITensor *input, ITensor *output, ConvolutionMethodHint conv_method_hint)
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100257{
258 std::unique_ptr<arm_compute::IFunction> func;
Georgios Pinitasff421f22017-10-04 16:53:58 +0100259 if(_target_hint == TargetHint::OPENCL)
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100260 {
Georgios Pinitas0c29cd32017-10-18 17:29:27 +0100261 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating CLConvolutionLayer");
Michalis Spyroue4720822017-10-02 17:44:52 +0100262 func = instantiate<TargetHint::OPENCL>(input, _weights.tensor(), _biases.tensor(), output, _conv_info, _weights_info, conv_method_hint);
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100263 }
264 else
265 {
Georgios Pinitas0c29cd32017-10-18 17:29:27 +0100266 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating NEConvolutionLayer");
Michalis Spyroue4720822017-10-02 17:44:52 +0100267 func = instantiate<TargetHint::NEON>(input, _weights.tensor(), _biases.tensor(), output, _conv_info, _weights_info, conv_method_hint);
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100268 }
269 return func;
270}
271
Michalis Spyroue4720822017-10-02 17:44:52 +0100272std::unique_ptr<arm_compute::IFunction> ConvolutionLayer::instantiate_grouped_convolution(ITensor *input, ITensor *output, ConvolutionMethodHint conv_method_hint)
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100273{
274 // Get tensor shapes
Michalis Spyroue4720822017-10-02 17:44:52 +0100275 TensorShape input_shape = input->info()->tensor_shape();
276 TensorShape output_shape = output->info()->tensor_shape();
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100277 TensorShape weights_shape = _weights.info().tensor_shape();
278 TensorShape biases_shape = _biases.info().tensor_shape();
279
280 ARM_COMPUTE_ERROR_ON_MSG((input_shape.z() % _num_groups) != 0, "Input depth not multiple of the number of groups!");
281 ARM_COMPUTE_ERROR_ON_MSG((output_shape.z() % _num_groups) != 0, "Output depth not multiple of the number of groups!");
282 ARM_COMPUTE_ERROR_ON_MSG((weights_shape[3] % _num_groups) != 0, "Number of kernels not multiple of the number of groups!");
283 ARM_COMPUTE_ERROR_ON_MSG((biases_shape.x() % _num_groups) != 0, "Biases not multiple of the number of groups!");
284
285 // Create a grouped convolution function
286 auto grouped_conv = arm_compute::support::cpp14::make_unique<GroupedConvolutionFunction>();
287
288 // Create sub-tensors vectors
289 _is = arm_compute::support::cpp14::make_unique<SubTensor[]>(_num_groups);
290 _os = arm_compute::support::cpp14::make_unique<SubTensor[]>(_num_groups);
291 _ws = arm_compute::support::cpp14::make_unique<SubTensor[]>(_num_groups);
292 _bs = arm_compute::support::cpp14::make_unique<SubTensor[]>(_num_groups);
293
294 // Calculate sub-tensor splits
295 const int input_split = input_shape.z() / _num_groups;
296 const int output_split = output_shape.z() / _num_groups;
297 const int weights_split = weights_shape[3] / _num_groups;
298 const int biases_split = biases_shape.x() / _num_groups;
299
300 // Calculate sub-tensor shapes
301 input_shape.set(2, input_split);
302 output_shape.set(2, output_split);
303 weights_shape.set(3, weights_split);
304 biases_shape.set(0, biases_split);
305
306 // Configure sub-tensors
307 for(int i = 0; i < static_cast<int>(_num_groups); ++i)
308 {
309 // Create convolution function
310 std::unique_ptr<arm_compute::IFunction> func;
311
312 // Calculate sub-tensors starting coordinates
313 Coordinates input_coord(0, 0, input_split * i);
314 Coordinates output_coord(0, 0, output_split * i);
315 Coordinates weights_coord(0, 0, 0, weights_split * i);
316 Coordinates biases_coord(biases_split * i);
317
318 // Create sub-tensors for input, output, weights and bias
Georgios Pinitasff421f22017-10-04 16:53:58 +0100319 auto hint_to_use = (_target_hint == TargetHint::OPENCL) ? TargetHint::OPENCL : TargetHint::NEON;
Michalis Spyroue4720822017-10-02 17:44:52 +0100320 _is[i] = SubTensor(input, input_shape, input_coord, hint_to_use);
321 _os[i] = SubTensor(output, output_shape, output_coord, hint_to_use);
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100322 _ws[i] = SubTensor(_weights.tensor(), weights_shape, weights_coord, hint_to_use);
323 _bs[i] = SubTensor(_biases.tensor(), biases_shape, biases_coord, hint_to_use);
324
325 // Instantiate convolution function
Georgios Pinitasff421f22017-10-04 16:53:58 +0100326 if(_target_hint == TargetHint::OPENCL)
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100327 {
Georgios Pinitas0c29cd32017-10-18 17:29:27 +0100328 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating CLConvolutionLayer");
Georgios Pinitasff421f22017-10-04 16:53:58 +0100329 func = instantiate<TargetHint::OPENCL>(_is[i].tensor(), _ws[i].tensor(), _bs[i].tensor(), _os[i].tensor(), _conv_info, _weights_info, conv_method_hint);
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100330 }
331 else
332 {
Georgios Pinitas0c29cd32017-10-18 17:29:27 +0100333 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating NEConvolutionLayer");
Georgios Pinitasff421f22017-10-04 16:53:58 +0100334 func = instantiate<TargetHint::NEON>(_is[i].tensor(), _ws[i].tensor(), _bs[i].tensor(), _os[i].tensor(), _conv_info, _weights_info, conv_method_hint);
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100335 }
336
337 // Add convolution function to the list of convolutions for the grouped convolution
338 grouped_conv->add_convolution_function(std::move(func));
339 }
340
341 return std::move(grouped_conv);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100342}