blob: ae4a8d7e6b444c246053c88ffcf1d8d20b4961cb [file] [log] [blame]
Kaizen8938bd32017-09-28 14:38: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/graph/nodes/ConvolutionLayer.h"
25
Anthony Barbier8140e1e2017-12-14 23:48:46 +000026#include "arm_compute/graph/Error.h"
Kaizen8938bd32017-09-28 14:38:23 +010027#include "arm_compute/runtime/CL/functions/CLConvolutionLayer.h"
Kaizenbf8b01d2017-10-12 14:26:51 +010028#include "arm_compute/runtime/CL/functions/CLDirectConvolutionLayer.h"
29#include "arm_compute/runtime/IFunction.h"
Kaizen8938bd32017-09-28 14:38:23 +010030#include "arm_compute/runtime/NEON/functions/NEConvolutionLayer.h"
Kaizenbf8b01d2017-10-12 14:26:51 +010031#include "arm_compute/runtime/NEON/functions/NEDirectConvolutionLayer.h"
Kaizen8938bd32017-09-28 14:38:23 +010032#include "support/ToolchainSupport.h"
Kaizenbf8b01d2017-10-12 14:26:51 +010033#include "utils/GraphTypePrinter.h"
Kaizen8938bd32017-09-28 14:38:23 +010034#include "utils/TypePrinter.h"
35
Kaizenbf8b01d2017-10-12 14:26:51 +010036#include <tuple>
37#include <vector>
38
Kaizen8938bd32017-09-28 14:38:23 +010039using namespace arm_compute::graph;
40
41namespace
42{
Kaizenbf8b01d2017-10-12 14:26:51 +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)
Kaizen8938bd32017-09-28 14:38:23 +010052{
Kaizenbf8b01d2017-10-12 14:26:51 +010053 unsigned int output_width = 0;
54 unsigned int output_height = 0;
Kaizen8938bd32017-09-28 14:38:23 +010055
Kaizenbf8b01d2017-10-12 14:26:51 +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
69template <typename ConvolutionType, typename TensorType, TargetHint target_hint>
Anthony Barbier8140e1e2017-12-14 23:48:46 +000070std::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)
Kaizenbf8b01d2017-10-12 14:26:51 +010072{
Kaizen8938bd32017-09-28 14:38:23 +010073 auto conv = arm_compute::support::cpp14::make_unique<ConvolutionType>();
74 conv->configure(
75 dynamic_cast<TensorType *>(input),
Kaizenbf8b01d2017-10-12 14:26:51 +010076 dynamic_cast<TensorType *>(weights),
77 dynamic_cast<TensorType *>(biases),
Kaizen8938bd32017-09-28 14:38:23 +010078 dynamic_cast<TensorType *>(output),
79 conv_info, weights_info);
Kaizen8938bd32017-09-28 14:38:23 +010080 return std::move(conv);
81}
82
Kaizenbf8b01d2017-10-12 14:26:51 +010083// Instantiate direct convolution layer
84template <typename ConvolutionType, typename TensorType, TargetHint target_hint>
Anthony Barbier8140e1e2017-12-14 23:48:46 +000085std::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)
Kaizenbf8b01d2017-10-12 14:26:51 +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);
95 return std::move(conv);
96}
97
98template <TargetHint target_hint>
Anthony Barbier8140e1e2017-12-14 23:48:46 +000099std::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,
Kaizenbf8b01d2017-10-12 14:26:51 +0100101 ConvolutionMethodHint conv_method);
Kaizen8938bd32017-09-28 14:38:23 +0100102
103template <>
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000104std::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,
Kaizenbf8b01d2017-10-12 14:26:51 +0100106 const WeightsInfo &weights_info,
107 ConvolutionMethodHint conv_method)
Kaizen8938bd32017-09-28 14:38:23 +0100108{
Kaizenbf8b01d2017-10-12 14:26:51 +0100109 if(conv_method == ConvolutionMethodHint::GEMM)
110 {
111 return instantiate_function<arm_compute::CLConvolutionLayer, arm_compute::ICLTensor, TargetHint::OPENCL>(input, weights, biases, output, conv_info, weights_info);
112 }
113 else
114 {
115 return instantiate_direct_function<arm_compute::CLDirectConvolutionLayer, arm_compute::ICLTensor, TargetHint::OPENCL>(input, weights, biases, output, conv_info);
116 }
Kaizen8938bd32017-09-28 14:38:23 +0100117}
118
119template <>
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000120std::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,
Kaizenbf8b01d2017-10-12 14:26:51 +0100122 const WeightsInfo &weights_info,
123 ConvolutionMethodHint conv_method)
Kaizen8938bd32017-09-28 14:38:23 +0100124{
Kaizenbf8b01d2017-10-12 14:26:51 +0100125 if(conv_method == ConvolutionMethodHint::GEMM)
126 {
127 return instantiate_function<arm_compute::NEConvolutionLayer, arm_compute::ITensor, TargetHint::NEON>(input, weights, biases, output, conv_info, weights_info);
128 }
129 else
130 {
131 return instantiate_direct_function<arm_compute::NEDirectConvolutionLayer, arm_compute::ITensor, TargetHint::NEON>(input, weights, biases, output, conv_info);
132 }
Kaizen8938bd32017-09-28 14:38:23 +0100133}
134} // namespace
135
Kaizenbf8b01d2017-10-12 14:26:51 +0100136/** Grouped Convolution function */
137class GroupedConvolutionFunction final : public arm_compute::IFunction
Kaizen8938bd32017-09-28 14:38:23 +0100138{
Kaizenbf8b01d2017-10-12 14:26:51 +0100139public:
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
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000177std::unique_ptr<arm_compute::IFunction> ConvolutionLayer::instantiate_node(GraphContext &ctx, ITensorObject *input, ITensorObject *output)
Kaizenbf8b01d2017-10-12 14:26:51 +0100178{
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000179 ARM_COMPUTE_ERROR_ON_UNALLOCATED_TENSOR_OBJECT(input, output);
180
181 arm_compute::ITensor *in = input->tensor();
182 arm_compute::ITensor *out = output->tensor();
183
Kaizenbf8b01d2017-10-12 14:26:51 +0100184 // Set weights and biases info
Kaizen8938bd32017-09-28 14:38:23 +0100185 if(_weights.tensor() == nullptr)
186 {
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000187 _weights.set_info(TensorInfo(TensorShape(_conv_width, _conv_height, in->info()->dimension(2) / _num_groups, _ofm),
188 in->info()->num_channels(),
189 in->info()->data_type(),
190 in->info()->fixed_point_position()));
Kaizen8938bd32017-09-28 14:38:23 +0100191 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000192 if(_biases.has_accessor() && _biases.tensor() == nullptr)
Kaizen8938bd32017-09-28 14:38:23 +0100193 {
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000194 _biases.set_info(TensorInfo(TensorShape(_ofm), in->info()->num_channels(), in->info()->data_type(), in->info()->fixed_point_position()));
Kaizen8938bd32017-09-28 14:38:23 +0100195 }
196
197 std::unique_ptr<arm_compute::IFunction> func;
Kaizenbf8b01d2017-10-12 14:26:51 +0100198 _target_hint = ctx.hints().target_hint();
199 const ConvolutionMethodHint conv_method_hint = ctx.hints().convolution_method_hint();
Kaizen8938bd32017-09-28 14:38:23 +0100200
Kaizenbf8b01d2017-10-12 14:26:51 +0100201 // Check if the weights and biases are loaded
202 bool weights_are_loaded = _weights.tensor() != nullptr;
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000203 bool biases_are_loaded = _biases.has_accessor() ? _biases.tensor() != nullptr : true;
Kaizenbf8b01d2017-10-12 14:26:51 +0100204
205 // Set bias and weights target
206 _weights.set_target(_target_hint);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000207 if(_biases.has_accessor())
208 {
209 _biases.set_target(_target_hint);
210 }
Kaizenbf8b01d2017-10-12 14:26:51 +0100211
212 // Calculate output shape
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000213 TensorShape output_shape = calculate_convolution_layer_output_shape(in->info()->tensor_shape(), _weights.info().tensor_shape(), _conv_info);
Kaizenbf8b01d2017-10-12 14:26:51 +0100214
215 // Output auto inizialitation if not yet initialized
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000216 arm_compute::auto_init_if_empty(*out->info(), output_shape, 1, in->info()->data_type(), in->info()->fixed_point_position());
Kaizenbf8b01d2017-10-12 14:26:51 +0100217
218 // Create appropriate convolution function
219 if(_num_groups == 1)
Kaizen8938bd32017-09-28 14:38:23 +0100220 {
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000221 func = instantiate_convolution(in, out, conv_method_hint);
Kaizen8938bd32017-09-28 14:38:23 +0100222 }
223 else
224 {
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000225 func = instantiate_grouped_convolution(in, out, conv_method_hint);
Kaizen8938bd32017-09-28 14:38:23 +0100226 }
227
Kaizenbf8b01d2017-10-12 14:26:51 +0100228 // Fill weights
229 if(!weights_are_loaded)
230 {
231 _weights.allocate_and_fill_if_needed();
232 }
233 // Fill biases
234 if(!biases_are_loaded)
235 {
236 _biases.allocate_and_fill_if_needed();
237 }
238
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000239 ARM_COMPUTE_LOG_GRAPH_INFO(" Data Type: " << in->info()->data_type()
240 << " Input Shape: " << in->info()->tensor_shape()
241 << " Weights shape: " << _weights.info().tensor_shape()
242 << " Biases Shape: " << _biases.info().tensor_shape()
243 << " Output Shape: " << out->info()->tensor_shape()
244 << " PadStrideInfo: " << _conv_info
245 << " Groups: " << _num_groups
246 << " WeightsInfo: " << _weights_info
247 << std::endl);
Kaizenbf8b01d2017-10-12 14:26:51 +0100248
Kaizen8938bd32017-09-28 14:38:23 +0100249 return func;
250}
251
Kaizenbf8b01d2017-10-12 14:26:51 +0100252std::unique_ptr<arm_compute::IFunction> ConvolutionLayer::instantiate_convolution(ITensor *input, ITensor *output, ConvolutionMethodHint conv_method_hint)
Kaizen8938bd32017-09-28 14:38:23 +0100253{
Kaizenbf8b01d2017-10-12 14:26:51 +0100254 std::unique_ptr<arm_compute::IFunction> func;
255 if(_target_hint == TargetHint::OPENCL)
Kaizen8938bd32017-09-28 14:38:23 +0100256 {
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000257 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating CLConvolutionLayer");
Kaizenbf8b01d2017-10-12 14:26:51 +0100258 func = instantiate<TargetHint::OPENCL>(input, _weights.tensor(), _biases.tensor(), output, _conv_info, _weights_info, conv_method_hint);
Kaizen8938bd32017-09-28 14:38:23 +0100259 }
260 else
261 {
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000262 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating NEConvolutionLayer");
Kaizenbf8b01d2017-10-12 14:26:51 +0100263 func = instantiate<TargetHint::NEON>(input, _weights.tensor(), _biases.tensor(), output, _conv_info, _weights_info, conv_method_hint);
Kaizen8938bd32017-09-28 14:38:23 +0100264 }
Kaizenbf8b01d2017-10-12 14:26:51 +0100265 return func;
266}
267
268std::unique_ptr<arm_compute::IFunction> ConvolutionLayer::instantiate_grouped_convolution(ITensor *input, ITensor *output, ConvolutionMethodHint conv_method_hint)
269{
270 // Get tensor shapes
271 TensorShape input_shape = input->info()->tensor_shape();
272 TensorShape output_shape = output->info()->tensor_shape();
273 TensorShape weights_shape = _weights.info().tensor_shape();
274 TensorShape biases_shape = _biases.info().tensor_shape();
275
276 ARM_COMPUTE_ERROR_ON_MSG((input_shape.z() % _num_groups) != 0, "Input depth not multiple of the number of groups!");
277 ARM_COMPUTE_ERROR_ON_MSG((output_shape.z() % _num_groups) != 0, "Output depth not multiple of the number of groups!");
278 ARM_COMPUTE_ERROR_ON_MSG((weights_shape[3] % _num_groups) != 0, "Number of kernels not multiple of the number of groups!");
279 ARM_COMPUTE_ERROR_ON_MSG((biases_shape.x() % _num_groups) != 0, "Biases not multiple of the number of groups!");
280
281 // Create a grouped convolution function
282 auto grouped_conv = arm_compute::support::cpp14::make_unique<GroupedConvolutionFunction>();
283
284 // Create sub-tensors vectors
285 _is = arm_compute::support::cpp14::make_unique<SubTensor[]>(_num_groups);
286 _os = arm_compute::support::cpp14::make_unique<SubTensor[]>(_num_groups);
287 _ws = arm_compute::support::cpp14::make_unique<SubTensor[]>(_num_groups);
288 _bs = arm_compute::support::cpp14::make_unique<SubTensor[]>(_num_groups);
289
290 // Calculate sub-tensor splits
291 const int input_split = input_shape.z() / _num_groups;
292 const int output_split = output_shape.z() / _num_groups;
293 const int weights_split = weights_shape[3] / _num_groups;
294 const int biases_split = biases_shape.x() / _num_groups;
295
296 // Calculate sub-tensor shapes
297 input_shape.set(2, input_split);
298 output_shape.set(2, output_split);
299 weights_shape.set(3, weights_split);
300 biases_shape.set(0, biases_split);
301
302 // Configure sub-tensors
303 for(int i = 0; i < static_cast<int>(_num_groups); ++i)
304 {
305 // Create convolution function
306 std::unique_ptr<arm_compute::IFunction> func;
307
308 // Calculate sub-tensors starting coordinates
309 Coordinates input_coord(0, 0, input_split * i);
310 Coordinates output_coord(0, 0, output_split * i);
311 Coordinates weights_coord(0, 0, 0, weights_split * i);
312 Coordinates biases_coord(biases_split * i);
313
314 // Create sub-tensors for input, output, weights and bias
315 auto hint_to_use = (_target_hint == TargetHint::OPENCL) ? TargetHint::OPENCL : TargetHint::NEON;
316 _is[i] = SubTensor(input, input_shape, input_coord, hint_to_use);
317 _os[i] = SubTensor(output, output_shape, output_coord, hint_to_use);
318 _ws[i] = SubTensor(_weights.tensor(), weights_shape, weights_coord, hint_to_use);
319 _bs[i] = SubTensor(_biases.tensor(), biases_shape, biases_coord, hint_to_use);
320
321 // Instantiate convolution function
322 if(_target_hint == TargetHint::OPENCL)
323 {
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000324 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating CLConvolutionLayer");
Kaizenbf8b01d2017-10-12 14:26:51 +0100325 func = instantiate<TargetHint::OPENCL>(_is[i].tensor(), _ws[i].tensor(), _bs[i].tensor(), _os[i].tensor(), _conv_info, _weights_info, conv_method_hint);
326 }
327 else
328 {
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000329 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating NEConvolutionLayer");
Kaizenbf8b01d2017-10-12 14:26:51 +0100330 func = instantiate<TargetHint::NEON>(_is[i].tensor(), _ws[i].tensor(), _bs[i].tensor(), _os[i].tensor(), _conv_info, _weights_info, conv_method_hint);
331 }
332
333 // Add convolution function to the list of convolutions for the grouped convolution
334 grouped_conv->add_convolution_function(std::move(func));
335 }
336
337 return std::move(grouped_conv);
Kaizen8938bd32017-09-28 14:38:23 +0100338}