blob: 797e11e4033e25c70690f57cf99a79a1c3b7f8fd [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
5
6#pragma once
7
Matteo Martincighc601aa62019-10-29 15:03:22 +00008#include "Schema.hpp"
9
keidav01222c7532019-03-14 17:12:10 +000010#include <armnn/Descriptors.hpp>
11#include <armnn/IRuntime.hpp>
12#include <armnn/TypesUtils.hpp>
Matteo Martincighc601aa62019-10-29 15:03:22 +000013#include <armnn/BackendRegistry.hpp>
keidav01222c7532019-03-14 17:12:10 +000014
Matteo Martincighc601aa62019-10-29 15:03:22 +000015#include <armnnTfLiteParser/ITfLiteParser.hpp>
16
17#include <ResolveType.hpp>
18
19#include <test/TensorHelpers.hpp>
20
telsoa01c577f2c2018-08-31 09:22:23 +010021#include <boost/filesystem.hpp>
22#include <boost/assert.hpp>
23#include <boost/format.hpp>
keidav01222c7532019-03-14 17:12:10 +000024
telsoa01c577f2c2018-08-31 09:22:23 +010025#include "flatbuffers/idl.h"
26#include "flatbuffers/util.h"
keidav01222c7532019-03-14 17:12:10 +000027#include "flatbuffers/flexbuffers.h"
telsoa01c577f2c2018-08-31 09:22:23 +010028
29#include <schema_generated.h>
Matteo Martincighc601aa62019-10-29 15:03:22 +000030
telsoa01c577f2c2018-08-31 09:22:23 +010031#include <iostream>
32
33using armnnTfLiteParser::ITfLiteParser;
Aron Virginas-Tarc975f922019-10-23 17:38:17 +010034using armnnTfLiteParser::ITfLiteParserPtr;
telsoa01c577f2c2018-08-31 09:22:23 +010035
Aron Virginas-Tarc975f922019-10-23 17:38:17 +010036using TensorRawPtr = const tflite::TensorT *;
telsoa01c577f2c2018-08-31 09:22:23 +010037struct ParserFlatbuffersFixture
38{
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +000039 ParserFlatbuffersFixture() :
Aron Virginas-Tarc975f922019-10-23 17:38:17 +010040 m_Parser(nullptr, &ITfLiteParser::Destroy),
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +000041 m_Runtime(armnn::IRuntime::Create(armnn::IRuntime::CreationOptions())),
42 m_NetworkIdentifier(-1)
telsoa01c577f2c2018-08-31 09:22:23 +010043 {
Aron Virginas-Tarc975f922019-10-23 17:38:17 +010044 ITfLiteParser::TfLiteParserOptions options;
45 options.m_StandInLayerForUnsupported = true;
46
47 m_Parser.reset(ITfLiteParser::CreateRaw(armnn::Optional<ITfLiteParser::TfLiteParserOptions>(options)));
telsoa01c577f2c2018-08-31 09:22:23 +010048 }
49
50 std::vector<uint8_t> m_GraphBinary;
Aron Virginas-Tarc975f922019-10-23 17:38:17 +010051 std::string m_JsonString;
52 ITfLiteParserPtr m_Parser;
53 armnn::IRuntimePtr m_Runtime;
54 armnn::NetworkId m_NetworkIdentifier;
telsoa01c577f2c2018-08-31 09:22:23 +010055
56 /// If the single-input-single-output overload of Setup() is called, these will store the input and output name
57 /// so they don't need to be passed to the single-input-single-output overload of RunTest().
58 std::string m_SingleInputName;
59 std::string m_SingleOutputName;
60
61 void Setup()
62 {
63 bool ok = ReadStringToBinary();
64 if (!ok) {
65 throw armnn::Exception("LoadNetwork failed while reading binary input");
66 }
67
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +000068 armnn::INetworkPtr network =
69 m_Parser->CreateNetworkFromBinary(m_GraphBinary);
70
71 if (!network) {
72 throw armnn::Exception("The parser failed to create an ArmNN network");
73 }
74
75 auto optimized = Optimize(*network, { armnn::Compute::CpuRef },
76 m_Runtime->GetDeviceSpec());
77 std::string errorMessage;
78
79 armnn::Status ret = m_Runtime->LoadNetwork(m_NetworkIdentifier, move(optimized), errorMessage);
80
81 if (ret != armnn::Status::Success)
telsoa01c577f2c2018-08-31 09:22:23 +010082 {
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +000083 throw armnn::Exception(
84 boost::str(
85 boost::format("The runtime failed to load the network. "
86 "Error was: %1%. in %2% [%3%:%4%]") %
87 errorMessage %
88 __func__ %
89 __FILE__ %
90 __LINE__));
telsoa01c577f2c2018-08-31 09:22:23 +010091 }
92 }
93
94 void SetupSingleInputSingleOutput(const std::string& inputName, const std::string& outputName)
95 {
96 // Store the input and output name so they don't need to be passed to the single-input-single-output RunTest().
97 m_SingleInputName = inputName;
98 m_SingleOutputName = outputName;
99 Setup();
100 }
101
102 bool ReadStringToBinary()
103 {
Rob Hughesff3c4262019-12-20 17:43:16 +0000104 std::string schemafile(g_TfLiteSchemaText, g_TfLiteSchemaText + g_TfLiteSchemaText_len);
telsoa01c577f2c2018-08-31 09:22:23 +0100105
106 // parse schema first, so we can use it to parse the data after
107 flatbuffers::Parser parser;
108
Matthew Bentham6c8e8e72019-01-15 17:57:00 +0000109 bool ok = parser.Parse(schemafile.c_str());
telsoa01c577f2c2018-08-31 09:22:23 +0100110 BOOST_ASSERT_MSG(ok, "Failed to parse schema file");
111
112 ok &= parser.Parse(m_JsonString.c_str());
113 BOOST_ASSERT_MSG(ok, "Failed to parse json input");
114
115 if (!ok)
116 {
117 return false;
118 }
119
120 {
121 const uint8_t * bufferPtr = parser.builder_.GetBufferPointer();
122 size_t size = static_cast<size_t>(parser.builder_.GetSize());
123 m_GraphBinary.assign(bufferPtr, bufferPtr+size);
124 }
125 return ok;
126 }
127
128 /// Executes the network with the given input tensor and checks the result against the given output tensor.
keidav011b3e2ea2019-02-21 10:07:37 +0000129 /// This assumes the network has a single input and a single output.
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000130 template <std::size_t NumOutputDimensions,
Rob Hughesfc6bf052019-12-16 17:10:51 +0000131 armnn::DataType ArmnnType>
telsoa01c577f2c2018-08-31 09:22:23 +0100132 void RunTest(size_t subgraphId,
Rob Hughesfc6bf052019-12-16 17:10:51 +0000133 const std::vector<armnn::ResolveType<ArmnnType>>& inputData,
134 const std::vector<armnn::ResolveType<ArmnnType>>& expectedOutputData);
telsoa01c577f2c2018-08-31 09:22:23 +0100135
136 /// Executes the network with the given input tensors and checks the results against the given output tensors.
137 /// This overload supports multiple inputs and multiple outputs, identified by name.
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000138 template <std::size_t NumOutputDimensions,
Rob Hughesfc6bf052019-12-16 17:10:51 +0000139 armnn::DataType ArmnnType>
telsoa01c577f2c2018-08-31 09:22:23 +0100140 void RunTest(size_t subgraphId,
Rob Hughesfc6bf052019-12-16 17:10:51 +0000141 const std::map<std::string, std::vector<armnn::ResolveType<ArmnnType>>>& inputData,
142 const std::map<std::string, std::vector<armnn::ResolveType<ArmnnType>>>& expectedOutputData);
telsoa01c577f2c2018-08-31 09:22:23 +0100143
keidav011b3e2ea2019-02-21 10:07:37 +0000144 /// Multiple Inputs, Multiple Outputs w/ Variable Datatypes and different dimension sizes.
145 /// Executes the network with the given input tensors and checks the results against the given output tensors.
146 /// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for
147 /// the input datatype to be different to the output
148 template <std::size_t NumOutputDimensions,
149 armnn::DataType ArmnnType1,
Rob Hughesfc6bf052019-12-16 17:10:51 +0000150 armnn::DataType ArmnnType2>
keidav011b3e2ea2019-02-21 10:07:37 +0000151 void RunTest(size_t subgraphId,
Rob Hughesfc6bf052019-12-16 17:10:51 +0000152 const std::map<std::string, std::vector<armnn::ResolveType<ArmnnType1>>>& inputData,
153 const std::map<std::string, std::vector<armnn::ResolveType<ArmnnType2>>>& expectedOutputData);
keidav011b3e2ea2019-02-21 10:07:37 +0000154
155
156 /// Multiple Inputs, Multiple Outputs w/ Variable Datatypes and different dimension sizes.
157 /// Executes the network with the given input tensors and checks the results against the given output tensors.
158 /// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for
159 /// the input datatype to be different to the output
160 template<armnn::DataType ArmnnType1,
Rob Hughesfc6bf052019-12-16 17:10:51 +0000161 armnn::DataType ArmnnType2>
keidav011b3e2ea2019-02-21 10:07:37 +0000162 void RunTest(std::size_t subgraphId,
Rob Hughesfc6bf052019-12-16 17:10:51 +0000163 const std::map<std::string, std::vector<armnn::ResolveType<ArmnnType1>>>& inputData,
164 const std::map<std::string, std::vector<armnn::ResolveType<ArmnnType2>>>& expectedOutputData);
keidav011b3e2ea2019-02-21 10:07:37 +0000165
keidav01222c7532019-03-14 17:12:10 +0000166 static inline std::string GenerateDetectionPostProcessJsonString(
167 const armnn::DetectionPostProcessDescriptor& descriptor)
168 {
169 flexbuffers::Builder detectPostProcess;
170 detectPostProcess.Map([&]() {
171 detectPostProcess.Bool("use_regular_nms", descriptor.m_UseRegularNms);
172 detectPostProcess.Int("max_detections", descriptor.m_MaxDetections);
173 detectPostProcess.Int("max_classes_per_detection", descriptor.m_MaxClassesPerDetection);
174 detectPostProcess.Int("detections_per_class", descriptor.m_DetectionsPerClass);
175 detectPostProcess.Int("num_classes", descriptor.m_NumClasses);
176 detectPostProcess.Float("nms_score_threshold", descriptor.m_NmsScoreThreshold);
177 detectPostProcess.Float("nms_iou_threshold", descriptor.m_NmsIouThreshold);
178 detectPostProcess.Float("h_scale", descriptor.m_ScaleH);
179 detectPostProcess.Float("w_scale", descriptor.m_ScaleW);
180 detectPostProcess.Float("x_scale", descriptor.m_ScaleX);
181 detectPostProcess.Float("y_scale", descriptor.m_ScaleY);
182 });
183 detectPostProcess.Finish();
184
185 // Create JSON string
186 std::stringstream strStream;
187 std::vector<uint8_t> buffer = detectPostProcess.GetBuffer();
188 std::copy(buffer.begin(), buffer.end(),std::ostream_iterator<int>(strStream,","));
189
190 return strStream.str();
191 }
192
telsoa01c577f2c2018-08-31 09:22:23 +0100193 void CheckTensors(const TensorRawPtr& tensors, size_t shapeSize, const std::vector<int32_t>& shape,
194 tflite::TensorType tensorType, uint32_t buffer, const std::string& name,
195 const std::vector<float>& min, const std::vector<float>& max,
196 const std::vector<float>& scale, const std::vector<int64_t>& zeroPoint)
197 {
198 BOOST_CHECK(tensors);
199 BOOST_CHECK_EQUAL(shapeSize, tensors->shape.size());
200 BOOST_CHECK_EQUAL_COLLECTIONS(shape.begin(), shape.end(), tensors->shape.begin(), tensors->shape.end());
201 BOOST_CHECK_EQUAL(tensorType, tensors->type);
202 BOOST_CHECK_EQUAL(buffer, tensors->buffer);
203 BOOST_CHECK_EQUAL(name, tensors->name);
204 BOOST_CHECK(tensors->quantization);
205 BOOST_CHECK_EQUAL_COLLECTIONS(min.begin(), min.end(), tensors->quantization.get()->min.begin(),
206 tensors->quantization.get()->min.end());
207 BOOST_CHECK_EQUAL_COLLECTIONS(max.begin(), max.end(), tensors->quantization.get()->max.begin(),
208 tensors->quantization.get()->max.end());
209 BOOST_CHECK_EQUAL_COLLECTIONS(scale.begin(), scale.end(), tensors->quantization.get()->scale.begin(),
210 tensors->quantization.get()->scale.end());
211 BOOST_CHECK_EQUAL_COLLECTIONS(zeroPoint.begin(), zeroPoint.end(),
212 tensors->quantization.get()->zero_point.begin(),
213 tensors->quantization.get()->zero_point.end());
214 }
215};
216
keidav011b3e2ea2019-02-21 10:07:37 +0000217/// Single Input, Single Output
218/// Executes the network with the given input tensor and checks the result against the given output tensor.
219/// This overload assumes the network has a single input and a single output.
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000220template <std::size_t NumOutputDimensions,
Rob Hughesfc6bf052019-12-16 17:10:51 +0000221 armnn::DataType armnnType>
telsoa01c577f2c2018-08-31 09:22:23 +0100222void ParserFlatbuffersFixture::RunTest(size_t subgraphId,
Rob Hughesfc6bf052019-12-16 17:10:51 +0000223 const std::vector<armnn::ResolveType<armnnType>>& inputData,
224 const std::vector<armnn::ResolveType<armnnType>>& expectedOutputData)
telsoa01c577f2c2018-08-31 09:22:23 +0100225{
keidav011b3e2ea2019-02-21 10:07:37 +0000226 RunTest<NumOutputDimensions, armnnType>(subgraphId,
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000227 { { m_SingleInputName, inputData } },
228 { { m_SingleOutputName, expectedOutputData } });
telsoa01c577f2c2018-08-31 09:22:23 +0100229}
230
keidav011b3e2ea2019-02-21 10:07:37 +0000231/// Multiple Inputs, Multiple Outputs
232/// Executes the network with the given input tensors and checks the results against the given output tensors.
233/// This overload supports multiple inputs and multiple outputs, identified by name.
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000234template <std::size_t NumOutputDimensions,
Rob Hughesfc6bf052019-12-16 17:10:51 +0000235 armnn::DataType armnnType>
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000236void ParserFlatbuffersFixture::RunTest(size_t subgraphId,
Rob Hughesfc6bf052019-12-16 17:10:51 +0000237 const std::map<std::string, std::vector<armnn::ResolveType<armnnType>>>& inputData,
238 const std::map<std::string, std::vector<armnn::ResolveType<armnnType>>>& expectedOutputData)
telsoa01c577f2c2018-08-31 09:22:23 +0100239{
keidav011b3e2ea2019-02-21 10:07:37 +0000240 RunTest<NumOutputDimensions, armnnType, armnnType>(subgraphId, inputData, expectedOutputData);
241}
242
243/// Multiple Inputs, Multiple Outputs w/ Variable Datatypes
244/// Executes the network with the given input tensors and checks the results against the given output tensors.
245/// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for
246/// the input datatype to be different to the output
247template <std::size_t NumOutputDimensions,
248 armnn::DataType armnnType1,
Rob Hughesfc6bf052019-12-16 17:10:51 +0000249 armnn::DataType armnnType2>
keidav011b3e2ea2019-02-21 10:07:37 +0000250void ParserFlatbuffersFixture::RunTest(size_t subgraphId,
Rob Hughesfc6bf052019-12-16 17:10:51 +0000251 const std::map<std::string, std::vector<armnn::ResolveType<armnnType1>>>& inputData,
252 const std::map<std::string, std::vector<armnn::ResolveType<armnnType2>>>& expectedOutputData)
keidav011b3e2ea2019-02-21 10:07:37 +0000253{
Rob Hughesfc6bf052019-12-16 17:10:51 +0000254 using DataType2 = armnn::ResolveType<armnnType2>;
255
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000256 // Setup the armnn input tensors from the given vectors.
257 armnn::InputTensors inputTensors;
258 for (auto&& it : inputData)
telsoa01c577f2c2018-08-31 09:22:23 +0100259 {
Jim Flynnb4d7eae2019-05-01 14:44:27 +0100260 armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkInputBindingInfo(subgraphId, it.first);
keidav011b3e2ea2019-02-21 10:07:37 +0000261 armnn::VerifyTensorInfoDataType(bindingInfo.second, armnnType1);
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000262 inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) });
263 }
telsoa01c577f2c2018-08-31 09:22:23 +0100264
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000265 // Allocate storage for the output tensors to be written to and setup the armnn output tensors.
keidav011b3e2ea2019-02-21 10:07:37 +0000266 std::map<std::string, boost::multi_array<DataType2, NumOutputDimensions>> outputStorage;
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000267 armnn::OutputTensors outputTensors;
268 for (auto&& it : expectedOutputData)
269 {
Narumol Prangnawarat386681a2019-04-29 16:40:55 +0100270 armnn::LayerBindingId outputBindingId = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first).first;
271 armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkIdentifier, outputBindingId);
272
273 // Check that output tensors have correct number of dimensions (NumOutputDimensions specified in test)
274 auto outputNumDimensions = outputTensorInfo.GetNumDimensions();
275 BOOST_CHECK_MESSAGE((outputNumDimensions == NumOutputDimensions),
276 boost::str(boost::format("Number of dimensions expected %1%, but got %2% for output layer %3%")
277 % NumOutputDimensions
278 % outputNumDimensions
279 % it.first));
280
281 armnn::VerifyTensorInfoDataType(outputTensorInfo, armnnType2);
282 outputStorage.emplace(it.first, MakeTensor<DataType2, NumOutputDimensions>(outputTensorInfo));
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000283 outputTensors.push_back(
Narumol Prangnawarat386681a2019-04-29 16:40:55 +0100284 { outputBindingId, armnn::Tensor(outputTensorInfo, outputStorage.at(it.first).data()) });
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000285 }
telsoa01c577f2c2018-08-31 09:22:23 +0100286
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000287 m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors);
telsoa01c577f2c2018-08-31 09:22:23 +0100288
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000289 // Compare each output tensor to the expected values
290 for (auto&& it : expectedOutputData)
291 {
Jim Flynnb4d7eae2019-05-01 14:44:27 +0100292 armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first);
keidav011b3e2ea2019-02-21 10:07:37 +0000293 auto outputExpected = MakeTensor<DataType2, NumOutputDimensions>(bindingInfo.second, it.second);
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000294 BOOST_TEST(CompareTensors(outputExpected, outputStorage[it.first]));
telsoa01c577f2c2018-08-31 09:22:23 +0100295 }
296}
keidav011b3e2ea2019-02-21 10:07:37 +0000297
298/// Multiple Inputs, Multiple Outputs w/ Variable Datatypes and different dimension sizes.
299/// Executes the network with the given input tensors and checks the results against the given output tensors.
300/// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for
301/// the input datatype to be different to the output.
302template <armnn::DataType armnnType1,
Rob Hughesfc6bf052019-12-16 17:10:51 +0000303 armnn::DataType armnnType2>
keidav011b3e2ea2019-02-21 10:07:37 +0000304void ParserFlatbuffersFixture::RunTest(std::size_t subgraphId,
Rob Hughesfc6bf052019-12-16 17:10:51 +0000305 const std::map<std::string, std::vector<armnn::ResolveType<armnnType1>>>& inputData,
306 const std::map<std::string, std::vector<armnn::ResolveType<armnnType2>>>& expectedOutputData)
keidav011b3e2ea2019-02-21 10:07:37 +0000307{
Rob Hughesfc6bf052019-12-16 17:10:51 +0000308 using DataType2 = armnn::ResolveType<armnnType2>;
309
keidav011b3e2ea2019-02-21 10:07:37 +0000310 // Setup the armnn input tensors from the given vectors.
311 armnn::InputTensors inputTensors;
312 for (auto&& it : inputData)
313 {
Jim Flynnb4d7eae2019-05-01 14:44:27 +0100314 armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkInputBindingInfo(subgraphId, it.first);
keidav011b3e2ea2019-02-21 10:07:37 +0000315 armnn::VerifyTensorInfoDataType(bindingInfo.second, armnnType1);
316
317 inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) });
318 }
319
320 armnn::OutputTensors outputTensors;
321 outputTensors.reserve(expectedOutputData.size());
322 std::map<std::string, std::vector<DataType2>> outputStorage;
323 for (auto&& it : expectedOutputData)
324 {
Jim Flynnb4d7eae2019-05-01 14:44:27 +0100325 armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first);
keidav011b3e2ea2019-02-21 10:07:37 +0000326 armnn::VerifyTensorInfoDataType(bindingInfo.second, armnnType2);
327
328 std::vector<DataType2> out(it.second.size());
329 outputStorage.emplace(it.first, out);
330 outputTensors.push_back({ bindingInfo.first,
331 armnn::Tensor(bindingInfo.second,
332 outputStorage.at(it.first).data()) });
333 }
334
335 m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors);
336
337 // Checks the results.
338 for (auto&& it : expectedOutputData)
339 {
Rob Hughesfc6bf052019-12-16 17:10:51 +0000340 std::vector<armnn::ResolveType<armnnType2>> out = outputStorage.at(it.first);
keidav011b3e2ea2019-02-21 10:07:37 +0000341 {
342 for (unsigned int i = 0; i < out.size(); ++i)
343 {
344 BOOST_TEST(it.second[i] == out[i], boost::test_tools::tolerance(0.000001f));
345 }
346 }
347 }
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100348}