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. |
Matthew Sloyan | da6bf9e | 2022-12-14 10:16:27 +0000 | [diff] [blame] | 42 | outputName = GenerateUniqueOutputName(*layer, 0); |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | std::vector<TosaSerializationTensor*> tensors; |
| 46 | std::vector<TosaSerializationOperator*> operators; |
| 47 | |
| 48 | // Setup input Tensor |
Matthew Sloyan | da6bf9e | 2022-12-14 10:16:27 +0000 | [diff] [blame] | 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()); |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 56 | |
Matthew Sloyan | da6bf9e | 2022-12-14 10:16:27 +0000 | [diff] [blame] | 57 | tensors.push_back(new TosaSerializationTensor(inputNames[0], inputShape0, inputDType0, {})); |
| 58 | } |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 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 | |
Matthew Sloyan | da6bf9e | 2022-12-14 10:16:27 +0000 | [diff] [blame] | 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 | |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 90 | std::vector<uint8_t> uint8Data; |
Matthew Sloyan | da6bf9e | 2022-12-14 10:16:27 +0000 | [diff] [blame] | 91 | std::vector<float> data(outputs[0]->GetShape()[index], 0.0f); |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 92 | |
| 93 | TosaSerializationHandler::ConvertF32toU8(data, uint8Data); |
| 94 | |
Matthew Sloyan | da6bf9e | 2022-12-14 10:16:27 +0000 | [diff] [blame] | 95 | tensors.push_back(new TosaSerializationTensor(constantName, |
| 96 | {static_cast<int32_t>(outputs[0]->GetShape()[index])}, |
| 97 | DType_FP32, |
| 98 | uint8Data)); |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 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, dilation, stride, 0, 0, ArmNNToDType(inputs[0]->GetDataType())); |
| 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 | operators, // operators |
| 130 | tensors, // tensors |
| 131 | inputNames, // inputs |
| 132 | {outputName}); // outputs |
| 133 | } |