diff options
Diffstat (limited to 'src/armnnTfLiteParser/test')
-rw-r--r-- | src/armnnTfLiteParser/test/DepthToSpace.cpp | 98 | ||||
-rw-r--r-- | src/armnnTfLiteParser/test/Gather.cpp | 121 | ||||
-rw-r--r-- | src/armnnTfLiteParser/test/ParserFlatbuffersFixture.hpp | 102 |
3 files changed, 308 insertions, 13 deletions
diff --git a/src/armnnTfLiteParser/test/DepthToSpace.cpp b/src/armnnTfLiteParser/test/DepthToSpace.cpp new file mode 100644 index 0000000000..efd1207297 --- /dev/null +++ b/src/armnnTfLiteParser/test/DepthToSpace.cpp @@ -0,0 +1,98 @@ +// +// Copyright © 2021 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include <boost/test/unit_test.hpp> +#include "ParserFlatbuffersFixture.hpp" +#include "../TfLiteParser.hpp" + +#include <string> +#include <iostream> + +BOOST_AUTO_TEST_SUITE(TensorflowLiteParser) + +struct DepthToSpaceFixture : public ParserFlatbuffersFixture +{ + explicit DepthToSpaceFixture(const std::string& inputShape, + const std::string& outputShape, + const std::string& dataType = "FLOAT32", + const std::string& scale = "1.0", + const std::string& offset = "0") + { + m_JsonString = R"( + { + "version": 3, + "operator_codes": [ { "builtin_code": "DEPTH_TO_SPACE" } ], + "subgraphs": [ { + "tensors": [ + { + "shape": )" + inputShape + R"(, + "type": )" + dataType + R"(, + "buffer": 0, + "name": "inputTensor", + "quantization": { + "min": [ 0.0 ], + "max": [ 255.0 ], + "scale": [ )" + scale + R"( ], + "zero_point": [ )" + offset + R"( ], + } + }, + { + "shape": )" + outputShape + R"(, + "type": )" + dataType + R"(, + "buffer": 1, + "name": "outputTensor", + "quantization": { + "min": [ 0.0 ], + "max": [ 255.0 ], + "scale": [ )" + scale + R"( ], + "zero_point": [ )" + offset + R"( ], + } + } + ], + "inputs": [ 0 ], + "outputs": [ 1 ], + "operators": [ + { + "opcode_index": 0, + "inputs": [ 0 ], + "outputs": [ 1 ], + "builtin_options_type": "DepthToSpaceOptions", + "builtin_options": { + "block_size": 2 + }, + "custom_options_format": "FLEXBUFFERS" + } + ], + } ], + "buffers" : [ + { }, + { }, + ] + } + )"; + SetupSingleInputSingleOutput("inputTensor", "outputTensor"); + } +}; + +struct SimpleDepthToSpaceFixture : public DepthToSpaceFixture +{ + SimpleDepthToSpaceFixture() : DepthToSpaceFixture("[ 1, 2, 2, 4 ]", "[ 1, 4, 4, 1 ]") {} +}; + +BOOST_FIXTURE_TEST_CASE(ParseDepthToSpace, SimpleDepthToSpaceFixture) +{ + RunTest<4, armnn::DataType::Float32> + (0, + {{ "inputTensor", { 1.f, 2.f, 3.f, 4.f, + 5.f, 6.f, 7.f, 8.f, + 9.f, 10.f, 11.f, 12.f, + 13.f, 14.f, 15.f, 16.f }}}, + {{ "outputTensor", { 1.f, 2.f, 5.f, 6.f, + 3.f, 4.f, 7.f, 8.f, + 9.f, 10.f, 13.f, 14.f, + 11.f, 12.f, 15.f, 16.f }}}); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfLiteParser/test/Gather.cpp b/src/armnnTfLiteParser/test/Gather.cpp new file mode 100644 index 0000000000..498d56d254 --- /dev/null +++ b/src/armnnTfLiteParser/test/Gather.cpp @@ -0,0 +1,121 @@ +// +// Copyright © 2021 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include <boost/test/unit_test.hpp> +#include "ParserFlatbuffersFixture.hpp" +#include "../TfLiteParser.hpp" + +#include <string> +#include <iostream> + +BOOST_AUTO_TEST_SUITE(TensorflowLiteParser) + +struct GatherFixture : public ParserFlatbuffersFixture +{ + explicit GatherFixture(const std::string& paramsShape, + const std::string& outputShape, + const std::string& indicesShape, + const std::string& dataType = "FLOAT32", + const std::string& scale = "1.0", + const std::string& offset = "0") + { + m_JsonString = R"( + { + "version": 3, + "operator_codes": [ { "builtin_code": "GATHER" } ], + "subgraphs": [ { + "tensors": [ + { + "shape": )" + paramsShape + R"(, + "type": )" + dataType + R"(, + "buffer": 0, + "name": "inputTensor", + "quantization": { + "min": [ 0.0 ], + "max": [ 255.0 ], + "scale": [ )" + scale + R"( ], + "zero_point": [ )" + offset + R"( ], + } + }, + { + "shape": )" + indicesShape + R"( , + "type": "INT32", + "buffer": 1, + "name": "indices", + "quantization": { + "min": [ 0.0 ], + "max": [ 255.0 ], + "scale": [ 1.0 ], + "zero_point": [ 0 ], + } + }, + { + "shape": )" + outputShape + R"(, + "type": )" + dataType + R"(, + "buffer": 2, + "name": "outputTensor", + "quantization": { + "min": [ 0.0 ], + "max": [ 255.0 ], + "scale": [ )" + scale + R"( ], + "zero_point": [ )" + offset + R"( ], + } + } + ], + "inputs": [ 0, 1 ], + "outputs": [ 2 ], + "operators": [ + { + "opcode_index": 0, + "inputs": [ 0, 1 ], + "outputs": [ 2 ], + "builtin_options_type": "GatherOptions", + "builtin_options": { + "axis": 0 + }, + "custom_options_format": "FLEXBUFFERS" + } + ], + } ], + "buffers" : [ + { }, + { }, + { }, + ] + } + )"; + Setup(); + } +}; + +struct SimpleGatherFixture : public GatherFixture +{ + SimpleGatherFixture() : GatherFixture("[ 5, 2 ]", "[ 3, 2 ]", "[ 3 ]") {} +}; + +BOOST_FIXTURE_TEST_CASE(ParseGather, SimpleGatherFixture) +{ + RunTest<2, armnn::DataType::Float32, armnn::DataType::Signed32, armnn::DataType::Float32> + (0, + {{ "inputTensor", { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }}}, + {{ "indices", { 1, 3, 4 }}}, + {{ "outputTensor", { 3, 4, 7, 8, 9, 10 }}}); +} + +struct GatherUint8Fixture : public GatherFixture +{ + GatherUint8Fixture() : GatherFixture("[ 8 ]", "[ 3 ]", "[ 3 ]", "UINT8") {} +}; + +BOOST_FIXTURE_TEST_CASE(ParseGatherUint8, GatherUint8Fixture) +{ + RunTest<1, armnn::DataType::QAsymmU8, armnn::DataType::Signed32, armnn::DataType::QAsymmU8> + (0, + {{ "inputTensor", { 1, 2, 3, 4, 5, 6, 7, 8 }}}, + {{ "indices", { 7, 6, 5 }}}, + {{ "outputTensor", { 8, 7, 6 }}}); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfLiteParser/test/ParserFlatbuffersFixture.hpp b/src/armnnTfLiteParser/test/ParserFlatbuffersFixture.hpp index 50a312fcf6..fc1d94e21f 100644 --- a/src/armnnTfLiteParser/test/ParserFlatbuffersFixture.hpp +++ b/src/armnnTfLiteParser/test/ParserFlatbuffersFixture.hpp @@ -152,6 +152,18 @@ struct ParserFlatbuffersFixture const std::map<std::string, std::vector<armnn::ResolveType<ArmnnType2>>>& expectedOutputData, bool isDynamic = false); + /// Multiple Inputs with different DataTypes, Multiple Outputs w/ Variable DataTypes + /// 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 along with the allowance for + /// the input datatype to be different to the output + template <std::size_t NumOutputDimensions, + armnn::DataType inputType1, + armnn::DataType inputType2, + armnn::DataType outputType> + void RunTest(size_t subgraphId, + const std::map<std::string, std::vector<armnn::ResolveType<inputType1>>>& input1Data, + const std::map<std::string, std::vector<armnn::ResolveType<inputType2>>>& input2Data, + const std::map<std::string, std::vector<armnn::ResolveType<outputType>>>& expectedOutputData); /// Multiple Inputs, Multiple Outputs w/ Variable Datatypes and different dimension sizes. /// Executes the network with the given input tensors and checks the results against the given output tensors. @@ -212,8 +224,30 @@ struct ParserFlatbuffersFixture tensors->quantization.get()->zero_point.begin(), tensors->quantization.get()->zero_point.end()); } + +private: + /// Fills the InputTensors with given input data + template <armnn::DataType dataType> + void FillInputTensors(armnn::InputTensors& inputTensors, + const std::map<std::string, std::vector<armnn::ResolveType<dataType>>>& inputData, + size_t subgraphId); }; +/// Fills the InputTensors with given input data +template <armnn::DataType dataType> +void ParserFlatbuffersFixture::FillInputTensors( + armnn::InputTensors& inputTensors, + const std::map<std::string, std::vector<armnn::ResolveType<dataType>>>& inputData, + size_t subgraphId) +{ + for (auto&& it : inputData) + { + armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkInputBindingInfo(subgraphId, it.first); + armnn::VerifyTensorInfoDataType(bindingInfo.second, dataType); + inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) }); + } +} + /// Single Input, Single Output /// 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. @@ -256,12 +290,7 @@ void ParserFlatbuffersFixture::RunTest(size_t subgraphId, // Setup the armnn input tensors from the given vectors. armnn::InputTensors inputTensors; - for (auto&& it : inputData) - { - armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkInputBindingInfo(subgraphId, it.first); - armnn::VerifyTensorInfoDataType(bindingInfo.second, armnnType1); - inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) }); - } + FillInputTensors<armnnType1>(inputTensors, inputData, subgraphId); // Allocate storage for the output tensors to be written to and setup the armnn output tensors. std::map<std::string, boost::multi_array<DataType2, NumOutputDimensions>> outputStorage; @@ -310,13 +339,7 @@ void ParserFlatbuffersFixture::RunTest(std::size_t subgraphId, // Setup the armnn input tensors from the given vectors. armnn::InputTensors inputTensors; - for (auto&& it : inputData) - { - armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkInputBindingInfo(subgraphId, it.first); - armnn::VerifyTensorInfoDataType(bindingInfo.second, armnnType1); - - inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) }); - } + FillInputTensors<armnnType1>(inputTensors, inputData, subgraphId); armnn::OutputTensors outputTensors; outputTensors.reserve(expectedOutputData.size()); @@ -347,3 +370,56 @@ void ParserFlatbuffersFixture::RunTest(std::size_t subgraphId, } } } + +/// Multiple Inputs with different DataTypes, Multiple Outputs w/ Variable DataTypes +/// 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 along with the allowance for +/// the input datatype to be different to the output +template <std::size_t NumOutputDimensions, + armnn::DataType inputType1, + armnn::DataType inputType2, + armnn::DataType outputType> +void ParserFlatbuffersFixture::RunTest(size_t subgraphId, + const std::map<std::string, std::vector<armnn::ResolveType<inputType1>>>& input1Data, + const std::map<std::string, std::vector<armnn::ResolveType<inputType2>>>& input2Data, + const std::map<std::string, std::vector<armnn::ResolveType<outputType>>>& expectedOutputData) +{ + using DataType2 = armnn::ResolveType<outputType>; + + // Setup the armnn input tensors from the given vectors. + armnn::InputTensors inputTensors; + FillInputTensors<inputType1>(inputTensors, input1Data, subgraphId); + FillInputTensors<inputType2>(inputTensors, input2Data, subgraphId); + + // Allocate storage for the output tensors to be written to and setup the armnn output tensors. + std::map<std::string, boost::multi_array<DataType2, NumOutputDimensions>> outputStorage; + armnn::OutputTensors outputTensors; + for (auto&& it : expectedOutputData) + { + armnn::LayerBindingId outputBindingId = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first).first; + armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkIdentifier, outputBindingId); + + // Check that output tensors have correct number of dimensions (NumOutputDimensions specified in test) + auto outputNumDimensions = outputTensorInfo.GetNumDimensions(); + BOOST_CHECK_MESSAGE((outputNumDimensions == NumOutputDimensions), + fmt::format("Number of dimensions expected {}, but got {} for output layer {}", + NumOutputDimensions, + outputNumDimensions, + it.first)); + + armnn::VerifyTensorInfoDataType(outputTensorInfo, outputType); + outputStorage.emplace(it.first, MakeTensor<DataType2, NumOutputDimensions>(outputTensorInfo)); + outputTensors.push_back( + { outputBindingId, armnn::Tensor(outputTensorInfo, outputStorage.at(it.first).data()) }); + } + + m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors); + + // Compare each output tensor to the expected values + for (auto&& it : expectedOutputData) + { + armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first); + auto outputExpected = MakeTensor<DataType2, NumOutputDimensions>(bindingInfo.second, it.second); + BOOST_TEST(CompareTensors(outputExpected, outputStorage[it.first], false)); + } +}
\ No newline at end of file |