Kevin May | 5b58e31 | 2022-12-15 10:15:21 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2022 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ConcatOperator.hpp" |
| 7 | |
| 8 | TosaSerializationBasicBlock* ConvertConcatToTosaOperator(const Layer* layer, |
| 9 | const std::vector<const TensorInfo*>& inputs, |
| 10 | const std::vector<const TensorInfo*>& outputs, |
| 11 | const OriginsDescriptor* concatDescriptor) |
| 12 | { |
| 13 | auto numInputs = inputs.size(); |
| 14 | std::vector<std::string> inputNames; |
| 15 | inputNames.reserve(numInputs); |
| 16 | std::string outputName = std::string("output0_"); |
| 17 | std::string blockName = std::string("Op_CONCAT_block_") + GetUniqueTosaMappingID(); |
| 18 | |
| 19 | // Set input names for validation purposes only. |
| 20 | if (layer == nullptr) |
| 21 | { |
| 22 | for (uint32_t i = 0; i < numInputs; ++i) |
| 23 | { |
| 24 | inputNames.push_back("input"+ std::to_string(i) +"_"); |
| 25 | } |
| 26 | } |
| 27 | // If a layer is present then the block will be used for execution, so input and output names need to be determined |
| 28 | // using the previous and following layers so the graph is connected correctly. For validation this doesn't matter. |
| 29 | else |
| 30 | { |
| 31 | // Get the layers connected to the input slots and determine unique tensor names. |
| 32 | for (uint32_t i = 0; i < numInputs; ++i) |
| 33 | { |
| 34 | Layer& connectedLayer = layer->GetInputSlot(i).GetConnectedOutputSlot()->GetOwningLayer(); |
| 35 | |
| 36 | std::string inputName = GenerateUniqueName(connectedLayer, i); |
| 37 | inputNames.push_back(inputName); |
| 38 | } |
| 39 | |
| 40 | // Determine unique output tensor name. |
| 41 | outputName = GenerateUniqueOutputName(*layer, 0); |
| 42 | } |
| 43 | |
| 44 | auto axis = static_cast<int32_t>(concatDescriptor->GetConcatAxis()); |
| 45 | TosaAxisAttribute attribute(axis); |
| 46 | |
| 47 | TosaSerializationOperator* op = new TosaSerializationOperator(Op_CONCAT, |
| 48 | Attribute_AxisAttribute, |
| 49 | &attribute, |
| 50 | inputNames, |
| 51 | {outputName}); |
| 52 | |
| 53 | std::vector<TosaSerializationTensor*> tensors; |
| 54 | tensors.reserve(numInputs); |
| 55 | |
| 56 | for (uint32_t i = 0; i < numInputs; ++i) |
| 57 | { |
| 58 | // Only add input tensors for validation or when the connected layer is an input layer. |
| 59 | // As there can't be duplicate tensors and intermediate or constant tensors are created separately. |
| 60 | if(inputNames[i].find("input") != std::string::npos) |
| 61 | { |
| 62 | std::vector<int32_t> inputShape = GetTosaTensorShape(inputs[i]->GetShape()); |
| 63 | DType inputDType = ArmNNToDType(inputs[i]->GetDataType()); |
| 64 | tensors.push_back(new TosaSerializationTensor(inputNames[i], inputShape, inputDType, {})); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | std::vector<int32_t> outputShape0 = GetTosaTensorShape(outputs[0]->GetShape()); |
| 69 | DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType()); |
| 70 | |
| 71 | TosaSerializationTensor* outputTensor0 = new TosaSerializationTensor(outputName, outputShape0, outputDType0, {}); |
| 72 | tensors.push_back(outputTensor0); |
| 73 | |
| 74 | // operatorInputNames/operatorOutputNames ends up being the same as |
| 75 | // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings |
| 76 | return new TosaSerializationBasicBlock(blockName, // name |
Narumol Prangnawarat | ad323af | 2023-09-29 17:00:38 +0100 | [diff] [blame] | 77 | mainName, // region name |
Kevin May | 5b58e31 | 2022-12-15 10:15:21 +0000 | [diff] [blame] | 78 | {op}, // operators |
| 79 | tensors, // tensors |
| 80 | inputNames, // inputs |
| 81 | {outputName}); // outputs |
| 82 | } |