Compute Library
 21.05
SplitLayerNode.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020 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 
27 #include "arm_compute/core/Utils.h"
30 
31 namespace arm_compute
32 {
33 namespace graph
34 {
35 SplitLayerNode::SplitLayerNode(unsigned int num_splits, int axis, std::vector<int> size_splits)
36  : _num_splits(num_splits), _axis(axis), _size_splits(size_splits)
37 {
38  _input_edges.resize(1, EmptyEdgeID);
39  _outputs.resize(num_splits, NullTensorID);
40 }
41 
42 unsigned int SplitLayerNode::num_splits() const
43 {
44  return _num_splits;
45 }
46 
47 unsigned int SplitLayerNode::axis() const
48 {
49  return _axis;
50 }
51 
52 std::pair<TensorDescriptor, Coordinates> SplitLayerNode::compute_output_descriptor(const TensorDescriptor &input_descriptor,
53  unsigned int num_splits, int axis, unsigned int idx)
54 {
55  // 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;
59  TensorDescriptor output_descriptor = input_descriptor;
60  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  }
81 
82  return std::make_pair(output_descriptor, coords);
83 }
84 
86 {
87  if(input_id(0) != NullTensorID)
88  {
89  validate();
90  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 
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 
112  TensorDescriptor input_descriptor = src->desc();
113  TensorDescriptor output_descriptor = input_descriptor;
114 
115  // 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;
129 }
130 
132 {
133  const Tensor *src = input(0);
134  ARM_COMPUTE_RETURN_ERROR_ON(src == nullptr);
135  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  }
145 
146  return Status{};
147 }
148 
150 {
151  return NodeType::SplitLayer;
152 }
153 
155 {
156  v.visit(*this);
157 }
158 } // namespace graph
159 } // namespace arm_compute
void set(size_t dimension, T value, bool increase_dim_unit=true)
Accessor to set the value of one of the dimensions.
Definition: Dimensions.h:76
#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.
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
TensorDescriptor & desc()
TensorInfo metadata accessor.
Definition: Tensor.cpp:40
T wrap_around(T x, T m)
Wrap-around a number within the range 0 <= x < m.
Definition: Helpers.h:231
#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:148
unsigned int axis() const
Split axis accessor.
SplitLayerNode(unsigned int num_splits, int axis=0, std::vector< int > size_splits=std::vector< int >())
Default Constructor.
constexpr EdgeID EmptyEdgeID
Definition: Types.h:77
void accept(INodeVisitor &v) override
Accepts a node visitor.
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:143
std::pair< TensorDescriptor, Coordinates > compute_output_descriptor(const TensorDescriptor &input_descriptor, unsigned int num_splits, int axis, unsigned int idx)
Computes split layer output descriptor.
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
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:73
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.
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true, bool increase_dim_unit=true)
Accessor to set the value of one of the dimensions.
Definition: TensorShape.h:79
TensorDescriptor configure_output(size_t idx) const override
Calculates output configuration.
Tensor object.
Definition: Tensor.h:41