IVGCVSW-2687 Add Serializer & Deserializer for Division

Change-Id: I230220eccbfb22e56e8d351cc963988d734836ac
Signed-off-by: Éanna Ó Catháin <eanna.ocathain@arm.com>
diff --git a/src/armnnDeserializer/Deserializer.cpp b/src/armnnDeserializer/Deserializer.cpp
index 30f7d05..1ef5651 100644
--- a/src/armnnDeserializer/Deserializer.cpp
+++ b/src/armnnDeserializer/Deserializer.cpp
@@ -191,6 +191,7 @@
     m_ParserFunctions[Layer_ConstantLayer]               = &Deserializer::ParseConstant;
     m_ParserFunctions[Layer_Convolution2dLayer]          = &Deserializer::ParseConvolution2d;
     m_ParserFunctions[Layer_DepthwiseConvolution2dLayer] = &Deserializer::ParseDepthwiseConvolution2d;
+    m_ParserFunctions[Layer_DivisionLayer]               = &Deserializer::ParseDivision;
     m_ParserFunctions[Layer_FullyConnectedLayer]         = &Deserializer::ParseFullyConnected;
     m_ParserFunctions[Layer_MultiplicationLayer]         = &Deserializer::ParseMultiplication;
     m_ParserFunctions[Layer_PermuteLayer]                = &Deserializer::ParsePermute;
@@ -218,6 +219,8 @@
             return graphPtr->layers()->Get(layerIndex)->layer_as_Convolution2dLayer()->base();
         case Layer::Layer_DepthwiseConvolution2dLayer:
             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_FullyConnectedLayer:
             return graphPtr->layers()->Get(layerIndex)->layer_as_FullyConnectedLayer()->base();
         case Layer::Layer_InputLayer:
@@ -932,6 +935,26 @@
     RegisterOutputSlots(graph, layerIndex, layer);
 }
 
