ArmNN
 24.02
Conv2dOperator.hpp File Reference
Include dependency graph for Conv2dOperator.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

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

Function Documentation

◆ ConvertConv2dToTosaOperator()

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

Definition at line 8 of file Conv2dOperator.cpp.

12 {
13  std::vector<std::string> inputNames;
14  std::string outputName = std::string("output0_");
15  std::string blockName = std::string("Op_CONV2D_block_") + GetUniqueTosaMappingID();
16 
17  // Set input names for validation purposes only.
18  if(layer == nullptr)
19  {
20  inputNames.emplace_back("input0_");
21  inputNames.emplace_back("input1_");
22  if(conv2dDescriptor->m_BiasEnabled)
23  {
24  inputNames.emplace_back("input2_");
25  }
26  }
27  // If a layer is present then the block will be used for execution, so input and output names need to be
28  // determined using the previous and following layers so the graph is connected correctly.
29  // For validation this doesn't matter.
30  else
31  {
32  // Get the layer connected to the input slot and determine unique tensor names.
33  for (uint32_t i = 0; i < inputs.size(); ++i)
34  {
35  Layer& connectedLayer = layer->GetInputSlot(i).GetConnectedOutputSlot()->GetOwningLayer();
36 
37  std::string inputName = GenerateUniqueName(connectedLayer, i);
38  inputNames.push_back(inputName);
39  }
40 
41  // Determine unique output tensor name.
42  outputName = GenerateUniqueOutputName(*layer, 0);
43  }
44 
45  std::vector<TosaSerializationTensor*> tensors;
46  std::vector<TosaSerializationOperator*> operators;
47 
48  // Setup input Tensor
49  // Only add tensor if connected layer is an input layer.
50  // As intermediate or constant tensors will be created separately.
51  // There also can't be duplicate tensors.
52  if(inputNames[0].find("input0_") != std::string::npos)
53  {
54  std::vector<int32_t> inputShape0 = GetTosaTensorShape(inputs[0]->GetShape());
55  DType inputDType0 = ArmNNToDType(inputs[0]->GetDataType());
56 
57  tensors.push_back(new TosaSerializationTensor(inputNames[0], inputShape0, inputDType0, {}));
58  }
59 
60  // Only add input tensors if weights and bias are not constant or if running validation.
61  // Constant tensors will be created in the ConvertConstantToTosaOperator function.
62  if(!inputs[1]->IsConstant() || layer == nullptr)
63  {
64  std::vector<int32_t> inputShape1 = GetTosaTensorShape(inputs[1]->GetShape());
65  DType inputDType1 = ArmNNToDType(inputs[1]->GetDataType());
66 
67  tensors.push_back(new TosaSerializationTensor(inputNames[1], inputShape1, inputDType1, {}));
68  }
69 
70  if(conv2dDescriptor->m_BiasEnabled)
71  {
72  if(!inputs[2]->IsConstant() || layer == nullptr)
73  {
74  std::vector<int32_t> inputShape2 = GetTosaTensorShape(inputs[2]->GetShape());
75  DType inputDType2 = ArmNNToDType(inputs[2]->GetDataType());
76 
77  tensors.push_back(new TosaSerializationTensor(inputNames[2], inputShape2, inputDType2, {}));
78  }
79  }
80  else
81  {
82  // If bias is disabled, create a constant bias of 0 as three inputs are required.
83  std::string constantName = std::string("constant_") + GetUniqueTosaMappingID();
84 
85  operators.push_back(new TosaSerializationOperator(Op_CONST, Attribute_NONE, nullptr, {}, {constantName}));
86 
87  // The size of the bias must match the channels dimension, so get the correct index.
88  unsigned int index = (conv2dDescriptor->m_DataLayout == DataLayout::NHWC) ? 3 : 1;
89 
90  std::vector<uint8_t> uint8Data;
91  std::vector<float> data(outputs[0]->GetShape()[index], 0.0f);
92 
93  TosaSerializationHandler::ConvertF32toU8(data, uint8Data);
94 
95  tensors.push_back(new TosaSerializationTensor(constantName,
96  {static_cast<int32_t>(outputs[0]->GetShape()[index])},
97  DType_FP32,
98  uint8Data));
99  inputNames.emplace_back(constantName);
100  }
101 
102  // Setup Output Tensor
103  std::vector<int32_t> outputShape0 = GetTosaTensorShape(outputs[0]->GetShape());
104  DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType());
105 
106  tensors.push_back(new TosaSerializationTensor(outputName, outputShape0, outputDType0, {}));
107 
108  // Set up CONV2D operator
109  std::vector<int> pad = {static_cast<int>(conv2dDescriptor->m_PadTop),
110  static_cast<int>(conv2dDescriptor->m_PadBottom),
111  static_cast<int>(conv2dDescriptor->m_PadLeft),
112  static_cast<int>(conv2dDescriptor->m_PadRight)};
113  std::vector<int> stride = {static_cast<int>(conv2dDescriptor->m_StrideY),
114  static_cast<int>(conv2dDescriptor->m_StrideX)};
115  std::vector<int> dilation = {static_cast<int>(conv2dDescriptor->m_DilationY),
116  static_cast<int>(conv2dDescriptor->m_DilationX)};
117  TosaConvAttribute attribute(pad, stride, dilation, 0, 0);
118 
119  auto* op = new TosaSerializationOperator(Op_CONV2D,
120  Attribute_ConvAttribute,
121  &attribute,
122  inputNames,
123  {outputName});
124  operators.push_back(op);
125 
126  // operatorInputNames/operatorOutputNames ends up being the same as
127  // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings
128  return new TosaSerializationBasicBlock(blockName, // name
129  mainName, // region name
130  operators, // operators
131  tensors, // tensors
132  inputNames, // inputs
133  {outputName}); // outputs
134 }

References GenerateUniqueName(), GenerateUniqueOutputName(), InputSlot::GetConnectedOutputSlot(), Layer::GetInputSlot(), OutputSlot::GetOwningLayer(), GetTosaTensorShape(), GetUniqueTosaMappingID(), and Convolution2dDescriptor::m_BiasEnabled.

Referenced by GetTosaMapping().

armnn::Convolution2dDescriptor::m_PadTop
uint32_t m_PadTop
Padding top value in the height dimension.
Definition: Descriptors.hpp:570
armnn::Convolution2dDescriptor::m_StrideY
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
Definition: Descriptors.hpp:576
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
armnn::OutputSlot::GetOwningLayer
Layer & GetOwningLayer() const
Definition: Layer.hpp:132
ArmNNToDType
DType ArmNNToDType(const DataType &type)
Definition: TosaOperatorUtils.hpp:22
GenerateUniqueOutputName
std::string GenerateUniqueOutputName(const Layer &layer, uint32_t layerSlot)
Definition: TosaOperatorUtils.hpp:82
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
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::m_DilationX
uint32_t m_DilationX
Dilation along x axis.
Definition: Descriptors.hpp:578
GenerateUniqueName
std::string GenerateUniqueName(const Layer &layer, uint32_t layerSlot)
Definition: TosaOperatorUtils.hpp:63
GetTosaTensorShape
std::vector< int32_t > GetTosaTensorShape(const TensorShape &shape)
Definition: TosaOperatorUtils.hpp:52
armnn::InputSlot::GetConnectedOutputSlot
const OutputSlot * GetConnectedOutputSlot() const
Definition: Layer.hpp:56
GetUniqueTosaMappingID
std::string GetUniqueTosaMappingID()
Definition: TosaOperatorUtils.hpp:100