blob: 3f3c70f3bb50cc023892f7060e2f3055ad3f47f2 [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2019 Arm Limited.
Georgios Pinitasd8734b52017-12-22 15:27:52 +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 */
Georgios Pinitase2220552018-07-20 13:23:44 +010024#include "arm_compute/graph/nodes/ConcatenateLayerNode.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000025
26#include "arm_compute/core/Utils.h"
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010027#include "arm_compute/graph/Graph.h"
28#include "arm_compute/graph/INodeVisitor.h"
Georgios Pinitase2220552018-07-20 13:23:44 +010029#include "arm_compute/graph/Utils.h"
30
31#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000032
33namespace arm_compute
34{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010035namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000036{
Isabella Gottardi0ae5de92019-03-14 10:32:11 +000037ConcatenateLayerNode::ConcatenateLayerNode(unsigned int total_nodes, descriptors::ConcatLayerDescriptor concat_descriptor)
38 : _total_nodes(total_nodes), _concat_descriptor(std::move(concat_descriptor)), _is_enabled(true)
Georgios Pinitasd8734b52017-12-22 15:27:52 +000039{
Georgios Pinitascac13b12018-04-27 19:07:19 +010040 _input_edges.resize(_total_nodes, EmptyEdgeID);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000041 _outputs.resize(1, NullTensorID);
42}
43
Georgios Pinitase2220552018-07-20 13:23:44 +010044void ConcatenateLayerNode::set_enabled(bool is_enabled)
Georgios Pinitasd8734b52017-12-22 15:27:52 +000045{
46 _is_enabled = is_enabled;
47}
48
Georgios Pinitase2220552018-07-20 13:23:44 +010049bool ConcatenateLayerNode::is_enabled() const
Georgios Pinitasd8734b52017-12-22 15:27:52 +000050{
51 return _is_enabled;
52}
53
Georgios Pinitase2220552018-07-20 13:23:44 +010054DataLayoutDimension ConcatenateLayerNode::concatenation_axis() const
55{
Isabella Gottardi0ae5de92019-03-14 10:32:11 +000056 return _concat_descriptor.axis;
57}
58
59QuantizationInfo ConcatenateLayerNode::output_quantization_info() const
60{
61 return _concat_descriptor.output_qinfo;
Georgios Pinitase2220552018-07-20 13:23:44 +010062}
63
64TensorDescriptor ConcatenateLayerNode::compute_output_descriptor(const std::vector<TensorDescriptor> &input_descriptors,
65 DataLayoutDimension axis)
Georgios Pinitasd8734b52017-12-22 15:27:52 +000066{
Georgios Pinitascac13b12018-04-27 19:07:19 +010067 ARM_COMPUTE_ERROR_ON(input_descriptors.size() == 0);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000068
Georgios Pinitascac13b12018-04-27 19:07:19 +010069 TensorDescriptor output_descriptor = input_descriptors[0];
Georgios Pinitas9e4824c2019-04-12 13:15:58 +010070 const int axis_idx = get_dimension_idx(output_descriptor.layout, axis);
Michalis Spyroua9c44722019-04-05 17:18:36 +010071 ARM_COMPUTE_ERROR_ON_MSG(axis_idx > 2, "Unsupported concatenation axis!");
Georgios Pinitasd8734b52017-12-22 15:27:52 +000072
Georgios Pinitase2220552018-07-20 13:23:44 +010073 // Extract shapes
74 std::vector<const TensorShape *> shapes;
Michalis Spyrou299fdd32019-05-01 13:03:59 +010075 shapes.reserve(input_descriptors.size());
Georgios Pinitase2220552018-07-20 13:23:44 +010076 for(auto &input_descriptor : input_descriptors)
Georgios Pinitasd8734b52017-12-22 15:27:52 +000077 {
Georgios Pinitase2220552018-07-20 13:23:44 +010078 shapes.emplace_back(&input_descriptor.shape);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000079 }
80
Michalis Spyroua9c44722019-04-05 17:18:36 +010081 output_descriptor.shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(shapes, axis_idx);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000082
Georgios Pinitascac13b12018-04-27 19:07:19 +010083 return output_descriptor;
Georgios Pinitasd8734b52017-12-22 15:27:52 +000084}
85
Georgios Pinitase2220552018-07-20 13:23:44 +010086bool ConcatenateLayerNode::forward_descriptors()
Georgios Pinitasd8734b52017-12-22 15:27:52 +000087{
88 if(_outputs[0] != NullTensorID)
89 {
90 Tensor *dst = output(0);
91 ARM_COMPUTE_ERROR_ON(dst == nullptr);
92 dst->desc() = configure_output(0);
93 return true;
94 }
95 return false;
96}
97
Georgios Pinitase2220552018-07-20 13:23:44 +010098TensorDescriptor ConcatenateLayerNode::configure_output(size_t idx) const
Georgios Pinitasd8734b52017-12-22 15:27:52 +000099{
100 ARM_COMPUTE_UNUSED(idx);
101 ARM_COMPUTE_ERROR_ON(idx >= _outputs.size());
102
103 // Check if all input tensors are set
104 bool are_all_inputs_set = std::all_of(std::begin(_input_edges), std::end(_input_edges), [](const EdgeID & eid)
105 {
106 return eid != EmptyEdgeID;
107 });
108
109 TensorDescriptor output_info = {};
110
111 if(are_all_inputs_set)
112 {
Georgios Pinitascac13b12018-04-27 19:07:19 +0100113 std::vector<TensorDescriptor> inputs_descriptors;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000114 for(unsigned int i = 0; i < _input_edges.size(); ++i)
115 {
116 const Tensor *t = _graph->tensor(input_id(i));
117 ARM_COMPUTE_ERROR_ON(t == nullptr);
Georgios Pinitascac13b12018-04-27 19:07:19 +0100118 inputs_descriptors.push_back(t->desc());
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000119 }
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000120 output_info = compute_output_descriptor(inputs_descriptors, _concat_descriptor.axis);
121 if(!_concat_descriptor.output_qinfo.empty())
122 {
123 output_info.quant_info = _concat_descriptor.output_qinfo;
124 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000125 }
126
127 return output_info;
128}
129
Georgios Pinitase2220552018-07-20 13:23:44 +0100130NodeType ConcatenateLayerNode::type() const
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000131{
Georgios Pinitase2220552018-07-20 13:23:44 +0100132 return NodeType::ConcatenateLayer;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000133}
134
Georgios Pinitase2220552018-07-20 13:23:44 +0100135void ConcatenateLayerNode::accept(INodeVisitor &v)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000136{
137 v.visit(*this);
138}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100139} // namespace graph
Pablo Tello3dd5b682019-03-04 14:14:02 +0000140} // namespace arm_compute