IVGCVSW-7170 Add Concat support to TOSA Reference Backend

* Change comment for the unique tensor names in all tosa common operators

Signed-off-by: Kevin May <kevin.may@arm.com>
Change-Id: I247b4b2365d5f0173218c5dfd11fba12d2399959
diff --git a/src/backends/tosaCommon/operatorMappings/AdditionOperator.cpp b/src/backends/tosaCommon/operatorMappings/AdditionOperator.cpp
index 20ba146..7014886 100644
--- a/src/backends/tosaCommon/operatorMappings/AdditionOperator.cpp
+++ b/src/backends/tosaCommon/operatorMappings/AdditionOperator.cpp
@@ -18,14 +18,14 @@
     // using the previous and following layers so the graph is connected correctly. For validation this doesn't matter.
     if(layer != nullptr)
     {
-        // Get the layers connected to the input slots and determine unique layer names.
+        // Get the layers connected to the input slots and determine unique tensors names.
         Layer& connectedLayer0 = layer->GetInputSlot(0).GetConnectedOutputSlot()->GetOwningLayer();
         input0Name = GenerateUniqueName(connectedLayer0, 0);
 
         Layer& connectedLayer1 = layer->GetInputSlot(1).GetConnectedOutputSlot()->GetOwningLayer();
         input1Name = GenerateUniqueName(connectedLayer1, 1);
 
-        // Get the layer connected to the output slot and determine unique layer name.
+        // Determine unique output tensor name.
         outputName = GenerateUniqueOutputName(*layer, 0);
     }
 
diff --git a/src/backends/tosaCommon/operatorMappings/AvgPool2DIgnoreValueOperator.cpp b/src/backends/tosaCommon/operatorMappings/AvgPool2DIgnoreValueOperator.cpp
index d268c2f..61de0ae 100644
--- a/src/backends/tosaCommon/operatorMappings/AvgPool2DIgnoreValueOperator.cpp
+++ b/src/backends/tosaCommon/operatorMappings/AvgPool2DIgnoreValueOperator.cpp
@@ -19,11 +19,11 @@
     // using the previous and following layers so the graph is connected correctly. For validation this doesn't matter.
     if(layer != nullptr)
     {
-        // Get the layers connected to the input slots and determine unique layer names.
+        // Get the layers connected to the input slots and determine unique tensors names.
         Layer& connectedInputLayer = layer->GetInputSlot(0).GetConnectedOutputSlot()->GetOwningLayer();
         padInputName = GenerateUniqueName(connectedInputLayer, 0);
 
-        // Get the layer connected to the output slot and determine unique layer name.
+        // Determine unique output tensor name.
         poolOutputName = GenerateUniqueOutputName(*layer, 0);
     }
 
diff --git a/src/backends/tosaCommon/operatorMappings/CMakeLists.txt b/src/backends/tosaCommon/operatorMappings/CMakeLists.txt
index 90c1a4f..2443dc0 100644
--- a/src/backends/tosaCommon/operatorMappings/CMakeLists.txt
+++ b/src/backends/tosaCommon/operatorMappings/CMakeLists.txt
@@ -8,6 +8,8 @@
         AdditionOperator.cpp
         AvgPool2DIgnoreValueOperator.hpp
         AvgPool2DIgnoreValueOperator.cpp
+        ConcatOperator.hpp
+        ConcatOperator.cpp
         ConstantOperator.hpp
         ConstantOperator.cpp
         Conv2dOperator.hpp
