IVGCVSW-3072 Add MergerLayer Deserialise Backcompat test

Change-Id: Ib8ce6f8db6de0ac4df92fa4c999fe3c78705ec7e
Signed-off-by: Jim Flynn <jim.flynn@arm.com>
diff --git a/src/armnnSerializer/test/SerializerTests.cpp b/src/armnnSerializer/test/SerializerTests.cpp
index 2724ba4..b022668 100644
--- a/src/armnnSerializer/test/SerializerTests.cpp
+++ b/src/armnnSerializer/test/SerializerTests.cpp
@@ -1225,45 +1225,45 @@
     deserializedNetwork->Accept(verifier);
 }
 
-BOOST_AUTO_TEST_CASE(SerializeMerger)
+class MergerLayerVerifier : public LayerVerifierBase
 {
-    class MergerLayerVerifier : public LayerVerifierBase
-    {
-    public:
-        MergerLayerVerifier(const std::string& layerName,
-                            const std::vector<armnn::TensorInfo>& inputInfos,
-                            const std::vector<armnn::TensorInfo>& outputInfos,
-                            const armnn::OriginsDescriptor& descriptor)
+public:
+    MergerLayerVerifier(const std::string& layerName,
+                        const std::vector<armnn::TensorInfo>& inputInfos,
+                        const std::vector<armnn::TensorInfo>& outputInfos,
+                        const armnn::OriginsDescriptor& descriptor)
         : LayerVerifierBase(layerName, inputInfos, outputInfos)
         , m_Descriptor(descriptor) {}
 
-        void VisitMergerLayer(const armnn::IConnectableLayer* layer,
-                            const armnn::OriginsDescriptor& descriptor,
-                            const char* name) override
-        {
-            VerifyNameAndConnections(layer, name);
-            VerifyDescriptor(descriptor);
-        }
+    void VisitMergerLayer(const armnn::IConnectableLayer* layer,
+                          const armnn::OriginsDescriptor& descriptor,
+                          const char* name) override
+    {
+        VerifyNameAndConnections(layer, name);
+        VerifyDescriptor(descriptor);
+    }
 
-    private:
-        void VerifyDescriptor(const armnn::OriginsDescriptor& descriptor)
-        {
-            BOOST_TEST(descriptor.GetConcatAxis() == m_Descriptor.GetConcatAxis());
-            BOOST_TEST(descriptor.GetNumViews() == m_Descriptor.GetNumViews());
-            BOOST_TEST(descriptor.GetNumDimensions() == m_Descriptor.GetNumDimensions());
+private:
+    void VerifyDescriptor(const armnn::OriginsDescriptor& descriptor)
+    {
+        BOOST_TEST(descriptor.GetConcatAxis() == m_Descriptor.GetConcatAxis());
+        BOOST_TEST(descriptor.GetNumViews() == m_Descriptor.GetNumViews());
+        BOOST_TEST(descriptor.GetNumDimensions() == m_Descriptor.GetNumDimensions());
 
-            for (uint32_t i = 0; i < descriptor.GetNumViews(); i++)
+        for (uint32_t i = 0; i < descriptor.GetNumViews(); i++)
+        {
+            for (uint32_t j = 0; j < descriptor.GetNumDimensions(); j++)
             {
-                for (uint32_t j = 0; j < descriptor.GetNumDimensions(); j++)
-                {
-                    BOOST_TEST(descriptor.GetViewOrigin(i)[j] == m_Descriptor.GetViewOrigin(i)[j]);
-                }
+                BOOST_TEST(descriptor.GetViewOrigin(i)[j] == m_Descriptor.GetViewOrigin(i)[j]);
             }
         }
+    }
 
-        armnn::OriginsDescriptor m_Descriptor;
-    };
+    armnn::OriginsDescriptor m_Descriptor;
+};
 
+BOOST_AUTO_TEST_CASE(SerializeMerger)
+{
     const std::string layerName("merger");
     const armnn::TensorInfo inputInfo = armnn::TensorInfo({2, 3, 2, 2}, armnn::DataType::Float32);
     const armnn::TensorInfo outputInfo = armnn::TensorInfo({4, 3, 2, 2}, armnn::DataType::Float32);
@@ -1287,13 +1287,89 @@
     inputLayerTwo->GetOutputSlot(0).SetTensorInfo(inputInfo);
     mergerLayer->GetOutputSlot(0).SetTensorInfo(outputInfo);
 
-    armnn::INetworkPtr deserializedNetwork = DeserializeNetwork(SerializeNetwork(*network));
+    std::string mergerLayerNetwork = SerializeNetwork(*network);
+    armnn::INetworkPtr deserializedNetwork = DeserializeNetwork(mergerLayerNetwork);
     BOOST_CHECK(deserializedNetwork);
 
     MergerLayerVerifier verifier(layerName, {inputInfo, inputInfo}, {outputInfo}, descriptor);
     deserializedNetwork->Accept(verifier);
 }
 
+BOOST_AUTO_TEST_CASE(EnsureMergerLayerBackwardCompatibility)
+{
+    // The hex array below is a flat buffer containing a simple network with two inputs
+    // a merger layer (soon to be a thing of the past) and an output layer with dimensions
+    // as per the tensor infos below.
+    // The intention is that this test will be repurposed as soon as the MergerLayer
+    // is replaced by a ConcatLayer to verify that we can still read back these old style
+    // models replacing the MergerLayers with ConcatLayers with the same parameters.
+    // To do this the MergerLayerVerifier will be changed to have a VisitConcatLayer
+    // which will do the work that the VisitMergerLayer currently does and the VisitMergerLayer
+    // so long as it remains (public API will drop Merger Layer at some future point)
+    // will throw an error if invoked because none of the graphs we create should contain
+    // Merger layers now regardless of whether we attempt to insert the Merger layer via
+    // the INetwork.AddMergerLayer call or by deserializing an old style flatbuffer file.
+    unsigned int size = 760;
+    const unsigned char mergerModel[] = {
+            0x10,0x00,0x00,0x00,0x00,0x00,0x0A,0x00,0x10,0x00,0x04,0x00,0x08,0x00,0x0C,0x00,0x0A,0x00,0x00,0x00,
+            0x0C,0x00,0x00,0x00,0x1C,0x00,0x00,0x00,0x24,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x38,0x02,0x00,0x00,
+            0x8C,0x01,0x00,0x00,0x70,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+            0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0xF4,0xFD,0xFF,0xFF,0x00,0x00,0x00,0x0B,
+            0x04,0x00,0x00,0x00,0x92,0xFE,0xFF,0xFF,0x04,0x00,0x00,0x00,0x9A,0xFE,0xFF,0xFF,0x04,0x00,0x00,0x00,
+            0x7E,0xFE,0xFF,0xFF,0x03,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x10,0x00,0x00,0x00,
+            0x14,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
+            0x00,0x00,0x00,0x00,0xF8,0xFE,0xFF,0xFF,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x48,0xFE,0xFF,0xFF,
+            0x00,0x00,0x00,0x1F,0x0C,0x00,0x00,0x00,0x08,0x00,0x0C,0x00,0x04,0x00,0x08,0x00,0x08,0x00,0x00,0x00,
+            0x68,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x0C,0x00,0x10,0x00,0x00,0x00,0x04,0x00,0x08,0x00,0x0C,0x00,
+            0x0C,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x02,0x00,0x00,0x00,
+            0x24,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x22,0xFF,0xFF,0xFF,0x04,0x00,0x00,0x00,0x04,0x00,0x00,0x00,
+            0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3E,0xFF,0xFF,0xFF,
+            0x04,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+            0x00,0x00,0x00,0x00,0x36,0xFF,0xFF,0xFF,0x02,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x1E,0x00,0x00,0x00,
+            0x14,0x00,0x00,0x00,0x1C,0x00,0x00,0x00,0x06,0x00,0x00,0x00,0x6D,0x65,0x72,0x67,0x65,0x72,0x00,0x00,
+            0x02,0x00,0x00,0x00,0x5C,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x04,0x00,0x00,0x00,
+            0x34,0xFF,0xFF,0xFF,0x04,0x00,0x00,0x00,0x92,0xFE,0xFF,0xFF,0x00,0x00,0x00,0x01,0x08,0x00,0x00,0x00,
+            0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x02,0x00,0x00,0x00,
+            0x02,0x00,0x00,0x00,0x08,0x00,0x10,0x00,0x04,0x00,0x08,0x00,0x08,0x00,0x00,0x00,0x01,0x00,0x00,0x00,
+            0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x0C,0x00,0x00,0x00,0x04,0x00,0x08,0x00,0x00,0x00,
+            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x0E,0x00,0x07,0x00,0x08,0x00,0x08,0x00,0x00,0x00,
+            0x00,0x00,0x00,0x09,0x0C,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x08,0x00,0x04,0x00,0x06,0x00,0x00,0x00,
+            0x0C,0x00,0x00,0x00,0x08,0x00,0x0E,0x00,0x04,0x00,0x08,0x00,0x08,0x00,0x00,0x00,0x18,0x00,0x00,0x00,
+            0x01,0x00,0x00,0x00,0x00,0x00,0x0E,0x00,0x18,0x00,0x04,0x00,0x08,0x00,0x0C,0x00,0x10,0x00,0x14,0x00,
+            0x0E,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x10,0x00,0x00,0x00,
+            0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,
+            0x0C,0x00,0x00,0x00,0x08,0x00,0x08,0x00,0x00,0x00,0x04,0x00,0x08,0x00,0x00,0x00,0x04,0x00,0x00,0x00,
+            0x66,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,
+            0x02,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x08,0x00,0x0C,0x00,
+            0x07,0x00,0x08,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x09,0x04,0x00,0x00,0x00,0xF6,0xFF,0xFF,0xFF,
+            0x0C,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x0A,0x00,0x04,0x00,0x06,0x00,0x00,0x00,0x14,0x00,0x00,0x00,
+            0x00,0x00,0x0E,0x00,0x14,0x00,0x00,0x00,0x04,0x00,0x08,0x00,0x0C,0x00,0x10,0x00,0x0E,0x00,0x00,0x00,
+            0x10,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x0C,0x00,0x00,0x00,0x08,0x00,0x0A,0x00,
+            0x00,0x00,0x04,0x00,0x08,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x0A,0x00,0x10,0x00,0x08,0x00,
+            0x07,0x00,0x0C,0x00,0x0A,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+            0x04,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x02,0x00,0x00,0x00};
+    std::stringstream ss;
+    for (unsigned int i = 0; i < size; ++i)
+    {
+        ss << mergerModel[i];
+    }
+    std::string mergerLayerNetwork = ss.str();
+    armnn::INetworkPtr deserializedNetwork = DeserializeNetwork(mergerLayerNetwork);
+    BOOST_CHECK(deserializedNetwork);
+    const std::string layerName("merger");
+    const armnn::TensorInfo inputInfo = armnn::TensorInfo({2, 3, 2, 2}, armnn::DataType::Float32);
+    const armnn::TensorInfo outputInfo = armnn::TensorInfo({4, 3, 2, 2}, armnn::DataType::Float32);
+
+    const std::vector<armnn::TensorShape> shapes({inputInfo.GetShape(), inputInfo.GetShape()});
+
+    armnn::OriginsDescriptor descriptor =
+            armnn::CreateMergerDescriptorForConcatenation(shapes.begin(), shapes.end(), 0);
+
+    MergerLayerVerifier verifier(layerName, {inputInfo, inputInfo}, {outputInfo}, descriptor);
+    deserializedNetwork->Accept(verifier);
+}
+
 BOOST_AUTO_TEST_CASE(SerializeMinimum)
 {
     class MinimumLayerVerifier : public LayerVerifierBase