blob: 31931c3a79e7c725f68a12ff4e718e3adedb7279 [file] [log] [blame]
Georgios Pinitasee33ea52018-03-08 16:01:29 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Georgios Pinitasee33ea52018-03-08 16:01:29 +00003 *
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 Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph/nodes/SplitLayerNode.h"
Georgios Pinitasee33ea52018-03-08 16:01:29 +000025
Sheri Zhang16dddd22020-05-27 15:03:48 +010026#include "arm_compute/core/Helpers.h"
Georgios Pinitasee33ea52018-03-08 16:01:29 +000027#include "arm_compute/core/Utils.h"
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010028#include "arm_compute/graph/Graph.h"
29#include "arm_compute/graph/INodeVisitor.h"
Georgios Pinitasee33ea52018-03-08 16:01:29 +000030
31namespace arm_compute
32{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010033namespace graph
Georgios Pinitasee33ea52018-03-08 16:01:29 +000034{
Sheri Zhang16dddd22020-05-27 15:03:48 +010035SplitLayerNode::SplitLayerNode(unsigned int num_splits, int axis, std::vector<int> size_splits)
36 : _num_splits(num_splits), _axis(axis), _size_splits(size_splits)
Georgios Pinitasee33ea52018-03-08 16:01:29 +000037{
38 _input_edges.resize(1, EmptyEdgeID);
39 _outputs.resize(num_splits, NullTensorID);
40}
41
42unsigned int SplitLayerNode::num_splits() const
43{
44 return _num_splits;
45}
46
47unsigned int SplitLayerNode::axis() const
48{
49 return _axis;
50}
51
Georgios Pinitascac13b12018-04-27 19:07:19 +010052std::pair<TensorDescriptor, Coordinates> SplitLayerNode::compute_output_descriptor(const TensorDescriptor &input_descriptor,
Sheri Zhang16dddd22020-05-27 15:03:48 +010053 unsigned int num_splits, int axis, unsigned int idx)
Georgios Pinitasee33ea52018-03-08 16:01:29 +000054{
Sheri Zhang16dddd22020-05-27 15:03:48 +010055 // 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 Pinitascac13b12018-04-27 19:07:19 +010059 TensorDescriptor output_descriptor = input_descriptor;
Sheri Zhang16dddd22020-05-27 15:03:48 +010060 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 Pinitasee33ea52018-03-08 16:01:29 +000081
Georgios Pinitascac13b12018-04-27 19:07:19 +010082 return std::make_pair(output_descriptor, coords);
Georgios Pinitasee33ea52018-03-08 16:01:29 +000083}
84
85bool SplitLayerNode::forward_descriptors()
86{
87 if(input_id(0) != NullTensorID)
88 {
Georgios Pinitascac13b12018-04-27 19:07:19 +010089 validate();
Georgios Pinitasee33ea52018-03-08 16:01:29 +000090 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
104TensorDescriptor 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 Zhang16dddd22020-05-27 15:03:48 +0100112 TensorDescriptor input_descriptor = src->desc();
113 TensorDescriptor output_descriptor = input_descriptor;
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000114
Sheri Zhang16dddd22020-05-27 15:03:48 +0100115 // 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 Pinitasee33ea52018-03-08 16:01:29 +0000129}
130
Georgios Pinitascac13b12018-04-27 19:07:19 +0100131Status SplitLayerNode::validate() const
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000132{
Georgios Pinitascac13b12018-04-27 19:07:19 +0100133 const Tensor *src = input(0);
134 ARM_COMPUTE_RETURN_ERROR_ON(src == nullptr);
Sheri Zhang16dddd22020-05-27 15:03:48 +0100135 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 Pinitascac13b12018-04-27 19:07:19 +0100145
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000146 return Status{};
147}
148
149NodeType SplitLayerNode::type() const
150{
151 return NodeType::SplitLayer;
152}
153
154void SplitLayerNode::accept(INodeVisitor &v)
155{
156 v.visit(*this);
157}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100158} // namespace graph
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000159} // namespace arm_compute