blob: ade3f6e1a9884ac5adee7df355b616a661bca636 [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
2 * Copyright (c) 2018 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 */
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{
Georgios Pinitase2220552018-07-20 13:23:44 +010037ConcatenateLayerNode::ConcatenateLayerNode(unsigned int total_nodes, DataLayoutDimension axis)
38 : _total_nodes(total_nodes), _axis(axis), _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{
56 return _axis;
57}
58
59TensorDescriptor ConcatenateLayerNode::compute_output_descriptor(const std::vector<TensorDescriptor> &input_descriptors,
60 DataLayoutDimension axis)
Georgios Pinitasd8734b52017-12-22 15:27:52 +000061{
Georgios Pinitascac13b12018-04-27 19:07:19 +010062 ARM_COMPUTE_ERROR_ON(input_descriptors.size() == 0);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000063
Georgios Pinitascac13b12018-04-27 19:07:19 +010064 TensorDescriptor output_descriptor = input_descriptors[0];
Georgios Pinitase2220552018-07-20 13:23:44 +010065 const int axis_idx = get_dimension_idx(output_descriptor, axis);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000066
Georgios Pinitase2220552018-07-20 13:23:44 +010067 // Extract shapes
68 std::vector<const TensorShape *> shapes;
69 for(auto &input_descriptor : input_descriptors)
Georgios Pinitasd8734b52017-12-22 15:27:52 +000070 {
Georgios Pinitase2220552018-07-20 13:23:44 +010071 shapes.emplace_back(&input_descriptor.shape);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000072 }
73
Georgios Pinitase2220552018-07-20 13:23:44 +010074 // Calculate output shape
75 if(axis_idx == 0)
76 {
77 output_descriptor.shape = arm_compute::misc::shape_calculator::calculate_width_concatenate_shape(shapes);
78 }
79 else if(axis_idx == 2)
80 {
81 output_descriptor.shape = arm_compute::misc::shape_calculator::calculate_depth_concatenate_shape(shapes);
82 }
83 else
84 {
85 ARM_COMPUTE_ERROR("Unsupported concatenation axis!");
86 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +000087
Georgios Pinitascac13b12018-04-27 19:07:19 +010088 return output_descriptor;
Georgios Pinitasd8734b52017-12-22 15:27:52 +000089}
90
Georgios Pinitase2220552018-07-20 13:23:44 +010091bool ConcatenateLayerNode::forward_descriptors()
Georgios Pinitasd8734b52017-12-22 15:27:52 +000092{
93 if(_outputs[0] != NullTensorID)
94 {
95 Tensor *dst = output(0);
96 ARM_COMPUTE_ERROR_ON(dst == nullptr);
97 dst->desc() = configure_output(0);
98 return true;
99 }
100 return false;
101}
102
Georgios Pinitase2220552018-07-20 13:23:44 +0100103TensorDescriptor ConcatenateLayerNode::configure_output(size_t idx) const
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000104{
105 ARM_COMPUTE_UNUSED(idx);
106 ARM_COMPUTE_ERROR_ON(idx >= _outputs.size());
107
108 // Check if all input tensors are set
109 bool are_all_inputs_set = std::all_of(std::begin(_input_edges), std::end(_input_edges), [](const EdgeID & eid)
110 {
111 return eid != EmptyEdgeID;
112 });
113
114 TensorDescriptor output_info = {};
115
116 if(are_all_inputs_set)
117 {
Georgios Pinitascac13b12018-04-27 19:07:19 +0100118 std::vector<TensorDescriptor> inputs_descriptors;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000119 for(unsigned int i = 0; i < _input_edges.size(); ++i)
120 {
121 const Tensor *t = _graph->tensor(input_id(i));
122 ARM_COMPUTE_ERROR_ON(t == nullptr);
Georgios Pinitascac13b12018-04-27 19:07:19 +0100123 inputs_descriptors.push_back(t->desc());
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000124 }
Georgios Pinitase2220552018-07-20 13:23:44 +0100125 output_info = compute_output_descriptor(inputs_descriptors, _axis);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000126 }
127
128 return output_info;
129}
130
Georgios Pinitase2220552018-07-20 13:23:44 +0100131NodeType ConcatenateLayerNode::type() const
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000132{
Georgios Pinitase2220552018-07-20 13:23:44 +0100133 return NodeType::ConcatenateLayer;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000134}
135
Georgios Pinitase2220552018-07-20 13:23:44 +0100136void ConcatenateLayerNode::accept(INodeVisitor &v)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000137{
138 v.visit(*this);
139}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100140} // namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000141} // namespace arm_compute