2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
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"
17 #include "TypeUtils.hpp"
18 #include "armnnTfLiteParser/ITfLiteParser.hpp"
20 #include <backendsCommon/BackendRegistry.hpp>
22 #include "flatbuffers/idl.h"
23 #include "flatbuffers/util.h"
25 #include <schema_generated.h>
28 using armnnTfLiteParser::ITfLiteParser;
29 using TensorRawPtr = const tflite::TensorT *;
31 struct ParserFlatbuffersFixture
33 ParserFlatbuffersFixture() :
34 m_Parser(ITfLiteParser::Create()),
35 m_Runtime(armnn::IRuntime::Create(armnn::IRuntime::CreationOptions())),
36 m_NetworkIdentifier(-1)
40 std::vector<uint8_t> m_GraphBinary;
41 std::string m_JsonString;
42 std::unique_ptr<ITfLiteParser, void (*)(ITfLiteParser *parser)> m_Parser;
43 armnn::IRuntimePtr m_Runtime;
44 armnn::NetworkId m_NetworkIdentifier;
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;
53 bool ok = ReadStringToBinary();
55 throw armnn::Exception("LoadNetwork failed while reading binary input");
58 armnn::INetworkPtr network =
59 m_Parser->CreateNetworkFromBinary(m_GraphBinary);
62 throw armnn::Exception("The parser failed to create an ArmNN network");
65 auto optimized = Optimize(*network, { armnn::Compute::CpuRef },
66 m_Runtime->GetDeviceSpec());
67 std::string errorMessage;
69 armnn::Status ret = m_Runtime->LoadNetwork(m_NetworkIdentifier, move(optimized), errorMessage);
71 if (ret != armnn::Status::Success)
73 throw armnn::Exception(
75 boost::format("The runtime failed to load the network. "
76 "Error was: %1%. in %2% [%3%:%4%]") %
84 void SetupSingleInputSingleOutput(const std::string& inputName, const std::string& outputName)
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;
92 bool ReadStringToBinary()
94 std::string schemafile(&tflite_schema_start, &tflite_schema_end);
96 // parse schema first, so we can use it to parse the data after
97 flatbuffers::Parser parser;
99 bool ok = parser.Parse(schemafile.c_str());
100 BOOST_ASSERT_MSG(ok, "Failed to parse schema file");
102 ok &= parser.Parse(m_JsonString.c_str());
103 BOOST_ASSERT_MSG(ok, "Failed to parse json input");
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);
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.
120 template <std::size_t NumOutputDimensions,
121 armnn::DataType ArmnnType,
122 typename DataType = armnn::ResolveType<ArmnnType>>
123 void RunTest(size_t subgraphId,
124 const std::vector<DataType>& inputData,
125 const std::vector<DataType>& expectedOutputData);
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.
129 template <std::size_t NumOutputDimensions,
130 armnn::DataType ArmnnType,
131 typename DataType = armnn::ResolveType<ArmnnType>>
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);
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)
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());
160 template <std::size_t NumOutputDimensions,
161 armnn::DataType ArmnnType,
163 void ParserFlatbuffersFixture::RunTest(size_t subgraphId,
164 const std::vector<DataType>& inputData,
165 const std::vector<DataType>& expectedOutputData)
167 RunTest<NumOutputDimensions, ArmnnType>(subgraphId,
168 { { m_SingleInputName, inputData } },
169 { { m_SingleOutputName, expectedOutputData } });
172 template <std::size_t NumOutputDimensions,
173 armnn::DataType ArmnnType,
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)
179 using BindingPointInfo = std::pair<armnn::LayerBindingId, armnn::TensorInfo>;
181 // Setup the armnn input tensors from the given vectors.
182 armnn::InputTensors inputTensors;
183 for (auto&& it : inputData)
185 BindingPointInfo bindingInfo = m_Parser->GetNetworkInputBindingInfo(subgraphId, it.first);
186 armnn::VerifyTensorInfoDataType<ArmnnType>(bindingInfo.second);
187 inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) });
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)
195 BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first);
196 armnn::VerifyTensorInfoDataType<ArmnnType>(bindingInfo.second);
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()) });
202 m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors);
204 // Compare each output tensor to the expected values
205 for (auto&& it : expectedOutputData)
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]));