ArmNN
 24.11
StridedSliceOperator.cpp File Reference
#include "SliceOperator.hpp"
Include dependency graph for StridedSliceOperator.cpp:

Go to the source code of this file.

Functions

TosaSerializationBasicBlock * ConvertStridedSliceToTosaOperator (const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const StridedSliceDescriptor *stridedSliceDescriptor)
 

Function Documentation

◆ ConvertStridedSliceToTosaOperator()

TosaSerializationBasicBlock* ConvertStridedSliceToTosaOperator ( const Layer layer,
const std::vector< const TensorInfo * > &  inputs,
const std::vector< const TensorInfo * > &  outputs,
const StridedSliceDescriptor stridedSliceDescriptor 
)

Begin with the slice

Definition at line 15 of file StridedSliceOperator.cpp.

19 {
20  // Limitations
21  if (stridedSliceDescriptor->m_EllipsisMask != 0)
22  {
23  throw armnn::Exception("ConvertStridedSliceToTosaOperator: Ellipses mask not supported.");
24  }
25 
26  /// Begin with the slice
27  std::vector<int32_t> begin(stridedSliceDescriptor->m_Begin);
28  std::vector<int32_t> end(stridedSliceDescriptor->m_End);
29  std::vector<int32_t> strides(stridedSliceDescriptor->m_Stride);
30 
31  for (auto stride : strides)
32  {
33  if (stride != 1)
34  {
35  // Only strides with values 1 supported otherwise reshape invoked which creates tensors with more than 5D
36  throw armnn::Exception("ConvertStridedSliceToTosaOperator: Strides greater than 1 not supported.");
37  }
38  }
39 
40  std::string inputName = std::string("input_");
41  std::string outputNameSlice = std::string("intermediate1_") + GetUniqueTosaMappingID();
42  std::string outputNameReshape = std::string("intermediate2_") + GetUniqueTosaMappingID();
43  std::string outputName = std::string("output0_");
44  std::string blockName = std::string("Op_SLICE_block_") + GetUniqueTosaMappingID();
45 
46  // If a layer is present then the block will be used for execution, so input and output names need to be determined
47  // using the previous and following layers so the graph is connected correctly. For validation this doesn't matter.
48  if(layer != nullptr)
49  {
50  inputName = GenerateUniqueInputName(layer->GetInputSlot(0));
51  outputName = GenerateUniqueOutputName(*layer);
52  }
53 
54  std::vector<TosaSerializationTensor*> tensors;
55  std::vector<TosaSerializationOperator *> operators;
56 
57  std::vector<int32_t> inputShape = GetTosaTensorShape(inputs[0]->GetShape());
58  DType inputDType = ArmNNToDType(inputs[0]->GetDataType());
59 
60  // Only add input tensors if connected layer is an input layer.
61  // As intermediate or constant tensors will be created separately.
62  // There also can't be duplicate tensor.
63  if(inputName.find("input_") != std::string::npos)
64  {
65  tensors.push_back(new TosaSerializationTensor(inputName, inputShape, inputDType, {}));
66  }
67 
68  DType outputDType = ArmNNToDType(outputs[0]->GetDataType());
69  std::vector<int32_t> outputShape = GetTosaTensorShape(outputs[0]->GetShape());
70 
71  // Figure out size
72  uint32_t inputRank = inputs[0]->GetShape().GetNumDimensions();
73  std::vector<int32_t> a1_size(inputRank);
74 
75  // If mask set default to begin and end size from input tensor
76  for (uint32_t i = 0; i < inputRank; ++i)
77  {
78  if (stridedSliceDescriptor->m_BeginMask & (1 << i))
79  {
80  begin[i] = 0;
81  }
82  if (stridedSliceDescriptor->m_EndMask & (1 << i))
83  {
84  end[i] = inputShape[i];
85  }
86 
87  a1_size[i] = end[i] - begin[i];
88  }
89 
90  TosaSliceAttribute sliceAttribute(begin, a1_size);
91 
92  auto* sliceOp1 = new TosaSerializationOperator(Op_SLICE,
93  Attribute_SliceAttribute,
94  &sliceAttribute,
95  {inputName},
96  {outputNameSlice});
97 
98  tensors.push_back(new TosaSerializationTensor(outputNameSlice, a1_size, outputDType, {}));
99  operators.push_back(sliceOp1);
100 
101  // If unary striding is used we can reverse, reshape, and return the result.
102  std::vector<int32_t> newShape;
103 
104  for (uint32_t i = 0; i < inputRank; ++i)
105  {
106  // Remove dimension specified in ShrinkAxisMask
107  if (!(stridedSliceDescriptor->m_ShrinkAxisMask & (1 << i)))
108  {
109  newShape.push_back(a1_size[i]);
110  }
111  }
112 
113  TosaReshapeAttribute reshapeAttribute2(newShape);
114 
115  auto* reshapeOp2 = new TosaSerializationOperator(Op_RESHAPE,
116  Attribute_ReshapeAttribute,
117  &reshapeAttribute2,
118  {outputNameSlice},
119  {outputName});
120 
121  tensors.push_back(new TosaSerializationTensor(outputName, newShape, outputDType, {}));
122  operators.push_back(reshapeOp2);
123 
124  // operatorInputNames/operatorOutputNames ends up being the same as
125  // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings
126  return new TosaSerializationBasicBlock(blockName, // name
127  mainName, // region name
128  operators, // operators
129  tensors, // tensors
130  {inputName}, // inputs
131  {outputName}); // outputs
132 }

