Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame^] | 1 | // |
| 2 | // Copyright © 2022 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "Conv2dOperator.hpp" |
| 7 | |
| 8 | TosaSerializationBasicBlock* 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 | } |
| 27 | else |
| 28 | { |
| 29 | // If a layer is present then the block will be used for execution, so input and output names need to be |
| 30 | // determined using the previous and following layers so the graph is connected correctly. |
| 31 | // For validation this doesn't matter. |
| 32 | for (uint32_t i = 0; i < inputs.size(); ++i) |
| 33 | { |
| 34 | // Get the layer connected to the input slot and determine unique layer name. |
| 35 | Layer& connectedLayer = layer->GetInputSlot(i).GetConnectedOutputSlot()->GetOwningLayer(); |
| 36 | |
| 37 | std::string inputName = GenerateUniqueName(connectedLayer, i); |
| 38 | inputNames.push_back(inputName); |
| 39 | } |
| 40 | |
| 41 | // Get the layer connected to the output slot and determine unique layer name. |
| 42 | Layer& connectedLayer = layer->GetOutputSlot().GetConnection(0)->GetOwningLayer(); |
| 43 | |
| 44 | outputName = GenerateUniqueName(connectedLayer, 0); |
| 45 | } |
| 46 | |
| 47 | std::vector<TosaSerializationTensor*> tensors; |
| 48 | std::vector<TosaSerializationOperator*> operators; |
| 49 | |
| 50 | // Setup input Tensor |
| 51 | std::vector<int32_t> inputShape0 = GetTosaTensorShape(inputs[0]->GetShape()); |
| 52 | DType inputDType0 = ArmNNToDType(inputs[0]->GetDataType()); |
| 53 | |
| 54 | tensors.push_back(new TosaSerializationTensor(inputNames[0], inputShape0, inputDType0, {})); |
| 55 | |
| 56 | // Only add input tensors if weights and bias are not constant or if running validation. |
| 57 | // Constant tensors will be created in the ConvertConstantToTosaOperator function. |
| 58 | if(!inputs[1]->IsConstant() || layer == nullptr) |
| 59 | { |
| 60 | std::vector<int32_t> inputShape1 = GetTosaTensorShape(inputs[1]->GetShape()); |
| 61 | DType inputDType1 = ArmNNToDType(inputs[1]->GetDataType()); |
| 62 | |
| 63 | tensors.push_back(new TosaSerializationTensor(inputNames[1], inputShape1, inputDType1, {})); |
| 64 | } |
| 65 | |
| 66 | if(conv2dDescriptor->m_BiasEnabled) |
| 67 | { |
| 68 | if(!inputs[2]->IsConstant() || layer == nullptr) |
| 69 | { |
| 70 | std::vector<int32_t> inputShape2 = GetTosaTensorShape(inputs[2]->GetShape()); |
| 71 | DType inputDType2 = ArmNNToDType(inputs[2]->GetDataType()); |
| 72 | |
| 73 | tensors.push_back(new TosaSerializationTensor(inputNames[2], inputShape2, inputDType2, {})); |
| 74 | } |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | // If bias is disabled, create a constant bias of 0 as three inputs are required. |
| 79 | std::string constantName = std::string("constant_") + GetUniqueTosaMappingID(); |
| 80 | |
| 81 | operators.push_back(new TosaSerializationOperator(Op_CONST, Attribute_NONE, nullptr, {}, {constantName})); |
| 82 | |
| 83 | std::vector<uint8_t> uint8Data; |
| 84 | std::vector<float> data = { 0.0 }; |
| 85 | |
| 86 | TosaSerializationHandler::ConvertF32toU8(data, uint8Data); |
| 87 | |
| 88 | tensors.push_back(new TosaSerializationTensor(constantName, {1}, DType_FP32, uint8Data)); |
| 89 | inputNames.emplace_back(constantName); |
| 90 | } |
| 91 | |
| 92 | // Setup Output Tensor |
| 93 | std::vector<int32_t> outputShape0 = GetTosaTensorShape(outputs[0]->GetShape()); |
| 94 | DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType()); |
| 95 | |
| 96 | tensors.push_back(new TosaSerializationTensor(outputName, outputShape0, outputDType0, {})); |
| 97 | |
| 98 | // Set up CONV2D operator |
| 99 | std::vector<int> pad = {static_cast<int>(conv2dDescriptor->m_PadTop), |
| 100 | static_cast<int>(conv2dDescriptor->m_PadBottom), |
| 101 | static_cast<int>(conv2dDescriptor->m_PadLeft), |
| 102 | static_cast<int>(conv2dDescriptor->m_PadRight)}; |
| 103 | std::vector<int> stride = {static_cast<int>(conv2dDescriptor->m_StrideY), |
| 104 | static_cast<int>(conv2dDescriptor->m_StrideX)}; |
| 105 | std::vector<int> dilation = {static_cast<int>(conv2dDescriptor->m_DilationY), |
| 106 | static_cast<int>(conv2dDescriptor->m_DilationX)}; |
| 107 | TosaConvAttribute attribute(pad, dilation, stride, 0, 0, ArmNNToDType(inputs[0]->GetDataType())); |
| 108 | |
| 109 | auto* op = new TosaSerializationOperator(Op_CONV2D, |
| 110 | Attribute_ConvAttribute, |
| 111 | &attribute, |
| 112 | inputNames, |
| 113 | {outputName}); |
| 114 | operators.push_back(op); |
| 115 | |
| 116 | // operatorInputNames/operatorOutputNames ends up being the same as |
| 117 | // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings |
| 118 | return new TosaSerializationBasicBlock(blockName, // name |
| 119 | operators, // operators |
| 120 | tensors, // tensors |
| 121 | inputNames, // inputs |
| 122 | {outputName}); // outputs |
| 123 | } |