ArmNN
 25.11
Loading...
Searching...
No Matches
DepthToSpaceOperator.cpp File Reference
Include dependency graph for DepthToSpaceOperator.cpp:

Go to the source code of this file.

Functions

TosaSerializationBasicBlock * ConvertDepthToSpaceToTosaOperator (const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const DepthToSpaceDescriptor *descriptor)

Function Documentation

◆ ConvertDepthToSpaceToTosaOperator()

TosaSerializationBasicBlock * ConvertDepthToSpaceToTosaOperator ( const Layer * layer,
const std::vector< const TensorInfo * > & inputs,
const std::vector< const TensorInfo * > & outputs,
const DepthToSpaceDescriptor * descriptor )

Definition at line 14 of file DepthToSpaceOperator.cpp.

18{
19 // TOSA currently only supports NHWC input
20 if (descriptor->m_DataLayout != DataLayout::NHWC)
21 {
22 throw InvalidArgumentException("Only NHWC input is supported for DepthToSpace");
23 }
24
25 // TOSA currently only supports 4D input
26 if (inputs[0]->GetNumDimensions() != 4)
27 {
28 throw InvalidArgumentException("Only 4D input is supported for DepthToSpace");
29 }
30
31 std::string inputName;
32 std::string outputName = std::string("output0_");
33 std::string outputReshapeName = std::string("layer_intermediate0_") + GetUniqueTosaMappingID();
34 std::string outputTransposeName = std::string("layer_intermediate1_") + GetUniqueTosaMappingID();
35 std::string blockName = std::string("Op_DEPTHTOSPACE_block_") + GetUniqueTosaMappingID();
36
37 DType inputDType0 = ArmNNToDType(inputs[0]->GetDataType());
38 DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType());
39
40 // Set input names for validation purposes only.
41 if(layer == nullptr)
42 {
43 inputName = "input_0";
44 }
45 // If a layer is present then the block will be used for execution, so input and output names need to be
46 // determined using the previous and following layers so the graph is connected correctly.
47 // For validation this doesn't matter.
48 else
49 {
50 // Get the layer connected to the input slot and determine unique tensor names.
51 for (uint32_t i = 0; i < inputs.size(); ++i)
52 {
53 inputName = GenerateUniqueInputName(layer->GetInputSlot(i));
54 }
55
56 // Determine unique output tensor name.
57 outputName = GenerateUniqueOutputName(*layer);
58 }
59
60 std::vector<TosaSerializationTensor*> tensors;
61 std::vector<TosaSerializationOperator*> operators;
62
63 // Setup input Tensor: Only add tensor if connected layer is an input layer.
64 // As intermediate or constant tensors will be created separately. There also can't be duplicate tensors.
65 if(inputName.find("input_") != std::string::npos)
66 {
67 std::vector<int32_t> inputShape0 = GetTosaTensorShape(inputs[0]->GetShape());
68 tensors.push_back(new TosaSerializationTensor(inputName, inputShape0, inputDType0, {}));
69 }
70
71 std::vector<int32_t> reshapeDims1
72 {
73 static_cast<int32_t>(inputs[0]->GetShape()[0]),
74 static_cast<int32_t>(inputs[0]->GetShape()[1]),
75 static_cast<int32_t>(inputs[0]->GetShape()[2]),
76 static_cast<int32_t>(descriptor->m_BlockSize),
77 static_cast<int32_t>(descriptor->m_BlockSize),
78 static_cast<int32_t>(inputs[0]->GetShape()[3] / (descriptor->m_BlockSize * descriptor->m_BlockSize))
79 };
80 TosaReshapeAttribute reshapeAttr1(reshapeDims1);
81 auto* reshapeOp1 = new TosaSerializationOperator(Op_RESHAPE,
82 Attribute_ReshapeAttribute,
83 &reshapeAttr1,
84 {inputName},
85 {outputReshapeName});
86 operators.push_back(reshapeOp1);
87 tensors.push_back(new TosaSerializationTensor(outputReshapeName, reshapeDims1, inputDType0, {}));
88
89 std::vector<int32_t> mappings {0, 1, 3, 2, 4, 5};
90 std::vector<int32_t> transposeShape
91 {
92 static_cast<int32_t>(reshapeDims1[0]),
93 static_cast<int32_t>(reshapeDims1[1]),
94 static_cast<int32_t>(reshapeDims1[3]),
95 static_cast<int32_t>(reshapeDims1[2]),
96 static_cast<int32_t>(reshapeDims1[4]),
97 static_cast<int32_t>(reshapeDims1[5])
98 };
99 TosaTransposeAttribute transposeAttr(mappings);
100 auto* transposeOp = new TosaSerializationOperator(Op_TRANSPOSE,
101 Attribute_TransposeAttribute,
102 &transposeAttr,
103 {outputReshapeName},
104 {outputTransposeName});
105 operators.push_back(transposeOp);
106 tensors.push_back(new TosaSerializationTensor(outputTransposeName, transposeShape, outputDType0, {}));
107
108 std::vector<int32_t> reshapeDims2
109 {
110 static_cast<int32_t>(inputs[0]->GetShape()[0]),
111 static_cast<int32_t>(inputs[0]->GetShape()[1] * descriptor->m_BlockSize),
112 static_cast<int32_t>(inputs[0]->GetShape()[2] * descriptor->m_BlockSize),
113 static_cast<int32_t>(inputs[0]->GetShape()[3] / (descriptor->m_BlockSize * descriptor->m_BlockSize))
114 };
115 TosaReshapeAttribute reshapeAttr2(reshapeDims2);
116 auto* reshapeOp2 = new TosaSerializationOperator(Op_RESHAPE,
117 Attribute_ReshapeAttribute,
118 &reshapeAttr2,
119 {outputTransposeName},
120 {outputName});
121 operators.push_back(reshapeOp2);
122 std::vector<int32_t> outputShape0 = GetTosaTensorShape(outputs[0]->GetShape());
123 tensors.push_back(new TosaSerializationTensor(outputName, outputShape0, outputDType0, {}));
124
125 return new TosaSerializationBasicBlock(blockName, // name
126 mainName, // region name
127 operators, // operators
128 tensors, // tensors
129 {inputName}, // inputs
130 {outputName});
131}
std::string GenerateUniqueOutputName(const Layer &layer, uint32_t layerSlot=0)
const std::string mainName
DType ArmNNToDType(const DataType &type)
std::string GenerateUniqueInputName(const armnn::InputSlot &slot)
std::string GetUniqueTosaMappingID()
std::vector< int32_t > GetTosaTensorShape(const TensorShape &shape)
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition Layer.hpp:337
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
unsigned int m_BlockSize
Scalar specifying the input block size. It must be >= 1.

References ArmNNToDType(), GenerateUniqueInputName(), GenerateUniqueOutputName(), Layer::GetInputSlot(), GetTosaTensorShape(), GetUniqueTosaMappingID(), SpaceToDepthDescriptor::m_BlockSize, SpaceToDepthDescriptor::m_DataLayout, and mainName.

Referenced by GetTosaMapping().