blob: e5101cc33c10f302c3fab01b549cce533e6508ea [file] [log] [blame]
Michalis Spyrou7bfe4c52017-11-24 09:54:20 +00001/*
Giorgio Arenaa66eaa22017-12-21 19:50:06 +00002 * Copyright (c) 2017-2018 ARM Limited.
Michalis Spyrou7bfe4c52017-11-24 09:54:20 +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 {
Georgios Pinitas240beb72018-02-09 11:28:58 +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());
Giorgio Arenaa66eaa22017-12-21 19:50:06 +000045 info.set_quantization_info(_quant_info);
46 _weights.set_info(std::move(info));
Michalis Spyrou7bfe4c52017-11-24 09:54:20 +000047 }
48 if(_biases.has_accessor() && _biases.tensor() == nullptr)
49 {
Giorgio Arenaa66eaa22017-12-21 19:50:06 +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()));
Michalis Spyrou7bfe4c52017-11-24 09:54:20 +000052 }
53
54 bool weights_is_loaded = _weights.tensor() != nullptr;
Georgios Pinitas236bfe72017-11-23 15:59:55 +000055 bool biases_is_loaded = _biases.has_accessor() ? _biases.tensor() != nullptr : true;
Michalis Spyrou7bfe4c52017-11-24 09:54:20 +000056
57 _weights.set_target(_target_hint);
Georgios Pinitas236bfe72017-11-23 15:59:55 +000058 if(_biases.has_accessor())
59 {
60 _biases.set_target(_target_hint);
61 }
Michalis Spyrou7bfe4c52017-11-24 09:54:20 +000062
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
Georgios Pinitas284c2042017-12-11 17:49:18 +000076 // Configure operation
77 auto func = OperationRegistry::get().find_operation(OperationType::DepthwiseConvolutionLayer, _target_hint)->configure(node_ctx);
78
Michalis Spyrou7bfe4c52017-11-24 09:54:20 +000079 // 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
Georgios Pinitas284c2042017-12-11 17:49:18 +000090 return func;
Michalis Spyrou7bfe4c52017-11-24 09:54:20 +000091}