Compute Library
 21.02
ConcatenateLayerNode.cpp
Go to the documentation of this file.
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  */
25 
26 #include "arm_compute/core/Utils.h"
30 
32 
33 namespace arm_compute
34 {
35 namespace graph
36 {
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 
45 {
46  _is_enabled = is_enabled;
47 }
48 
50 {
51  return _is_enabled;
52 }
53 
55 {
56  return _concat_descriptor.axis;
57 }
58 
60 {
61  return _concat_descriptor.output_qinfo;
62 }
63 
64 TensorDescriptor ConcatenateLayerNode::compute_output_descriptor(const std::vector<TensorDescriptor> &input_descriptors,
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 
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 
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 
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 
131 {
133 }
134 
136 {
137  v.visit(*this);
138 }
139 } // namespace graph
140 } // namespace arm_compute
DataLayoutDimension concatenation_axis() const
Concatenation axis parameter accessor.
DataLayoutDimension
[DataLayout enum definition]
Definition: Types.h:129
const QuantizationInfo output_qinfo
Output quantizazion info.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Copyright (c) 2017-2021 Arm Limited.
TensorDescriptor & desc()
TensorInfo metadata accessor.
Definition: Tensor.cpp:40
Quantization information.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
virtual void visit(INode &n)=0
Visit INode.
bool is_enabled() const
Enabled parameter accessor.
Tensor * output(size_t idx) const
Returns the tensor of a given output of the node.
Definition: INode.cpp:158
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
bool forward_descriptors() override
Forwards descriptor information to outputs if possible.
TensorID input_id(size_t idx) const
Returns the tensor ID of a given input of the node.
Definition: INode.cpp:137
TensorDescriptor configure_output(size_t idx) const override
Calculates output configuration.
static TensorDescriptor compute_output_descriptor(const std::vector< TensorDescriptor > &input_descriptors, DataLayoutDimension axis)
Computes concatenations output descriptor.
NodeType
Supported nodes.
Definition: Types.h:142
void end(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:290
constexpr EdgeID EmptyEdgeID
Definition: Types.h:75
ConcatenateLayerNode(unsigned int total_nodes, descriptors::ConcatLayerDescriptor concat_descriptor)
Constructor.
unsigned int EdgeID
Definition: Types.h:67
bool empty() const
Indicates whether this QuantizationInfo has valid settings or not.
const DataLayoutDimension axis
Concatenation Axis.
QuantizationInfo output_quantization_info() const
Concatenation output quantization info accessor.
constexpr TensorID NullTensorID
Constant NodeID specifying an equivalent of null node.
Definition: Types.h:71
Node visitor interface.
Definition: INodeVisitor.h:34
NodeType type() const override
Returns node&#39;s type.
TensorShape calculate_concatenate_shape(const std::vector< T *> &input, size_t axis)
Calculate the concatenate output shape of the concatenate operation along a single axis...
void accept(INodeVisitor &v) override
Accepts a node visitor.
size_t get_dimension_idx(DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get index of a tensor&#39;s given dimension depending on its layout.
Definition: Utils.cpp:129
Tensor object.
Definition: Tensor.h:41
void set_enabled(bool is_enabled)
Disables or not the depth concatenate node.