Compute Library
 20.05
SplitLayerNode.cpp
Go to the documentation of this file.
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  */
25 
26 #include "arm_compute/core/Utils.h"
29 
30 namespace arm_compute
31 {
32 namespace graph
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 
51 std::pair<TensorDescriptor, Coordinates> SplitLayerNode::compute_output_descriptor(const TensorDescriptor &input_descriptor,
52  unsigned int num_splits, unsigned int axis, unsigned int idx)
53 {
54  const unsigned int split_size = input_descriptor.shape[axis] / num_splits;
55 
56  TensorDescriptor output_descriptor = input_descriptor;
57  output_descriptor.shape.set(axis, split_size);
58 
59  Coordinates coords;
60  coords.set(axis, idx * split_size);
61 
62  return std::make_pair(output_descriptor, coords);
63 }
64 
66 {
67  if(input_id(0) != NullTensorID)
68  {
69  validate();
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 
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 
93  std::tie(output_info, std::ignore) = compute_output_descriptor(src->desc(), _num_splits, _axis, idx);
94 
95  return output_info;
96 }
97 
99 {
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 
105  return Status{};
106 }
107 
109 {
110  return NodeType::SplitLayer;
111 }
112 
114 {
115  v.visit(*this);
116 }
117 } // namespace graph
118 } // namespace arm_compute
static std::pair< TensorDescriptor, Coordinates > compute_output_descriptor(const TensorDescriptor &input_descriptor, unsigned int num_splits, unsigned int axis, unsigned int idx)
Computes split layer output descriptor.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
NodeType type() const override
Returns node's type.
Status class.
Definition: Error.h:52
unsigned int num_splits() const
Number of splits accessor.
SplitLayerNode(unsigned int num_splits, unsigned int axis=0)
Default Constructor.
void set(size_t dimension, T value)
Accessor to set the value of one of the dimensions.
Definition: Dimensions.h:74
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
Copyright (c) 2017-2020 ARM Limited.
TensorDescriptor & desc()
TensorInfo metadata accessor.
Definition: Tensor.cpp:40
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
virtual void visit(INode &n)=0
Visit INode.
Tensor * output(size_t idx) const
Returns the tensor of a given output of the node.
Definition: INode.cpp:158
Coordinates of an item.
Definition: Coordinates.h:37
TensorID input_id(size_t idx) const
Returns the tensor ID of a given input of the node.
Definition: INode.cpp:137
NodeType
Supported nodes.
Definition: Types.h:134
unsigned int axis() const
Split axis accessor.
constexpr EdgeID EmptyEdgeID
Definition: Types.h:75
void accept(INodeVisitor &v) override
Accepts a node visitor.
TensorID output_id(size_t idx) const
Returns the tensor ID of a given output of the node.
Definition: INode.cpp:144
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:244
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true)
Accessor to set the value of one of the dimensions.
Definition: TensorShape.h:78
bool forward_descriptors() override
Forwards descriptor information to outputs if possible.
constexpr TensorID NullTensorID
Constant NodeID specifying an equivalent of null node.
Definition: Types.h:71
Node visitor interface.
Definition: INodeVisitor.h:34
Tensor * input(size_t idx) const
Returns the tensor of a given input of the node.
Definition: INode.cpp:150
Status validate() const override
Validate node.
TensorDescriptor configure_output(size_t idx) const override
Calculates output configuration.
cast configure & src
Definition: Cast.cpp:169
Tensor object.
Definition: Tensor.h:41