IVGCVSW-2688 Add Serializer and Deserializer for Equal

Change-Id: If613f4394e161483eea4930dab2afba74c5e26fd
Signed-off-by: Nattapat Chaimanowong <nattapat.chaimanowong@arm.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ecc6337..a078d73 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -601,6 +601,7 @@
                 src/armnnDeserializer/test/DeserializeConstant.cpp
                 src/armnnDeserializer/test/DeserializeConvolution2d.cpp
                 src/armnnDeserializer/test/DeserializeDivision.cpp
+                src/armnnDeserializer/test/DeserializeEqual.cpp
                 src/armnnDeserializer/test/DeserializeFullyConnected.cpp
                 src/armnnDeserializer/test/DeserializeMultiplication.cpp
                 src/armnnDeserializer/test/DeserializePermute.cpp
diff --git a/src/armnnDeserializer/Deserializer.cpp b/src/armnnDeserializer/Deserializer.cpp
index 28c6c94..317269e 100644
--- a/src/armnnDeserializer/Deserializer.cpp
+++ b/src/armnnDeserializer/Deserializer.cpp
@@ -192,6 +192,7 @@
     m_ParserFunctions[Layer_Convolution2dLayer]          = &Deserializer::ParseConvolution2d;
     m_ParserFunctions[Layer_DepthwiseConvolution2dLayer] = &Deserializer::ParseDepthwiseConvolution2d;
     m_ParserFunctions[Layer_DivisionLayer]               = &Deserializer::ParseDivision;
+    m_ParserFunctions[Layer_EqualLayer]                  = &Deserializer::ParseEqual;
     m_ParserFunctions[Layer_FullyConnectedLayer]         = &Deserializer::ParseFullyConnected;
     m_ParserFunctions[Layer_MinimumLayer]                = &Deserializer::ParseMinimum;
     m_ParserFunctions[Layer_MultiplicationLayer]         = &Deserializer::ParseMultiplication;
@@ -222,6 +223,8 @@
             return graphPtr->layers()->Get(layerIndex)->layer_as_DepthwiseConvolution2dLayer()->base();
         case Layer::Layer_DivisionLayer:
             return graphPtr->layers()->Get(layerIndex)->layer_as_DivisionLayer()->base();
+        case Layer::Layer_EqualLayer:
+            return graphPtr->layers()->Get(layerIndex)->layer_as_EqualLayer()->base();
         case Layer::Layer_FullyConnectedLayer:
             return graphPtr->layers()->Get(layerIndex)->layer_as_FullyConnectedLayer()->base();
         case Layer::Layer_InputLayer:
@@ -958,6 +961,26 @@
     RegisterOutputSlots(graph, layerIndex, layer);
 }
 