References GenerateUniqueInputName(), GenerateUniqueOutputName(), Layer::GetInputSlot(), GetTosaTensorShape(), GetUniqueTosaMappingID(), StridedSliceDescriptor::m_Begin, StridedSliceDescriptor::m_EllipsisMask, StridedSliceDescriptor::m_End, and StridedSliceDescriptor::m_Stride.

Referenced by GetTosaMapping().

armnn::StridedSliceDescriptor::m_Begin
std::vector< int > m_Begin
Begin values for the input that will be sliced.
Definition: Descriptors.hpp:1342
armnn::StridedSliceDescriptor::m_EllipsisMask
int32_t m_EllipsisMask
Ellipsis mask value.
Definition: Descriptors.hpp:1357
GenerateUniqueOutputName
std::string GenerateUniqueOutputName(const Layer &layer, uint32_t layerSlot=0)
Definition: TosaOperatorUtils.hpp:120
armnn::StridedSliceDescriptor::m_BeginMask
int32_t m_BeginMask
Begin mask value.
Definition: Descriptors.hpp:1350
armnn::Layer::GetInputSlot
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:337
mainName
const std::string mainName
Definition: TosaOperatorUtils.hpp:19
ArmNNToDType
DType ArmNNToDType(const DataType &type)
Definition: TosaOperatorUtils.hpp:22
armnn::Exception
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
armnn::StridedSliceDescriptor::m_EndMask
int32_t m_EndMask
End mask value.
Definition: Descriptors.hpp:1353
armnn::StridedSliceDescriptor::m_ShrinkAxisMask
int32_t m_ShrinkAxisMask
Shrink axis mask value. If set, the nth specification shrinks the dimensionality by 1.
Definition: Descriptors.hpp:1355
armnn::StridedSliceDescriptor::m_Stride
std::vector< int > m_Stride
Stride values for the input that will be sliced.
Definition: Descriptors.hpp:1346
GetTosaTensorShape
std::vector< int32_t > GetTosaTensorShape(const TensorShape &shape)
Definition: TosaOperatorUtils.hpp:79
armnn::StridedSliceDescriptor::m_End
std::vector< int > m_End
End values for the input that will be sliced.
Definition: Descriptors.hpp:1344
GenerateUniqueInputName
std::string GenerateUniqueInputName(const armnn::InputSlot &slot)
Definition: TosaOperatorUtils.hpp:109
GetUniqueTosaMappingID
std::string GetUniqueTosaMappingID()
Definition: TosaOperatorUtils.hpp:138