blob: 96701d438435b2cdde21c95a546e7aaf321504e5 [file] [log] [blame]
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +00001//
Teresa Charlin571a4f72024-03-26 11:18:42 +00002// Copyright © 2022-2024 Arm Ltd and Contributors. All rights reserved.
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +00003// SPDX-License-Identifier: MIT
4//
5
6#include "Conv2dOperator.hpp"
John Mcloughlinceb44282024-04-23 16:47:04 +01007#include "TosaRescaleOperatorUtils.hpp"
8#include <ResolveType.hpp>
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +00009
10TosaSerializationBasicBlock* 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
John Mcloughlinceb44282024-04-23 16:47:04 +010019 DType inputDType0 = ArmNNToDType(inputs[0]->GetDataType());
20 DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType());
21
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000022 // Set input names for validation purposes only.
23 if(layer == nullptr)
24 {
25 inputNames.emplace_back("input0_");
26 inputNames.emplace_back("input1_");
27 if(conv2dDescriptor->m_BiasEnabled)
28 {
29 inputNames.emplace_back("input2_");
30 }
31 }
Kevin May5b58e312022-12-15 10:15:21 +000032 // 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.
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000035 else
36 {
Kevin May5b58e312022-12-15 10:15:21 +000037 // Get the layer connected to the input slot and determine unique tensor names.
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000038 for (uint32_t i = 0; i < inputs.size(); ++i)
39 {
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000040 Layer& connectedLayer = layer->GetInputSlot(i).GetConnectedOutputSlot()->GetOwningLayer();
41
42 std::string inputName = GenerateUniqueName(connectedLayer, i);
43 inputNames.push_back(inputName);
44 }
45
Kevin May5b58e312022-12-15 10:15:21 +000046 // Determine unique output tensor name.
Matthew Sloyanda6bf9e2022-12-14 10:16:27 +000047 outputName = GenerateUniqueOutputName(*layer, 0);
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000048 }
49
50 std::vector<TosaSerializationTensor*> tensors;
51 std::vector<TosaSerializationOperator*> operators;
52
53 // Setup input Tensor
Matthew Sloyanda6bf9e2022-12-14 10:16:27 +000054 // Only add tensor if connected layer is an input layer.
55 // As intermediate or constant tensors will be created separately.
56 // There also can't be duplicate tensors.
57 if(inputNames[0].find("input0_") != std::string::npos)
58 {
59 std::vector<int32_t> inputShape0 = GetTosaTensorShape(inputs[0]->GetShape());
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000060
Matthew Sloyanda6bf9e2022-12-14 10:16:27 +000061 tensors.push_back(new TosaSerializationTensor(inputNames[0], inputShape0, inputDType0, {}));
62 }
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000063
64 // Only add input tensors if weights and bias are not constant or if running validation.
65 // Constant tensors will be created in the ConvertConstantToTosaOperator function.
66 if(!inputs[1]->IsConstant() || layer == nullptr)
67 {
68 std::vector<int32_t> inputShape1 = GetTosaTensorShape(inputs[1]->GetShape());
69 DType inputDType1 = ArmNNToDType(inputs[1]->GetDataType());
70
71 tensors.push_back(new TosaSerializationTensor(inputNames[1], inputShape1, inputDType1, {}));
72 }
73
74 if(conv2dDescriptor->m_BiasEnabled)
75 {
76 if(!inputs[2]->IsConstant() || layer == nullptr)
77 {
78 std::vector<int32_t> inputShape2 = GetTosaTensorShape(inputs[2]->GetShape());
79 DType inputDType2 = ArmNNToDType(inputs[2]->GetDataType());
80
81 tensors.push_back(new TosaSerializationTensor(inputNames[2], inputShape2, inputDType2, {}));
82 }
83 }
84 else
85 {
86 // If bias is disabled, create a constant bias of 0 as three inputs are required.
87 std::string constantName = std::string("constant_") + GetUniqueTosaMappingID();
88
89 operators.push_back(new TosaSerializationOperator(Op_CONST, Attribute_NONE, nullptr, {}, {constantName}));
90
Matthew Sloyanda6bf9e2022-12-14 10:16:27 +000091 // The size of the bias must match the channels dimension, so get the correct index.
92 unsigned int index = (conv2dDescriptor->m_DataLayout == DataLayout::NHWC) ? 3 : 1;
93
John Mcloughlinceb44282024-04-23 16:47:04 +010094 const DType dType = (inputDType0 == DType_INT8) ? DType_INT32 : outputDType0;
95 std::vector<float> data(outputs[0]->GetShape()[index], 0);
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000096
John Mcloughlinceb44282024-04-23 16:47:04 +010097 std::vector<uint8_t> uint8Data;
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000098 TosaSerializationHandler::ConvertF32toU8(data, uint8Data);
99
Matthew Sloyanda6bf9e2022-12-14 10:16:27 +0000100 tensors.push_back(new TosaSerializationTensor(constantName,
101 {static_cast<int32_t>(outputs[0]->GetShape()[index])},
John Mcloughlinceb44282024-04-23 16:47:04 +0100102 dType,
Matthew Sloyanda6bf9e2022-12-14 10:16:27 +0000103 uint8Data));
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000104 inputNames.emplace_back(constantName);
105 }
106
107 // Setup Output Tensor
John Mcloughlinceb44282024-04-23 16:47:04 +0100108 std::vector<int32_t> outputShape0 = {GetTosaTensorShape(outputs[0]->GetShape())};
109 std::string outputConv2dName;
110 bool isInputInt8 = (inputDType0 == DType_INT8);
111 if (isInputInt8)
112 {
113 outputConv2dName = std::string("intermediate0_") + GetUniqueTosaMappingID();
114 tensors.push_back(new TosaSerializationTensor(outputConv2dName, outputShape0, DType_INT32, {}));
115 }
116 else
117 {
118 tensors.push_back(new TosaSerializationTensor(outputName, outputShape0, outputDType0, {}));
119 }
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000120
121 // Set up CONV2D operator
122 std::vector<int> pad = {static_cast<int>(conv2dDescriptor->m_PadTop),
123 static_cast<int>(conv2dDescriptor->m_PadBottom),
124 static_cast<int>(conv2dDescriptor->m_PadLeft),
125 static_cast<int>(conv2dDescriptor->m_PadRight)};
126 std::vector<int> stride = {static_cast<int>(conv2dDescriptor->m_StrideY),
127 static_cast<int>(conv2dDescriptor->m_StrideX)};
128 std::vector<int> dilation = {static_cast<int>(conv2dDescriptor->m_DilationY),
129 static_cast<int>(conv2dDescriptor->m_DilationX)};
John Mcloughlinceb44282024-04-23 16:47:04 +0100130 TosaConvAttribute attribute(pad, stride, dilation,
131 inputs[0]->GetQuantizationOffset(), // input_zp
132 inputs[1]->GetQuantizationOffset(), // weight_zp
133 false); // local_bound
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000134
John Mcloughlinceb44282024-04-23 16:47:04 +0100135 std::string& convOutStr = isInputInt8 ? outputConv2dName : outputName;
136 auto* conv2d_op = new TosaSerializationOperator(Op_CONV2D,
137 Attribute_ConvAttribute,
138 &attribute,
139 inputNames,
140 {convOutStr});
141 operators.push_back(conv2d_op);
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000142
John Mcloughlinceb44282024-04-23 16:47:04 +0100143 if (isInputInt8)
144 {
145 int32_t output_zp = outputs[0]->GetQuantizationOffset();
146 double output_scale = outputs[0]->GetQuantizationScales()[0];
147 double input_scale = inputs[0]->GetQuantizationScales()[0];
148 const std::vector<float>& weight_scales = inputs[1]->GetQuantizationScales();
149
150 TosaSerializationOperator* rescaleOp = nullptr;
John Mcloughlinceb44282024-04-23 16:47:04 +0100151 CreateRescaleTosaOperatorPerChannel(outputConv2dName,
152 outputName,
John Mcloughlinceb44282024-04-23 16:47:04 +0100153 0,
154 output_zp,
155 true,
156 true,
157 input_scale,
158 output_scale,
159 weight_scales,
Teresa Charlince48d1d2024-04-24 13:30:58 +0100160 &rescaleOp);
John Mcloughlinceb44282024-04-23 16:47:04 +0100161 operators.push_back(rescaleOp);
Teresa Charlince48d1d2024-04-24 13:30:58 +0100162 tensors.push_back(new TosaSerializationTensor(outputName,
163 outputShape0,
164 DType_INT8, {}));
John Mcloughlinceb44282024-04-23 16:47:04 +0100165 }
Teresa Charlince48d1d2024-04-24 13:30:58 +0100166
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000167 // operatorInputNames/operatorOutputNames ends up being the same as
168 // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings
169 return new TosaSerializationBasicBlock(blockName, // name
Narumol Prangnawaratad323af2023-09-29 17:00:38 +0100170 mainName, // region name
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000171 operators, // operators
172 tensors, // tensors
173 inputNames, // inputs
174 {outputName}); // outputs
175}