+void Deserializer::ParseEqual(GraphPtr graph, unsigned int layerIndex)
+{
+    CHECK_LAYERS(graph, 0, layerIndex);
+    auto inputs = GetInputs(graph, layerIndex);
+    CHECK_LOCATION();
+    CHECK_VALID_SIZE(inputs.size(), 2);
+
+    auto outputs = GetOutputs(graph, layerIndex);
+    CHECK_VALID_SIZE(outputs.size(), 1);
+
+    auto layerName = GetLayerName(graph, layerIndex);
+    IConnectableLayer* layer = m_Network->AddEqualLayer(layerName.c_str());
+
+    armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
+    layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
+
+    RegisterInputSlots(graph, layerIndex, layer);
+    RegisterOutputSlots(graph, layerIndex, layer);
+}
+
 void Deserializer::ParseMinimum(GraphPtr graph, unsigned int layerIndex)
 {
     CHECK_LAYERS(graph, 0, layerIndex);
diff --git a/src/armnnDeserializer/Deserializer.hpp b/src/armnnDeserializer/Deserializer.hpp
index 657c7f5..e75ab1c 100644
--- a/src/armnnDeserializer/Deserializer.hpp
+++ b/src/armnnDeserializer/Deserializer.hpp
@@ -75,6 +75,7 @@
     void ParseConvolution2d(GraphPtr graph, unsigned int layerIndex);
     void ParseDepthwiseConvolution2d(GraphPtr graph, unsigned int layerIndex);
     void ParseDivision(GraphPtr graph, unsigned int layerIndex);
+    void ParseEqual(GraphPtr graph, unsigned int layerIndex);
     void ParseFullyConnected(GraphPtr graph, unsigned int layerIndex);
     void ParseMinimum(GraphPtr graph, unsigned int layerIndex);
     void ParseMultiplication(GraphPtr graph, unsigned int layerIndex);
diff --git a/src/armnnDeserializer/DeserializerSupport.md b/src/armnnDeserializer/DeserializerSupport.md
index 8fd7b42..7910683 100644
--- a/src/armnnDeserializer/DeserializerSupport.md
+++ b/src/armnnDeserializer/DeserializerSupport.md
@@ -13,6 +13,7 @@
 * Convolution2d
 * DepthwiseConvolution2d
 * Division
+* Equal
 * FullyConnected
 * Minimum
 * Multiplication
diff --git a/src/armnnDeserializer/test/DeserializeEqual.cpp b/src/armnnDeserializer/test/DeserializeEqual.cpp
new file mode 100644
index 0000000..7d8213c
--- /dev/null
+++ b/src/armnnDeserializer/test/DeserializeEqual.cpp
@@ -0,0 +1,148 @@
+//
+// Copyright © 2017 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#include <boost/test/unit_test.hpp>
+#include "ParserFlatbuffersSerializeFixture.hpp"
+#include "../Deserializer.hpp"
+
+#include <string>
+#include <iostream>
+
+BOOST_AUTO_TEST_SUITE(Deserializer)
+
+struct EqualFixture : public ParserFlatbuffersSerializeFixture
+{
+    explicit EqualFixture(const std::string & inputShape1,
+                          const std::string & inputShape2,
+                          const std::string & outputShape,
+                          const std::string & dataType)
+    {
+        m_JsonString = R"(
+            {
+                inputIds: [0, 1],
+                outputIds: [3],
+                layers: [
+                    {
+                        layer_type: "InputLayer",
+                        layer: {
+                            base: {
+                                layerBindingId: 0,
+                                base: {
+                                    index: 0,
+                                    layerName: "InputLayer1",
+                                    layerType: "Input",
+                                    inputSlots: [{
+                                        index: 0,
+                                        connection: {sourceLayerIndex:0, outputSlotIndex:0 },
+                                    }],
+                                    outputSlots: [{
+                                        index: 0,
+                                        tensorInfo: {
+                                            dimensions: )" + inputShape1 + R"(,
+                                            dataType: )" + dataType + R"(
+                                        },
+                                    }],
+                                },
+                            }
+                        },
+                    },
+                    {
+                        layer_type: "InputLayer",
+                        layer: {
+                            base: {
+                                layerBindingId: 1,
+                                base: {
+                                      index:1,
+                                      layerName: "InputLayer2",
+                                      layerType: "Input",
+                                      inputSlots: [{
+                                          index: 0,
+                                          connection: {sourceLayerIndex:0, outputSlotIndex:0 },
+                                      }],
+                                      outputSlots: [{
+                                          index: 0,
+                                          tensorInfo: {
+                                              dimensions: )" + inputShape2 + R"(,
+                                              dataType: )" + dataType + R"(
+                                          },
+                                      }],
+                                },
+                            }
+                        },
+                    },
+                    {
+                        layer_type: "EqualLayer",
+                        layer: {
+                            base: {
+                                 index:2,
+                                 layerName: "EqualLayer",
+                                 layerType: "Equal",
+                                 inputSlots: [{
+                                     index: 0,
+                                     connection: {sourceLayerIndex:0, outputSlotIndex:0 },
+                                 },
+                                 {
+                                     index: 1,
+                                     connection: {sourceLayerIndex:1, outputSlotIndex:0 },
+                                 }],
+                                 outputSlots: [{
+                                     index: 0,
+                                     tensorInfo: {
+                                         dimensions: )" + outputShape + R"(,
+                                         dataType: Boolean
+                                     },
+                                 }],
+                            }
+                        },
+                    },
+                    {
+                        layer_type: "OutputLayer",
+                        layer: {
+                            base:{
+                                layerBindingId: 0,
+                                base: {
+                                    index: 3,
+                                    layerName: "OutputLayer",
+                                    layerType: "Output",
+                                    inputSlots: [{
+                                        index: 0,
+                                        connection: {sourceLayerIndex:2, outputSlotIndex:0 },
+                                    }],
+                                    outputSlots: [{
+                                        index: 0,
+                                        tensorInfo: {
+                                            dimensions: )" + outputShape + R"(,
+                                            dataType: Boolean
+                                        },
+                                    }],
+                                }
+                            }
+                        },
+                    }
+                ]
+            }
+        )";
+        Setup();
+    }
+};
+
+struct SimpleEqualFixture : EqualFixture
+{
+    SimpleEqualFixture() : EqualFixture("[ 2, 2, 2, 1 ]",
+                                        "[ 2, 2, 2, 1 ]",
+                                        "[ 2, 2, 2, 1 ]",
+                                        "QuantisedAsymm8") {}
+};
+
+BOOST_FIXTURE_TEST_CASE(EqualQuantisedAsymm8, SimpleEqualFixture)
+{
+  RunTest<4, armnn::DataType::QuantisedAsymm8, armnn::DataType::Boolean>(
+      0,
+      {{"InputLayer1", { 0, 1, 2, 3, 4, 5, 6, 7 }},
+       {"InputLayer2", { 0, 0, 0, 3, 0, 0, 6, 7 }}},
+      {{"OutputLayer", { 1, 0, 0, 1, 0, 0, 1, 1 }}});
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/armnnSerializer/ArmnnSchema.fbs b/src/armnnSerializer/ArmnnSchema.fbs
index 501953c..5787f51 100644
--- a/src/armnnSerializer/ArmnnSchema.fbs
+++ b/src/armnnSerializer/ArmnnSchema.fbs
@@ -97,7 +97,8 @@
     SpaceToBatchNd = 13,
     BatchToSpaceNd = 14,
     Division = 15,
-    Minimum = 16
+    Minimum = 16,
+    Equal = 17
 }
 
 // Base layer table to be used as part of other layers
