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

Go to the source code of this file.

Functions

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

Function Documentation

◆ ConvertTransposeConv2dToTosaOperator()

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

Definition at line 10 of file TransposeConv2dOperator.cpp.

14{
15 std::string input0Name = std::string("input_");
16 std::string input1Name = std::string("constant_") + GetUniqueTosaMappingID();
17 std::string input2Name = std::string("constant_") + GetUniqueTosaMappingID();
18 std::string outputName = std::string("output0_");
19 std::string blockName = std::string("Op_TRANSPOSE_CONV2D_block_") + GetUniqueTosaMappingID();
20
21 // If a layer is present then the block will be used for execution, so input and output names need to be determined
22 // using the previous and following layers so the graph is connected correctly. For validation this doesn't matter.
23 if(layer != nullptr)
24 {
25 input0Name = GenerateUniqueInputName(layer->GetInputSlot(0));
26 outputName = GenerateUniqueOutputName(*layer);
27 }
28
29 std::vector<TosaSerializationTensor*> tensors;
30 std::vector<TosaSerializationOperator*> operators;
31
32 // Setup input tensor
33 // Only add tensor if connected layer is an input layer.
34 // As intermediate or constant tensors will be created separately.
35 // There also can't be duplicate tensors.
36 if(input0Name.find("input_") != std::string::npos)
37 {
38 std::vector<int32_t> inputShape0 = GetTosaTensorShape(inputs[0]->GetShape());
39 DType inputDType0 = ArmNNToDType(inputs[0]->GetDataType());
40
41 tensors.push_back(new TosaSerializationTensor(input0Name, inputShape0, inputDType0, {}));
42 }
43
44 // Setup weights tensor, constant data will get copied during SetConstantTensorData
45 operators.push_back(new TosaSerializationOperator(Op_CONST, Attribute_NONE, nullptr, {}, {input1Name}));
46
47 // During validation the TensorInfo can be retrieved from the inputs.
48 // During execution, it is only available through the layer so use m_Weight.
49 if(layer == nullptr)
50 {
51 std::vector<int32_t> inputShape1 = GetTosaTensorShape(inputs[1]->GetShape());
52 DType inputDType1 = ArmNNToDType(inputs[1]->GetDataType());
53
54 tensors.push_back(new TosaSerializationTensor(input1Name, inputShape1, inputDType1, {}));
55 }
56 else
57 {
58 auto transposeConv2dLayer = PolymorphicDowncast<const TransposeConvolution2dLayer*>(layer);
59
60 std::vector<int32_t> inputShape1 = GetTosaTensorShape(
61 transposeConv2dLayer->m_Weight->GetTensorInfo().GetShape());
62 DType inputDType1 = ArmNNToDType(transposeConv2dLayer->m_Weight->GetTensorInfo().GetDataType());
63
64 std::vector<uint8_t> uint8Data = ConvertConstantTensorDataToBuffer(transposeConv2dLayer->m_Weight);
65 tensors.push_back(new TosaSerializationTensor(input1Name, inputShape1, inputDType1, uint8Data));
66 }
67
68 // Setup bias operator and tensor, constant data will get copied during SetConstantTensorData
69 operators.push_back(new TosaSerializationOperator(Op_CONST, Attribute_NONE, nullptr, {}, {input2Name}));
70
71 // During validation the TensorInfo can be retrieved from the inputs.
72 // During execution, it is only available through the layer so use m_Bias.
73 if(layer == nullptr && descriptor->m_BiasEnabled)
74 {
75 std::vector<int32_t> inputShape2 = GetTosaTensorShape(inputs[2]->GetShape());
76 DType inputDType2 = ArmNNToDType(inputs[2]->GetDataType());
77
78 tensors.push_back(new TosaSerializationTensor(input2Name, inputShape2, inputDType2, {}));
79 }
80 else if(descriptor->m_BiasEnabled)
81 {
82 auto transposeConv2dLayer = PolymorphicDowncast<const TransposeConvolution2dLayer*>(layer);
83
84 std::vector<int32_t> inputShape2 = GetTosaTensorShape(
85 transposeConv2dLayer->m_Bias->GetTensorInfo().GetShape());
86 DType inputDType2 = ArmNNToDType(transposeConv2dLayer->m_Bias->GetTensorInfo().GetDataType());
87
88 std::vector<uint8_t> uint8Data = ConvertConstantTensorDataToBuffer(transposeConv2dLayer->m_Bias);
89 tensors.push_back(new TosaSerializationTensor(input2Name, inputShape2, inputDType2, uint8Data));
90 }
91 else
92 {
93 // If bias is disabled, create a constant bias tensor of 0's as three inputs are required.
94 // The size of the bias must match the channels dimension, so get the correct index.
95 unsigned int index = (descriptor->m_DataLayout == DataLayout::NHWC) ? 3 : 1;
96
97 std::vector<uint8_t> uint8Data;
98 std::vector<float> data(outputs[0]->GetShape()[index], 0.0f);
99
100 TosaSerializationHandler::ConvertF32toU8(data, uint8Data);
101
102 tensors.push_back(new TosaSerializationTensor(input2Name,
103 {static_cast<int32_t>(outputs[0]->GetShape()[index])},
104 DType_FP32,
105 uint8Data));
106 }
107
108 // Setup Output Tensor
109 std::vector<int32_t> outputShape0 = GetTosaTensorShape(outputs[0]->GetShape());
110 DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType());
111
112 tensors.push_back(new TosaSerializationTensor(outputName, outputShape0, outputDType0, {}));
113
114 // Set up TRANSPOSE_CONV2D operator
115 // The TOSA Reference Model pads the output shape, so it is added to output shape.
116 // In Arm NN we pad the input shape, so it is taken away.
117 // To offset this the negative padding value can be used.
118 std::vector<int> pad = {-static_cast<int>(descriptor->m_PadTop),
119 -static_cast<int>(descriptor->m_PadBottom),
120 -static_cast<int>(descriptor->m_PadLeft),
121 -static_cast<int>(descriptor->m_PadRight)};
122 std::vector<int> stride = {static_cast<int>(descriptor->m_StrideY),
123 static_cast<int>(descriptor->m_StrideX)};
124
125 std::vector<int> outputShape;
126 // If available use shape in descriptor otherwise use output shape.
127 if (descriptor->m_OutputShape.size() == 4)
128 {
129 for (uint32_t i = 0; i < descriptor->m_OutputShape.size(); ++i)
130 {
131 outputShape.push_back(static_cast<int>(descriptor->m_OutputShape[i]));
132 }
133 }
134 else
135 {
136 for (uint32_t i = 0; i < outputs[0]->GetNumDimensions(); ++i)
137 {
138 outputShape.push_back(static_cast<int>(outputs[0]->GetShape()[i]));
139 }
140 }
141
142 TosaTransposeConvAttribute attribute(pad, stride, outputShape, 0, 0, false); // input_zp, weight_zp, local_bound
143
144 auto* op = new TosaSerializationOperator(Op_TRANSPOSE_CONV2D,
145 Attribute_TransposeConvAttribute,
146 &attribute,
147 {input0Name, input1Name, input2Name},
148 {outputName});
149 operators.push_back(op);
150
151 // operatorInputNames/operatorOutputNames ends up being the same as
152 // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings
153 return new TosaSerializationBasicBlock(blockName, // name
154 mainName, // region name
155 operators, // operators
156 tensors, // tensors
157 {input0Name, input1Name, input2Name}, // inputs
158 {outputName}); // outputs
159}
std::string GenerateUniqueOutputName(const Layer &layer, uint32_t layerSlot=0)
const std::string mainName
std::vector< uint8_t > ConvertConstantTensorDataToBuffer(const std::shared_ptr< ConstTensorHandle > &tensorHandle)
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
DestType PolymorphicDowncast(SourceType *value)
Polymorphic downcast for build in pointers only.
uint32_t m_PadRight
Padding right value in the width dimension.
uint32_t m_PadTop
Padding top value in the height dimension.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
std::vector< unsigned int > m_OutputShape
uint32_t m_PadBottom
Padding bottom value in the height dimension.
uint32_t m_PadLeft
Padding left value in the width dimension.
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.

References ArmNNToDType(), ConvertConstantTensorDataToBuffer(), GenerateUniqueInputName(), GenerateUniqueOutputName(), Layer::GetInputSlot(), GetTosaTensorShape(), GetUniqueTosaMappingID(), TransposeConvolution2dDescriptor::m_BiasEnabled, TransposeConvolution2dDescriptor::m_DataLayout, TransposeConvolution2dDescriptor::m_OutputShape, TransposeConvolution2dDescriptor::m_PadBottom, TransposeConvolution2dDescriptor::m_PadLeft, TransposeConvolution2dDescriptor::m_PadRight, TransposeConvolution2dDescriptor::m_PadTop, TransposeConvolution2dDescriptor::m_StrideX, TransposeConvolution2dDescriptor::m_StrideY, mainName, and armnn::PolymorphicDowncast().

Referenced by GetTosaMapping().