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