blob: 1209d0376ec09076fd63cccece7be2ee51990255 [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 {
43 TensorShape shape = in->info()->tensor_shape();
44 shape.set(Window::DimX, _conv_width);
45 shape.set(Window::DimY, _conv_height);
Anthony Barbierf45d5a92018-01-24 16:23:15 +000046 TensorInfo info = TensorInfo(TensorShape(shape), in->info()->num_channels(), in->info()->data_type(), in->info()->fixed_point_position());
47 info.set_quantization_info(_quant_info);
48 _weights.set_info(std::move(info));
Anthony Barbier8140e1e2017-12-14 23:48:46 +000049 }
50 if(_biases.has_accessor() && _biases.tensor() == nullptr)
51 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +000052 DataType dt = in->info()->data_type();
53 _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 +000054 }
55
56 bool weights_is_loaded = _weights.tensor() != nullptr;
57 bool biases_is_loaded = _biases.has_accessor() ? _biases.tensor() != nullptr : true;
58
59 _weights.set_target(_target_hint);
60 if(_biases.has_accessor())
61 {
62 _biases.set_target(_target_hint);
63 }
64
65 // Create node context
66 NodeContext node_ctx(OperationType::DepthwiseConvolutionLayer);
67 node_ctx.set_target(_target_hint);
68 node_ctx.add_input(in);
69 node_ctx.add_input(_weights.tensor());
70 if(_biases.has_accessor())
71 {
72 node_ctx.add_input(_biases.tensor());
73 }
74 node_ctx.add_output(out);
75 node_ctx.add_parameter<PadStrideInfo>("ConvolutionInfo", _conv_info);
76 node_ctx.add_parameter<bool>("Optimized3x3", _opt3x3);
77
78 // Configure operation
79 auto func = OperationRegistry::get().find_operation(OperationType::DepthwiseConvolutionLayer, _target_hint)->configure(node_ctx);
80
81 // Fill tensors
82 if(!weights_is_loaded)
83 {
84 _weights.allocate_and_fill_if_needed();
85 }
86 if(!biases_is_loaded)
87 {
88 _biases.allocate_and_fill_if_needed();
89 }
90
91 // Get function
92 return func;
93}