From ebb0f9c1dd97b43c0495eab4f2d4414e2fa3d4b1 Mon Sep 17 00:00:00 2001 From: Nattapat Chaimanowong Date: Fri, 1 Mar 2019 12:14:06 +0000 Subject: IVGCVSW-2701 Add Serializer and Deserializer for Pad Change-Id: I71184236f0394518f29944a77d4b934cbde9e53d Signed-off-by: Nattapat Chaimanowong --- CMakeLists.txt | 1 + src/armnnDeserializer/Deserializer.cpp | 41 ++++++++ src/armnnDeserializer/Deserializer.hpp | 1 + src/armnnDeserializer/DeserializerSupport.md | 3 +- src/armnnDeserializer/test/DeserializePad.cpp | 129 ++++++++++++++++++++++++++ src/armnnSerializer/ArmnnSchema.fbs | 15 ++- src/armnnSerializer/Serializer.cpp | 23 +++++ src/armnnSerializer/Serializer.hpp | 4 + src/armnnSerializer/SerializerSupport.md | 3 +- src/armnnSerializer/test/SerializerTests.cpp | 41 ++++++++ 10 files changed, 257 insertions(+), 4 deletions(-) create mode 100644 src/armnnDeserializer/test/DeserializePad.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a21b5f21e8..037a9aee2c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -605,6 +605,7 @@ if(BUILD_UNIT_TESTS) src/armnnDeserializer/test/DeserializeFullyConnected.cpp src/armnnDeserializer/test/DeserializeMultiplication.cpp src/armnnDeserializer/test/DeserializeNormalization.cpp + src/armnnDeserializer/test/DeserializePad.cpp src/armnnDeserializer/test/DeserializePermute.cpp src/armnnDeserializer/test/DeserializePooling2d.cpp src/armnnDeserializer/test/DeserializeReshape.cpp diff --git a/src/armnnDeserializer/Deserializer.cpp b/src/armnnDeserializer/Deserializer.cpp index c7049f6a15..b8a1eaa84a 100644 --- a/src/armnnDeserializer/Deserializer.cpp +++ b/src/armnnDeserializer/Deserializer.cpp @@ -198,6 +198,7 @@ m_ParserFunctions(Layer_MAX+1, &Deserializer::ParseUnsupportedLayer) m_ParserFunctions[Layer_MaximumLayer] = &Deserializer::ParseMaximum; m_ParserFunctions[Layer_MultiplicationLayer] = &Deserializer::ParseMultiplication; m_ParserFunctions[Layer_NormalizationLayer] = &Deserializer::ParseNormalization; + m_ParserFunctions[Layer_PadLayer] = &Deserializer::ParsePad; m_ParserFunctions[Layer_PermuteLayer] = &Deserializer::ParsePermute; m_ParserFunctions[Layer_Pooling2dLayer] = &Deserializer::ParsePooling2d; m_ParserFunctions[Layer_ReshapeLayer] = &Deserializer::ParseReshape; @@ -241,6 +242,8 @@ Deserializer::LayerBaseRawPtr Deserializer::GetBaseLayer(const GraphPtr& graphPt return graphPtr->layers()->Get(layerIndex)->layer_as_NormalizationLayer()->base(); case Layer::Layer_OutputLayer: return graphPtr->layers()->Get(layerIndex)->layer_as_OutputLayer()->base()->base(); + case Layer::Layer_PadLayer: + return graphPtr->layers()->Get(layerIndex)->layer_as_PadLayer()->base(); case Layer::Layer_PermuteLayer: return graphPtr->layers()->Get(layerIndex)->layer_as_PermuteLayer()->base(); case Layer::Layer_Pooling2dLayer: @@ -1090,6 +1093,44 @@ void Deserializer::ParseFullyConnected(GraphPtr graph, unsigned int layerIndex) RegisterOutputSlots(graph, layerIndex, layer); } +void Deserializer::ParsePad(GraphPtr graph, unsigned int layerIndex) +{ + CHECK_LAYERS(graph, 0, layerIndex); + + Deserializer::TensorRawPtrVector inputs = GetInputs(graph, layerIndex); + CHECK_VALID_SIZE(inputs.size(), 1); + + Deserializer::TensorRawPtrVector outputs = GetOutputs(graph, layerIndex); + CHECK_VALID_SIZE(outputs.size(), 1); + + auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_PadLayer()->descriptor(); + auto flatBufferPadList = flatBufferDescriptor->padList(); + + if (flatBufferPadList->Length() % 2 != 0) + { + throw ParseException(boost::str( + boost::format("The size of the pad list must be divisible by 2 %1%") % CHECK_LOCATION().AsString())); + } + + std::vector> padList; + padList.reserve(flatBufferPadList->Length() / 2); + for (unsigned int i = 0; i < flatBufferPadList->Length() - 1; i += 2) + { + padList.emplace_back(flatBufferPadList->Get(i), flatBufferPadList->Get(i+1)); + } + + armnn::PadDescriptor descriptor(padList); + + auto layerName = GetLayerName(graph, layerIndex); + IConnectableLayer* layer = m_Network->AddPadLayer(descriptor, layerName.c_str()); + + armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); + layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); + + RegisterInputSlots(graph, layerIndex, layer); + RegisterOutputSlots(graph, layerIndex, layer); +} + void Deserializer::ParsePermute(GraphPtr graph, unsigned int layerIndex) { CHECK_LAYERS(graph, 0, layerIndex); diff --git a/src/armnnDeserializer/Deserializer.hpp b/src/armnnDeserializer/Deserializer.hpp index fba8b88044..ec10cb5817 100644 --- a/src/armnnDeserializer/Deserializer.hpp +++ b/src/armnnDeserializer/Deserializer.hpp @@ -84,6 +84,7 @@ private: void ParseMaximum(GraphPtr graph, unsigned int layerIndex); void ParseMultiplication(GraphPtr graph, unsigned int layerIndex); void ParseNormalization(GraphPtr graph, unsigned int layerIndex); + void ParsePad(GraphPtr graph, unsigned int layerIndex); void ParsePermute(GraphPtr graph, unsigned int layerIndex); void ParsePooling2d(GraphPtr graph, unsigned int layerIndex); void ParseReshape(GraphPtr graph, unsigned int layerIndex); diff --git a/src/armnnDeserializer/DeserializerSupport.md b/src/armnnDeserializer/DeserializerSupport.md index cf8f6dea2d..1f479b94b8 100644 --- a/src/armnnDeserializer/DeserializerSupport.md +++ b/src/armnnDeserializer/DeserializerSupport.md @@ -19,10 +19,11 @@ The Arm NN SDK Deserialize parser currently supports the following layers: * Minimum * Multiplication * Normalization +* Pad * Permute * Pooling2d * Reshape * Softmax * SpaceToBatchNd -More machine learning layers will be supported in future releases. \ No newline at end of file +More machine learning layers will be supported in future releases. diff --git a/src/armnnDeserializer/test/DeserializePad.cpp b/src/armnnDeserializer/test/DeserializePad.cpp new file mode 100644 index 0000000000..b18710a381 --- /dev/null +++ b/src/armnnDeserializer/test/DeserializePad.cpp @@ -0,0 +1,129 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include +#include "ParserFlatbuffersSerializeFixture.hpp" +#include "../Deserializer.hpp" + +#include + +BOOST_AUTO_TEST_SUITE(Deserializer) + +struct PadFixture : public ParserFlatbuffersSerializeFixture +{ + explicit PadFixture(const std::string &inputShape, + const std::string &padList, + 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: "PadLayer", + layer: { + base: { + index: 1, + layerName: "PadLayer", + layerType: "Pad", + inputSlots: [{ + index: 0, + connection: {sourceLayerIndex:0, outputSlotIndex:0 }, + }], + outputSlots: [{ + index: 0, + tensorInfo: { + dimensions: )" + outputShape + R"(, + dataType: )" + dataType + R"( + } + }] + }, + descriptor: { + padList: )" + padList + 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"( + }, + }], + } + } + }, + } + ] + } + )"; + SetupSingleInputSingleOutput("InputLayer", "OutputLayer"); + } +}; + +struct SimplePadFixture : PadFixture +{ + SimplePadFixture() : PadFixture("[ 2, 2, 2 ]", + "[ 0, 1, 2, 1, 2, 2 ]", + "[ 3, 5, 6 ]", + "QuantisedAsymm8") {} +}; + +BOOST_FIXTURE_TEST_CASE(SimplePadQuantisedAsymm8, SimplePadFixture) +{ + RunTest<3, armnn::DataType::QuantisedAsymm8>(0, + { + 0, 4, 2, 5, 6, 1, 5, 2 + }, + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 4, 0, 0, 0, 0, 2, 5, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, + 1, 0, 0, 0, 0, 5, 2, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnSerializer/ArmnnSchema.fbs b/src/armnnSerializer/ArmnnSchema.fbs index cde0087d6f..552c2cc056 100644 --- a/src/armnnSerializer/ArmnnSchema.fbs +++ b/src/armnnSerializer/ArmnnSchema.fbs @@ -100,7 +100,8 @@ enum LayerType : uint { Minimum = 16, Equal = 17, Maximum = 18, - Normalization = 19 + Normalization = 19, + Pad = 20 } // Base layer table to be used as part of other layers @@ -324,6 +325,15 @@ table NormalizationDescriptor { dataLayout:DataLayout = NCHW; } +table PadLayer { + base:LayerBase; + descriptor:PadDescriptor; +} + +table PadDescriptor { + padList:[uint]; +} + union Layer { ActivationLayer, AdditionLayer, @@ -344,7 +354,8 @@ union Layer { MinimumLayer, EqualLayer, MaximumLayer, - NormalizationLayer + NormalizationLayer, + PadLayer } table AnyLayer { diff --git a/src/armnnSerializer/Serializer.cpp b/src/armnnSerializer/Serializer.cpp index 2000726526..868a36d42e 100644 --- a/src/armnnSerializer/Serializer.cpp +++ b/src/armnnSerializer/Serializer.cpp @@ -306,6 +306,29 @@ void SerializerVisitor::VisitMultiplicationLayer(const armnn::IConnectableLayer* CreateAnyLayer(flatBufferMultiplicationLayer.o, serializer::Layer::Layer_MultiplicationLayer); } +void SerializerVisitor::VisitPadLayer(const armnn::IConnectableLayer* layer, + const armnn::PadDescriptor& padDescriptor, + const char* name) +{ + auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Pad); + + std::vector padList; + for (auto& p: padDescriptor.m_PadList) + { + padList.push_back(p.first); + padList.push_back(p.second); + } + + auto flatBufferPadDesc = serializer::CreatePadDescriptor(m_flatBufferBuilder, + m_flatBufferBuilder.CreateVector(padList)); + + auto flatBufferPadLayer = serializer::CreatePadLayer(m_flatBufferBuilder, + flatBufferBaseLayer, + flatBufferPadDesc); + + CreateAnyLayer(flatBufferPadLayer.o, serializer::Layer::Layer_PadLayer); +} + void SerializerVisitor::VisitPermuteLayer(const armnn::IConnectableLayer* layer, const armnn::PermuteDescriptor& permuteDescriptor, const char* name) diff --git a/src/armnnSerializer/Serializer.hpp b/src/armnnSerializer/Serializer.hpp index 7e6097c465..ef56c25f2c 100644 --- a/src/armnnSerializer/Serializer.hpp +++ b/src/armnnSerializer/Serializer.hpp @@ -98,6 +98,10 @@ public: armnn::LayerBindingId id, const char* name = nullptr) override; + void VisitPadLayer(const armnn::IConnectableLayer* layer, + const armnn::PadDescriptor& PadDescriptor, + const char* name = nullptr) override; + void VisitPermuteLayer(const armnn::IConnectableLayer* layer, const armnn::PermuteDescriptor& PermuteDescriptor, const char* name = nullptr) override; diff --git a/src/armnnSerializer/SerializerSupport.md b/src/armnnSerializer/SerializerSupport.md index d018a35c3a..a77e8860a2 100644 --- a/src/armnnSerializer/SerializerSupport.md +++ b/src/armnnSerializer/SerializerSupport.md @@ -19,10 +19,11 @@ The Arm NN SDK Serializer currently supports the following layers: * Minimum * Multiplication * Normalization +* Pad * Permute * Pooling2d * Reshape * Softmax * SpaceToBatchNd -More machine learning layers will be supported in future releases. \ No newline at end of file +More machine learning layers will be supported in future releases. diff --git a/src/armnnSerializer/test/SerializerTests.cpp b/src/armnnSerializer/test/SerializerTests.cpp index 271b3e71bd..110bf0c581 100644 --- a/src/armnnSerializer/test/SerializerTests.cpp +++ b/src/armnnSerializer/test/SerializerTests.cpp @@ -980,4 +980,45 @@ BOOST_AUTO_TEST_CASE(SerializeDeserializeEqual) {outputTensorInfo.GetShape()}, {0, 1}); } + +BOOST_AUTO_TEST_CASE(SerializeDeserializePad) +{ + class VerifyPadName : public armnn::LayerVisitorBase + { + public: + void VisitPadLayer(const armnn::IConnectableLayer*, + const armnn::PadDescriptor& descriptor, + const char* name) override + { + BOOST_TEST(name == "PadLayer"); + } + }; + + armnn::PadDescriptor desc({{0, 0}, {1, 0}, {1, 1}, {1, 2}}); + + const armnn::TensorInfo inputTensorInfo = armnn::TensorInfo({1, 2, 3, 4}, armnn::DataType::Float32); + const armnn::TensorInfo outputTensorInfo = armnn::TensorInfo({1, 3, 5, 7}, armnn::DataType::Float32); + + armnn::INetworkPtr network = armnn::INetwork::Create(); + armnn::IConnectableLayer* const inputLayer = network->AddInputLayer(0); + armnn::IConnectableLayer* const padLayer = network->AddPadLayer(desc, "PadLayer"); + armnn::IConnectableLayer* const outputLayer = network->AddOutputLayer(0); + + inputLayer->GetOutputSlot(0).Connect(padLayer->GetInputSlot(0)); + inputLayer->GetOutputSlot(0).SetTensorInfo(inputTensorInfo); + padLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0)); + padLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); + + armnn::INetworkPtr deserializedNetwork = DeserializeNetwork(SerializeNetwork(*network)); + BOOST_CHECK(deserializedNetwork); + + VerifyPadName nameChecker; + deserializedNetwork->Accept(nameChecker); + + CheckDeserializedNetworkAgainstOriginal(*network, + *deserializedNetwork, + {inputTensorInfo.GetShape()}, + {outputTensorInfo.GetShape()}); +} + BOOST_AUTO_TEST_SUITE_END() -- cgit v1.2.1