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()