ArmNN
 24.08
DepthwiseConv2dOperator.cpp File Reference
Include dependency graph for DepthwiseConv2dOperator.cpp:

Go to the source code of this file.

Functions

TosaSerializationBasicBlock * ConvertDepthwiseConv2dToTosaOperator (const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const DepthwiseConvolution2dDescriptor *conv2dDescriptor)
 

Function Documentation

◆ ConvertDepthwiseConv2dToTosaOperator()

TosaSerializationBasicBlock* ConvertDepthwiseConv2dToTosaOperator ( const Layer layer,
const std::vector< const TensorInfo * > &  inputs,
const std::vector< const TensorInfo * > &  outputs,
const DepthwiseConvolution2dDescriptor conv2dDescriptor 
)

Definition at line 10 of file DepthwiseConv2dOperator.cpp.

15 {
16  std::vector<std::string> inputNames;
17  std::string outputName = std::string("output0_");
18  std::string blockName = std::string("Op_DEPTHWISE_CONV2D_block_") + GetUniqueTosaMappingID();
19 
20  DType inputDType0 = ArmNNToDType(inputs[0]->GetDataType());
21  DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType());
22 
23  // Set input names for validation purposes only.
24  if(layer == nullptr)
25  {
26  inputNames.emplace_back("input_0");
27  inputNames.emplace_back("input_1");
28  if(conv2dDescriptor->m_BiasEnabled)
29  {
30  inputNames.emplace_back("input_2");
31  }
32  }
33  // If a layer is present then the block will be used for execution, so input and output names need to be
34  // determined using the previous and following layers so the graph is connected correctly.
35  // For validation this doesn't matter.
36  else
37  {
38  // Get the layer connected to the input slot and determine unique tensor names.
39  for (uint32_t i = 0; i < inputs.size(); ++i)
40  {
41  std::string inputName = GenerateUniqueInputName(layer->GetInputSlot(i));
42  inputNames.push_back(inputName);
43  }
44 
45  // Determine unique output tensor name.
46  outputName = GenerateUniqueOutputName(*layer);
47  }
48 
49  std::vector<TosaSerializationTensor*> tensors;
50  std::vector<TosaSerializationOperator*> operators;
51 
52  // Setup input Tensor
53  // Only add tensor if connected layer is an input layer.
54  // As intermediate or constant tensors will be created separately.
55  // There also can't be duplicate tensors.
56  if(inputNames[0].find("input_") != std::string::npos)
57  {
58  std::vector<int32_t> inputShape0 = GetTosaTensorShape(inputs[0]->GetShape());
59  tensors.push_back(new TosaSerializationTensor(inputNames[0], inputShape0, inputDType0, {}));
60  }
61 
62  // Only add input tensors if weights and bias are not constant or if running validation.
63  // Constant tensors will be created in the ConvertConstantToTosaOperator function.
64  if(!inputs[1]->IsConstant() || layer == nullptr)
65  {
66  std::vector<int32_t> inputShape0 = GetTosaTensorShape(inputs[0]->GetShape());
67  std::vector<int32_t> inputShape1 = GetTosaTensorShape(inputs[1]->GetShape());
68 
69  int32_t multiplier = inputShape1[3]/inputShape0[3];
70 
71  // TOSA requires depthwise conv2d kernel to be converted from from [1, H, W, C * M] to [H, W, C, M]
72  std::vector<int32_t> inputShapeHWCM = {
73  inputShape1[1], inputShape1[2], inputShape0[3], multiplier
74  };
75 
76  DType inputDType1 = ArmNNToDType(inputs[1]->GetDataType());
77 
78  tensors.push_back(new TosaSerializationTensor(inputNames[1], inputShapeHWCM, inputDType1, {}));
79  }
80 
81  if(conv2dDescriptor->m_BiasEnabled)
82  {
83  if(!inputs[2]->IsConstant() || layer == nullptr)
84  {
85  std::vector<int32_t> inputShape2 = GetTosaTensorShape(inputs[2]->GetShape());
86  DType inputDType2 = ArmNNToDType(inputs[2]->GetDataType());
87 
88  tensors.push_back(new TosaSerializationTensor(inputNames[2], inputShape2, inputDType2, {}));
89  }
90  }
91  else
92  {
93  // If bias is disabled, create a constant bias of 0 as three inputs are required.
94  std::string constantName = std::string("constant_") + GetUniqueTosaMappingID();
95 
96  operators.push_back(new TosaSerializationOperator(Op_CONST, Attribute_NONE, nullptr, {}, {constantName}));
97 
98  // The size of the bias must match the channels dimension, so get the correct index.
99  unsigned int index = (conv2dDescriptor->m_DataLayout == DataLayout::NHWC) ? 3 : 1;
100 
101  const DType dType = (inputDType0 == DType_INT8) ? DType_INT32 : outputDType0;
102  std::vector<float> data(outputs[0]->GetShape()[index], 0);
103 
104  std::vector<uint8_t> uint8Data;
105  TosaSerializationHandler::ConvertF32toU8(data, uint8Data);
106 
107  tensors.push_back(new TosaSerializationTensor(constantName,
108  {static_cast<int32_t>(outputs[0]->GetShape()[index])},
109  dType,
110  uint8Data));
111  inputNames.emplace_back(constantName);
112  }
113 
114  // Setup Output Tensor
115  std::vector<int32_t> outputShape0 = {GetTosaTensorShape(outputs[0]->GetShape())};
116  std::string outputConv2dName;
117  bool isInputInt8 = (inputDType0 == DType_INT8);
118  if (isInputInt8)
119  {
120  outputConv2dName = std::string("intermediate0_") + GetUniqueTosaMappingID();
121  tensors.push_back(new TosaSerializationTensor(outputConv2dName, outputShape0, DType_INT32, {}));
122  }
123  else
124  {
125  tensors.push_back(new TosaSerializationTensor(outputName, outputShape0, outputDType0, {}));
126  }
127 
128  // Set up CONV2D operator
129  std::vector<int> pad = {static_cast<int>(conv2dDescriptor->m_PadTop),
130  static_cast<int>(conv2dDescriptor->m_PadBottom),
131  static_cast<int>(conv2dDescriptor->m_PadLeft),
132  static_cast<int>(conv2dDescriptor->m_PadRight)};
133  std::vector<int> stride = {static_cast<int>(conv2dDescriptor->m_StrideY),
134  static_cast<int>(conv2dDescriptor->m_StrideX)};
135  std::vector<int> dilation = {static_cast<int>(conv2dDescriptor->m_DilationY),
136  static_cast<int>(conv2dDescriptor->m_DilationX)};
137  TosaConvAttribute attribute(pad, stride, dilation,
138  inputs[0]->GetQuantizationOffset(), // input_zp
139  inputs[1]->GetQuantizationOffset(), // weight_zp
140  false); // local_bound
141 
142  std::string& convOutStr = isInputInt8 ? outputConv2dName : outputName;
143  auto* conv2d_op = new TosaSerializationOperator(Op_DEPTHWISE_CONV2D,
144  Attribute_ConvAttribute,
145  &attribute,
146  inputNames,
147  {convOutStr});
148  operators.push_back(conv2d_op);
149 
150  if (isInputInt8)
151  {
152  int32_t output_zp = outputs[0]->GetQuantizationOffset();
153  double output_scale = outputs[0]->GetQuantizationScales()[0];
154  double input_scale = inputs[0]->GetQuantizationScales()[0];
155  const std::vector<float>& weight_scales = inputs[1]->GetQuantizationScales();
156 
157  TosaSerializationOperator* rescaleOp = nullptr;
158  CreateRescaleTosaOperatorPerChannel(outputConv2dName,
159  outputName,
160  0,
161  output_zp,
162  true,
163  true,
164  input_scale,
165  output_scale,
166  weight_scales,
167  &rescaleOp);
168  operators.push_back(rescaleOp);
169  tensors.push_back(new TosaSerializationTensor(outputName,
170  outputShape0,
171  DType_INT8, {}));
172  }
173 
174  // operatorInputNames/operatorOutputNames ends up being the same as
175  // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings
176  return new TosaSerializationBasicBlock(blockName, // name
177  mainName, // region name
178  operators, // operators
179  tensors, // tensors
180  inputNames, // inputs
181  {outputName}); // outputs
182 }