+void Deserializer::ParseDivision(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->AddDivisionLayer(layerName.c_str());
+
+    armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
+    layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
+
+    RegisterInputSlots(graph, layerIndex, layer);
+    RegisterOutputSlots(graph, layerIndex, layer);
+}
+
 void Deserializer::ParseMultiplication(GraphPtr graph, unsigned int layerIndex)
 {
     CHECK_LAYERS(graph, 0, layerIndex);
diff --git a/src/armnnDeserializer/Deserializer.hpp b/src/armnnDeserializer/Deserializer.hpp
index 0e0261f..cb0590b 100644
--- a/src/armnnDeserializer/Deserializer.hpp
+++ b/src/armnnDeserializer/Deserializer.hpp
@@ -74,6 +74,7 @@
     void ParseConstant(GraphPtr graph, unsigned int layerIndex);
     void ParseConvolution2d(GraphPtr graph, unsigned int layerIndex);
     void ParseDepthwiseConvolution2d(GraphPtr graph, unsigned int layerIndex);
+    void ParseDivision(GraphPtr graph, unsigned int layerIndex);
     void ParseFullyConnected(GraphPtr graph, unsigned int layerIndex);
     void ParseMultiplication(GraphPtr graph, unsigned int layerIndex);
     void ParsePermute(GraphPtr graph, unsigned int layerIndex);
diff --git a/src/armnnDeserializer/DeserializerSupport.md b/src/armnnDeserializer/DeserializerSupport.md
index 31a5356..c8c0d6c 100644
--- a/src/armnnDeserializer/DeserializerSupport.md
+++ b/src/armnnDeserializer/DeserializerSupport.md
@@ -12,6 +12,7 @@
 * Constant
 * Convolution2d
 * DepthwiseConvolution2d
+* Division
 * FullyConnected
 * Multiplication
 * Permute
diff --git a/src/armnnDeserializer/test/DeserializeDivision.cpp b/src/armnnDeserializer/test/DeserializeDivision.cpp
new file mode 100644
index 0000000..dc6f582
--- /dev/null
+++ b/src/armnnDeserializer/test/DeserializeDivision.cpp
@@ -0,0 +1,160 @@
+//
+// 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 DivisionFixture : public ParserFlatbuffersSerializeFixture
+{
+    explicit DivisionFixture(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: "DivisionLayer",
+                layer : {
+                        base: {
+                             index:2,
+                             layerName: "DivisionLayer",
+                             layerType: "Division",
+                             inputSlots: [
+                                            {
+                                             index: 0,
+                                             connection: {sourceLayerIndex:0, outputSlotIndex:0 },
+                                            },
+                                            {
+                                             index: 1,
+                                             connection: {sourceLayerIndex:1, outputSlotIndex:0 },
+                                            }
+                             ],
+                             outputSlots: [ {
+                                 index: 0,
+                                 tensorInfo: {
+                                     dimensions: )" + outputShape + R"(,
+                                     dataType: )" + dataType + R"(
+                                 },
+                             }],
+                            }},
+                },
+                {
+                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: )" + dataType + R"(
+                                        },
+                                }],
+                            }}},
+                }]
+         }
+        )";
+        Setup();
+    }
+};
+
+
+struct SimpleDivisionFixture : DivisionFixture
+{
+    SimpleDivisionFixture() : DivisionFixture("[ 2, 2 ]",
+                                              "[ 2, 2 ]",
+                                              "[ 2, 2 ]",
+                                              "QuantisedAsymm8") {}
+};
+
+struct SimpleDivisionFixture2 : DivisionFixture
+{
+    SimpleDivisionFixture2() : DivisionFixture("[ 2, 2, 1, 1 ]",
+                                               "[ 2, 2, 1, 1 ]",
+                                               "[ 2, 2, 1, 1 ]",
+                                               "Float32") {}
+};
+
+BOOST_FIXTURE_TEST_CASE(DivisionQuantisedAsymm8, SimpleDivisionFixture)
+{
+    RunTest<2, armnn::DataType::QuantisedAsymm8>(
+        0,
+        {{"InputLayer1", { 0, 5, 24, 21 }},
+         {"InputLayer2", { 4, 1, 6,  7 }}},
+        {{"OutputLayer", { 0, 5, 3,  3 }}});
+}
+
+BOOST_FIXTURE_TEST_CASE(DivisionFloat32, SimpleDivisionFixture2)
+{
+    RunTest<4, armnn::DataType::Float32>(
+        0,
+        {{"InputLayer1", { 100, 40, 226, 9 }},
+         {"InputLayer2", { 5,   8,  1,   3 }}},
+        {{"OutputLayer", { 20,  5,  226, 3 }}});
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/armnnSerializer/ArmnnSchema.fbs b/src/armnnSerializer/ArmnnSchema.fbs
index 7d64dca..95b4cdc 100644
--- a/src/armnnSerializer/ArmnnSchema.fbs
+++ b/src/armnnSerializer/ArmnnSchema.fbs
@@ -95,7 +95,8 @@
     FullyConnected = 11,
     Constant = 12,
     SpaceToBatchNd = 13,
-    BatchToSpaceNd = 14
+    BatchToSpaceNd = 14,
+    Division = 15
 }
 
 // Base layer table to be used as part of other layers
@@ -151,6 +152,10 @@
     dataLayout:DataLayout = NCHW;
 }
 
+table DivisionLayer {
+    base:LayerBase;
+}
+
 table FullyConnectedLayer {
     base:LayerBase;
     descriptor:FullyConnectedDescriptor;
@@ -293,7 +298,8 @@
     Pooling2dLayer,
     ReshapeLayer,
     SoftmaxLayer,
-    SpaceToBatchNdLayer
+    SpaceToBatchNdLayer,
+    DivisionLayer
 }
 
 table AnyLayer {
diff --git a/src/armnnSerializer/Serializer.cpp b/src/armnnSerializer/Serializer.cpp
index dc25bab..5165d17 100644
--- a/src/armnnSerializer/Serializer.cpp
+++ b/src/armnnSerializer/Serializer.cpp
@@ -260,6 +260,20 @@
     CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_DepthwiseConvolution2dLayer);
 }
 
