From 0fe3245e8b8eea98d25cb41bcf9b15c8d4bfa915 Mon Sep 17 00:00:00 2001 From: Aron Virginas-Tar Date: Thu, 28 Feb 2019 13:12:47 +0000 Subject: IVGCVSW-2698 Serialize / deserialize the Minimum layer Signed-off-by: Aron Virginas-Tar Change-Id: I8a931c920ff38506c210e9369b81fae150096776 --- src/armnnDeserializer/Deserializer.cpp | 25 +++++++++++++- src/armnnDeserializer/Deserializer.hpp | 1 + src/armnnDeserializer/DeserializerSupport.md | 1 + src/armnnSerializer/ArmnnSchema.fbs | 10 ++++-- src/armnnSerializer/Serializer.cpp | 18 +++++----- src/armnnSerializer/Serializer.hpp | 3 ++ src/armnnSerializer/SerializerSupport.md | 1 + src/armnnSerializer/test/SerializerTests.cpp | 51 ++++++++++++++++++++++++++++ 8 files changed, 99 insertions(+), 11 deletions(-) diff --git a/src/armnnDeserializer/Deserializer.cpp b/src/armnnDeserializer/Deserializer.cpp index 1ef5651032..28c6c94c7a 100644 --- a/src/armnnDeserializer/Deserializer.cpp +++ b/src/armnnDeserializer/Deserializer.cpp @@ -193,6 +193,7 @@ m_ParserFunctions(Layer_MAX+1, &Deserializer::ParseUnsupportedLayer) m_ParserFunctions[Layer_DepthwiseConvolution2dLayer] = &Deserializer::ParseDepthwiseConvolution2d; m_ParserFunctions[Layer_DivisionLayer] = &Deserializer::ParseDivision; m_ParserFunctions[Layer_FullyConnectedLayer] = &Deserializer::ParseFullyConnected; + m_ParserFunctions[Layer_MinimumLayer] = &Deserializer::ParseMinimum; m_ParserFunctions[Layer_MultiplicationLayer] = &Deserializer::ParseMultiplication; m_ParserFunctions[Layer_PermuteLayer] = &Deserializer::ParsePermute; m_ParserFunctions[Layer_Pooling2dLayer] = &Deserializer::ParsePooling2d; @@ -224,7 +225,9 @@ Deserializer::LayerBaseRawPtr Deserializer::GetBaseLayer(const GraphPtr& graphPt case Layer::Layer_FullyConnectedLayer: return graphPtr->layers()->Get(layerIndex)->layer_as_FullyConnectedLayer()->base(); case Layer::Layer_InputLayer: - return graphPtr->layers()->Get(layerIndex)->layer_as_InputLayer()->base()->base(); + return graphPtr->layers()->Get(layerIndex)->layer_as_InputLayer()->base()->base(); + case Layer::Layer_MinimumLayer: + return graphPtr->layers()->Get(layerIndex)->layer_as_MinimumLayer()->base(); case Layer::Layer_MultiplicationLayer: return graphPtr->layers()->Get(layerIndex)->layer_as_MultiplicationLayer()->base(); case Layer::Layer_OutputLayer: @@ -955,6 +958,26 @@ void Deserializer::ParseDivision(GraphPtr graph, unsigned int layerIndex) RegisterOutputSlots(graph, layerIndex, layer); } +void Deserializer::ParseMinimum(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->AddMinimumLayer(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 cb0590bc5f..657c7f5ed8 100644 --- a/src/armnnDeserializer/Deserializer.hpp +++ b/src/armnnDeserializer/Deserializer.hpp @@ -76,6 +76,7 @@ private: void ParseDepthwiseConvolution2d(GraphPtr graph, unsigned int layerIndex); void ParseDivision(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); void ParsePermute(GraphPtr graph, unsigned int layerIndex); void ParsePooling2d(GraphPtr graph, unsigned int layerIndex); diff --git a/src/armnnDeserializer/DeserializerSupport.md b/src/armnnDeserializer/DeserializerSupport.md index c8c0d6c9d3..8fd7b421a3 100644 --- a/src/armnnDeserializer/DeserializerSupport.md +++ b/src/armnnDeserializer/DeserializerSupport.md @@ -14,6 +14,7 @@ The Arm NN SDK Deserialize parser currently supports the following layers: * DepthwiseConvolution2d * Division * FullyConnected +* Minimum * Multiplication * Permute * Pooling2d diff --git a/src/armnnSerializer/ArmnnSchema.fbs b/src/armnnSerializer/ArmnnSchema.fbs index 95b4cdc1c4..501953c60b 100644 --- a/src/armnnSerializer/ArmnnSchema.fbs +++ b/src/armnnSerializer/ArmnnSchema.fbs @@ -96,7 +96,8 @@ enum LayerType : uint { Constant = 12, SpaceToBatchNd = 13, BatchToSpaceNd = 14, - Division = 15 + Division = 15, + Minimum = 16 } // Base layer table to be used as part of other layers @@ -172,6 +173,10 @@ table InputLayer { base:BindableLayerBase; } +table MinimumLayer { + base:LayerBase; +} + table MultiplicationLayer { base:LayerBase; } @@ -299,7 +304,8 @@ union Layer { ReshapeLayer, SoftmaxLayer, SpaceToBatchNdLayer, - DivisionLayer + DivisionLayer, + MinimumLayer } table AnyLayer { diff --git a/src/armnnSerializer/Serializer.cpp b/src/armnnSerializer/Serializer.cpp index 5165d178ec..8b8a88dfbc 100644 --- a/src/armnnSerializer/Serializer.cpp +++ b/src/armnnSerializer/Serializer.cpp @@ -260,18 +260,20 @@ void SerializerVisitor::VisitDepthwiseConvolution2dLayer(const armnn::IConnectab 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); + auto fbDivisionBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Division); + auto fbDivisionLayer = serializer::CreateDivisionLayer(m_flatBufferBuilder, fbDivisionBaseLayer); - // Create the FlatBuffer DivisionLayer - auto flatBufferDivisionLayer = - serializer::CreateDivisionLayer(m_flatBufferBuilder, flatBufferDivisionBaseLayer); + CreateAnyLayer(fbDivisionLayer.o, serializer::Layer::Layer_DivisionLayer); +} - // Add the AnyLayer to the FlatBufferLayers - CreateAnyLayer(flatBufferDivisionLayer.o, serializer::Layer::Layer_DivisionLayer); +void SerializerVisitor::VisitMinimumLayer(const armnn::IConnectableLayer* layer, const char* name) +{ + auto fbMinimumBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Minimum); + auto fbMinimumLayer = serializer::CreateMinimumLayer(m_flatBufferBuilder, fbMinimumBaseLayer); + + CreateAnyLayer(fbMinimumLayer.o, serializer::Layer::Layer_MinimumLayer); } // Build FlatBuffer for Multiplication Layer diff --git a/src/armnnSerializer/Serializer.hpp b/src/armnnSerializer/Serializer.hpp index 6715951d55..ecc966cd6c 100644 --- a/src/armnnSerializer/Serializer.hpp +++ b/src/armnnSerializer/Serializer.hpp @@ -82,6 +82,9 @@ public: armnn::LayerBindingId id, const char* name = nullptr) override; + void VisitMinimumLayer(const armnn::IConnectableLayer* layer, + const char* name = nullptr) override; + void VisitMultiplicationLayer(const armnn::IConnectableLayer* layer, const char* name = nullptr) override; diff --git a/src/armnnSerializer/SerializerSupport.md b/src/armnnSerializer/SerializerSupport.md index c6d7b85995..31f631030c 100644 --- a/src/armnnSerializer/SerializerSupport.md +++ b/src/armnnSerializer/SerializerSupport.md @@ -14,6 +14,7 @@ The Arm NN SDK Serializer currently supports the following layers: * DepthwiseConvolution2d * Division * FullyConnected +* Minimum * Multiplication * Permute * Pooling2d diff --git a/src/armnnSerializer/test/SerializerTests.cpp b/src/armnnSerializer/test/SerializerTests.cpp index 282b87bc85..4b0077cd65 100644 --- a/src/armnnSerializer/test/SerializerTests.cpp +++ b/src/armnnSerializer/test/SerializerTests.cpp @@ -255,6 +255,57 @@ BOOST_AUTO_TEST_CASE(SerializeDeserializeConstant) {commonTensorInfo.GetShape()}); } +BOOST_AUTO_TEST_CASE(SerializeMinimum) +{ + class VerifyMinimumName : public armnn::LayerVisitorBase + { + public: + explicit VerifyMinimumName(const std::string& expectedMinimumLayerName) + : m_ExpectedMinimumLayerName(expectedMinimumLayerName) {} + + void VisitMinimumLayer(const armnn::IConnectableLayer*, const char* name) override + { + BOOST_TEST(name == m_ExpectedMinimumLayerName.c_str()); + } + + private: + std::string m_ExpectedMinimumLayerName; + }; + + const armnn::TensorInfo info({ 1, 2, 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 std::string minimumLayerName("minimum"); + + armnn::IConnectableLayer* const minimumLayer = network->AddMinimumLayer(minimumLayerName.c_str()); + inputLayer0->GetOutputSlot(0).Connect(minimumLayer->GetInputSlot(0)); + inputLayer1->GetOutputSlot(0).Connect(minimumLayer->GetInputSlot(1)); + + armnn::IConnectableLayer* const outputLayer = network->AddOutputLayer(0); + minimumLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0)); + + inputLayer0->GetOutputSlot(0).SetTensorInfo(info); + inputLayer1->GetOutputSlot(0).SetTensorInfo(info); + minimumLayer->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(minimumLayerName) != stream.str().npos); + + armnn::INetworkPtr deserializedNetwork = DeserializeNetwork(stream.str()); + BOOST_CHECK(deserializedNetwork); + + VerifyMinimumName nameChecker(minimumLayerName); + deserializedNetwork->Accept(nameChecker); +} + BOOST_AUTO_TEST_CASE(SerializeMultiplication) { class VerifyMultiplicationName : public armnn::LayerVisitorBase -- cgit v1.2.1