blob: 5d46c9dcc9d38e30e3563bd9d672596e33f3fabf [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
Georgios Pinitascac13b12018-04-27 19:07:19 +010051std::pair<TensorDescriptor, Coordinates> SplitLayerNode::compute_output_descriptor(const TensorDescriptor &input_descriptor,
52 unsigned int num_splits, unsigned int axis, unsigned int idx)
Georgios Pinitasee33ea52018-03-08 16:01:29 +000053{
Georgios Pinitascac13b12018-04-27 19:07:19 +010054 const unsigned int split_size = input_descriptor.shape[axis] / num_splits;
Georgios Pinitasee33ea52018-03-08 16:01:29 +000055
Georgios Pinitascac13b12018-04-27 19:07:19 +010056 TensorDescriptor output_descriptor = input_descriptor;
57 output_descriptor.shape.set(axis, split_size);
Georgios Pinitasee33ea52018-03-08 16:01:29 +000058
59 Coordinates coords;
60 coords.set(axis, idx * split_size);
61
Georgios Pinitascac13b12018-04-27 19:07:19 +010062 return std::make_pair(output_descriptor, coords);
Georgios Pinitasee33ea52018-03-08 16:01:29 +000063}
64
65bool SplitLayerNode::forward_descriptors()
66{
67 if(input_id(0) != NullTensorID)
68 {
Georgios Pinitascac13b12018-04-27 19:07:19 +010069 validate();
Georgios Pinitasee33ea52018-03-08 16:01:29 +000070 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
84TensorDescriptor 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 Pinitascac13b12018-04-27 19:07:19 +010092 TensorDescriptor output_info;
93 std::tie(output_info, std::ignore) = compute_output_descriptor(src->desc(), _num_splits, _axis, idx);
Georgios Pinitasee33ea52018-03-08 16:01:29 +000094
95 return output_info;
96}
97
Georgios Pinitascac13b12018-04-27 19:07:19 +010098Status SplitLayerNode::validate() const
Georgios Pinitasee33ea52018-03-08 16:01:29 +000099{
Georgios Pinitascac13b12018-04-27 19:07:19 +0100100 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 Pinitasee33ea52018-03-08 16:01:29 +0000105 return Status{};
106}
107
108NodeType SplitLayerNode::type() const
109{
110 return NodeType::SplitLayer;
111}
112
113void SplitLayerNode::accept(INodeVisitor &v)
114{
115 v.visit(*this);
116}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100117} // namespace graph
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000118} // namespace arm_compute