@@ -157,6 +158,10 @@
     base:LayerBase;
 }
 
+table EqualLayer {
+    base:LayerBase;
+}
+
 table FullyConnectedLayer {
     base:LayerBase;
     descriptor:FullyConnectedDescriptor;
@@ -305,7 +310,8 @@
     SoftmaxLayer,
     SpaceToBatchNdLayer,
     DivisionLayer,
-    MinimumLayer
+    MinimumLayer,
+    EqualLayer
 }
 
 table AnyLayer {
diff --git a/src/armnnSerializer/Serializer.cpp b/src/armnnSerializer/Serializer.cpp
index 8b8a88d..05b3691 100644
--- a/src/armnnSerializer/Serializer.cpp
+++ b/src/armnnSerializer/Serializer.cpp
@@ -276,6 +276,16 @@
     CreateAnyLayer(fbMinimumLayer.o, serializer::Layer::Layer_MinimumLayer);
 }
 
+// Build FlatBuffer for Equal Layer
+void SerializerVisitor::VisitEqualLayer(const armnn::IConnectableLayer* layer, const char* name)
+{
+    auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Equal);
+
+    auto flatBufferEqualLayer = serializer::CreateEqualLayer(m_flatBufferBuilder, flatBufferBaseLayer);
+
+    CreateAnyLayer(flatBufferEqualLayer.o, serializer::Layer::Layer_EqualLayer);
+}
+
 // Build FlatBuffer for Multiplication Layer
 void SerializerVisitor::VisitMultiplicationLayer(const armnn::IConnectableLayer* layer, const char* name)
 {
diff --git a/src/armnnSerializer/Serializer.hpp b/src/armnnSerializer/Serializer.hpp
index ecc966c..9118041 100644
--- a/src/armnnSerializer/Serializer.hpp
+++ b/src/armnnSerializer/Serializer.hpp
@@ -72,6 +72,9 @@
     void VisitDivisionLayer(const armnn::IConnectableLayer* layer,
                             const char* name = nullptr) override;
 
+    void VisitEqualLayer(const armnn::IConnectableLayer* layer,
+                         const char* name = nullptr) override;
+
     void VisitFullyConnectedLayer(const armnn::IConnectableLayer* layer,
                                   const armnn::FullyConnectedDescriptor& fullyConnectedDescriptor,
                                   const armnn::ConstTensor& weights,
diff --git a/src/armnnSerializer/SerializerSupport.md b/src/armnnSerializer/SerializerSupport.md
index 31f6310..426b71e 100644
--- a/src/armnnSerializer/SerializerSupport.md
+++ b/src/armnnSerializer/SerializerSupport.md
@@ -13,6 +13,7 @@
 * Convolution2d
 * DepthwiseConvolution2d
 * Division
+* Equal
 * FullyConnected
 * Minimum
 * Multiplication
diff --git a/src/armnnSerializer/test/SerializerTests.cpp b/src/armnnSerializer/test/SerializerTests.cpp
index 4b0077c..1d65b1b 100644
--- a/src/armnnSerializer/test/SerializerTests.cpp
+++ b/src/armnnSerializer/test/SerializerTests.cpp
@@ -840,4 +840,46 @@
     deserializedNetwork->Accept(nameChecker);
 }
 
+BOOST_AUTO_TEST_CASE(SerializeDeserializeEqual)
+{
+    class VerifyEqualName : public armnn::LayerVisitorBase<armnn::VisitorNoThrowPolicy>
+    {
+    public:
+        void VisitEqualLayer(const armnn::IConnectableLayer*,
+                             const char* name) override
+        {
+            BOOST_TEST(name == "EqualLayer");
+        }
+    };
+
+    const armnn::TensorInfo inputTensorInfo1 = armnn::TensorInfo({2, 1, 2, 4}, armnn::DataType::Float32);
+    const armnn::TensorInfo inputTensorInfo2 = armnn::TensorInfo({2, 1, 2, 4}, armnn::DataType::Float32);
+    const armnn::TensorInfo outputTensorInfo = armnn::TensorInfo({2, 1, 2, 4}, armnn::DataType::Boolean);
+
+    armnn::INetworkPtr network = armnn::INetwork::Create();
+    armnn::IConnectableLayer* const inputLayer1 = network->AddInputLayer(0);
+    armnn::IConnectableLayer* const inputLayer2 = network->AddInputLayer(1);
+    armnn::IConnectableLayer* const equalLayer = network->AddEqualLayer("EqualLayer");
+    armnn::IConnectableLayer* const outputLayer = network->AddOutputLayer(0);
+
+    inputLayer1->GetOutputSlot(0).Connect(equalLayer->GetInputSlot(0));
+    inputLayer1->GetOutputSlot(0).SetTensorInfo(inputTensorInfo1);
+    inputLayer2->GetOutputSlot(0).Connect(equalLayer->GetInputSlot(1));
+    inputLayer2->GetOutputSlot(0).SetTensorInfo(inputTensorInfo2);
+    equalLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0));
+    equalLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
+
+    armnn::INetworkPtr deserializedNetwork = DeserializeNetwork(SerializeNetwork(*network));
+    BOOST_CHECK(deserializedNetwork);
+
+    VerifyEqualName nameChecker;
+    deserializedNetwork->Accept(nameChecker);
+
+    CheckDeserializedNetworkAgainstOriginal(*network,
+                                            *deserializedNetwork,
+                                            {inputTensorInfo1.GetShape(), inputTensorInfo2.GetShape()},
+                                            {outputTensorInfo.GetShape()},
+                                            {0, 1});
+}
+
 BOOST_AUTO_TEST_SUITE_END()