IVGCVSW-3739 Add serialization support for Abs
authorFinnWilliamsArm <Finn.Williams@arm.com>
Thu, 5 Sep 2019 13:34:20 +0000 (14:34 +0100)
committerfinn.williams <finn.williams@arm.com>
Tue, 10 Sep 2019 13:20:58 +0000 (13:20 +0000)
Signed-off-by: FinnWilliamsArm <Finn.Williams@arm.com>
Change-Id: I42c348515f3f93efc2e0570bbebdc77306f12468

CMakeLists.txt
src/armnnDeserializer/Deserializer.cpp
src/armnnDeserializer/Deserializer.hpp
src/armnnDeserializer/DeserializerSupport.md
src/armnnDeserializer/test/DeserializeAbs.cpp [new file with mode: 0644]
src/armnnSerializer/ArmnnSchema.fbs
src/armnnSerializer/Serializer.cpp
src/armnnSerializer/SerializerSupport.md
src/armnnSerializer/test/SerializerTests.cpp

index 217c420..7fd05eb 100644 (file)
@@ -695,6 +695,7 @@ if(BUILD_UNIT_TESTS)
         list(APPEND unittest_sources
             src/armnnSerializer/test/ActivationSerializationTests.cpp
             src/armnnSerializer/test/SerializerTests.cpp
+            src/armnnDeserializer/test/DeserializeAbs.cpp
             src/armnnDeserializer/test/DeserializeActivation.cpp
             src/armnnDeserializer/test/DeserializeAdd.cpp
             src/armnnDeserializer/test/DeserializeBatchToSpaceNd.cpp
index ef12357..8572023 100644 (file)
@@ -185,6 +185,7 @@ Deserializer::Deserializer()
 m_ParserFunctions(Layer_MAX+1, &Deserializer::ParseUnsupportedLayer)
 {
     // register supported layers
+    m_ParserFunctions[Layer_AbsLayer]                    = &Deserializer::ParseAbs;
     m_ParserFunctions[Layer_ActivationLayer]             = &Deserializer::ParseActivation;
     m_ParserFunctions[Layer_AdditionLayer]               = &Deserializer::ParseAdd;
     m_ParserFunctions[Layer_BatchToSpaceNdLayer]         = &Deserializer::ParseBatchToSpaceNd;
@@ -237,6 +238,8 @@ Deserializer::LayerBaseRawPtr Deserializer::GetBaseLayer(const GraphPtr& graphPt
 
     switch(layerType)
     {
+        case Layer::Layer_AbsLayer:
+            return graphPtr->layers()->Get(layerIndex)->layer_as_AbsLayer()->base();
         case Layer::Layer_ActivationLayer:
             return graphPtr->layers()->Get(layerIndex)->layer_as_ActivationLayer()->base();
         case Layer::Layer_AdditionLayer:
@@ -851,6 +854,26 @@ void Deserializer::RegisterOutputSlotOfConnection(uint32_t sourceLayerIndex,
     connections.outputSlots[outputSlotIndex] = outputSlot;
 }
 
+void Deserializer::ParseAbs(armnnDeserializer::Deserializer::GraphPtr graph, unsigned int layerIndex)
+{
+    CHECK_LAYERS(graph, 0, layerIndex);
+    auto inputs = GetInputs(graph, layerIndex);
+    CHECK_LOCATION();
+    CHECK_VALID_SIZE(inputs.size(), 1);
+
+    auto outputs = GetOutputs(graph, layerIndex);
+    CHECK_VALID_SIZE(outputs.size(), 1);
+
+    auto layerName = GetLayerName(graph, layerIndex);
+
+    IConnectableLayer* layer = m_Network->AddAbsLayer(layerName.c_str());
+    armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
+    layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
+
+    RegisterInputSlots(graph, layerIndex, layer);
+    RegisterOutputSlots(graph, layerIndex, layer);
+}
+
 void Deserializer::ParseActivation(GraphPtr graph, unsigned int layerIndex)
 {
     CHECK_LAYERS(graph, 0, layerIndex);
index 591447d..9ec59fb 100644 (file)
@@ -77,6 +77,7 @@ private:
     using LayerParsingFunction = void(Deserializer::*)(GraphPtr graph, unsigned int layerIndex);
 
     void ParseUnsupportedLayer(GraphPtr graph, unsigned int layerIndex);
+    void ParseAbs(GraphPtr graph, unsigned int layerIndex);
     void ParseActivation(GraphPtr graph, unsigned int layerIndex);
     void ParseAdd(GraphPtr graph, unsigned int layerIndex);
     void ParseBatchToSpaceNd(GraphPtr graph, unsigned int layerIndex);
index 1bda123..c780071 100644 (file)
@@ -6,6 +6,7 @@ This reference guide provides a list of layers which can be deserialized current
 
 The Arm NN SDK Deserialize parser currently supports the following layers:
 
+* Abs
 * Activation
 * Addition
 * BatchToSpaceNd
diff --git a/src/armnnDeserializer/test/DeserializeAbs.cpp b/src/armnnDeserializer/test/DeserializeAbs.cpp
new file mode 100644 (file)
index 0000000..4d6504b
--- /dev/null
@@ -0,0 +1,120 @@
+//
+// 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 AbsFixture : public ParserFlatbuffersSerializeFixture
+    {
+        explicit AbsFixture(const std::string &inputShape,
+                             const std::string &outputShape,
+                             const std::string &dataType)
+        {
+            m_JsonString = R"(
+            {
+                inputIds: [0],
+                outputIds: [2],
+                layers: [
+                    {
+                        layer_type: "InputLayer",
+                        layer: {
+                            base: {
+                                layerBindingId: 0,
+                                base: {
+                                    index: 0,
+                                    layerName: "InputLayer",
+                                    layerType: "Input",
+                                    inputSlots: [{
+                                        index: 0,
+                                        connection: {sourceLayerIndex:0, outputSlotIndex:0 },
+                                    }],
+                                    outputSlots: [{
+                                        index: 0,
+                                        tensorInfo: {
+                                            dimensions: )" + inputShape + R"(,
+                                            dataType: )" + dataType + R"(
+                                        }
+                                    }]
+                                }
+                            }
+                        }
+                    },
+                    {
+                        layer_type: "AbsLayer",
+                        layer: {
+                            base: {
+                                index: 1,
+                                layerName: "AbsLayer",
+                                layerType: "Abs",
+                                inputSlots: [{
+                                    index: 0,
+                                    connection: {sourceLayerIndex:0, outputSlotIndex:0 },
+                                }],
+                                outputSlots: [{
+                                    index: 0,
+                                    tensorInfo: {
+                                        dimensions: )" + outputShape + R"(,
+                                        dataType: )" + dataType + R"(
+                                    }
+                                }]
+                            }
+
+                        }
+                    },
+                    {
+                        layer_type: "OutputLayer",
+                        layer: {
+                            base:{
+                                layerBindingId: 2,
+                                base: {
+                                    index: 2,
+                                    layerName: "OutputLayer",
+                                    layerType: "Output",
+                                    inputSlots: [{
+                                        index: 0,
+                                        connection: {sourceLayerIndex:1, outputSlotIndex:0 },
+                                    }],
+                                    outputSlots: [{
+                                        index: 0,
+                                        tensorInfo: {
+                                            dimensions: )" + outputShape + R"(,
+                                            dataType: )" + dataType + R"(
+                                        },
+                                    }],
+                                }
+                            }
+                        },
+                    }
+                ]
+            }
+        )";
+            Setup();
+        }
+    };
+
+    struct SimpleAbsFixture : AbsFixture
+    {
+        SimpleAbsFixture()
+                : AbsFixture("[ 1, 2, 2, 2 ]",     // inputShape
+                              "[ 1, 2, 2, 2 ]",     // outputShape
+                              "Float32")            // dataType
+        {}
+    };
+
+    BOOST_FIXTURE_TEST_CASE(SimpleAbsTest, SimpleAbsFixture)
+    {
+        RunTest<4, armnn::DataType::Float32>(
+                0,
+                {{"InputLayer",  { -100.0f, -50.5f, -25.9999f, -0.5f , 0.0f, 1.5555f, 25.5f, 100.0f }}},
+                {{"OutputLayer", { 100.0f, 50.0f, 25.9999f, 0.5f , 0.0f, 1.5555f, 25.5f, 100.0f }}});
+    }
+
+BOOST_AUTO_TEST_SUITE_END()
\ No newline at end of file
index 513c74e..2b3ec49 100644 (file)
@@ -132,7 +132,8 @@ enum LayerType : uint {
     TransposeConvolution2d = 42,
     Resize = 43,
     Stack = 44,
-    QuantizedLstm = 45
+    QuantizedLstm = 45,
+    Abs = 46
 }
 
 // Base layer table to be used as part of other layers
@@ -150,6 +151,11 @@ table BindableLayerBase {
 }
 
 // Table for each layer defined below
+
+table AbsLayer {
+    base:LayerBase;
+}
+
 table ActivationLayer {
     base:LayerBase;
     descriptor:ActivationDescriptor;
@@ -686,7 +692,8 @@ union Layer {
     PreluLayer,
     TransposeConvolution2dLayer,
     ResizeLayer,
-    StackLayer
+    StackLayer,
+    AbsLayer
 }
 
 table AnyLayer {
index a8d9c23..595573c 100644 (file)
@@ -107,7 +107,10 @@ void SerializerVisitor::VisitOutputLayer(const armnn::IConnectableLayer* layer,
 
 void SerializerVisitor::VisitAbsLayer(const armnn::IConnectableLayer* layer, const char* name)
 {
-    throw UnimplementedException("SerializerVisitor::VisitAbsLayer is not implemented");
+    auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Abs);
+    auto flatBufferAbsLayer  = serializer::CreateAbsLayer(m_flatBufferBuilder, flatBufferBaseLayer);
+
+    CreateAnyLayer(flatBufferAbsLayer.o, serializer::Layer::Layer_AbsLayer);
 }
 
 // Build FlatBuffer for Activation Layer
index f81aa71..9babe25 100644 (file)
@@ -6,6 +6,7 @@ This reference guide provides a list of layers which can be serialized currently
 
 The Arm NN SDK Serializer currently supports the following layers:
 
+* Abs
 * Activation
 * Addition
 * BatchToSpaceNd
index 79d83f0..b5ef8c6 100644 (file)
@@ -210,6 +210,44 @@ static std::vector<DataType> GenerateRandomData(size_t size)
 
 BOOST_AUTO_TEST_SUITE(SerializerTests)
 
+BOOST_AUTO_TEST_CASE(SerializeAbs)
+{
+    class AbsLayerVerifier : public LayerVerifierBase
+    {
+    public:
+        AbsLayerVerifier(const std::string& layerName,
+                              const std::vector<armnn::TensorInfo>& inputInfos,
+                              const std::vector<armnn::TensorInfo>& outputInfos)
+                : LayerVerifierBase(layerName, inputInfos, outputInfos) {}
+
+        void VisitAbsLayer(const armnn::IConnectableLayer* layer, const char* name) override
+        {
+            VerifyNameAndConnections(layer, name);
+        }
+    };
+
+    const std::string layerName("abs");
+    const armnn::TensorInfo tensorInfo({1, 2, 3}, armnn::DataType::Float32);
+
+    armnn::INetworkPtr network = armnn::INetwork::Create();
+    armnn::IConnectableLayer* const inputLayer = network->AddInputLayer(0);
+
+    armnn::IConnectableLayer* const absLayer = network->AddAbsLayer(layerName.c_str());
+    armnn::IConnectableLayer* const outputLayer = network->AddOutputLayer(0);
+
+    inputLayer->GetOutputSlot(0).Connect(absLayer->GetInputSlot(0));
+    absLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0));
+
+    inputLayer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
+    absLayer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
+
+    armnn::INetworkPtr deserializedNetwork = DeserializeNetwork(SerializeNetwork(*network));
+    BOOST_CHECK(deserializedNetwork);
+
+    AbsLayerVerifier verifier(layerName, {tensorInfo}, {tensorInfo});
+    deserializedNetwork->Accept(verifier);
+}
+
 BOOST_AUTO_TEST_CASE(SerializeAddition)
 {
     class AdditionLayerVerifier : public LayerVerifierBase