telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Matthew Bentham | 6c8e8e7 | 2019-01-15 17:57:00 +0000 | [diff] [blame^] | 8 | #include "Schema.hpp" |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 9 | #include <boost/filesystem.hpp> |
| 10 | #include <boost/assert.hpp> |
| 11 | #include <boost/format.hpp> |
| 12 | #include <experimental/filesystem> |
| 13 | #include <armnn/IRuntime.hpp> |
| 14 | #include <armnn/TypesUtils.hpp> |
| 15 | #include "test/TensorHelpers.hpp" |
| 16 | |
| 17 | #include "armnnTfLiteParser/ITfLiteParser.hpp" |
| 18 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 19 | #include <backendsCommon/BackendRegistry.hpp> |
Aron Virginas-Tar | 54e9572 | 2018-10-25 11:47:31 +0100 | [diff] [blame] | 20 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 21 | #include "flatbuffers/idl.h" |
| 22 | #include "flatbuffers/util.h" |
| 23 | |
| 24 | #include <schema_generated.h> |
| 25 | #include <iostream> |
| 26 | |
| 27 | using armnnTfLiteParser::ITfLiteParser; |
| 28 | using TensorRawPtr = const tflite::TensorT *; |
| 29 | |
| 30 | struct ParserFlatbuffersFixture |
| 31 | { |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 32 | ParserFlatbuffersFixture() : |
| 33 | m_Parser(ITfLiteParser::Create()), |
| 34 | m_Runtime(armnn::IRuntime::Create(armnn::IRuntime::CreationOptions())), |
| 35 | m_NetworkIdentifier(-1) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 36 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | std::vector<uint8_t> m_GraphBinary; |
| 40 | std::string m_JsonString; |
| 41 | std::unique_ptr<ITfLiteParser, void (*)(ITfLiteParser *parser)> m_Parser; |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 42 | armnn::IRuntimePtr m_Runtime; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 43 | armnn::NetworkId m_NetworkIdentifier; |
| 44 | |
| 45 | /// If the single-input-single-output overload of Setup() is called, these will store the input and output name |
| 46 | /// so they don't need to be passed to the single-input-single-output overload of RunTest(). |
| 47 | std::string m_SingleInputName; |
| 48 | std::string m_SingleOutputName; |
| 49 | |
| 50 | void Setup() |
| 51 | { |
| 52 | bool ok = ReadStringToBinary(); |
| 53 | if (!ok) { |
| 54 | throw armnn::Exception("LoadNetwork failed while reading binary input"); |
| 55 | } |
| 56 | |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 57 | armnn::INetworkPtr network = |
| 58 | m_Parser->CreateNetworkFromBinary(m_GraphBinary); |
| 59 | |
| 60 | if (!network) { |
| 61 | throw armnn::Exception("The parser failed to create an ArmNN network"); |
| 62 | } |
| 63 | |
| 64 | auto optimized = Optimize(*network, { armnn::Compute::CpuRef }, |
| 65 | m_Runtime->GetDeviceSpec()); |
| 66 | std::string errorMessage; |
| 67 | |
| 68 | armnn::Status ret = m_Runtime->LoadNetwork(m_NetworkIdentifier, move(optimized), errorMessage); |
| 69 | |
| 70 | if (ret != armnn::Status::Success) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 71 | { |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 72 | throw armnn::Exception( |
| 73 | boost::str( |
| 74 | boost::format("The runtime failed to load the network. " |
| 75 | "Error was: %1%. in %2% [%3%:%4%]") % |
| 76 | errorMessage % |
| 77 | __func__ % |
| 78 | __FILE__ % |
| 79 | __LINE__)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | void SetupSingleInputSingleOutput(const std::string& inputName, const std::string& outputName) |
| 84 | { |
| 85 | // Store the input and output name so they don't need to be passed to the single-input-single-output RunTest(). |
| 86 | m_SingleInputName = inputName; |
| 87 | m_SingleOutputName = outputName; |
| 88 | Setup(); |
| 89 | } |
| 90 | |
| 91 | bool ReadStringToBinary() |
| 92 | { |
Matthew Bentham | 6c8e8e7 | 2019-01-15 17:57:00 +0000 | [diff] [blame^] | 93 | std::string schemafile(&tflite_schema_start, &tflite_schema_end); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 94 | |
| 95 | // parse schema first, so we can use it to parse the data after |
| 96 | flatbuffers::Parser parser; |
| 97 | |
Matthew Bentham | 6c8e8e7 | 2019-01-15 17:57:00 +0000 | [diff] [blame^] | 98 | bool ok = parser.Parse(schemafile.c_str()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 99 | BOOST_ASSERT_MSG(ok, "Failed to parse schema file"); |
| 100 | |
| 101 | ok &= parser.Parse(m_JsonString.c_str()); |
| 102 | BOOST_ASSERT_MSG(ok, "Failed to parse json input"); |
| 103 | |
| 104 | if (!ok) |
| 105 | { |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | { |
| 110 | const uint8_t * bufferPtr = parser.builder_.GetBufferPointer(); |
| 111 | size_t size = static_cast<size_t>(parser.builder_.GetSize()); |
| 112 | m_GraphBinary.assign(bufferPtr, bufferPtr+size); |
| 113 | } |
| 114 | return ok; |
| 115 | } |
| 116 | |
| 117 | /// Executes the network with the given input tensor and checks the result against the given output tensor. |
| 118 | /// This overload assumes the network has a single input and a single output. |
| 119 | template <std::size_t NumOutputDimensions, typename DataType> |
| 120 | void RunTest(size_t subgraphId, |
| 121 | const std::vector<DataType>& inputData, |
| 122 | const std::vector<DataType>& expectedOutputData); |
| 123 | |
| 124 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 125 | /// This overload supports multiple inputs and multiple outputs, identified by name. |
| 126 | template <std::size_t NumOutputDimensions, typename DataType> |
| 127 | void RunTest(size_t subgraphId, |
| 128 | const std::map<std::string, std::vector<DataType>>& inputData, |
| 129 | const std::map<std::string, std::vector<DataType>>& expectedOutputData); |
| 130 | |
| 131 | void CheckTensors(const TensorRawPtr& tensors, size_t shapeSize, const std::vector<int32_t>& shape, |
| 132 | tflite::TensorType tensorType, uint32_t buffer, const std::string& name, |
| 133 | const std::vector<float>& min, const std::vector<float>& max, |
| 134 | const std::vector<float>& scale, const std::vector<int64_t>& zeroPoint) |
| 135 | { |
| 136 | BOOST_CHECK(tensors); |
| 137 | BOOST_CHECK_EQUAL(shapeSize, tensors->shape.size()); |
| 138 | BOOST_CHECK_EQUAL_COLLECTIONS(shape.begin(), shape.end(), tensors->shape.begin(), tensors->shape.end()); |
| 139 | BOOST_CHECK_EQUAL(tensorType, tensors->type); |
| 140 | BOOST_CHECK_EQUAL(buffer, tensors->buffer); |
| 141 | BOOST_CHECK_EQUAL(name, tensors->name); |
| 142 | BOOST_CHECK(tensors->quantization); |
| 143 | BOOST_CHECK_EQUAL_COLLECTIONS(min.begin(), min.end(), tensors->quantization.get()->min.begin(), |
| 144 | tensors->quantization.get()->min.end()); |
| 145 | BOOST_CHECK_EQUAL_COLLECTIONS(max.begin(), max.end(), tensors->quantization.get()->max.begin(), |
| 146 | tensors->quantization.get()->max.end()); |
| 147 | BOOST_CHECK_EQUAL_COLLECTIONS(scale.begin(), scale.end(), tensors->quantization.get()->scale.begin(), |
| 148 | tensors->quantization.get()->scale.end()); |
| 149 | BOOST_CHECK_EQUAL_COLLECTIONS(zeroPoint.begin(), zeroPoint.end(), |
| 150 | tensors->quantization.get()->zero_point.begin(), |
| 151 | tensors->quantization.get()->zero_point.end()); |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | template <std::size_t NumOutputDimensions, typename DataType> |
| 156 | void ParserFlatbuffersFixture::RunTest(size_t subgraphId, |
| 157 | const std::vector<DataType>& inputData, |
| 158 | const std::vector<DataType>& expectedOutputData) |
| 159 | { |
| 160 | RunTest<NumOutputDimensions, DataType>(subgraphId, |
| 161 | { { m_SingleInputName, inputData } }, |
| 162 | { { m_SingleOutputName, expectedOutputData } }); |
| 163 | } |
| 164 | |
| 165 | template <std::size_t NumOutputDimensions, typename DataType> |
| 166 | void |
| 167 | ParserFlatbuffersFixture::RunTest(size_t subgraphId, |
| 168 | const std::map<std::string, std::vector<DataType>>& inputData, |
| 169 | const std::map<std::string, std::vector<DataType>>& expectedOutputData) |
| 170 | { |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 171 | using BindingPointInfo = std::pair<armnn::LayerBindingId, armnn::TensorInfo>; |
| 172 | |
| 173 | // Setup the armnn input tensors from the given vectors. |
| 174 | armnn::InputTensors inputTensors; |
| 175 | for (auto&& it : inputData) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 176 | { |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 177 | BindingPointInfo bindingInfo = m_Parser->GetNetworkInputBindingInfo(subgraphId, it.first); |
| 178 | armnn::VerifyTensorInfoDataType<DataType>(bindingInfo.second); |
| 179 | inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) }); |
| 180 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 181 | |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 182 | // Allocate storage for the output tensors to be written to and setup the armnn output tensors. |
| 183 | std::map<std::string, boost::multi_array<DataType, NumOutputDimensions>> outputStorage; |
| 184 | armnn::OutputTensors outputTensors; |
| 185 | for (auto&& it : expectedOutputData) |
| 186 | { |
| 187 | BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first); |
| 188 | armnn::VerifyTensorInfoDataType<DataType>(bindingInfo.second); |
| 189 | outputStorage.emplace(it.first, MakeTensor<DataType, NumOutputDimensions>(bindingInfo.second)); |
| 190 | outputTensors.push_back( |
| 191 | { bindingInfo.first, armnn::Tensor(bindingInfo.second, outputStorage.at(it.first).data()) }); |
| 192 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 193 | |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 194 | m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 195 | |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 196 | // Compare each output tensor to the expected values |
| 197 | for (auto&& it : expectedOutputData) |
| 198 | { |
| 199 | BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first); |
| 200 | auto outputExpected = MakeTensor<DataType, NumOutputDimensions>(bindingInfo.second, it.second); |
| 201 | BOOST_TEST(CompareTensors(outputExpected, outputStorage[it.first])); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 202 | } |
| 203 | } |