Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +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 | 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 | |
| 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 | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 29 | |
| 30 | namespace arm_compute |
| 31 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 32 | namespace graph |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 33 | { |
| 34 | SplitLayerNode::SplitLayerNode(unsigned int num_splits, unsigned int axis) |
| 35 | : _num_splits(num_splits), _axis(axis) |
| 36 | { |
| 37 | _input_edges.resize(1, EmptyEdgeID); |
| 38 | _outputs.resize(num_splits, NullTensorID); |
| 39 | } |
| 40 | |
| 41 | unsigned int SplitLayerNode::num_splits() const |
| 42 | { |
| 43 | return _num_splits; |
| 44 | } |
| 45 | |
| 46 | unsigned int SplitLayerNode::axis() const |
| 47 | { |
| 48 | return _axis; |
| 49 | } |
| 50 | |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 51 | std::pair<TensorDescriptor, Coordinates> SplitLayerNode::compute_output_descriptor(const TensorDescriptor &input_descriptor, |
| 52 | unsigned int num_splits, unsigned int axis, unsigned int idx) |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 53 | { |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 54 | const unsigned int split_size = input_descriptor.shape[axis] / num_splits; |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 55 | |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 56 | TensorDescriptor output_descriptor = input_descriptor; |
| 57 | output_descriptor.shape.set(axis, split_size); |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 58 | |
| 59 | Coordinates coords; |
| 60 | coords.set(axis, idx * split_size); |
| 61 | |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 62 | return std::make_pair(output_descriptor, coords); |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | bool SplitLayerNode::forward_descriptors() |
| 66 | { |
| 67 | if(input_id(0) != NullTensorID) |
| 68 | { |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 69 | validate(); |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 70 | for(unsigned int i = 0; i < _outputs.size(); ++i) |
| 71 | { |
| 72 | if(output_id(i) != NullTensorID) |
| 73 | { |
| 74 | Tensor *dst_i = output(i); |
| 75 | ARM_COMPUTE_ERROR_ON(dst_i == nullptr); |
| 76 | dst_i->desc() = configure_output(i); |
| 77 | } |
| 78 | } |
| 79 | return true; |
| 80 | } |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | TensorDescriptor SplitLayerNode::configure_output(size_t idx) const |
| 85 | { |
| 86 | ARM_COMPUTE_UNUSED(idx); |
| 87 | ARM_COMPUTE_ERROR_ON(idx >= _outputs.size()); |
| 88 | |
| 89 | const Tensor *src = input(0); |
| 90 | ARM_COMPUTE_ERROR_ON(src == nullptr); |
| 91 | |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 92 | TensorDescriptor output_info; |
| 93 | std::tie(output_info, std::ignore) = compute_output_descriptor(src->desc(), _num_splits, _axis, idx); |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 94 | |
| 95 | return output_info; |
| 96 | } |
| 97 | |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 98 | Status SplitLayerNode::validate() const |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 99 | { |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 100 | const Tensor *src = input(0); |
| 101 | ARM_COMPUTE_RETURN_ERROR_ON(src == nullptr); |
| 102 | ARM_COMPUTE_RETURN_ERROR_ON(_axis >= src->desc().shape.num_dimensions()); |
| 103 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(src->desc().shape[_axis] % _num_splits, "Split should be exact"); |
| 104 | |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 105 | return Status{}; |
| 106 | } |
| 107 | |
| 108 | NodeType SplitLayerNode::type() const |
| 109 | { |
| 110 | return NodeType::SplitLayer; |
| 111 | } |
| 112 | |
| 113 | void SplitLayerNode::accept(INodeVisitor &v) |
| 114 | { |
| 115 | v.visit(*this); |
| 116 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 117 | } // namespace graph |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 118 | } // namespace arm_compute |