blob: c8fb43c2a1e1382a6fcf0768d2790dd88ff0fc5f [file] [log] [blame]
Georgios Pinitasee33ea52018-03-08 16:01:29 +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 Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph/nodes/SplitLayerNode.h"
Georgios Pinitasee33ea52018-03-08 16:01:29 +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 Pinitasee33ea52018-03-08 16:01:29 +000029
30namespace arm_compute
31{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010032namespace graph
Georgios Pinitasee33ea52018-03-08 16:01:29 +000033{
34SplitLayerNode::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
41unsigned int SplitLayerNode::num_splits() const
42{
43 return _num_splits;
44}
45
46unsigned int SplitLayerNode::axis() const
47{
48 return _axis;
49}
50
51std::pair<TensorShape, Coordinates> SplitLayerNode::compute_output_shape(TensorShape input_shape, unsigned int num_splits, unsigned int axis, unsigned int idx)
52{
53 ARM_COMPUTE_ERROR_ON(axis >= input_shape.num_dimensions());
54 ARM_COMPUTE_ERROR_ON_MSG(input_shape[axis] % num_splits, "Split should be exact");
55
56 const unsigned int split_size = input_shape[axis] / num_splits;
57
58 TensorShape output_shape = input_shape;
59 output_shape.set(axis, split_size);
60
61 Coordinates coords;
62 coords.set(axis, idx * split_size);
63
64 return std::make_pair(output_shape, coords);
65}
66
67bool SplitLayerNode::forward_descriptors()
68{
69 if(input_id(0) != NullTensorID)
70 {
71 for(unsigned int i = 0; i < _outputs.size(); ++i)
72 {
73 if(output_id(i) != NullTensorID)
74 {
75 Tensor *dst_i = output(i);
76 ARM_COMPUTE_ERROR_ON(dst_i == nullptr);
77 dst_i->desc() = configure_output(i);
78 }
79 }
80 return true;
81 }
82 return false;
83}
84
85TensorDescriptor SplitLayerNode::configure_output(size_t idx) const
86{
87 ARM_COMPUTE_UNUSED(idx);
88 ARM_COMPUTE_ERROR_ON(idx >= _outputs.size());
89
90 const Tensor *src = input(0);
91 ARM_COMPUTE_ERROR_ON(src == nullptr);
92
93 TensorShape output_shape;
94
95 TensorDescriptor output_info = src->desc();
96 std::tie(output_shape, std::ignore) = compute_output_shape(src->desc().shape, _num_splits, _axis, idx);
97 output_info.shape = output_shape;
98
99 return output_info;
100}
101
102Status SplitLayerNode::validate()
103{
104 return Status{};
105}
106
107NodeType SplitLayerNode::type() const
108{
109 return NodeType::SplitLayer;
110}
111
112void SplitLayerNode::accept(INodeVisitor &v)
113{
114 v.visit(*this);
115}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100116} // namespace graph
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000117} // namespace arm_compute