blob: 50e674ef2c97bd2e510bab1f5eb5cf42f10c8d7e [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
Matthew Bentham6c8e8e72019-01-15 17:57:00 +00008#include "Schema.hpp"
telsoa01c577f2c2018-08-31 09:22:23 +01009#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 Chaimanowong649dd952019-01-22 16:10:44 +000017#include "TypeUtils.hpp"
telsoa01c577f2c2018-08-31 09:22:23 +010018#include "armnnTfLiteParser/ITfLiteParser.hpp"
19
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +000020#include <backendsCommon/BackendRegistry.hpp>
Aron Virginas-Tar54e95722018-10-25 11:47:31 +010021
telsoa01c577f2c2018-08-31 09:22:23 +010022#include "flatbuffers/idl.h"
23#include "flatbuffers/util.h"
24
25#include <schema_generated.h>
26#include <iostream>
27
28using armnnTfLiteParser::ITfLiteParser;
29using TensorRawPtr = const tflite::TensorT *;
30
31struct ParserFlatbuffersFixture
32{
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +000033 ParserFlatbuffersFixture() :
34 m_Parser(ITfLiteParser::Create()),
35 m_Runtime(armnn::IRuntime::Create(armnn::IRuntime::CreationOptions())),
36 m_NetworkIdentifier(-1)
telsoa01c577f2c2018-08-31 09:22:23 +010037 {
telsoa01c577f2c2018-08-31 09:22:23 +010038 }
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-Tar1d67a6902018-11-19 10:58:30 +000043 armnn::IRuntimePtr m_Runtime;
telsoa01c577f2c2018-08-31 09:22:23 +010044 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-Tar1d67a6902018-11-19 10:58:30 +000058 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)
telsoa01c577f2c2018-08-31 09:22:23 +010072 {
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +000073 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__));
telsoa01c577f2c2018-08-31 09:22:23 +010081 }
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 Bentham6c8e8e72019-01-15 17:57:00 +000094 std::string schemafile(&tflite_schema_start, &tflite_schema_end);
telsoa01c577f2c2018-08-31 09:22:23 +010095
96 // parse schema first, so we can use it to parse the data after
97 flatbuffers::Parser parser;
98
Matthew Bentham6c8e8e72019-01-15 17:57:00 +000099 bool ok = parser.Parse(schemafile.c_str());
telsoa01c577f2c2018-08-31 09:22:23 +0100100 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.
keidav011b3e2ea2019-02-21 10:07:37 +0000119 /// This assumes the network has a single input and a single output.
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000120 template <std::size_t NumOutputDimensions,
121 armnn::DataType ArmnnType,
122 typename DataType = armnn::ResolveType<ArmnnType>>
telsoa01c577f2c2018-08-31 09:22:23 +0100123 void RunTest(size_t subgraphId,
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000124 const std::vector<DataType>& inputData,
125 const std::vector<DataType>& expectedOutputData);
telsoa01c577f2c2018-08-31 09:22:23 +0100126
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 Chaimanowong649dd952019-01-22 16:10:44 +0000129 template <std::size_t NumOutputDimensions,
130 armnn::DataType ArmnnType,
131 typename DataType = armnn::ResolveType<ArmnnType>>
telsoa01c577f2c2018-08-31 09:22:23 +0100132 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
keidav011b3e2ea2019-02-21 10:07:37 +0000136 /// Multiple Inputs, Multiple Outputs w/ Variable Datatypes and different dimension sizes.
137 /// Executes the network with the given input tensors and checks the results against the given output tensors.
138 /// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for
139 /// the input datatype to be different to the output
140 template <std::size_t NumOutputDimensions,
141 armnn::DataType ArmnnType1,
142 armnn::DataType ArmnnType2,
143 typename DataType1 = armnn::ResolveType<ArmnnType1>,
144 typename DataType2 = armnn::ResolveType<ArmnnType2>>
145 void RunTest(size_t subgraphId,
146 const std::map<std::string, std::vector<DataType1>>& inputData,
147 const std::map<std::string, std::vector<DataType2>>& expectedOutputData);
148
149
150 /// Multiple Inputs, Multiple Outputs w/ Variable Datatypes and different dimension sizes.
151 /// Executes the network with the given input tensors and checks the results against the given output tensors.
152 /// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for
153 /// the input datatype to be different to the output
154 template<armnn::DataType ArmnnType1,
155 armnn::DataType ArmnnType2,
156 typename DataType1 = armnn::ResolveType<ArmnnType1>,
157 typename DataType2 = armnn::ResolveType<ArmnnType2>>
158 void RunTest(std::size_t subgraphId,
159 const std::map<std::string, std::vector<DataType1>>& inputData,
160 const std::map<std::string, std::vector<DataType2>>& expectedOutputData);
161
telsoa01c577f2c2018-08-31 09:22:23 +0100162 void CheckTensors(const TensorRawPtr& tensors, size_t shapeSize, const std::vector<int32_t>& shape,
163 tflite::TensorType tensorType, uint32_t buffer, const std::string& name,
164 const std::vector<float>& min, const std::vector<float>& max,
165 const std::vector<float>& scale, const std::vector<int64_t>& zeroPoint)
166 {
167 BOOST_CHECK(tensors);
168 BOOST_CHECK_EQUAL(shapeSize, tensors->shape.size());
169 BOOST_CHECK_EQUAL_COLLECTIONS(shape.begin(), shape.end(), tensors->shape.begin(), tensors->shape.end());
170 BOOST_CHECK_EQUAL(tensorType, tensors->type);
171 BOOST_CHECK_EQUAL(buffer, tensors->buffer);
172 BOOST_CHECK_EQUAL(name, tensors->name);
173 BOOST_CHECK(tensors->quantization);
174 BOOST_CHECK_EQUAL_COLLECTIONS(min.begin(), min.end(), tensors->quantization.get()->min.begin(),
175 tensors->quantization.get()->min.end());
176 BOOST_CHECK_EQUAL_COLLECTIONS(max.begin(), max.end(), tensors->quantization.get()->max.begin(),
177 tensors->quantization.get()->max.end());
178 BOOST_CHECK_EQUAL_COLLECTIONS(scale.begin(), scale.end(), tensors->quantization.get()->scale.begin(),
179 tensors->quantization.get()->scale.end());
180 BOOST_CHECK_EQUAL_COLLECTIONS(zeroPoint.begin(), zeroPoint.end(),
181 tensors->quantization.get()->zero_point.begin(),
182 tensors->quantization.get()->zero_point.end());
183 }
184};
185
keidav011b3e2ea2019-02-21 10:07:37 +0000186/// Single Input, Single Output
187/// Executes the network with the given input tensor and checks the result against the given output tensor.
188/// This overload assumes the network has a single input and a single output.
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000189template <std::size_t NumOutputDimensions,
keidav011b3e2ea2019-02-21 10:07:37 +0000190 armnn::DataType armnnType,
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000191 typename DataType>
telsoa01c577f2c2018-08-31 09:22:23 +0100192void ParserFlatbuffersFixture::RunTest(size_t subgraphId,
193 const std::vector<DataType>& inputData,
194 const std::vector<DataType>& expectedOutputData)
195{
keidav011b3e2ea2019-02-21 10:07:37 +0000196 RunTest<NumOutputDimensions, armnnType>(subgraphId,
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000197 { { m_SingleInputName, inputData } },
198 { { m_SingleOutputName, expectedOutputData } });
telsoa01c577f2c2018-08-31 09:22:23 +0100199}
200
keidav011b3e2ea2019-02-21 10:07:37 +0000201/// Multiple Inputs, Multiple Outputs
202/// Executes the network with the given input tensors and checks the results against the given output tensors.
203/// This overload supports multiple inputs and multiple outputs, identified by name.
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000204template <std::size_t NumOutputDimensions,
keidav011b3e2ea2019-02-21 10:07:37 +0000205 armnn::DataType armnnType,
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000206 typename DataType>
207void ParserFlatbuffersFixture::RunTest(size_t subgraphId,
208 const std::map<std::string, std::vector<DataType>>& inputData,
209 const std::map<std::string, std::vector<DataType>>& expectedOutputData)
telsoa01c577f2c2018-08-31 09:22:23 +0100210{
keidav011b3e2ea2019-02-21 10:07:37 +0000211 RunTest<NumOutputDimensions, armnnType, armnnType>(subgraphId, inputData, expectedOutputData);
212}
213
214/// Multiple Inputs, Multiple Outputs w/ Variable Datatypes
215/// Executes the network with the given input tensors and checks the results against the given output tensors.
216/// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for
217/// the input datatype to be different to the output
218template <std::size_t NumOutputDimensions,
219 armnn::DataType armnnType1,
220 armnn::DataType armnnType2,
221 typename DataType1,
222 typename DataType2>
223void ParserFlatbuffersFixture::RunTest(size_t subgraphId,
224 const std::map<std::string, std::vector<DataType1>>& inputData,
225 const std::map<std::string, std::vector<DataType2>>& expectedOutputData)
226{
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000227 using BindingPointInfo = std::pair<armnn::LayerBindingId, armnn::TensorInfo>;
228
229 // Setup the armnn input tensors from the given vectors.
230 armnn::InputTensors inputTensors;
231 for (auto&& it : inputData)
telsoa01c577f2c2018-08-31 09:22:23 +0100232 {
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000233 BindingPointInfo bindingInfo = m_Parser->GetNetworkInputBindingInfo(subgraphId, it.first);
keidav011b3e2ea2019-02-21 10:07:37 +0000234 armnn::VerifyTensorInfoDataType(bindingInfo.second, armnnType1);
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000235 inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) });
236 }
telsoa01c577f2c2018-08-31 09:22:23 +0100237
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000238 // Allocate storage for the output tensors to be written to and setup the armnn output tensors.
keidav011b3e2ea2019-02-21 10:07:37 +0000239 std::map<std::string, boost::multi_array<DataType2, NumOutputDimensions>> outputStorage;
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000240 armnn::OutputTensors outputTensors;
241 for (auto&& it : expectedOutputData)
242 {
243 BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first);
keidav011b3e2ea2019-02-21 10:07:37 +0000244 armnn::VerifyTensorInfoDataType(bindingInfo.second, armnnType2);
245 outputStorage.emplace(it.first, MakeTensor<DataType2, NumOutputDimensions>(bindingInfo.second));
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000246 outputTensors.push_back(
247 { bindingInfo.first, armnn::Tensor(bindingInfo.second, outputStorage.at(it.first).data()) });
248 }
telsoa01c577f2c2018-08-31 09:22:23 +0100249
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000250 m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors);
telsoa01c577f2c2018-08-31 09:22:23 +0100251
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000252 // Compare each output tensor to the expected values
253 for (auto&& it : expectedOutputData)
254 {
255 BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first);
keidav011b3e2ea2019-02-21 10:07:37 +0000256 auto outputExpected = MakeTensor<DataType2, NumOutputDimensions>(bindingInfo.second, it.second);
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000257 BOOST_TEST(CompareTensors(outputExpected, outputStorage[it.first]));
telsoa01c577f2c2018-08-31 09:22:23 +0100258 }
259}
keidav011b3e2ea2019-02-21 10:07:37 +0000260
261/// Multiple Inputs, Multiple Outputs w/ Variable Datatypes and different dimension sizes.
262/// Executes the network with the given input tensors and checks the results against the given output tensors.
263/// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for
264/// the input datatype to be different to the output.
265template <armnn::DataType armnnType1,
266 armnn::DataType armnnType2,
267 typename DataType1,
268 typename DataType2>
269void ParserFlatbuffersFixture::RunTest(std::size_t subgraphId,
270 const std::map<std::string, std::vector<DataType1>>& inputData,
271 const std::map<std::string, std::vector<DataType2>>& expectedOutputData)
272{
273 using BindingPointInfo = std::pair<armnn::LayerBindingId, armnn::TensorInfo>;
274
275 // Setup the armnn input tensors from the given vectors.
276 armnn::InputTensors inputTensors;
277 for (auto&& it : inputData)
278 {
279 BindingPointInfo bindingInfo = m_Parser->GetNetworkInputBindingInfo(subgraphId, it.first);
280 armnn::VerifyTensorInfoDataType(bindingInfo.second, armnnType1);
281
282 inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) });
283 }
284
285 armnn::OutputTensors outputTensors;
286 outputTensors.reserve(expectedOutputData.size());
287 std::map<std::string, std::vector<DataType2>> outputStorage;
288 for (auto&& it : expectedOutputData)
289 {
290 BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first);
291 armnn::VerifyTensorInfoDataType(bindingInfo.second, armnnType2);
292
293 std::vector<DataType2> out(it.second.size());
294 outputStorage.emplace(it.first, out);
295 outputTensors.push_back({ bindingInfo.first,
296 armnn::Tensor(bindingInfo.second,
297 outputStorage.at(it.first).data()) });
298 }
299
300 m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors);
301
302 // Checks the results.
303 for (auto&& it : expectedOutputData)
304 {
305 std::vector<DataType2> out = outputStorage.at(it.first);
306 {
307 for (unsigned int i = 0; i < out.size(); ++i)
308 {
309 BOOST_TEST(it.second[i] == out[i], boost::test_tools::tolerance(0.000001f));
310 }
311 }
312 }
313}