ArmNN
 25.11
Loading...
Searching...
No Matches
Conv3dOperator.cpp
Go to the documentation of this file.
1//
2// Copyright © 2024 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5//
6// Copyright © 2020 The TensorFlow Authors. All Rights Reserved.
7// SPDX-License-Identifier: Apache-2.0
8//
9
10#include "Conv3dOperator.hpp"
12#include <ResolveType.hpp>
13
14// This function is paraphrased from:
15// tensorflow/compiler/mlir/tosa/transforms/legalize_tfl.cc from function ConvertTFLConv3DOp
16TosaSerializationBasicBlock* ConvertConv3dToTosaOperator(const Layer* layer,
17 const std::vector<const TensorInfo*>& inputs,
18 const std::vector<const TensorInfo*>& outputs,
19 const Convolution3dDescriptor* conv3dDescriptor)
20{
21 // TOSA currently only supports NDHWC input
22 if (conv3dDescriptor->m_DataLayout != DataLayout::NDHWC)
23 {
24 throw InvalidArgumentException("Only NDHWC input is supported for Conv3D");
25 }
26
27 std::vector<std::string> inputNames;
28 std::string outputName = std::string("output0_");
29 std::string blockName = std::string("Op_CONV3D_block_") + GetUniqueTosaMappingID();
30
31 DType inputDType0 = ArmNNToDType(inputs[0]->GetDataType());
32 DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType());
33
34 // Set input names for validation purposes only.
35 if(layer == nullptr)
36 {
37 inputNames.emplace_back("input_0");
38 inputNames.emplace_back("input_1");
39 if(conv3dDescriptor->m_BiasEnabled)
40 {
41 inputNames.emplace_back("input_2");
42 }
43 }
44 // If a layer is present then the block will be used for execution, so input and output names need to be
45 // determined using the previous and following layers so the graph is connected correctly.
46 // For validation this doesn't matter.
47 else
48 {
49 // Get the layer connected to the input slot and determine unique tensor names.
50 for (uint32_t i = 0; i < inputs.size(); ++i)
51 {
52 std::string inputName = GenerateUniqueInputName(layer->GetInputSlot(i));
53 inputNames.push_back(inputName);
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
64 // Only add tensor if connected layer is an input layer.
65 // As intermediate or constant tensors will be created separately.
66 // There also can't be duplicate tensors.
67 std::vector<int32_t> inputShape0 = GetTosaTensorShape(inputs[0]->GetShape());
68 if(inputNames[0].find("input_") != std::string::npos)
69 {
70 tensors.push_back(new TosaSerializationTensor(inputNames[0], inputShape0, inputDType0, {}));
71 }
72
73 // Only add input tensors if weights and bias are not constant or if running validation.
74 // Constant tensors will be created in the ConvertConstantToTosaOperator function.
75 std::vector<int32_t> inputShape1 = GetTosaTensorShape(inputs[1]->GetShape());
76 if(!inputs[1]->IsConstant() || layer == nullptr)
77 {
78 DType inputDType1 = ArmNNToDType(inputs[1]->GetDataType());
79
80 tensors.push_back(new TosaSerializationTensor(inputNames[1], inputShape1, inputDType1, {}));
81 }
82
83 if(conv3dDescriptor->m_BiasEnabled)
84 {
85 if(!inputs[2]->IsConstant() || layer == nullptr)
86 {
87 std::vector<int32_t> inputShape2 = GetTosaTensorShape(inputs[2]->GetShape());
88 DType inputDType2 = ArmNNToDType(inputs[2]->GetDataType());
89
90 tensors.push_back(new TosaSerializationTensor(inputNames[2], inputShape2, inputDType2, {}));
91 }
92 }
93 else
94 {
95 // If bias is disabled, create a constant bias of 0 as three inputs are required.
96 std::string constantName = std::string("constant_") + GetUniqueTosaMappingID();
97
98 operators.push_back(new TosaSerializationOperator(Op_CONST, Attribute_NONE, nullptr, {}, {constantName}));
99
100 // The size of the bias must match the channels dimension, so get the correct index for NDHWC input.
101 unsigned int index = 4;
102
103 const DType dType = (inputDType0 == DType_INT8) ? DType_INT32 : outputDType0;
104 std::vector<float> data(outputs[0]->GetShape()[index], 0);
105
106 std::vector<uint8_t> uint8Data;
107 TosaSerializationHandler::ConvertF32toU8(data, uint8Data);
108
109 tensors.push_back(new TosaSerializationTensor(constantName,
110 {static_cast<int32_t>(outputs[0]->GetShape()[index])},
111 dType,
112 uint8Data));
113 inputNames.emplace_back(constantName);
114 }
115
116 // Setup Output Tensor
117 std::vector<int32_t> outputShape0 = {GetTosaTensorShape(outputs[0]->GetShape())};
118 std::string outputConv3dName;
119 bool isInputInt8 = (inputDType0 == DType_INT8);
120 if (isInputInt8)
121 {
122 outputConv3dName = std::string("layer_intermediate0_") + GetUniqueTosaMappingID();
123 tensors.push_back(new TosaSerializationTensor(outputConv3dName, outputShape0, DType_INT32, {}));
124 }
125 else
126 {
127 tensors.push_back(new TosaSerializationTensor(outputName, outputShape0, outputDType0, {}));
128 }
129
130 // Setup Transpose Output Tensor
131 auto transposeInputShape = GetTosaTensorShape(inputs[1]->GetShape());
132 std::vector<int32_t> transposeOutputShape = {transposeInputShape[4],
133 transposeInputShape[0],
134 transposeInputShape[1],
135 transposeInputShape[2],
136 transposeInputShape[3]};
137
138 std::string transposeOutputName = std::string("layer_intermediate1_") + GetUniqueTosaMappingID();
139 tensors.push_back(new TosaSerializationTensor(transposeOutputName, transposeOutputShape, inputDType0, {}));
140
141 // Connect the layer input to Transpose
142 std::string transposeInputName = inputNames[1];
143 std::string conv3dInput = inputNames[0];
144 std::string conv3dWeight = transposeOutputName;
145 std::string conv3dBias = inputNames[2];
146
147 // Set up TRANSPOSE operator for weight
148 // The weight shape of tflite conv3d is not NDHWC but DHWCN, so will need to transpose DHWCN to NDHWC
149 std::vector<int> perm = {4, 0, 1, 2, 3};
150 TosaTransposeAttribute transposeAttribute(perm);
151 auto transpose_op = new TosaSerializationOperator(Op_TRANSPOSE,
152 Attribute_TransposeAttribute,
153 &transposeAttribute,
154 {transposeInputName},
155 {transposeOutputName});
156 operators.push_back(transpose_op);
157
158 // Set up CONV3D operator
159 std::vector<int> pad = {static_cast<int>(conv3dDescriptor->m_PadFront),
160 static_cast<int>(conv3dDescriptor->m_PadBack),
161 static_cast<int>(conv3dDescriptor->m_PadTop),
162 static_cast<int>(conv3dDescriptor->m_PadBottom),
163 static_cast<int>(conv3dDescriptor->m_PadLeft),
164 static_cast<int>(conv3dDescriptor->m_PadRight)};
165 std::vector<int> stride = {static_cast<int>(conv3dDescriptor->m_StrideZ),
166 static_cast<int>(conv3dDescriptor->m_StrideY),
167 static_cast<int>(conv3dDescriptor->m_StrideX)};
168 std::vector<int> dilation = {static_cast<int>(conv3dDescriptor->m_DilationZ),
169 static_cast<int>(conv3dDescriptor->m_DilationY),
170 static_cast<int>(conv3dDescriptor->m_DilationX)};
171
172 std::string sliceOutputName = GetInputSlicedToItsUsedSize(inputShape0,
173 conv3dInput,
174 conv3dDescriptor->m_DataLayout,
175 inputDType0,
176 transposeOutputShape,
177 pad,
178 stride,
179 dilation,
180 tensors,
181 operators);
182
183 TosaConvAttribute attribute(pad, stride, dilation,
184 inputs[0]->GetQuantizationOffset(), // input_zp
185 inputs[1]->GetQuantizationOffset(), // weight_zp
186 false); // local_bound
187
188 std::string& convOutStr = isInputInt8 ? outputConv3dName : outputName;
189 auto* conv3d_op = new TosaSerializationOperator(Op_CONV3D,
190 Attribute_ConvAttribute,
191 &attribute,
192 {sliceOutputName, conv3dWeight, conv3dBias},
193 {convOutStr});
194 operators.push_back(conv3d_op);
195
196 if (isInputInt8)
197 {
198 int32_t output_zp = outputs[0]->GetQuantizationOffset();
199 double output_scale = outputs[0]->GetQuantizationScales()[0];
200 double input_scale = inputs[0]->GetQuantizationScales()[0];
201 const std::vector<float>& weight_scales = inputs[1]->GetQuantizationScales();
202
203 TosaSerializationOperator* rescaleOp = nullptr;
205 outputName,
206 0,
207 output_zp,
208 false,
209 false,
210 true,
211 true,
212 input_scale,
213 output_scale,
214 weight_scales,
215 &rescaleOp);
216 operators.push_back(rescaleOp);
217 tensors.push_back(new TosaSerializationTensor(outputName,
218 outputShape0,
219 DType_INT8, {}));
220 }
221
222 return new TosaSerializationBasicBlock(blockName, // name
223 mainName, // region name
224 operators, // operators
225 tensors, // tensors
226 inputNames, // inputs
227 {outputName});
228}
TosaSerializationBasicBlock * ConvertConv3dToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const Convolution3dDescriptor *conv3dDescriptor)
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 GetInputSlicedToItsUsedSize(const std::vector< int32_t > &inputShape, const std::string &inputName, const DataLayout layout, const DType datatype, const std::vector< int32_t > &kernel, const std::vector< int32_t > &pad, const std::vector< int32_t > &stride, const std::vector< int32_t > &dilations, std::vector< TosaSerializationTensor * > &tensors, std::vector< TosaSerializationOperator * > &operators, const bool isPoolingOp=false)
std::string GetUniqueTosaMappingID()
std::vector< int32_t > GetTosaTensorShape(const TensorShape &shape)
void CreateRescaleTosaOperatorForWeights(const std::string &inputName, const std::string &outputName, int32_t input_zp, int32_t output_zp, bool input_unsigned, bool output_unsigned, bool double_round, bool scale32, double input_scale, double output_scale, const std::vector< float > &weight_scales, TosaSerializationOperator **op)
Creates a TOSA rescale operator for weight tensors.
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition Layer.hpp:337
A Convolution3dDescriptor for the Convolution3dLayer.
uint32_t m_PadRight
Padding right value in the width dimension.
uint32_t m_PadBack
Padding back value in the depth dimension.
uint32_t m_DilationZ
Dilation along z axis.
uint32_t m_DilationY
Dilation along y axis.
uint32_t m_StrideZ
Stride value when proceeding through input for the depth dimension.
uint32_t m_PadTop
Padding top value in the height dimension.
DataLayout m_DataLayout
The data layout to be used (NDHWC, NCDHW).
uint32_t m_PadFront
Padding front value in the depth dimension.
uint32_t m_DilationX
Dilation along x axis.
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.
bool m_BiasEnabled
Enable/disable bias.
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.