Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1 | /* |
| 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 Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 24 | #include "arm_compute/graph/nodes/ConcatenateLayerNode.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Utils.h" |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 27 | #include "arm_compute/graph/Graph.h" |
| 28 | #include "arm_compute/graph/INodeVisitor.h" |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 29 | #include "arm_compute/graph/Utils.h" |
| 30 | |
| 31 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 32 | |
| 33 | namespace arm_compute |
| 34 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 35 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 36 | { |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 37 | ConcatenateLayerNode::ConcatenateLayerNode(unsigned int total_nodes, DataLayoutDimension axis) |
| 38 | : _total_nodes(total_nodes), _axis(axis), _is_enabled(true) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 39 | { |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 40 | _input_edges.resize(_total_nodes, EmptyEdgeID); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 41 | _outputs.resize(1, NullTensorID); |
| 42 | } |
| 43 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 44 | void ConcatenateLayerNode::set_enabled(bool is_enabled) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 45 | { |
| 46 | _is_enabled = is_enabled; |
| 47 | } |
| 48 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 49 | bool ConcatenateLayerNode::is_enabled() const |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 50 | { |
| 51 | return _is_enabled; |
| 52 | } |
| 53 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 54 | DataLayoutDimension ConcatenateLayerNode::concatenation_axis() const |
| 55 | { |
| 56 | return _axis; |
| 57 | } |
| 58 | |
| 59 | TensorDescriptor ConcatenateLayerNode::compute_output_descriptor(const std::vector<TensorDescriptor> &input_descriptors, |
| 60 | DataLayoutDimension axis) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 61 | { |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 62 | ARM_COMPUTE_ERROR_ON(input_descriptors.size() == 0); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 63 | |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 64 | TensorDescriptor output_descriptor = input_descriptors[0]; |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 65 | const int axis_idx = get_dimension_idx(output_descriptor, axis); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 66 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 67 | // Extract shapes |
| 68 | std::vector<const TensorShape *> shapes; |
| 69 | for(auto &input_descriptor : input_descriptors) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 70 | { |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 71 | shapes.emplace_back(&input_descriptor.shape); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 74 | // 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 Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 87 | |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 88 | return output_descriptor; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 91 | bool ConcatenateLayerNode::forward_descriptors() |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 92 | { |
| 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 Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 103 | TensorDescriptor ConcatenateLayerNode::configure_output(size_t idx) const |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 104 | { |
| 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 Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 118 | std::vector<TensorDescriptor> inputs_descriptors; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 119 | 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 Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 123 | inputs_descriptors.push_back(t->desc()); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 124 | } |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 125 | output_info = compute_output_descriptor(inputs_descriptors, _axis); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | return output_info; |
| 129 | } |
| 130 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 131 | NodeType ConcatenateLayerNode::type() const |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 132 | { |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 133 | return NodeType::ConcatenateLayer; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 136 | void ConcatenateLayerNode::accept(INodeVisitor &v) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 137 | { |
| 138 | v.visit(*this); |
| 139 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 140 | } // namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 141 | } // namespace arm_compute |