References ArmNNToDType(), GenerateUniqueInputName(), GenerateUniqueOutputName(), Layer::GetInputSlot(), GetTosaTensorShape(), GetUniqueTosaMappingID(), and DepthwiseConvolution2dDescriptor::m_BiasEnabled.

Referenced by GetTosaMapping().

armnn::DepthwiseConvolution2dDescriptor::m_BiasEnabled
bool m_BiasEnabled
Enable/disable bias.
Definition: Descriptors.hpp:708
armnn::DepthwiseConvolution2dDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:710
armnn::DepthwiseConvolution2dDescriptor::m_PadLeft
uint32_t m_PadLeft
Padding left value in the width dimension.
Definition: Descriptors.hpp:692
GenerateUniqueOutputName
std::string GenerateUniqueOutputName(const Layer &layer, uint32_t layerSlot=0)
Definition: TosaOperatorUtils.hpp:120
armnn::DepthwiseConvolution2dDescriptor::m_StrideY
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
Definition: Descriptors.hpp:702
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
armnn::DepthwiseConvolution2dDescriptor::m_DilationY
uint32_t m_DilationY
Dilation factor value for height dimension.
Definition: Descriptors.hpp:706
ArmNNToDType
DType ArmNNToDType(const DataType &type)
Definition: TosaOperatorUtils.hpp:22
armnn::DepthwiseConvolution2dDescriptor::m_PadRight
uint32_t m_PadRight
Padding right value in the width dimension.
Definition: Descriptors.hpp:694
armnn::DepthwiseConvolution2dDescriptor::m_PadBottom
uint32_t m_PadBottom
Padding bottom value in the height dimension.
Definition: Descriptors.hpp:698
CreateRescaleTosaOperatorPerChannel
void CreateRescaleTosaOperatorPerChannel(const std::string &inputName, const std::string &outputName, int32_t input_zp, int32_t output_zp, bool double_round, bool scale32, double input_scale, double output_scale, const std::vector< float > &weight_scales, TosaSerializationOperator **op)
Definition: TosaRescaleOperatorUtils.hpp:176
GetTosaTensorShape
std::vector< int32_t > GetTosaTensorShape(const TensorShape &shape)
Definition: TosaOperatorUtils.hpp:79
GenerateUniqueInputName
std::string GenerateUniqueInputName(const armnn::InputSlot &slot)
Definition: TosaOperatorUtils.hpp:109
armnn::DepthwiseConvolution2dDescriptor::m_DilationX
uint32_t m_DilationX
Dilation factor value for width dimension.
Definition: Descriptors.hpp:704
GetUniqueTosaMappingID
std::string GetUniqueTosaMappingID()
Definition: TosaOperatorUtils.hpp:138
armnn::DepthwiseConvolution2dDescriptor::m_StrideX
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
Definition: Descriptors.hpp:700
armnn::DepthwiseConvolution2dDescriptor::m_PadTop
uint32_t m_PadTop
Padding top value in the height dimension.
Definition: Descriptors.hpp:696