diff options
Diffstat (limited to 'src/armnnDeserializeParser/test')
4 files changed, 382 insertions, 0 deletions
diff --git a/src/armnnDeserializeParser/test/DeserializeAdd.cpp b/src/armnnDeserializeParser/test/DeserializeAdd.cpp new file mode 100644 index 0000000000..03d50e3fe0 --- /dev/null +++ b/src/armnnDeserializeParser/test/DeserializeAdd.cpp @@ -0,0 +1,161 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include <boost/test/unit_test.hpp> +#include "ParserFlatbuffersSerializeFixture.hpp" +#include "../DeserializeParser.hpp" + +#include <string> +#include <iostream> + +BOOST_AUTO_TEST_SUITE(DeserializeParser) + +struct AddFixture : public ParserFlatbuffersSerializeFixture +{ + explicit AddFixture(const std::string & inputShape1, + const std::string & inputShape2, + const std::string & outputShape, + const std::string & dataType, + const std::string & activation="NONE") + { + 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: "AdditionLayer", + layer : { + base: { + index:2, + layerName: "AdditionLayer", + layerType: "Addition", + 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: 3, + 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 SimpleAddFixture : AddFixture +{ + SimpleAddFixture() : AddFixture("[ 2, 2 ]", + "[ 2, 2 ]", + "[ 2, 2 ]", + "QuantisedAsymm8") {} +}; + +struct SimpleAddFixture2 : AddFixture +{ + SimpleAddFixture2() : AddFixture("[ 2, 2, 1, 1 ]", + "[ 2, 2, 1, 1 ]", + "[ 2, 2, 1, 1 ]", + "Float32") {} +}; + +BOOST_FIXTURE_TEST_CASE(AddQuantisedAsymm8, SimpleAddFixture) +{ + RunTest<2, armnn::DataType::QuantisedAsymm8>( + 0, + {{"InputLayer1", { 0, 1, 2, 3 }}, + {"InputLayer2", { 4, 5, 6, 7 }}}, + {{"OutputLayer", { 4, 6, 8, 10 }}}); +} + +BOOST_FIXTURE_TEST_CASE(AddFloat32, SimpleAddFixture2) +{ + RunTest<4, armnn::DataType::Float32>( + 0, + {{"InputLayer1", { 111, 85, 226, 3 }}, + {"InputLayer2", { 5, 8, 10, 12 }}}, + {{"OutputLayer", { 116, 93, 236, 15 }}}); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnDeserializeParser/test/ParserFlatbuffersSerializeFixture.hpp b/src/armnnDeserializeParser/test/ParserFlatbuffersSerializeFixture.hpp new file mode 100644 index 0000000000..5d8c377981 --- /dev/null +++ b/src/armnnDeserializeParser/test/ParserFlatbuffersSerializeFixture.hpp @@ -0,0 +1,199 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +#include "SchemaSerialize.hpp" + +#include <armnn/IRuntime.hpp> +#include <armnnDeserializeParser/IDeserializeParser.hpp> + +#include <boost/assert.hpp> +#include <boost/format.hpp> + +#include "TypeUtils.hpp" +#include "test/TensorHelpers.hpp" + +#include "flatbuffers/idl.h" +#include "flatbuffers/util.h" + +#include <Schema_generated.h> + +using armnnDeserializeParser::IDeserializeParser; +using TensorRawPtr = armnn::armnnSerializer::TensorInfo*; + +struct ParserFlatbuffersSerializeFixture +{ + ParserFlatbuffersSerializeFixture() : + m_Parser(IDeserializeParser::Create()), + m_Runtime(armnn::IRuntime::Create(armnn::IRuntime::CreationOptions())), + m_NetworkIdentifier(-1) + { + } + + std::vector<uint8_t> m_GraphBinary; + std::string m_JsonString; + std::unique_ptr<IDeserializeParser, void (*)(IDeserializeParser* parser)> m_Parser; + armnn::IRuntimePtr m_Runtime; + armnn::NetworkId m_NetworkIdentifier; + + /// If the single-input-single-output overload of Setup() is called, these will store the input and output name + /// so they don't need to be passed to the single-input-single-output overload of RunTest(). + std::string m_SingleInputName; + std::string m_SingleOutputName; + + void Setup() + { + bool ok = ReadStringToBinary(); + if (!ok) + { + throw armnn::Exception("LoadNetwork failed while reading binary input"); + } + + armnn::INetworkPtr network = + m_Parser->CreateNetworkFromBinary(m_GraphBinary); + + if (!network) + { + throw armnn::Exception("The parser failed to create an ArmNN network"); + } + + auto optimized = Optimize(*network, {armnn::Compute::CpuRef}, + m_Runtime->GetDeviceSpec()); + + std::string errorMessage; + armnn::Status ret = m_Runtime->LoadNetwork(m_NetworkIdentifier, move(optimized), errorMessage); + + if (ret != armnn::Status::Success) + { + throw armnn::Exception( + boost::str( + boost::format("The runtime failed to load the network. " + "Error was: %1%. in %2% [%3%:%4%]") % + errorMessage % + __func__ % + __FILE__ % + __LINE__)); + } + + } + + void SetupSingleInputSingleOutput(const std::string& inputName, const std::string& outputName) + { + // Store the input and output name so they don't need to be passed to the single-input-single-output RunTest(). + m_SingleInputName = inputName; + m_SingleOutputName = outputName; + Setup(); + } + + bool ReadStringToBinary() + { + std::string schemafile(&deserialize_schema_start, &deserialize_schema_end); + + // parse schema first, so we can use it to parse the data after + flatbuffers::Parser parser; + + bool ok = parser.Parse(schemafile.c_str()); + BOOST_ASSERT_MSG(ok, "Failed to parse schema file"); + + ok &= parser.Parse(m_JsonString.c_str()); + BOOST_ASSERT_MSG(ok, "Failed to parse json input"); + + if (!ok) + { + return false; + } + + { + const uint8_t* bufferPtr = parser.builder_.GetBufferPointer(); + size_t size = static_cast<size_t>(parser.builder_.GetSize()); + m_GraphBinary.assign(bufferPtr, bufferPtr+size); + } + return ok; + } + + /// Executes the network with the given input tensor and checks the result against the given output tensor. + /// This overload assumes the network has a single input and a single output. + template <std::size_t NumOutputDimensions, + armnn::DataType ArmnnType, + typename DataType = armnn::ResolveType<ArmnnType>> + void RunTest(unsigned int layersId, + const std::vector<DataType>& inputData, + const std::vector<DataType>& expectedOutputData); + + /// Executes the network with the given input tensors and checks the results against the given output tensors. + /// This overload supports multiple inputs and multiple outputs, identified by name. + template <std::size_t NumOutputDimensions, + armnn::DataType ArmnnType, + typename DataType = armnn::ResolveType<ArmnnType>> + void RunTest(unsigned int layersId, + const std::map<std::string, std::vector<DataType>>& inputData, + const std::map<std::string, std::vector<DataType>>& expectedOutputData); + + void CheckTensors(const TensorRawPtr& tensors, size_t shapeSize, const std::vector<int32_t>& shape, + armnn::armnnSerializer::TensorInfo tensorType, const std::string& name, + const float scale, const int64_t zeroPoint) + { + BOOST_CHECK_EQUAL(shapeSize, tensors->dimensions()->size()); + BOOST_CHECK_EQUAL_COLLECTIONS(shape.begin(), shape.end(), + tensors->dimensions()->begin(), tensors->dimensions()->end()); + BOOST_CHECK_EQUAL(tensorType.dataType(), tensors->dataType()); + BOOST_CHECK_EQUAL(scale, tensors->quantizationScale()); + BOOST_CHECK_EQUAL(zeroPoint, tensors->quantizationOffset()); + } +}; + +template <std::size_t NumOutputDimensions, + armnn::DataType ArmnnType, + typename DataType> +void ParserFlatbuffersSerializeFixture::RunTest(unsigned int layersId, + const std::vector<DataType>& inputData, + const std::vector<DataType>& expectedOutputData) +{ + RunTest<NumOutputDimensions, ArmnnType>(layersId, + { { m_SingleInputName, inputData } }, + { { m_SingleOutputName, expectedOutputData } }); +} + +template <std::size_t NumOutputDimensions, + armnn::DataType ArmnnType, + typename DataType> +void ParserFlatbuffersSerializeFixture::RunTest(unsigned int layersId, + const std::map<std::string, std::vector<DataType>>& inputData, + const std::map<std::string, std::vector<DataType>>& expectedOutputData) +{ + using BindingPointInfo = std::pair<armnn::LayerBindingId, armnn::TensorInfo>; + + // Setup the armnn input tensors from the given vectors. + armnn::InputTensors inputTensors; + for (auto&& it : inputData) + { + BindingPointInfo bindingInfo = m_Parser->GetNetworkInputBindingInfo(layersId, it.first); + armnn::VerifyTensorInfoDataType<ArmnnType>(bindingInfo.second); + inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) }); + } + + // Allocate storage for the output tensors to be written to and setup the armnn output tensors. + std::map<std::string, boost::multi_array<DataType, NumOutputDimensions>> outputStorage; + armnn::OutputTensors outputTensors; + for (auto&& it : expectedOutputData) + { + BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(layersId, it.first); + armnn::VerifyTensorInfoDataType<ArmnnType>(bindingInfo.second); + outputStorage.emplace(it.first, MakeTensor<DataType, NumOutputDimensions>(bindingInfo.second)); + outputTensors.push_back( + { bindingInfo.first, armnn::Tensor(bindingInfo.second, outputStorage.at(it.first).data()) }); + } + + m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors); + + // Compare each output tensor to the expected values + for (auto&& it : expectedOutputData) + { + BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(layersId, it.first); + auto outputExpected = MakeTensor<DataType, NumOutputDimensions>(bindingInfo.second, it.second); + BOOST_TEST(CompareTensors(outputExpected, outputStorage[it.first])); + } +} diff --git a/src/armnnDeserializeParser/test/SchemaSerialize.hpp b/src/armnnDeserializeParser/test/SchemaSerialize.hpp new file mode 100644 index 0000000000..ec7e6bab6a --- /dev/null +++ b/src/armnnDeserializeParser/test/SchemaSerialize.hpp @@ -0,0 +1,9 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +extern "C" { +extern const char deserialize_schema_start; +extern const char deserialize_schema_end; +} diff --git a/src/armnnDeserializeParser/test/SchemaSerialize.s b/src/armnnDeserializeParser/test/SchemaSerialize.s new file mode 100644 index 0000000000..dbbb7db3e5 --- /dev/null +++ b/src/armnnDeserializeParser/test/SchemaSerialize.s @@ -0,0 +1,13 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +.section .rodata + +.global deserialize_schema_start +.global deserialize_schema_end + +deserialize_schema_start: +.incbin ARMNN_SERIALIZER_SCHEMA_PATH +deserialize_schema_end: |