blob: e5101cc33c10f302c3fab01b549cce533e6508ea [file] [log] [blame]
Anthony Barbier8140e1e2017-12-14 23:48:46 +00001/*
Anthony Barbierf45d5a92018-01-24 16:23:15 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier8140e1e2017-12-14 23:48:46 +00003 *
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/DepthwiseConvolutionLayer.h"
25
26#include "arm_compute/graph/Error.h"
27#include "arm_compute/graph/NodeContext.h"
28#include "arm_compute/graph/OperationRegistry.h"
29#include "support/ToolchainSupport.h"
30
31using namespace arm_compute::graph;
32
33std::unique_ptr<arm_compute::IFunction> DepthwiseConvolutionLayer::instantiate_node(GraphContext &ctx, ITensorObject *input, ITensorObject *output)
34{
35 ARM_COMPUTE_ERROR_ON_UNALLOCATED_TENSOR_OBJECT(input, output);
36
37 arm_compute::ITensor *in = input->tensor();
38 arm_compute::ITensor *out = output->tensor();
39 _target_hint = ctx.hints().target_hint();
40
41 if(_weights.tensor() == nullptr)
42 {
Anthony Barbier06ea0482018-02-22 15:45:35 +000043 TensorShape weights_shape(_conv_width, _conv_height, input->tensor()->info()->tensor_shape().z());
44 TensorInfo info = TensorInfo(TensorShape(weights_shape), in->info()->num_channels(), in->info()->data_type(), in->info()->fixed_point_position());
Anthony Barbierf45d5a92018-01-24 16:23:15 +000045 info.set_quantization_info(_quant_info);
46 _weights.set_info(std::move(info));
Anthony Barbier8140e1e2017-12-14 23:48:46 +000047 }
48 if(_biases.has_accessor() && _biases.tensor() == nullptr)
49 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +000050 DataType dt = in->info()->data_type();
51 _biases.set_info(TensorInfo(TensorShape(in->info()->dimension(2)), in->info()->num_channels(), is_data_type_quantized_asymmetric(dt) ? DataType::S32 : dt, in->info()->fixed_point_position()));
Anthony Barbier8140e1e2017-12-14 23:48:46 +000052 }
53
54 bool weights_is_loaded = _weights.tensor() != nullptr;
55 bool biases_is_loaded = _biases.has_accessor() ? _biases.tensor() != nullptr : true;
56
57 _weights.set_target(_target_hint);
58 if(_biases.has_accessor())
59 {
60 _biases.set_target(_target_hint);
61 }
62
63 // Create node context
64 NodeContext node_ctx(OperationType::DepthwiseConvolutionLayer);
65 node_ctx.set_target(_target_hint);
66 node_ctx.add_input(in);
67 node_ctx.add_input(_weights.tensor());
68 if(_biases.has_accessor())
69 {
70 node_ctx.add_input(_biases.tensor());
71 }
72 node_ctx.add_output(out);
73 node_ctx.add_parameter<PadStrideInfo>("ConvolutionInfo", _conv_info);
74 node_ctx.add_parameter<bool>("Optimized3x3", _opt3x3);
75
76 // Configure operation
77 auto func = OperationRegistry::get().find_operation(OperationType::DepthwiseConvolutionLayer, _target_hint)->configure(node_ctx);
78
79 // Fill tensors
80 if(!weights_is_loaded)
81 {
82 _weights.allocate_and_fill_if_needed();
83 }
84 if(!biases_is_loaded)
85 {
86 _biases.allocate_and_fill_if_needed();
87 }
88
89 // Get function
90 return func;
91}