IVGCVSW-2711 Add Serializer and Deserializer for Subtraction

Change-Id: I87836b5314c1f791b4df2ca90d239573ca28a2da
Signed-off-by: Conor Kennedy <conor.kennedy@arm.com>
Signed-off-by: Matteo Martincigh <matteo.martincigh@arm.com>
diff --git a/src/armnnDeserializer/Deserializer.cpp b/src/armnnDeserializer/Deserializer.cpp
index aa74543..73c2042 100644
--- a/src/armnnDeserializer/Deserializer.cpp
+++ b/src/armnnDeserializer/Deserializer.cpp
@@ -209,6 +209,7 @@
     m_ParserFunctions[Layer_RsqrtLayer]                  = &Deserializer::ParseRsqrt;
     m_ParserFunctions[Layer_SoftmaxLayer]                = &Deserializer::ParseSoftmax;
     m_ParserFunctions[Layer_SpaceToBatchNdLayer]         = &Deserializer::ParseSpaceToBatchNd;
+    m_ParserFunctions[Layer_SubtractionLayer]            = &Deserializer::ParseSubtraction;
 }
 
 Deserializer::LayerBaseRawPtr Deserializer::GetBaseLayer(const GraphPtr& graphPtr, unsigned int layerIndex)
@@ -269,6 +270,8 @@
             return graphPtr->layers()->Get(layerIndex)->layer_as_SoftmaxLayer()->base();
         case Layer::Layer_SpaceToBatchNdLayer:
             return graphPtr->layers()->Get(layerIndex)->layer_as_SpaceToBatchNdLayer()->base();
+        case Layer::Layer_SubtractionLayer:
+            return graphPtr->layers()->Get(layerIndex)->layer_as_SubtractionLayer()->base();
         case Layer::Layer_NONE:
         default:
             throw ParseException(boost::str(
@@ -1639,4 +1642,24 @@
     RegisterOutputSlots(graph, layerIndex, layer);
 }
 
+void Deserializer::ParseSubtraction(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->AddSubtractionLayer(layerName.c_str());
+
+    armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
+    layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
+
+    RegisterInputSlots(graph, layerIndex, layer);
+    RegisterOutputSlots(graph, layerIndex, layer);
+}
+
 } // namespace armnnDeserializer
diff --git a/src/armnnDeserializer/Deserializer.hpp b/src/armnnDeserializer/Deserializer.hpp
index cabc91f..7595818 100644
--- a/src/armnnDeserializer/Deserializer.hpp
+++ b/src/armnnDeserializer/Deserializer.hpp
@@ -95,6 +95,7 @@
     void ParseRsqrt(GraphPtr graph, unsigned int layerIndex);
     void ParseSoftmax(GraphPtr graph, unsigned int layerIndex);
     void ParseSpaceToBatchNd(GraphPtr graph, unsigned int layerIndex);
+    void ParseSubtraction(GraphPtr graph, unsigned int layerIndex);
 
     void RegisterOutputSlotOfConnection(uint32_t connectionIndex, armnn::IOutputSlot* slot);
     void RegisterInputSlotOfConnection(uint32_t connectionIndex, armnn::IInputSlot* slot);
diff --git a/src/armnnDeserializer/DeserializerSupport.md b/src/armnnDeserializer/DeserializerSupport.md
index 0a1ef75..42da558 100644
--- a/src/armnnDeserializer/DeserializerSupport.md
+++ b/src/armnnDeserializer/DeserializerSupport.md
@@ -30,5 +30,6 @@
 * Rsqrt
 * Softmax
 * SpaceToBatchNd
+* Subtraction
 
 More machine learning layers will be supported in future releases.
diff --git a/src/armnnDeserializer/test/DeserializeSubtraction.cpp b/src/armnnDeserializer/test/DeserializeSubtraction.cpp
new file mode 100644
index 0000000..5058bb8
--- /dev/null
+++ b/src/armnnDeserializer/test/DeserializeSubtraction.cpp
@@ -0,0 +1,176 @@
+//
+// 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 SubtractionFixture : public ParserFlatbuffersSerializeFixture
+{
+    explicit SubtractionFixture(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: "SubtractionLayer",
+            layer : {
+                    base: {
+                          index:2,
+                          layerName: "subtractionLayer",
+                          layerType: "Subtraction",
+                          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 SimpleSubtractionFixture : SubtractionFixture
+{
+    SimpleSubtractionFixture() : SubtractionFixture("[ 1, 4 ]",
+                                                    "[ 1, 4 ]",
+                                                    "[ 1, 4 ]",
+                                                    "QuantisedAsymm8") {}
+};
+
+struct SimpleSubtractionFixture2 : SubtractionFixture
+{
+    SimpleSubtractionFixture2() : SubtractionFixture("[ 1, 4 ]",
+                                                     "[ 1, 4 ]",
+                                                     "[ 1, 4 ]",
+                                                     "Float32") {}
+};
+
+struct SimpleSubtractionFixtureBroadcast : SubtractionFixture
+{
+    SimpleSubtractionFixtureBroadcast() : SubtractionFixture("[ 1, 4 ]",
+                                                             "[ 1, 1 ]",
+                                                             "[ 1, 4 ]",
+                                                             "Float32") {}
+};
+
+BOOST_FIXTURE_TEST_CASE(SubtractionQuantisedAsymm8, SimpleSubtractionFixture)
+{
+    RunTest<2, armnn::DataType::QuantisedAsymm8>(
+        0,
+        {{"inputLayer1", { 4, 5, 6, 7 }},
+         {"inputLayer2", { 3, 2, 1, 0 }}},
+        {{"outputLayer", { 1, 3, 5, 7 }}});
+}
+
+BOOST_FIXTURE_TEST_CASE(SubtractionFloat32, SimpleSubtractionFixture2)
+{
+    RunTest<2, armnn::DataType::Float32>(
+        0,
+        {{"inputLayer1", { 4, 5, 6, 7 }},
+         {"inputLayer2", { 3, 2, 1, 0 }}},
+        {{"outputLayer", { 1, 3, 5, 7 }}});
+}
+
+BOOST_FIXTURE_TEST_CASE(SubtractionBroadcast, SimpleSubtractionFixtureBroadcast)
+{
+    RunTest<2, armnn::DataType::Float32>(
+        0,
+        {{"inputLayer1", { 4, 5, 6, 7 }},
+         {"inputLayer2", { 2 }}},
+        {{"outputLayer", { 2, 3, 4, 5 }}});
+}
+
+BOOST_AUTO_TEST_SUITE_END()