ArmNN  NotReleased
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 
8 #include <boost/assert.hpp>
9 #include <boost/core/ignore_unused.hpp>
10 #include <boost/numeric/conversion/cast.hpp>
11 
12 namespace armnn
13 {
14 
15 void Slice(const TensorInfo& inputInfo,
16  const SliceDescriptor& descriptor,
17  const void* inputData,
18  void* outputData,
19  unsigned int dataTypeSize)
20 {
21  const TensorShape& inputShape = inputInfo.GetShape();
22  const unsigned int numDims = inputShape.GetNumDimensions();
23 
24  BOOST_ASSERT(descriptor.m_Begin.size() == numDims);
25  BOOST_ASSERT(descriptor.m_Size.size() == numDims);
26 
27  constexpr unsigned int maxNumDims = 4;
28  BOOST_ASSERT(numDims <= maxNumDims);
29 
30  std::vector<unsigned int> paddedInput(4);
31  std::vector<unsigned int> paddedBegin(4);
32  std::vector<unsigned int> paddedSize (4);
33 
34  const unsigned int numPaddingDims = maxNumDims - numDims;
35  for (unsigned int i = 0u; i < maxNumDims; ++i)
36  {
37  if (i < numPaddingDims)
38  {
39  paddedInput[i] = 1u;
40  paddedBegin[i] = 0u;
41  paddedSize[i] = 1u;
42  }
43  else
44  {
45  const unsigned int j = i - numPaddingDims;
46  paddedInput[i] = inputShape[j];
47  paddedBegin[i] = descriptor.m_Begin[j];
48  paddedSize[i] = descriptor.m_Size[j];
49  }
50  }
51 
52  unsigned int dim0 = paddedInput[0];
53  unsigned int dim1 = paddedInput[1];
54  unsigned int dim2 = paddedInput[2];
55  unsigned int dim3 = paddedInput[3];
56 
57  unsigned int begin0 = paddedBegin[0];
58  unsigned int begin1 = paddedBegin[1];
59  unsigned int begin2 = paddedBegin[2];
60  unsigned int begin3 = paddedBegin[3];
61 
62  unsigned int size0 = paddedSize[0];
63  unsigned int size1 = paddedSize[1];
64  unsigned int size2 = paddedSize[2];
65  unsigned int size3 = paddedSize[3];
66 
67  BOOST_ASSERT(begin0 + size0 <= dim0);
68  BOOST_ASSERT(begin1 + size1 <= dim1);
69  BOOST_ASSERT(begin2 + size2 <= dim2);
70  BOOST_ASSERT(begin3 + size3 <= dim3);
71 
72  const unsigned char* input = reinterpret_cast<const unsigned char*>(inputData);
73  unsigned char* output = reinterpret_cast<unsigned char*>(outputData);
74 
75  boost::ignore_unused(dim0);
76  for (unsigned int idx0 = begin0; idx0 < begin0 + size0; ++idx0)
77  {
78  for (unsigned int idx1 = begin1; idx1 < begin1 + size1; ++idx1)
79  {
80  for (unsigned int idx2 = begin2; idx2 < begin2 + size2; ++idx2)
81  {
82  for (unsigned int idx3 = begin3; idx3 < begin3 + size3; ++idx3)
83  {
84  const unsigned int inputOffset =
85  (((idx0 * dim1 + idx1) * dim2 + idx2) * dim3 + idx3) * dataTypeSize;
86 
87  ::memcpy(output, input + inputOffset, dataTypeSize);
88  output += dataTypeSize;
89  }
90  }
91  }
92  }
93 }
94 
95 } // namespace armnn
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:43
std::vector< unsigned int > m_Begin
Beginning indices of the slice in each dimension.
void Slice(const TensorInfo &inputInfo, const SliceDescriptor &descriptor, const void *inputData, void *outputData, unsigned int dataTypeSize)
Definition: Slice.cpp:15
std::vector< unsigned int > m_Size
Size of the slice in each dimension.
A SliceDescriptor for the SliceLayer.
const TensorShape & GetShape() const
Definition: Tensor.hpp:88