ArmNN
 25.11
Loading...
Searching...
No Matches
DepthwiseConv2dOperator.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
8#include <ResolveType.hpp>
9
10TosaSerializationBasicBlock* ConvertDepthwiseConv2dToTosaOperator(
11 const Layer* layer,
12 const std::vector<const TensorInfo*>& inputs,
13 const std::vector<const TensorInfo*>& outputs,
14 const DepthwiseConvolution2dDescriptor* conv2dDescriptor)
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 std::vector<int32_t> inputShape0 = GetTosaTensorShape(inputs[0]->GetShape());
57 if(inputNames[0].find("input_") != std::string::npos)
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 std::vector<int32_t> inputShape1 = GetTosaTensorShape(inputs[1]->GetShape());
65 if(!inputs[1]->IsConstant() || layer == nullptr)
66 {
67 int32_t multiplier = inputShape1[3]/inputShape0[3];
68
69 // TOSA requires depthwise conv2d kernel to be converted from from [1, H, W, C * M] to [H, W, C, M]
70 std::vector<int32_t> inputShapeHWCM = {
71 inputShape1[1], inputShape1[2], inputShape0[3], multiplier
72 };
73
74 DType inputDType1 = ArmNNToDType(inputs[1]->GetDataType());
75
76 tensors.push_back(new TosaSerializationTensor(inputNames[1], inputShapeHWCM, inputDType1, {}));
77 }
78
79 if(conv2dDescriptor->m_BiasEnabled)
80 {
81 if(!inputs[2]->IsConstant() || layer == nullptr)
82 {
83 std::vector<int32_t> inputShape2 = GetTosaTensorShape(inputs[2]->GetShape());
84 DType inputDType2 = ArmNNToDType(inputs[2]->GetDataType());
85
86 tensors.push_back(new TosaSerializationTensor(inputNames[2], inputShape2, inputDType2, {}));
87 }
88 }
89 else
90 {
91 // If bias is disabled, create a constant bias of 0 as three inputs are required.
92 std::string constantName = std::string("constant_") + GetUniqueTosaMappingID();
93
94 operators.push_back(new TosaSerializationOperator(Op_CONST, Attribute_NONE, nullptr, {}, {constantName}));
95
96 // The size of the bias must match the channels dimension, so get the correct index.
97 unsigned int index = (conv2dDescriptor->m_DataLayout == DataLayout::NHWC) ? 3 : 1;
98
99 const DType dType = (inputDType0 == DType_INT8) ? DType_INT32 : outputDType0;
100 std::vector<float> data(outputs[0]->GetShape()[index], 0);
101
102 std::vector<uint8_t> uint8Data;
103 TosaSerializationHandler::ConvertF32toU8(data, uint8Data);
104
105 tensors.push_back(new TosaSerializationTensor(constantName,
106 {static_cast<int32_t>(outputs[0]->GetShape()[index])},
107 dType,
108 uint8Data));
109 inputNames.emplace_back(constantName);
110 }
111
112 // Setup Output Tensor
113 std::vector<int32_t> outputShape0 = {GetTosaTensorShape(outputs[0]->GetShape())};
114 std::string outputConv2dName;
115 bool isInputInt8 = (inputDType0 == DType_INT8);
116 if (isInputInt8)
117 {
118 outputConv2dName = std::string("layer_intermediate0_") + GetUniqueTosaMappingID();
119 tensors.push_back(new TosaSerializationTensor(outputConv2dName, outputShape0, DType_INT32, {}));
120 }
121 else
122 {
123 tensors.push_back(new TosaSerializationTensor(outputName, outputShape0, outputDType0, {}));
124 }
125
126 // Set up CONV2D operator
127 std::vector<int> pad = {static_cast<int>(conv2dDescriptor->m_PadTop),
128 static_cast<int>(conv2dDescriptor->m_PadBottom),
129 static_cast<int>(conv2dDescriptor->m_PadLeft),
130 static_cast<int>(conv2dDescriptor->m_PadRight)};
131 std::vector<int> stride = {static_cast<int>(conv2dDescriptor->m_StrideY),
132 static_cast<int>(conv2dDescriptor->m_StrideX)};
133 std::vector<int> dilation = {static_cast<int>(conv2dDescriptor->m_DilationY),
134 static_cast<int>(conv2dDescriptor->m_DilationX)};
135 TosaConvAttribute attribute(pad, stride, dilation,
136 inputs[0]->GetQuantizationOffset(), // input_zp
137 inputs[1]->GetQuantizationOffset(), // weight_zp
138 false); // local_bound
139
140 std::string sliceOutputName = GetInputSlicedToItsUsedSize(inputShape0,
141 inputNames[0],
142 conv2dDescriptor->m_DataLayout,
143 inputDType0,
144 inputShape1,
145 pad,
146 stride,
147 dilation,
148 tensors,
149 operators);
150
151 std::string& convOutStr = isInputInt8 ? outputConv2dName : outputName;
152 auto* conv2d_op = new TosaSerializationOperator(Op_DEPTHWISE_CONV2D,
153 Attribute_ConvAttribute,
154 &attribute,
155 {sliceOutputName, inputNames[1], inputNames[2]},
156 {convOutStr});
157 operators.push_back(conv2d_op);
158
159 if (isInputInt8)
160 {
161 int32_t output_zp = outputs[0]->GetQuantizationOffset();
162 double output_scale = outputs[0]->GetQuantizationScales()[0];
163 double input_scale = inputs[0]->GetQuantizationScales()[0];
164 const std::vector<float>& weight_scales = inputs[1]->GetQuantizationScales();
165
166 TosaSerializationOperator* rescaleOp = nullptr;
168 outputName,
169 0,
170 output_zp,
171 false,
172 false,
173 true,
174 true,
175 input_scale,
176 output_scale,
177 weight_scales,
178 &rescaleOp);
179 operators.push_back(rescaleOp);
180 tensors.push_back(new TosaSerializationTensor(outputName,
181 outputShape0,
182 DType_INT8, {}));
183 }
184
185 // operatorInputNames/operatorOutputNames ends up being the same as
186 // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings
187 return new TosaSerializationBasicBlock(blockName, // name
188 mainName, // region name
189 operators, // operators
190 tensors, // tensors
191 inputNames, // inputs
192 {outputName}); // outputs
193}
TosaSerializationBasicBlock * ConvertDepthwiseConv2dToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const DepthwiseConvolution2dDescriptor *conv2dDescriptor)
std::string GenerateUniqueOutputName(const Layer &layer, uint32_t layerSlot=0)
const std::string mainName
DType ArmNNToDType(const DataType &type)
std::string GenerateUniqueInputName(const armnn::InputSlot &slot)
std::string GetInputSlicedToItsUsedSize(const std::vector< int32_t > &inputShape, const std::string &inputName, const DataLayout layout, const DType datatype, const std::vector< int32_t > &kernel, const std::vector< int32_t > &pad, const std::vector< int32_t > &stride, const std::vector< int32_t > &dilations, std::vector< TosaSerializationTensor * > &tensors, std::vector< TosaSerializationOperator * > &operators, const bool isPoolingOp=false)
std::string GetUniqueTosaMappingID()
std::vector< int32_t > GetTosaTensorShape(const TensorShape &shape)
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)
Creates a TOSA rescale operator for weight tensors.
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition Layer.hpp:337
A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.
uint32_t m_PadRight
Padding right value in the width dimension.
uint32_t m_DilationY
Dilation factor value for height dimension.
uint32_t m_PadTop
Padding top value in the height dimension.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
uint32_t m_DilationX
Dilation factor value for width dimension.
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.