+// Build FlatBuffer for Division Layer
+void SerializerVisitor::VisitDivisionLayer(const armnn::IConnectableLayer* layer, const char* name)
+{
+    // Create FlatBuffer BaseLayer
+    auto flatBufferDivisionBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Division);
+
+    // Create the FlatBuffer DivisionLayer
+    auto flatBufferDivisionLayer =
+        serializer::CreateDivisionLayer(m_flatBufferBuilder, flatBufferDivisionBaseLayer);
+
+    // Add the AnyLayer to the FlatBufferLayers
+    CreateAnyLayer(flatBufferDivisionLayer.o, serializer::Layer::Layer_DivisionLayer);
+}
+
 // 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 f8e2b16..6715951 100644
--- a/src/armnnSerializer/Serializer.hpp
+++ b/src/armnnSerializer/Serializer.hpp
@@ -69,6 +69,9 @@
                                           const armnn::Optional<armnn::ConstTensor>& biases,
                                           const char* name = nullptr) override;
 
+    void VisitDivisionLayer(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 6340dd2..c6d7b85 100644
--- a/src/armnnSerializer/SerializerSupport.md
+++ b/src/armnnSerializer/SerializerSupport.md
@@ -12,6 +12,7 @@
 * Constant
 * Convolution2d
 * DepthwiseConvolution2d
+* Division
 * FullyConnected
 * Multiplication
 * Permute
diff --git a/src/armnnSerializer/test/SerializerTests.cpp b/src/armnnSerializer/test/SerializerTests.cpp
index 8ce9049..13fb0b2 100644
--- a/src/armnnSerializer/test/SerializerTests.cpp
+++ b/src/armnnSerializer/test/SerializerTests.cpp
@@ -728,4 +728,49 @@
                                             outputTensorInfo.GetShape());
 }
 
+BOOST_AUTO_TEST_CASE(SerializeDivision)
+{
+    class VerifyDivisionName : public armnn::LayerVisitorBase<armnn::VisitorNoThrowPolicy>
+    {
+    public:
+        void VisitDivisionLayer(const armnn::IConnectableLayer*, const char* name) override
+        {
+            BOOST_TEST(name == "division");
+        }
+    };
+
+    const armnn::TensorInfo info({ 1, 5, 2, 3 }, armnn::DataType::Float32);
+
+    armnn::INetworkPtr network = armnn::INetwork::Create();
+    armnn::IConnectableLayer* const inputLayer0 = network->AddInputLayer(0);
+    armnn::IConnectableLayer* const inputLayer1 = network->AddInputLayer(1);
+
+    const char* divLayerName = "division";
+
+    armnn::IConnectableLayer* const divisionLayer = network->AddDivisionLayer(divLayerName);
+    inputLayer0->GetOutputSlot(0).Connect(divisionLayer->GetInputSlot(0));
+    inputLayer1->GetOutputSlot(0).Connect(divisionLayer->GetInputSlot(1));
+
+    armnn::IConnectableLayer* const outputLayer = network->AddOutputLayer(0);
+    divisionLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0));
+
+    inputLayer0->GetOutputSlot(0).SetTensorInfo(info);
+    inputLayer1->GetOutputSlot(0).SetTensorInfo(info);
+    divisionLayer->GetOutputSlot(0).SetTensorInfo(info);
+
+    armnnSerializer::Serializer serializer;
+    serializer.Serialize(*network);
+
+    std::stringstream stream;
+    serializer.SaveSerializedToStream(stream);
+    BOOST_TEST(stream.str().length() > 0);
+    BOOST_TEST(stream.str().find(divLayerName) != stream.str().npos);
+
+    armnn::INetworkPtr deserializedNetwork = DeserializeNetwork(stream.str());
+    BOOST_CHECK(deserializedNetwork);
+
+    VerifyDivisionName nameChecker;
+    deserializedNetwork->Accept(nameChecker);
+}
+
 BOOST_AUTO_TEST_SUITE_END()