Compute Library
 23.05
NEUnstack.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2021 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/Error.h"
29 #include "arm_compute/core/Types.h"
31 #include "src/common/utils/Log.h"
32 
33 namespace arm_compute
34 {
35 namespace
36 {
37 inline unsigned int wrap_axis(int axis, const ITensorInfo *const tensor)
38 {
39  return wrap_around(axis, static_cast<int>(tensor->num_dimensions()));
40 }
41 
42 inline void setup_slice_coordinates_and_mask(Coordinates &slice_start, int32_t &slice_end_mask, const unsigned int input_num_dimensions)
43 {
44  // Setups up coordinates to slice the input tensor: start coordinates to all 0s and the unstacking axis of both Start/End to slice just one 2d tensor at a time.
45  Coordinates slice_end;
46  slice_start.set_num_dimensions(input_num_dimensions);
47  slice_end.set_num_dimensions(input_num_dimensions);
48  for(size_t k = 0; k < input_num_dimensions; ++k)
49  {
50  slice_start.set(k, 0);
51  slice_end.set(k, -1);
52  }
54 }
55 } // namespace
56 
58  : _num_slices(0),
59  _strided_slice_vector()
60 {
61 }
62 
63 void NEUnstack::configure(const ITensor *input, const std::vector<ITensor *> &output_vector, int axis)
64 {
65  std::vector<ITensorInfo *> outputs_vector_info(output_vector.size());
66  std::transform(output_vector.begin(), output_vector.end(), outputs_vector_info.begin(), [](ITensor * t)
67  {
69  return t->info();
70  });
71 
73  ARM_COMPUTE_ERROR_THROW_ON(NEUnstack::validate(input->info(), outputs_vector_info, axis));
74  ARM_COMPUTE_LOG_PARAMS(input, output_vector, axis);
75 
76  // Wrap around negative values
77  const unsigned int axis_u = wrap_axis(axis, input->info());
78  _num_slices = std::min(outputs_vector_info.size(), input->info()->dimension(axis_u));
79  _strided_slice_vector.resize(_num_slices);
80 
81  Coordinates slice_start;
82  int32_t slice_end_mask;
83  setup_slice_coordinates_and_mask(slice_start, slice_end_mask, input->info()->tensor_shape().num_dimensions());
84  for(unsigned int slice = 0; slice < _num_slices; ++slice)
85  {
86  // Adjusts start and end coordinates to take a 2D slice at a time
87  slice_start.set(axis_u, slice);
88  _strided_slice_vector[slice].configure(input, output_vector[slice], slice_start, Coordinates(), BiStrides(), 0, slice_end_mask, (1 << axis_u));
89  }
90 }
91 
92 Status NEUnstack::validate(const ITensorInfo *input, const std::vector<ITensorInfo *> &output_vector, int axis)
93 {
95  ARM_COMPUTE_RETURN_ERROR_ON(output_vector.empty());
96  ARM_COMPUTE_RETURN_ERROR_ON(axis < (-static_cast<int>(input->tensor_shape().num_dimensions())));
97  ARM_COMPUTE_RETURN_ERROR_ON(axis >= static_cast<int>(input->tensor_shape().num_dimensions()));
98 
99  const unsigned int num_slices = std::min(output_vector.size(), input->dimension(wrap_axis(axis, input)));
100  ARM_COMPUTE_RETURN_ERROR_ON(num_slices > input->dimension(wrap_axis(axis, input)));
101  ARM_COMPUTE_RETURN_ERROR_ON(num_slices > output_vector.size());
102 
103  Coordinates slice_start;
104  int32_t slice_end_mask;
105  for(size_t k = 0; k < num_slices; ++k)
106  {
107  slice_start.set(wrap_axis(axis, input), k);
108  setup_slice_coordinates_and_mask(slice_start, slice_end_mask, input->tensor_shape().num_dimensions());
109  ARM_COMPUTE_RETURN_ON_ERROR(NEStridedSlice::validate(input, output_vector[k], slice_start, Coordinates(), BiStrides(), 0, slice_end_mask, (1 << wrap_axis(axis, input))));
110  }
111  return Status{};
112 }
113 
115 {
116  for(unsigned i = 0; i < _num_slices; ++i)
117  {
118  _strided_slice_vector[i].run();
119  }
120 }
121 } // 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
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
Coordinates BiStrides
Bidirectional strides.
Definition: Types.h:53
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:43
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
Interface for CPU tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2023 Arm Limited.
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const Coordinates &starts, const Coordinates &ends, const BiStrides &strides, int32_t begin_mask=0, int32_t end_mask=0, int32_t shrink_axis_mask=0)
Static function to check if given info will lead to a valid configuration of NEStridedSlice.
int32_t construct_slice_end_mask(Coordinates ends)
Constructs end mask in case we want to perform a slice operation using the strided slice interface...
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
T wrap_around(T x, T m)
Wrap-around a number within the range 0 <= x < m.
Definition: Helpers.h:268
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Coordinates of an item.
Definition: Coordinates.h:37
void configure(const ITensor *input, const std::vector< ITensor *> &output_vector, int axis)
Set the input, output and unstacking axis.
Definition: NEUnstack.cpp:63
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
void run() override
Run the kernels contained in the function.
Definition: NEUnstack.cpp:114
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:143
CLTensor * tensor
Pointer to the auxiliary tensor.
#define ARM_COMPUTE_LOG_PARAMS(...)
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
static Status validate(const ITensorInfo *input, const std::vector< ITensorInfo *> &output_vector, int axis)
Static function to check if given info will lead to a valid configuration of NEUnstack.
Definition: NEUnstack.cpp:92
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)
NEUnstack()
Default constructor.
Definition: NEUnstack.cpp:57