ArmNN
 25.02
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
16 TosaSerializationBasicBlock* 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  if(inputNames[0].find("input_") != std::string::npos)
68  {
69  std::vector<int32_t> inputShape0 = GetTosaTensorShape(inputs[0]->GetShape());
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  if(!inputs[1]->IsConstant() || layer == nullptr)
76  {
77  std::vector<int32_t> inputShape1 = GetTosaTensorShape(inputs[1]->GetShape());
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("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("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  TosaConvAttribute attribute(pad, stride, dilation,
173  inputs[0]->GetQuantizationOffset(), // input_zp
174  inputs[1]->GetQuantizationOffset(), // weight_zp
175  false); // local_bound
176 
177  std::string& convOutStr = isInputInt8 ? outputConv3dName : outputName;
178  auto* conv3d_op = new TosaSerializationOperator(Op_CONV3D,
179  Attribute_ConvAttribute,
180  &attribute,
181  {conv3dInput, conv3dWeight, conv3dBias},
182  {convOutStr});
183  operators.push_back(conv3d_op);
184 
185  if (isInputInt8)
186  {
187  int32_t output_zp = outputs[0]->GetQuantizationOffset();
188  double output_scale = outputs[0]->GetQuantizationScales()[0];
189  double input_scale = inputs[0]->GetQuantizationScales()[0];
190  const std::vector<float>& weight_scales = inputs[1]->GetQuantizationScales();
191 
192  TosaSerializationOperator* rescaleOp = nullptr;
193  CreateRescaleTosaOperatorForWeights(outputConv3dName,
194  outputName,
195  0,
196  output_zp,
197  false,
198  false,
199  true,
200  true,
201  input_scale,
202  output_scale,
203  weight_scales,
204  &rescaleOp);
205  operators.push_back(rescaleOp);
206  tensors.push_back(new TosaSerializationTensor(outputName,
207  outputShape0,
208  DType_INT8, {}));
209  }
210 
211  return new TosaSerializationBasicBlock(blockName, // name
212  mainName, // region name
213  operators, // operators
214  tensors, // tensors
215  inputNames, // inputs
216  {outputName});
217 }
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::vector< int32_t > GetTosaTensorShape(const TensorShape &shape)
std::string GenerateUniqueInputName(const armnn::InputSlot &slot)
std::string GetUniqueTosaMappingID()
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)
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.