From 5888589ec877d61d21848816c6eacdde5dc068d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89anna=20=C3=93=20Cath=C3=A1in?= Date: Wed, 27 Feb 2019 16:16:39 +0000 Subject: IVGCVSW-2687 Add Serializer & Deserializer for Division MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I230220eccbfb22e56e8d351cc963988d734836ac Signed-off-by: Éanna Ó Catháin --- CMakeLists.txt | 1 + src/armnnDeserializer/Deserializer.cpp | 23 +++ src/armnnDeserializer/Deserializer.hpp | 1 + src/armnnDeserializer/DeserializerSupport.md | 1 + src/armnnDeserializer/test/DeserializeDivision.cpp | 160 +++++++++++++++++++++ src/armnnSerializer/ArmnnSchema.fbs | 10 +- src/armnnSerializer/Serializer.cpp | 14 ++ src/armnnSerializer/Serializer.hpp | 3 + src/armnnSerializer/SerializerSupport.md | 1 + src/armnnSerializer/test/SerializerTests.cpp | 45 ++++++ 10 files changed, 257 insertions(+), 2 deletions(-) create mode 100644 src/armnnDeserializer/test/DeserializeDivision.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 639d745228..ecc633734f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -600,6 +600,7 @@ if(BUILD_UNIT_TESTS) src/armnnDeserializer/test/DeserializeBatchToSpaceNd.cpp src/armnnDeserializer/test/DeserializeConstant.cpp src/armnnDeserializer/test/DeserializeConvolution2d.cpp + src/armnnDeserializer/test/DeserializeDivision.cpp src/armnnDeserializer/test/DeserializeFullyConnected.cpp src/armnnDeserializer/test/DeserializeMultiplication.cpp src/armnnDeserializer/test/DeserializePermute.cpp diff --git a/src/armnnDeserializer/Deserializer.cpp b/src/armnnDeserializer/Deserializer.cpp index 30f7d05c93..1ef5651032 100644 --- a/src/armnnDeserializer/Deserializer.cpp +++ b/src/armnnDeserializer/Deserializer.cpp @@ -191,6 +191,7 @@ m_ParserFunctions(Layer_MAX+1, &Deserializer::ParseUnsupportedLayer) 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 @@ Deserializer::LayerBaseRawPtr Deserializer::GetBaseLayer(const GraphPtr& graphPt 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 @@ void Deserializer::ParseDepthwiseConvolution2d(GraphPtr graph, unsigned int laye 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 0e0261f27b..cb0590bc5f 100644 --- a/src/armnnDeserializer/Deserializer.hpp +++ b/src/armnnDeserializer/Deserializer.hpp @@ -74,6 +74,7 @@ private: 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 31a53569ad..c8c0d6c9d3 100644 --- a/src/armnnDeserializer/DeserializerSupport.md +++ b/src/armnnDeserializer/DeserializerSupport.md @@ -12,6 +12,7 @@ The Arm NN SDK Deserialize parser currently supports the following layers: * 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 0000000000..dc6f5820cf --- /dev/null +++ b/src/armnnDeserializer/test/DeserializeDivision.cpp @@ -0,0 +1,160 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include +#include "ParserFlatbuffersSerializeFixture.hpp" +#include "../Deserializer.hpp" + +#include +#include + +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 7d64dca6f3..95b4cdc1c4 100644 --- a/src/armnnSerializer/ArmnnSchema.fbs +++ b/src/armnnSerializer/ArmnnSchema.fbs @@ -95,7 +95,8 @@ enum LayerType : uint { 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 @@ table Convolution2dDescriptor { dataLayout:DataLayout = NCHW; } +table DivisionLayer { + base:LayerBase; +} + table FullyConnectedLayer { base:LayerBase; descriptor:FullyConnectedDescriptor; @@ -293,7 +298,8 @@ union Layer { Pooling2dLayer, ReshapeLayer, SoftmaxLayer, - SpaceToBatchNdLayer + SpaceToBatchNdLayer, + DivisionLayer } table AnyLayer { diff --git a/src/armnnSerializer/Serializer.cpp b/src/armnnSerializer/Serializer.cpp index dc25bab0c6..5165d178ec 100644 --- a/src/armnnSerializer/Serializer.cpp +++ b/src/armnnSerializer/Serializer.cpp @@ -260,6 +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); + + // 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 f8e2b16bf9..6715951d55 100644 --- a/src/armnnSerializer/Serializer.hpp +++ b/src/armnnSerializer/Serializer.hpp @@ -69,6 +69,9 @@ public: const armnn::Optional& 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 6340dd2e9a..c6d7b85995 100644 --- a/src/armnnSerializer/SerializerSupport.md +++ b/src/armnnSerializer/SerializerSupport.md @@ -12,6 +12,7 @@ The Arm NN SDK Serializer currently supports the following layers: * Constant * Convolution2d * DepthwiseConvolution2d +* Division * FullyConnected * Multiplication * Permute diff --git a/src/armnnSerializer/test/SerializerTests.cpp b/src/armnnSerializer/test/SerializerTests.cpp index 8ce90499df..13fb0b22f8 100644 --- a/src/armnnSerializer/test/SerializerTests.cpp +++ b/src/armnnSerializer/test/SerializerTests.cpp @@ -728,4 +728,49 @@ BOOST_AUTO_TEST_CASE(SerializeDeserializeBatchToSpaceNd) outputTensorInfo.GetShape()); } +BOOST_AUTO_TEST_CASE(SerializeDivision) +{ + class VerifyDivisionName : public armnn::LayerVisitorBase + { + 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() -- cgit v1.2.1