xref: /aosp_15_r20/external/ComputeLibrary/src/graph/nodes/ConcatenateLayerNode.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2018-2019 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/ConcatenateLayerNode.h"
25 
26 #include "arm_compute/core/Utils.h"
27 #include "arm_compute/graph/Graph.h"
28 #include "arm_compute/graph/INodeVisitor.h"
29 #include "arm_compute/graph/Utils.h"
30 
31 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
32 
33 namespace arm_compute
34 {
35 namespace graph
36 {
ConcatenateLayerNode(unsigned int total_nodes,descriptors::ConcatLayerDescriptor concat_descriptor)37 ConcatenateLayerNode::ConcatenateLayerNode(unsigned int total_nodes, descriptors::ConcatLayerDescriptor concat_descriptor)
38     : _total_nodes(total_nodes), _concat_descriptor(std::move(concat_descriptor)), _is_enabled(true)
39 {
40     _input_edges.resize(_total_nodes, EmptyEdgeID);
41     _outputs.resize(1, NullTensorID);
42 }
43 
set_enabled(bool is_enabled)44 void ConcatenateLayerNode::set_enabled(bool is_enabled)
45 {
46     _is_enabled = is_enabled;
47 }
48 
is_enabled() const49 bool ConcatenateLayerNode::is_enabled() const
50 {
51     return _is_enabled;
52 }
53 
concatenation_axis() const54 DataLayoutDimension ConcatenateLayerNode::concatenation_axis() const
55 {
56     return _concat_descriptor.axis;
57 }
58 
output_quantization_info() const59 QuantizationInfo ConcatenateLayerNode::output_quantization_info() const
60 {
61     return _concat_descriptor.output_qinfo;
62 }
63 
compute_output_descriptor(const std::vector<TensorDescriptor> & input_descriptors,DataLayoutDimension axis)64 TensorDescriptor ConcatenateLayerNode::compute_output_descriptor(const std::vector<TensorDescriptor> &input_descriptors,
65                                                                  DataLayoutDimension                  axis)
66 {
67     ARM_COMPUTE_ERROR_ON(input_descriptors.size() == 0);
68 
69     TensorDescriptor output_descriptor = input_descriptors[0];
70     const int        axis_idx          = get_dimension_idx(output_descriptor.layout, axis);
71     ARM_COMPUTE_ERROR_ON_MSG(axis_idx > 2, "Unsupported concatenation axis!");
72 
73     // Extract shapes
74     std::vector<const TensorShape *> shapes;
75     shapes.reserve(input_descriptors.size());
76     for(auto &input_descriptor : input_descriptors)
77     {
78         shapes.emplace_back(&input_descriptor.shape);
79     }
80 
81     output_descriptor.shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(shapes, axis_idx);
82 
83     return output_descriptor;
84 }
85 
forward_descriptors()86 bool ConcatenateLayerNode::forward_descriptors()
87 {
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 
configure_output(size_t idx) const98 TensorDescriptor ConcatenateLayerNode::configure_output(size_t idx) const
99 {
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     {
113         std::vector<TensorDescriptor> inputs_descriptors;
114         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);
118             inputs_descriptors.push_back(t->desc());
119         }
120         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         }
125     }
126 
127     return output_info;
128 }
129 
type() const130 NodeType ConcatenateLayerNode::type() const
131 {
132     return NodeType::ConcatenateLayer;
133 }
134 
accept(INodeVisitor & v)135 void ConcatenateLayerNode::accept(INodeVisitor &v)
136 {
137     v.visit(*this);
138 }
139 } // namespace graph
140 } // namespace arm_compute
141