ArmNN
 24.08
Conv2dOperator.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2022-2024 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "Conv2dOperator.hpp"
8 #include <ResolveType.hpp>
9 
10 TosaSerializationBasicBlock* ConvertConv2dToTosaOperator(const Layer* layer,
11  const std::vector<const TensorInfo*>& inputs,
12  const std::vector<const TensorInfo*>& outputs,
13  const Convolution2dDescriptor* conv2dDescriptor)
14 {
15  std::vector<std::string> inputNames;
16  std::string outputName = std::string("output0_");
17  std::string blockName = std::string("Op_CONV2D_block_") + GetUniqueTosaMappingID();
18 
19  DType inputDType0 = ArmNNToDType(inputs[0]->GetDataType());
20  DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType());
21 
22  // Set input names for validation purposes only.
23  if(layer == nullptr)
24  {
25  inputNames.emplace_back("input_0");
26  inputNames.emplace_back("input_1");
27  if(conv2dDescriptor->m_BiasEnabled)
28  {
29  inputNames.emplace_back("input_2");
30  }
31  }
32  // If a layer is present then the block will be used for execution, so input and output names need to be
33  // determined using the previous and following layers so the graph is connected correctly.
34  // For validation this doesn't matter.
35  else
36  {
37  // Get the layer connected to the input slot and determine unique tensor names.
38  for (uint32_t i = 0; i < inputs.size(); ++i)
39  {
40  std::string inputName = GenerateUniqueInputName(layer->GetInputSlot(i));
41  inputNames.push_back(inputName);
42  }
43 
44  // Determine unique output tensor name.
45  outputName = GenerateUniqueOutputName(*layer);
46  }
47 
48  std::vector<TosaSerializationTensor*> tensors;
49  std::vector<TosaSerializationOperator*> operators;
50 
51  // Setup input Tensor
52  // Only add tensor if connected layer is an input layer.
53  // As intermediate or constant tensors will be created separately.
54  // There also can't be duplicate tensors.
55  if(inputNames[0].find("input_") != std::string::npos)
56  {
57  std::vector<int32_t> inputShape0 = GetTosaTensorShape(inputs[0]->GetShape());
58 
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> inputShape1 = GetTosaTensorShape(inputs[1]->GetShape());
67  DType inputDType1 = ArmNNToDType(inputs[1]->GetDataType());
68 
69  tensors.push_back(new TosaSerializationTensor(inputNames[1], inputShape1, inputDType1, {}));
70  }
71 
72  if(conv2dDescriptor->m_BiasEnabled)
73  {
74  if(!inputs[2]->IsConstant() || layer == nullptr)
75  {
76  std::vector<int32_t> inputShape2 = GetTosaTensorShape(inputs[2]->GetShape());
77  DType inputDType2 = ArmNNToDType(inputs[2]->GetDataType());
78 
79  tensors.push_back(new TosaSerializationTensor(inputNames[2], inputShape2, inputDType2, {}));
80  }
81  }
82  else
83  {
84  // If bias is disabled, create a constant bias of 0 as three inputs are required.
85  std::string constantName = std::string("constant_") + GetUniqueTosaMappingID();
86 
87  operators.push_back(new TosaSerializationOperator(Op_CONST, Attribute_NONE, nullptr, {}, {constantName}));
88 
89  // The size of the bias must match the channels dimension, so get the correct index.
90  unsigned int index = (conv2dDescriptor->m_DataLayout == DataLayout::NHWC) ? 3 : 1;
91 
92  const DType dType = (inputDType0 == DType_INT8) ? DType_INT32 : outputDType0;
93  std::vector<float> data(outputs[0]->GetShape()[index], 0);
94 
95  std::vector<uint8_t> uint8Data;
96  TosaSerializationHandler::ConvertF32toU8(data, uint8Data);
97 
98  tensors.push_back(new TosaSerializationTensor(constantName,
99  {static_cast<int32_t>(outputs[0]->GetShape()[index])},
100  dType,
101  uint8Data));
102  inputNames.emplace_back(constantName);
103  }
104 
105  // Setup Output Tensor
106  std::vector<int32_t> outputShape0 = {GetTosaTensorShape(outputs[0]->GetShape())};
107  std::string outputConv2dName;
108  bool isInputInt8 = (inputDType0 == DType_INT8);
109  if (isInputInt8)
110  {
111  outputConv2dName = std::string("intermediate0_") + GetUniqueTosaMappingID();
112  tensors.push_back(new TosaSerializationTensor(outputConv2dName, outputShape0, DType_INT32, {}));
113  }
114  else
115  {
116  tensors.push_back(new TosaSerializationTensor(outputName, outputShape0, outputDType0, {}));
117  }
118 
119  // Set up CONV2D operator
120  std::vector<int> pad = {static_cast<int>(conv2dDescriptor->m_PadTop),
121  static_cast<int>(conv2dDescriptor->m_PadBottom),
122  static_cast<int>(conv2dDescriptor->m_PadLeft),
123  static_cast<int>(conv2dDescriptor->m_PadRight)};
124  std::vector<int> stride = {static_cast<int>(conv2dDescriptor->m_StrideY),
125  static_cast<int>(conv2dDescriptor->m_StrideX)};
126  std::vector<int> dilation = {static_cast<int>(conv2dDescriptor->m_DilationY),
127  static_cast<int>(conv2dDescriptor->m_DilationX)};
128  TosaConvAttribute attribute(pad, stride, dilation,
129  inputs[0]->GetQuantizationOffset(), // input_zp
130  inputs[1]->GetQuantizationOffset(), // weight_zp
131  false); // local_bound
132 
133  std::string& convOutStr = isInputInt8 ? outputConv2dName : outputName;
134  auto* conv2d_op = new TosaSerializationOperator(Op_CONV2D,
135  Attribute_ConvAttribute,
136  &attribute,
137  inputNames,
138  {convOutStr});
139  operators.push_back(conv2d_op);
140 
141  if (isInputInt8)
142  {
143  int32_t output_zp = outputs[0]->GetQuantizationOffset();
144  double output_scale = outputs[0]->GetQuantizationScales()[0];
145  double input_scale = inputs[0]->GetQuantizationScales()[0];
146  const std::vector<float>& weight_scales = inputs[1]->GetQuantizationScales();
147 
148  TosaSerializationOperator* rescaleOp = nullptr;
149  CreateRescaleTosaOperatorPerChannel(outputConv2dName,
150  outputName,
151  0,
152  output_zp,
153  true,
154  true,
155  input_scale,
156  output_scale,
157  weight_scales,
158  &rescaleOp);
159  operators.push_back(rescaleOp);
160  tensors.push_back(new TosaSerializationTensor(outputName,
161  outputShape0,
162  DType_INT8, {}));
163  }
164 
165  // operatorInputNames/operatorOutputNames ends up being the same as
166  // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings
167  return new TosaSerializationBasicBlock(blockName, // name
168  mainName, // region name
169  operators, // operators
170  tensors, // tensors
171  inputNames, // inputs
172  {outputName}); // outputs
173 }
armnn::Convolution2dDescriptor::m_PadTop
uint32_t m_PadTop
Padding top value in the height dimension.
Definition: Descriptors.hpp:570
ConvertConv2dToTosaOperator
TosaSerializationBasicBlock * ConvertConv2dToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const Convolution2dDescriptor *conv2dDescriptor)
Definition: Conv2dOperator.cpp:10
TosaRescaleOperatorUtils.hpp
armnn::Convolution2dDescriptor::m_StrideY
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
Definition: Descriptors.hpp:576
GenerateUniqueOutputName
std::string GenerateUniqueOutputName(const Layer &layer, uint32_t layerSlot=0)
Definition: TosaOperatorUtils.hpp:120
ResolveType.hpp
armnn::Convolution2dDescriptor::m_PadLeft
uint32_t m_PadLeft
Padding left value in the width dimension.
Definition: Descriptors.hpp:566
armnn::Convolution2dDescriptor::m_DilationY
uint32_t m_DilationY
Dilation along y axis.
Definition: Descriptors.hpp:580
armnn::Layer::GetInputSlot
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:337
armnn::Layer
Definition: Layer.hpp:230
mainName
const std::string mainName
Definition: TosaOperatorUtils.hpp:19
ArmNNToDType
DType ArmNNToDType(const DataType &type)
Definition: TosaOperatorUtils.hpp:22
armnn::Convolution2dDescriptor::m_BiasEnabled
bool m_BiasEnabled
Enable/disable bias.
Definition: Descriptors.hpp:582
armnn::Convolution2dDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:584
Conv2dOperator.hpp
armnn::Convolution2dDescriptor::m_PadBottom
uint32_t m_PadBottom
Padding bottom value in the height dimension.
Definition: Descriptors.hpp:572
armnn::Convolution2dDescriptor::m_StrideX
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
Definition: Descriptors.hpp:574
armnn::Convolution2dDescriptor::m_PadRight
uint32_t m_PadRight
Padding right value in the width dimension.
Definition: Descriptors.hpp:568
armnn::Convolution2dDescriptor
A Convolution2dDescriptor for the Convolution2dLayer.
Definition: Descriptors.hpp:534
armnn::Convolution2dDescriptor::m_DilationX
uint32_t m_DilationX
Dilation along x axis.
Definition: Descriptors.hpp:578
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
GetUniqueTosaMappingID
std::string GetUniqueTosaMappingID()
Definition: TosaOperatorUtils.hpp:138