1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #pragma once
7
8 #include "SchemaSerialize.hpp"
9 #include <armnnTestUtils/TensorHelpers.hpp>
10
11 #include "flatbuffers/idl.h"
12 #include "flatbuffers/util.h"
13
14 #include <ArmnnSchema_generated.h>
15 #include <armnn/IRuntime.hpp>
16 #include <armnnDeserializer/IDeserializer.hpp>
17 #include <armnn/utility/Assert.hpp>
18 #include <armnn/utility/IgnoreUnused.hpp>
19 #include <ResolveType.hpp>
20
21 #include <fmt/format.h>
22 #include <doctest/doctest.h>
23
24 #include <vector>
25
26 using armnnDeserializer::IDeserializer;
27 using TensorRawPtr = armnnSerializer::TensorInfo*;
28
29 struct ParserFlatbuffersSerializeFixture
30 {
ParserFlatbuffersSerializeFixtureParserFlatbuffersSerializeFixture31 ParserFlatbuffersSerializeFixture() :
32 m_Parser(IDeserializer::Create()),
33 m_Runtime(armnn::IRuntime::Create(armnn::IRuntime::CreationOptions())),
34 m_NetworkIdentifier(-1)
35 {
36 }
37
38 std::vector<uint8_t> m_GraphBinary;
39 std::string m_JsonString;
40 std::unique_ptr<IDeserializer, void (*)(IDeserializer* parser)> m_Parser;
41 armnn::IRuntimePtr m_Runtime;
42 armnn::NetworkId m_NetworkIdentifier;
43
44 /// If the single-input-single-output overload of Setup() is called, these will store the input and output name
45 /// so they don't need to be passed to the single-input-single-output overload of RunTest().
46 std::string m_SingleInputName;
47 std::string m_SingleOutputName;
48
SetupParserFlatbuffersSerializeFixture49 void Setup()
50 {
51 bool ok = ReadStringToBinary();
52 if (!ok)
53 {
54 throw armnn::Exception("LoadNetwork failed while reading binary input");
55 }
56
57 armnn::INetworkPtr network =
58 m_Parser->CreateNetworkFromBinary(m_GraphBinary);
59
60 if (!network)
61 {
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
68 std::string errorMessage;
69 armnn::Status ret = m_Runtime->LoadNetwork(m_NetworkIdentifier, move(optimized), errorMessage);
70
71 if (ret != armnn::Status::Success)
72 {
73 throw armnn::Exception(fmt::format("The runtime failed to load the network. "
74 "Error was: {0}. in {1} [{2}:{3}]",
75 errorMessage,
76 __func__,
77 __FILE__,
78 __LINE__));
79 }
80
81 }
82
SetupSingleInputSingleOutputParserFlatbuffersSerializeFixture83 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
ReadStringToBinaryParserFlatbuffersSerializeFixture91 bool ReadStringToBinary()
92 {
93 std::string schemafile(&deserialize_schema_start, &deserialize_schema_end);
94
95 // parse schema first, so we can use it to parse the data after
96 flatbuffers::Parser parser;
97
98 bool ok = parser.Parse(schemafile.c_str());
99 CHECK_MESSAGE(ok, std::string("Failed to parse schema file. Error was: " + parser.error_).c_str());
100
101 ok &= parser.Parse(m_JsonString.c_str());
102 CHECK_MESSAGE(ok, std::string("Failed to parse json input. Error was: " + parser.error_).c_str());
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,
120 armnn::DataType ArmnnType,
121 typename DataType = armnn::ResolveType<ArmnnType>>
122 void RunTest(unsigned int layersId,
123 const std::vector<DataType>& inputData,
124 const std::vector<DataType>& expectedOutputData);
125
126 template<std::size_t NumOutputDimensions,
127 armnn::DataType ArmnnInputType,
128 armnn::DataType ArmnnOutputType,
129 typename InputDataType = armnn::ResolveType<ArmnnInputType>,
130 typename OutputDataType = armnn::ResolveType<ArmnnOutputType>>
131 void RunTest(unsigned int layersId,
132 const std::vector<InputDataType>& inputData,
133 const std::vector<OutputDataType>& expectedOutputData);
134
135 /// Executes the network with the given input tensors and checks the results against the given output tensors.
136 /// This overload supports multiple inputs and multiple outputs, identified by name.
137 template<std::size_t NumOutputDimensions,
138 armnn::DataType ArmnnType,
139 typename DataType = armnn::ResolveType<ArmnnType>>
140 void RunTest(unsigned int layersId,
141 const std::map<std::string, std::vector<DataType>>& inputData,
142 const std::map<std::string, std::vector<DataType>>& expectedOutputData);
143
144 template<std::size_t NumOutputDimensions,
145 armnn::DataType ArmnnInputType,
146 armnn::DataType ArmnnOutputType,
147 typename InputDataType = armnn::ResolveType<ArmnnInputType>,
148 typename OutputDataType = armnn::ResolveType<ArmnnOutputType>>
149 void RunTest(unsigned int layersId,
150 const std::map<std::string, std::vector<InputDataType>>& inputData,
151 const std::map<std::string, std::vector<OutputDataType>>& expectedOutputData);
152
CheckTensorsParserFlatbuffersSerializeFixture153 void CheckTensors(const TensorRawPtr& tensors, size_t shapeSize, const std::vector<int32_t>& shape,
154 armnnSerializer::TensorInfo tensorType, const std::string& name,
155 const float scale, const int64_t zeroPoint)
156 {
157 armnn::IgnoreUnused(name);
158 CHECK_EQ(shapeSize, tensors->dimensions()->size());
159 CHECK(std::equal(shape.begin(), shape.end(),
160 tensors->dimensions()->begin(), tensors->dimensions()->end()));
161 CHECK_EQ(tensorType.dataType(), tensors->dataType());
162 CHECK_EQ(scale, tensors->quantizationScale());
163 CHECK_EQ(zeroPoint, tensors->quantizationOffset());
164 }
165 };
166
167 template<std::size_t NumOutputDimensions, armnn::DataType ArmnnType, typename DataType>
RunTest(unsigned int layersId,const std::vector<DataType> & inputData,const std::vector<DataType> & expectedOutputData)168 void ParserFlatbuffersSerializeFixture::RunTest(unsigned int layersId,
169 const std::vector<DataType>& inputData,
170 const std::vector<DataType>& expectedOutputData)
171 {
172 RunTest<NumOutputDimensions, ArmnnType, ArmnnType, DataType, DataType>(layersId, inputData, expectedOutputData);
173 }
174
175 template<std::size_t NumOutputDimensions,
176 armnn::DataType ArmnnInputType,
177 armnn::DataType ArmnnOutputType,
178 typename InputDataType,
179 typename OutputDataType>
RunTest(unsigned int layersId,const std::vector<InputDataType> & inputData,const std::vector<OutputDataType> & expectedOutputData)180 void ParserFlatbuffersSerializeFixture::RunTest(unsigned int layersId,
181 const std::vector<InputDataType>& inputData,
182 const std::vector<OutputDataType>& expectedOutputData)
183 {
184 RunTest<NumOutputDimensions, ArmnnInputType, ArmnnOutputType>(layersId,
185 { { m_SingleInputName, inputData } },
186 { { m_SingleOutputName, expectedOutputData } });
187 }
188
189 template<std::size_t NumOutputDimensions, armnn::DataType ArmnnType, typename DataType>
RunTest(unsigned int layersId,const std::map<std::string,std::vector<DataType>> & inputData,const std::map<std::string,std::vector<DataType>> & expectedOutputData)190 void ParserFlatbuffersSerializeFixture::RunTest(unsigned int layersId,
191 const std::map<std::string, std::vector<DataType>>& inputData,
192 const std::map<std::string, std::vector<DataType>>& expectedOutputData)
193 {
194 RunTest<NumOutputDimensions, ArmnnType, ArmnnType, DataType, DataType>(layersId, inputData, expectedOutputData);
195 }
196
197 template<std::size_t NumOutputDimensions,
198 armnn::DataType ArmnnInputType,
199 armnn::DataType ArmnnOutputType,
200 typename InputDataType,
201 typename OutputDataType>
RunTest(unsigned int layersId,const std::map<std::string,std::vector<InputDataType>> & inputData,const std::map<std::string,std::vector<OutputDataType>> & expectedOutputData)202 void ParserFlatbuffersSerializeFixture::RunTest(
203 unsigned int layersId,
204 const std::map<std::string, std::vector<InputDataType>>& inputData,
205 const std::map<std::string, std::vector<OutputDataType>>& expectedOutputData)
206 {
207 auto ConvertBindingInfo = [](const armnnDeserializer::BindingPointInfo& bindingInfo)
208 {
209 return std::make_pair(bindingInfo.m_BindingId, bindingInfo.m_TensorInfo);
210 };
211
212 // Setup the armnn input tensors from the given vectors.
213 armnn::InputTensors inputTensors;
214 for (auto&& it : inputData)
215 {
216 armnn::BindingPointInfo bindingInfo = ConvertBindingInfo(
217 m_Parser->GetNetworkInputBindingInfo(layersId, it.first));
218 bindingInfo.second.SetConstant(true);
219 armnn::VerifyTensorInfoDataType(bindingInfo.second, ArmnnInputType);
220 inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) });
221 }
222
223 // Allocate storage for the output tensors to be written to and setup the armnn output tensors.
224 std::map<std::string, std::vector<OutputDataType>> outputStorage;
225 armnn::OutputTensors outputTensors;
226 for (auto&& it : expectedOutputData)
227 {
228 armnn::BindingPointInfo bindingInfo = ConvertBindingInfo(
229 m_Parser->GetNetworkOutputBindingInfo(layersId, it.first));
230 armnn::VerifyTensorInfoDataType(bindingInfo.second, ArmnnOutputType);
231 outputStorage.emplace(it.first, std::vector<OutputDataType>(bindingInfo.second.GetNumElements()));
232 outputTensors.push_back(
233 { bindingInfo.first, armnn::Tensor(bindingInfo.second, outputStorage.at(it.first).data()) });
234 }
235
236 m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors);
237
238 // Compare each output tensor to the expected values
239 for (auto&& it : expectedOutputData)
240 {
241 armnn::BindingPointInfo bindingInfo = ConvertBindingInfo(
242 m_Parser->GetNetworkOutputBindingInfo(layersId, it.first));
243 auto outputExpected = it.second;
244 auto result = CompareTensors(outputExpected, outputStorage[it.first],
245 bindingInfo.second.GetShape(), bindingInfo.second.GetShape());
246 CHECK_MESSAGE(result.m_Result, result.m_Message.str());
247 }
248 }
249