ArmNN
 24.05
StridedSlice.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017, 2024 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "StridedSlice.hpp"
7 
9 
10 #include <cstring>
11 
12 namespace armnn
13 {
14 
15 namespace
16 {
17 
18 void PadParams(StridedSliceDescriptor& p, unsigned int dimCount)
19 {
20  ARMNN_THROW_INVALIDARG_MSG_IF_FALSE(dimCount <= 4, "Expected input with at most 4 dimensions");
21 
22  const unsigned int beginIndicesCount =
23  armnn::numeric_cast<unsigned int>(p.m_Begin.size());
24 
25  const unsigned int padCount = dimCount - beginIndicesCount;
26 
27  p.m_Begin.resize(dimCount);
28  p.m_End.resize(dimCount);
29  p.m_Stride.resize(dimCount);
30 
31  for (unsigned int i = beginIndicesCount; i > 0; --i)
32  {
33  p.m_Stride[i + padCount - 1] = p.m_Stride[i - 1];
34  p.m_Begin[i + padCount - 1] = p.m_Begin[i - 1];
35  p.m_End[i + padCount - 1] = p.m_End[i - 1];
36  }
37 
38  for (unsigned int i = 0; i < padCount; ++i)
39  {
40  p.m_Stride[i] = 1;
41  p.m_Begin[i] = 0;
42  p.m_End[i] = 0;
43  }
44 
45  p.m_ShrinkAxisMask <<= padCount;
46  p.m_EllipsisMask <<= padCount;
47  p.m_NewAxisMask <<= padCount;
48  p.m_BeginMask <<= padCount;
49  p.m_EndMask <<= padCount;
50  p.m_BeginMask |= (1 << padCount) - 1;
51  p.m_EndMask |= (1 << padCount) - 1;
52 }
53 
54 bool LoopCondition(int index, int stop, int stride)
55 {
56  return stride > 0 ? index >= stop : index <= stop;
57 }
58 
59 TensorShape ExtendShape(const TensorShape& inputShape,
60  unsigned int newNumDimensions)
61 {
62  if (inputShape.GetNumDimensions() >= newNumDimensions)
63  {
64  return inputShape;
65  }
66 
67  std::vector<unsigned int> newSizes(newNumDimensions, 0);
68 
69  unsigned int diff = newNumDimensions - inputShape.GetNumDimensions();
70 
71  for (unsigned int i = 0; i < diff; i++)
72  {
73  newSizes[i] = 1;
74  }
75 
76  for (unsigned int i = diff; i < newNumDimensions; i++)
77  {
78  newSizes[i] = inputShape[i - diff];
79  }
80 
81  return TensorShape(newNumDimensions, newSizes.data());
82 }
83 
84 } // Anonymous namespace
85 
86 void StridedSlice(const TensorInfo& inputInfo,
87  const StridedSliceDescriptor& params,
88  const void* inputData,
89  void* outputData,
90  unsigned int dataTypeSize)
91 {
92  if (inputData == nullptr)
93  {
94  throw armnn::InvalidArgumentException("Slice: Null inputData pointer");
95  }
96  if (outputData == nullptr)
97  {
98  throw armnn::InvalidArgumentException("Slice: Null outputData pointer");
99  }
100 
101  const unsigned char* input = reinterpret_cast<const unsigned char*>(inputData);
102  unsigned char* output = reinterpret_cast<unsigned char*>(outputData);
103 
104  const TensorShape inputShape = ExtendShape(inputInfo.GetShape(), 4);
105 
106  StridedSliceDescriptor paddedParams = params;
107 
108  // Pad parameters to 4 dimensions
109  PadParams(paddedParams, 4);
110 
111  const int start0 = paddedParams.GetStartForAxis(inputShape, 0);
112  const int stop0 = paddedParams.GetStopForAxis (inputShape, 0, start0);
113 
114  const int start1 = paddedParams.GetStartForAxis(inputShape, 1);
115  const int stop1 = paddedParams.GetStopForAxis (inputShape, 1, start1);
116 
117  const int start2 = paddedParams.GetStartForAxis(inputShape, 2);
118  const int stop2 = paddedParams.GetStopForAxis (inputShape, 2, start2);
119 
120  const int start3 = paddedParams.GetStartForAxis(inputShape, 3);
121  const int stop3 = paddedParams.GetStopForAxis (inputShape, 3, start3);
122 
123  const int step = armnn::numeric_cast<int>(dataTypeSize);
124 
125  for (int in0 = start0;
126  !LoopCondition(in0, stop0, paddedParams.m_Stride[0]);
127  in0 += paddedParams.m_Stride[0])
128  {
129  for (int in1 = start1;
130  !LoopCondition(in1, stop1, paddedParams.m_Stride[1]);
131  in1 += paddedParams.m_Stride[1])
132  {
133  for (int in2 = start2;
134  !LoopCondition(in2, stop2, paddedParams.m_Stride[2]);
135  in2 += paddedParams.m_Stride[2])
136  {
137  for (int in3 = start3;
138  !LoopCondition(in3, stop3, paddedParams.m_Stride[3]);
139  in3 += paddedParams.m_Stride[3])
140  {
141  int dim1 = armnn::numeric_cast<int>(inputShape[1]);
142  int dim2 = armnn::numeric_cast<int>(inputShape[2]);
143  int dim3 = armnn::numeric_cast<int>(inputShape[3]);
144 
145  int inputOffset = (((in0 * dim1 + in1) * dim2 + in2) * dim3 + in3) * step;
146  ::memcpy(output, input + inputOffset, dataTypeSize);
147  output += step;
148  }
149  }
150  }
151  }
152 }
153 
154 } // namespace armnn
armnn::TensorInfo
Definition: Tensor.hpp:152
NumericCast.hpp
armnn::StridedSliceDescriptor::GetStopForAxis
int GetStopForAxis(const TensorShape &inputShape, unsigned int axis, int startForAxis) const
Definition: Descriptors.cpp:420
armnn::TensorShape
Definition: Tensor.hpp:20
armnn::InvalidArgumentException
Definition: Exceptions.hpp:80
armnn::StridedSliceDescriptor::m_Stride
std::vector< int > m_Stride
Stride values for the input that will be sliced.
Definition: Descriptors.hpp:1346
armnn::StridedSliceDescriptor
A StridedSliceDescriptor for the StridedSliceLayer.
Definition: Descriptors.hpp:1303
armnn::TensorInfo::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:193
armnn::StridedSliceDescriptor::GetStartForAxis
int GetStartForAxis(const TensorShape &inputShape, unsigned int axis) const
Definition: Descriptors.cpp:393
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::StridedSlice
void StridedSlice(const TensorInfo &inputInfo, const StridedSliceDescriptor &params, const void *inputData, void *outputData, unsigned int dataTypeSize)
Definition: StridedSlice.cpp:86
StridedSlice.hpp
ARMNN_THROW_INVALIDARG_MSG_IF_FALSE
#define ARMNN_THROW_INVALIDARG_MSG_IF_FALSE(_cond, _str)
Definition: Exceptions.hpp:210