blob: c65f1891da993361982ba4f1fc56499c56ba2e0e [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"
7
8TosaSerializationBasicBlock* ConvertConv2dToTosaOperator(const Layer* layer,
9 const std::vector<const TensorInfo*>& inputs,
10 const std::vector<const TensorInfo*>& outputs,
11 const Convolution2dDescriptor* conv2dDescriptor)
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 }
Kevin May5b58e312022-12-15 10:15:21 +000027 // 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.
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000030 else
31 {
Kevin May5b58e312022-12-15 10:15:21 +000032 // Get the layer connected to the input slot and determine unique tensor names.
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000033 for (uint32_t i = 0; i < inputs.size(); ++i)
34 {
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000035 Layer& connectedLayer = layer->GetInputSlot(i).GetConnectedOutputSlot()->GetOwningLayer();
36
37 std::string inputName = GenerateUniqueName(connectedLayer, i);
38 inputNames.push_back(inputName);
39 }
40
Kevin May5b58e312022-12-15 10:15:21 +000041 // Determine unique output tensor name.
Matthew Sloyanda6bf9e2022-12-14 10:16:27 +000042 outputName = GenerateUniqueOutputName(*layer, 0);
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000043 }
44
45 std::vector<TosaSerializationTensor*> tensors;
46 std::vector<TosaSerializationOperator*> operators;
47
48 // Setup input Tensor
Matthew Sloyanda6bf9e2022-12-14 10:16:27 +000049 // 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());
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000056
Matthew Sloyanda6bf9e2022-12-14 10:16:27 +000057 tensors.push_back(new TosaSerializationTensor(inputNames[0], inputShape0, inputDType0, {}));
58 }
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000059
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
Matthew Sloyanda6bf9e2022-12-14 10:16:27 +000087 // 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
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000090 std::vector<uint8_t> uint8Data;
Matthew Sloyanda6bf9e2022-12-14 10:16:27 +000091 std::vector<float> data(outputs[0]->GetShape()[index], 0.0f);
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000092
93 TosaSerializationHandler::ConvertF32toU8(data, uint8Data);
94
Matthew Sloyanda6bf9e2022-12-14 10:16:27 +000095 tensors.push_back(new TosaSerializationTensor(constantName,
96 {static_cast<int32_t>(outputs[0]->GetShape()[index])},
97 DType_FP32,
98 uint8Data));
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000099 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)};
Teresa Charlin571a4f72024-03-26 11:18:42 +0000117 TosaConvAttribute attribute(pad, stride, dilation, 0, 0, false); // input_zp, weight_zp, local_bound
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000118
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
Narumol Prangnawaratad323af2023-09-29 17:00:38 +0100129 mainName, // region name
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000130 operators, // operators
131 tensors, // tensors
132 inputNames, // inputs
133 {outputName}); // outputs
134}