Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 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 | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 24 | #include "arm_compute/graph/nodes/SplitLayerNode.h" |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 25 | |
Sheri Zhang | 16dddd2 | 2020-05-27 15:03:48 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Helpers.h" |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/Utils.h" |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 28 | #include "arm_compute/graph/Graph.h" |
| 29 | #include "arm_compute/graph/INodeVisitor.h" |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 30 | |
| 31 | namespace arm_compute |
| 32 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 33 | namespace graph |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 34 | { |
Sheri Zhang | 16dddd2 | 2020-05-27 15:03:48 +0100 | [diff] [blame] | 35 | SplitLayerNode::SplitLayerNode(unsigned int num_splits, int axis, std::vector<int> size_splits) |
| 36 | : _num_splits(num_splits), _axis(axis), _size_splits(size_splits) |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 37 | { |
| 38 | _input_edges.resize(1, EmptyEdgeID); |
| 39 | _outputs.resize(num_splits, NullTensorID); |
| 40 | } |
| 41 | |
| 42 | unsigned int SplitLayerNode::num_splits() const |
| 43 | { |
| 44 | return _num_splits; |
| 45 | } |
| 46 | |
| 47 | unsigned int SplitLayerNode::axis() const |
| 48 | { |
| 49 | return _axis; |
| 50 | } |
| 51 | |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 52 | std::pair<TensorDescriptor, Coordinates> SplitLayerNode::compute_output_descriptor(const TensorDescriptor &input_descriptor, |
Sheri Zhang | 16dddd2 | 2020-05-27 15:03:48 +0100 | [diff] [blame] | 53 | unsigned int num_splits, int axis, unsigned int idx) |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 54 | { |
Sheri Zhang | 16dddd2 | 2020-05-27 15:03:48 +0100 | [diff] [blame] | 55 | // Handle negative axis, negative index is used to specify axis from the end (e.g. -1 for the last axis). |
| 56 | int num_dimension = static_cast<int32_t>(input_descriptor.shape.num_dimensions()); |
| 57 | int tmp_axis = wrap_around(axis, num_dimension); |
| 58 | Coordinates coords; |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 59 | TensorDescriptor output_descriptor = input_descriptor; |
Sheri Zhang | 16dddd2 | 2020-05-27 15:03:48 +0100 | [diff] [blame] | 60 | int split_size = input_descriptor.shape[tmp_axis] / num_splits; |
| 61 | if(_size_splits.empty()) |
| 62 | { |
| 63 | output_descriptor.shape.set(tmp_axis, split_size); |
| 64 | coords.set(tmp_axis, idx * split_size); |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | int split_size = _size_splits[idx]; |
| 69 | if(split_size == -1) |
| 70 | { |
| 71 | split_size = input_descriptor.shape[tmp_axis]; |
| 72 | for(unsigned int i = 0; i < _size_splits.size() - 1; ++i) |
| 73 | split_size -= _size_splits[i]; |
| 74 | } |
| 75 | output_descriptor.shape.set(tmp_axis, split_size); |
| 76 | int coord_value = 0; |
| 77 | for(unsigned int i = 0; i < idx; ++i) |
| 78 | coord_value += _size_splits[i]; |
| 79 | coords.set(tmp_axis, coord_value); |
| 80 | } |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 81 | |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 82 | return std::make_pair(output_descriptor, coords); |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | bool SplitLayerNode::forward_descriptors() |
| 86 | { |
| 87 | if(input_id(0) != NullTensorID) |
| 88 | { |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 89 | validate(); |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 90 | for(unsigned int i = 0; i < _outputs.size(); ++i) |
| 91 | { |
| 92 | if(output_id(i) != NullTensorID) |
| 93 | { |
| 94 | Tensor *dst_i = output(i); |
| 95 | ARM_COMPUTE_ERROR_ON(dst_i == nullptr); |
| 96 | dst_i->desc() = configure_output(i); |
| 97 | } |
| 98 | } |
| 99 | return true; |
| 100 | } |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | TensorDescriptor SplitLayerNode::configure_output(size_t idx) const |
| 105 | { |
| 106 | ARM_COMPUTE_UNUSED(idx); |
| 107 | ARM_COMPUTE_ERROR_ON(idx >= _outputs.size()); |
| 108 | |
| 109 | const Tensor *src = input(0); |
| 110 | ARM_COMPUTE_ERROR_ON(src == nullptr); |
| 111 | |
Sheri Zhang | 16dddd2 | 2020-05-27 15:03:48 +0100 | [diff] [blame] | 112 | TensorDescriptor input_descriptor = src->desc(); |
| 113 | TensorDescriptor output_descriptor = input_descriptor; |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 114 | |
Sheri Zhang | 16dddd2 | 2020-05-27 15:03:48 +0100 | [diff] [blame] | 115 | // Handle negative axis, negative index is used to specify axis from the end (e.g. -1 for the last axis). |
| 116 | int num_dimension = static_cast<int32_t>(src->desc().shape.num_dimensions()); |
| 117 | int tmp_axis = wrap_around(_axis, num_dimension); |
| 118 | |
| 119 | int split_size = (_size_splits.empty()) ? (input_descriptor.shape[tmp_axis] / _num_splits) : _size_splits[idx]; |
| 120 | if(split_size == -1) |
| 121 | { |
| 122 | split_size = input_descriptor.shape[tmp_axis]; |
| 123 | for(unsigned int i = 0; i < _size_splits.size() - 1; ++i) |
| 124 | split_size -= _size_splits[i]; |
| 125 | } |
| 126 | output_descriptor.shape.set(tmp_axis, split_size); |
| 127 | |
| 128 | return output_descriptor; |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 131 | Status SplitLayerNode::validate() const |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 132 | { |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 133 | const Tensor *src = input(0); |
| 134 | ARM_COMPUTE_RETURN_ERROR_ON(src == nullptr); |
Sheri Zhang | 16dddd2 | 2020-05-27 15:03:48 +0100 | [diff] [blame] | 135 | int num_dimension = static_cast<int32_t>(src->desc().shape.num_dimensions()); |
| 136 | ARM_COMPUTE_RETURN_ERROR_ON(_axis < (-num_dimension) || _axis >= num_dimension); |
| 137 | |
| 138 | // Handle negative axis, negative index is used to specify axis from the end (e.g. -1 for the last axis). |
| 139 | int tmp_axis = wrap_around(_axis, num_dimension); |
| 140 | |
| 141 | if(_size_splits.empty()) |
| 142 | { |
| 143 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(src->desc().shape[tmp_axis] % _num_splits, "Split should be exact"); |
| 144 | } |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 145 | |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 146 | return Status{}; |
| 147 | } |
| 148 | |
| 149 | NodeType SplitLayerNode::type() const |
| 150 | { |
| 151 | return NodeType::SplitLayer; |
| 152 | } |
| 153 | |
| 154 | void SplitLayerNode::accept(INodeVisitor &v) |
| 155 | { |
| 156 | v.visit(*this); |
| 157 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 158 | } // namespace graph |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 159 | } // namespace arm_compute |