diff --git a/src/backends/tosaCommon/operatorMappings/ConcatOperator.cpp b/src/backends/tosaCommon/operatorMappings/ConcatOperator.cpp
new file mode 100644
index 0000000..8c651be
--- /dev/null
+++ b/src/backends/tosaCommon/operatorMappings/ConcatOperator.cpp
@@ -0,0 +1,81 @@
+//
+// Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#include "ConcatOperator.hpp"
+
+TosaSerializationBasicBlock* ConvertConcatToTosaOperator(const Layer* layer,
+                                                         const std::vector<const TensorInfo*>& inputs,
+                                                         const std::vector<const TensorInfo*>& outputs,
+                                                         const OriginsDescriptor* concatDescriptor)
+{
+    auto numInputs = inputs.size();
+    std::vector<std::string> inputNames;
+    inputNames.reserve(numInputs);
+    std::string outputName = std::string("output0_");
+    std::string blockName  = std::string("Op_CONCAT_block_") + GetUniqueTosaMappingID();
+
+    // Set input names for validation purposes only.
+    if (layer == nullptr)
+    {
+        for (uint32_t i = 0; i < numInputs; ++i)
+        {
+            inputNames.push_back("input"+ std::to_string(i) +"_");
+        }
+    }
+    // If a layer is present then the block will be used for execution, so input and output names need to be determined
+    // using the previous and following layers so the graph is connected correctly. For validation this doesn't matter.
+    else
+    {
+        // Get the layers connected to the input slots and determine unique tensor names.
+        for (uint32_t i = 0; i < numInputs; ++i)
+        {
+            Layer& connectedLayer = layer->GetInputSlot(i).GetConnectedOutputSlot()->GetOwningLayer();
+
+            std::string inputName = GenerateUniqueName(connectedLayer, i);
+            inputNames.push_back(inputName);
+        }
+
+        // Determine unique output tensor name.
+        outputName = GenerateUniqueOutputName(*layer, 0);
+    }
+
+    auto axis = static_cast<int32_t>(concatDescriptor->GetConcatAxis());
+    TosaAxisAttribute attribute(axis);
+
+    TosaSerializationOperator* op = new TosaSerializationOperator(Op_CONCAT,
+                                                                  Attribute_AxisAttribute,
+                                                                  &attribute,
+                                                                  inputNames,
+                                                                  {outputName});
+
+    std::vector<TosaSerializationTensor*> tensors;
+    tensors.reserve(numInputs);
+
+    for (uint32_t i = 0; i < numInputs; ++i)
+    {
+        // Only add input tensors for validation or when the connected layer is an input layer.
+        // As there can't be duplicate tensors and intermediate or constant tensors are created separately.
+        if(inputNames[i].find("input") != std::string::npos)
+        {
+            std::vector<int32_t> inputShape = GetTosaTensorShape(inputs[i]->GetShape());
+            DType inputDType = ArmNNToDType(inputs[i]->GetDataType());
+            tensors.push_back(new TosaSerializationTensor(inputNames[i], inputShape, inputDType, {}));
+        }
+    }
+
+    std::vector<int32_t> outputShape0 = GetTosaTensorShape(outputs[0]->GetShape());
+    DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType());
+
+    TosaSerializationTensor* outputTensor0 = new TosaSerializationTensor(outputName, outputShape0, outputDType0, {});
+    tensors.push_back(outputTensor0);
+
+    // operatorInputNames/operatorOutputNames ends up being the same as
+    // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings
+    return new TosaSerializationBasicBlock(blockName,     // name
+                                           {op},          // operators
+                                           tensors,       // tensors
+                                           inputNames,    // inputs
+                                           {outputName}); // outputs
+}
\ No newline at end of file
diff --git a/src/backends/tosaCommon/operatorMappings/ConcatOperator.hpp b/src/backends/tosaCommon/operatorMappings/ConcatOperator.hpp
new file mode 100644
index 0000000..e6094ce
--- /dev/null
+++ b/src/backends/tosaCommon/operatorMappings/ConcatOperator.hpp
@@ -0,0 +1,20 @@
+//
+// Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#pragma once
+
+#include <Layer.hpp>
+
+#include <tosa_serialization_handler.h>
+
+#include "TosaOperatorUtils.hpp"
+
+using namespace armnn;
+using namespace tosa;
+
+TosaSerializationBasicBlock* ConvertConcatToTosaOperator(const Layer* layer,
+                                                         const std::vector<const TensorInfo*>& inputs,
+                                                         const std::vector<const TensorInfo*>& outputs,
+                                                         const OriginsDescriptor* concatDescriptor);
diff --git a/src/backends/tosaCommon/operatorMappings/Conv2dOperator.cpp b/src/backends/tosaCommon/operatorMappings/Conv2dOperator.cpp
index 6fc1678..a7af083 100644
--- a/src/backends/tosaCommon/operatorMappings/Conv2dOperator.cpp
+++ b/src/backends/tosaCommon/operatorMappings/Conv2dOperator.cpp
@@ -24,21 +24,21 @@
             inputNames.emplace_back("input2_");
         }
     }
+    // If a layer is present then the block will be used for execution, so input and output names need to be
+    // determined using the previous and following layers so the graph is connected correctly.
+    // For validation this doesn't matter.
     else
     {
-        // If a layer is present then the block will be used for execution, so input and output names need to be
-        // determined using the previous and following layers so the graph is connected correctly.
-        // For validation this doesn't matter.
+        // Get the layer connected to the input slot and determine unique tensor names.
         for (uint32_t i = 0; i < inputs.size(); ++i)
         {
-            // Get the layer connected to the input slot and determine unique layer name.
             Layer& connectedLayer = layer->GetInputSlot(i).GetConnectedOutputSlot()->GetOwningLayer();
 
             std::string inputName = GenerateUniqueName(connectedLayer, i);
             inputNames.push_back(inputName);
         }
 
-        // Get the layer connected to the output slot and determine unique layer name.
+        // Determine unique output tensor name.
         outputName = GenerateUniqueOutputName(*layer, 0);
     }
 
