blob: b4598538c1c725eec6743bd42ddff234e3738b2e [file] [log] [blame]
Anthony Barbier8140e1e2017-12-14 23:48:46 +00001/*
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/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);
46 _weights.set_info(TensorInfo(TensorShape(shape), in->info()->num_channels(), in->info()->data_type(), in->info()->fixed_point_position()));
47 }
48 if(_biases.has_accessor() && _biases.tensor() == nullptr)
49 {
50 _biases.set_info(TensorInfo(TensorShape(in->info()->dimension(2)), in->info()->num_channels(), in->info()->data_type(), in->info()->fixed_point_position()));
51 }
52
53 bool weights_is_loaded = _weights.tensor() != nullptr;
54 bool biases_is_loaded = _biases.has_accessor() ? _biases.tensor() != nullptr : true;
55
56 _weights.set_target(_target_hint);
57 if(_biases.has_accessor())
58 {
59 _biases.set_target(_target_hint);
60 }
61
62 // Create node context
63 NodeContext node_ctx(OperationType::DepthwiseConvolutionLayer);
64 node_ctx.set_target(_target_hint);
65 node_ctx.add_input(in);
66 node_ctx.add_input(_weights.tensor());
67 if(_biases.has_accessor())
68 {
69 node_ctx.add_input(_biases.tensor());
70 }
71 node_ctx.add_output(out);
72 node_ctx.add_parameter<PadStrideInfo>("ConvolutionInfo", _conv_info);
73 node_ctx.add_parameter<bool>("Optimized3x3", _opt3x3);
74
75 // Configure operation
76 auto func = OperationRegistry::get().find_operation(OperationType::DepthwiseConvolutionLayer, _target_hint)->configure(node_ctx);
77
78 // Fill tensors
79 if(!weights_is_loaded)
80 {
81 _weights.allocate_and_fill_if_needed();
82 }
83 if(!biases_is_loaded)
84 {
85 _biases.allocate_and_fill_if_needed();
86 }
87
88 // Get function
89 return func;
90}