ArmNN
 24.02
Slice.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2019 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "Slice.hpp"
7 
10 
11 namespace armnn
12 {
13 
14 void Slice(const TensorInfo& inputInfo,
15  const SliceDescriptor& descriptor,
16  const void* inputData,
17  void* outputData,
18  unsigned int dataTypeSize)
19 {
20  const TensorShape& inputShape = inputInfo.GetShape();
21  const unsigned int numDims = inputShape.GetNumDimensions();
22 
23  constexpr unsigned int maxNumDims = 4;
24  if (descriptor.m_Begin.size() != numDims)
25  {
26  std::stringstream msg;
27  msg << "Slice: Number of dimensions (" << numDims <<
28  ") does not match the Begin vector in the descriptor (" << descriptor.m_Begin.size() << ")";
29  throw InvalidArgumentException(msg.str());
30  }
31  if (descriptor.m_Size.size() != numDims)
32  {
33  std::stringstream msg;
34  msg << "Slice: Number of dimensions (" << numDims <<
35  ") does not match the Size vector in the descriptor (" << descriptor.m_Size.size() << ")";
36  throw InvalidArgumentException(msg.str());
37  }
38  if (numDims > maxNumDims)
39  {
40  std::stringstream msg;
41  msg << "Slice: Number of dimensions (" << numDims <<
42  ") is greater than the maximum supported (" << maxNumDims << ")";
43  throw InvalidArgumentException(msg.str());
44  }
45 
46  std::vector<unsigned int> paddedInput(4);
47  std::vector<unsigned int> paddedBegin(4);
48  std::vector<unsigned int> paddedSize (4);
49 
50  const unsigned int numPaddingDims = maxNumDims - numDims;
51  for (unsigned int i = 0u; i < maxNumDims; ++i)
52  {
53  if (i < numPaddingDims)
54  {
55  paddedInput[i] = 1u;
56  paddedBegin[i] = 0u;
57  paddedSize[i] = 1u;
58  }
59  else
60  {
61  const unsigned int j = i - numPaddingDims;
62  paddedInput[i] = inputShape[j];
63  paddedBegin[i] = descriptor.m_Begin[j];
64  paddedSize[i] = descriptor.m_Size[j];
65  }
66  }
67 
68  unsigned int dim0 = paddedInput[0];
69  unsigned int dim1 = paddedInput[1];
70  unsigned int dim2 = paddedInput[2];
71  unsigned int dim3 = paddedInput[3];
72 
73  unsigned int begin0 = paddedBegin[0];
74  unsigned int begin1 = paddedBegin[1];
75  unsigned int begin2 = paddedBegin[2];
76  unsigned int begin3 = paddedBegin[3];
77 
78  unsigned int size0 = paddedSize[0];
79  unsigned int size1 = paddedSize[1];
80  unsigned int size2 = paddedSize[2];
81  unsigned int size3 = paddedSize[3];
82 
83  if (begin0 + size0 > dim0)
84  {
85  std::stringstream msg;
86  msg << "Slice: begin0 + size0 (" << (begin0 + size0) <<
87  ") exceeds dim0 (" << dim0 << ")";
88  throw InvalidArgumentException(msg.str());
89  }
90  if (begin1 + size1 > dim1)
91  {
92  std::stringstream msg;
93  msg << "Slice: begin1 + size1 (" << (begin1 + size1) <<
94  ") exceeds dim2 (" << dim1 << ")";
95  throw InvalidArgumentException(msg.str());
96  }
97  if (begin2 + size2 > dim2)
98  {
99  std::stringstream msg;
100  msg << "Slice: begin2 + size2 (" << (begin2 + size2) <<
101  ") exceeds dim2 (" << dim2 << ")";
102  throw InvalidArgumentException(msg.str());
103  }
104  if (begin3 + size3 > dim3)
105  {
106  std::stringstream msg;
107  msg << "Slice: begin3 + size3 (" << (begin3 + size3) <<
108  ") exceeds dim3 (" << dim3 << ")";
109  throw InvalidArgumentException(msg.str());
110  }
111 
112  if (inputData == nullptr)
113  {
114  throw armnn::NullPointerException("Slice: Null inputData pointer");
115  }
116  if (outputData == nullptr)
117  {
118  throw armnn::NullPointerException("Slice: Null outputData pointer");
119  }
120 
121  const unsigned char* input = reinterpret_cast<const unsigned char*>(inputData);
122  unsigned char* output = reinterpret_cast<unsigned char*>(outputData);
123 
124  for (unsigned int idx0 = begin0; idx0 < begin0 + size0; ++idx0)
125  {
126  for (unsigned int idx1 = begin1; idx1 < begin1 + size1; ++idx1)
127  {
128  for (unsigned int idx2 = begin2; idx2 < begin2 + size2; ++idx2)
129  {
130  for (unsigned int idx3 = begin3; idx3 < begin3 + size3; ++idx3)
131  {
132  const unsigned int inputOffset =
133  (((idx0 * dim1 + idx1) * dim2 + idx2) * dim3 + idx3) * dataTypeSize;
134 
135  ::memcpy(output, input + inputOffset, dataTypeSize);
136  output += dataTypeSize;
137  }
138  }
139  }
140  }
141 }
142 
143 } // namespace armnn
armnn::SliceDescriptor::m_Begin
std::vector< unsigned int > m_Begin
Beginning indices of the slice in each dimension.
Definition: Descriptors.hpp:1244
armnn::TensorInfo
Definition: Tensor.hpp:152
IgnoreUnused.hpp
Assert.hpp
armnn::TensorShape
Definition: Tensor.hpp:20
armnn::TensorShape::GetNumDimensions
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition: Tensor.cpp:174
armnn::Slice
void Slice(const TensorInfo &inputInfo, const SliceDescriptor &descriptor, const void *inputData, void *outputData, unsigned int dataTypeSize)
Definition: Slice.cpp:14
armnn::SliceDescriptor
A SliceDescriptor for the SliceLayer.
Definition: Descriptors.hpp:1228
armnn::InvalidArgumentException
Definition: Exceptions.hpp:80
armnn::TensorInfo::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:193
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::SliceDescriptor::m_Size
std::vector< unsigned int > m_Size
Size of the slice in each dimension.
Definition: Descriptors.hpp:1247
armnn::NullPointerException
Definition: Exceptions.hpp:146
Slice.hpp