blob: b372a604f3f36b3501f4b723024e039b796f9b94 [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
17#include "armnnTfLiteParser/ITfLiteParser.hpp"
18
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +000019#include <backendsCommon/BackendRegistry.hpp>
Aron Virginas-Tar54e95722018-10-25 11:47:31 +010020
telsoa01c577f2c2018-08-31 09:22:23 +010021#include "flatbuffers/idl.h"
22#include "flatbuffers/util.h"
23
24#include <schema_generated.h>
25#include <iostream>
26
27using armnnTfLiteParser::ITfLiteParser;
28using TensorRawPtr = const tflite::TensorT *;
29
30struct ParserFlatbuffersFixture
31{
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +000032 ParserFlatbuffersFixture() :
33 m_Parser(ITfLiteParser::Create()),
34 m_Runtime(armnn::IRuntime::Create(armnn::IRuntime::CreationOptions())),
35 m_NetworkIdentifier(-1)
telsoa01c577f2c2018-08-31 09:22:23 +010036 {
telsoa01c577f2c2018-08-31 09:22:23 +010037 }
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-Tar1d67a6902018-11-19 10:58:30 +000042 armnn::IRuntimePtr m_Runtime;
telsoa01c577f2c2018-08-31 09:22:23 +010043 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-Tar1d67a6902018-11-19 10:58:30 +000057 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)
telsoa01c577f2c2018-08-31 09:22:23 +010071 {
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +000072 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__));
telsoa01c577f2c2018-08-31 09:22:23 +010080 }
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 Bentham6c8e8e72019-01-15 17:57:00 +000093 std::string schemafile(&tflite_schema_start, &tflite_schema_end);
telsoa01c577f2c2018-08-31 09:22:23 +010094
95 // parse schema first, so we can use it to parse the data after
96 flatbuffers::Parser parser;
97
Matthew Bentham6c8e8e72019-01-15 17:57:00 +000098 bool ok = parser.Parse(schemafile.c_str());
telsoa01c577f2c2018-08-31 09:22:23 +010099 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
155template <std::size_t NumOutputDimensions, typename DataType>
156void 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
165template <std::size_t NumOutputDimensions, typename DataType>
166void
167ParserFlatbuffersFixture::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-Tar1d67a6902018-11-19 10:58:30 +0000171 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)
telsoa01c577f2c2018-08-31 09:22:23 +0100176 {
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000177 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 }
telsoa01c577f2c2018-08-31 09:22:23 +0100181
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000182 // 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 }
telsoa01c577f2c2018-08-31 09:22:23 +0100193
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000194 m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors);
telsoa01c577f2c2018-08-31 09:22:23 +0100195
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000196 // 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]));
telsoa01c577f2c2018-08-31 09:22:23 +0100202 }
203}