diff --git a/src/backends/tosaCommon/operatorMappings/Pooling2DOperator.cpp b/src/backends/tosaCommon/operatorMappings/Pooling2DOperator.cpp
index ee02425..444d99a 100644
--- a/src/backends/tosaCommon/operatorMappings/Pooling2DOperator.cpp
+++ b/src/backends/tosaCommon/operatorMappings/Pooling2DOperator.cpp
@@ -21,11 +21,11 @@
     // using the previous and following layers so the graph is connected correctly. For validation this doesn't matter.
     if(layer != nullptr)
     {
-        // Get the layers connected to the input slots and determine unique layer names.
+        // Get the layers connected to the input slots and determine unique tensor names.
         Layer& connectedInputLayer = layer->GetInputSlot(0).GetConnectedOutputSlot()->GetOwningLayer();
         input0Name = GenerateUniqueName(connectedInputLayer, 0);
 
-        // Get the layer connected to the output slot and determine unique layer name.
+        // Determine unique output tensor name.
         outputName = GenerateUniqueOutputName(*layer, 0);
     }
 
diff --git a/src/backends/tosaCommon/operatorMappings/ReshapeOperator.cpp b/src/backends/tosaCommon/operatorMappings/ReshapeOperator.cpp
index 3027e2e..10670ec 100644
--- a/src/backends/tosaCommon/operatorMappings/ReshapeOperator.cpp
+++ b/src/backends/tosaCommon/operatorMappings/ReshapeOperator.cpp
@@ -18,11 +18,11 @@
     // using the previous and following layers so the graph is connected correctly. For validation this doesn't matter.
     if(layer != nullptr)
     {
-        // Get the layers connected to the input slots and determine unique layer names.
+        // Get the layers connected to the input slots and determine unique tensor names.
         Layer& connectedLayer = layer->GetInputSlot(0).GetConnectedOutputSlot()->GetOwningLayer();
         inputName = GenerateUniqueName(connectedLayer, 0);
 
-        // Get the layer connected to the output slot and determine unique layer name.
+        // Determine unique output tensor name.
         outputName = GenerateUniqueOutputName(*layer, 0);
     }
 
diff --git a/src/backends/tosaCommon/operatorMappings/SliceOperator.cpp b/src/backends/tosaCommon/operatorMappings/SliceOperator.cpp
index 742ba88..b98576f 100644
--- a/src/backends/tosaCommon/operatorMappings/SliceOperator.cpp
+++ b/src/backends/tosaCommon/operatorMappings/SliceOperator.cpp
@@ -18,11 +18,11 @@
     // using the previous and following layers so the graph is connected correctly. For validation this doesn't matter.
     if(layer != nullptr)
     {
-        // Get the layers connected to the input slots and determine unique layer names.
+        // Get the layers connected to the input slots and determine unique tensor names.
         Layer& connectedLayer = layer->GetInputSlot(0).GetConnectedOutputSlot()->GetOwningLayer();
         inputName = GenerateUniqueName(connectedLayer, 0);
 
-        // Get the layer connected to the output slot and determine unique layer name.
+        // Determine unique output tensor name.
         outputName = GenerateUniqueOutputName(*layer, 0);
     }
 
diff --git a/src/backends/tosaCommon/operatorMappings/TosaCommonOperators.hpp b/src/backends/tosaCommon/operatorMappings/TosaCommonOperators.hpp
index 1a9d6be..052c54c 100644
--- a/src/backends/tosaCommon/operatorMappings/TosaCommonOperators.hpp
+++ b/src/backends/tosaCommon/operatorMappings/TosaCommonOperators.hpp
@@ -6,6 +6,7 @@
 #pragma once
 
 #include "AdditionOperator.hpp"
+#include "ConcatOperator.hpp"
 #include "ConstantOperator.hpp"
 #include "Conv2dOperator.hpp"
 #include "AvgPool2DIgnoreValueOperator.hpp"
diff --git a/src/backends/tosaCommon/operatorMappings/TransposeConv2dOperator.cpp b/src/backends/tosaCommon/operatorMappings/TransposeConv2dOperator.cpp
index 1ad8c95..c8af5c2 100644
--- a/src/backends/tosaCommon/operatorMappings/TransposeConv2dOperator.cpp
+++ b/src/backends/tosaCommon/operatorMappings/TransposeConv2dOperator.cpp
@@ -22,9 +22,11 @@
     // using the previous and following layers so the graph is connected correctly. For validation this doesn't matter.
     if(layer != nullptr)
     {
+        // Get the layers connected to the input slots and determine unique tensor names.
         Layer& connectedInputLayer = layer->GetInputSlot(0).GetConnectedOutputSlot()->GetOwningLayer();
         input0Name = GenerateUniqueName(connectedInputLayer, 0);
 
+        // Determine unique output tensor name.
         outputName = GenerateUniqueOutputName(*layer, 0);
     }