blob: bb38d5f4b46d4d09221f612428a11aebdc805fc1 [file] [log] [blame]
Kevin May43a799c2019-02-08 16:31:42 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
8#include "SchemaSerialize.hpp"
Kevin May43a799c2019-02-08 16:31:42 +00009#include "test/TensorHelpers.hpp"
10
11#include "flatbuffers/idl.h"
12#include "flatbuffers/util.h"
13
Matthew Bentham268509a2019-02-25 13:58:24 +000014#include <ArmnnSchema_generated.h>
Jan Eilers8eb25602020-03-09 12:13:48 +000015#include <armnn/IRuntime.hpp>
16#include <armnnDeserializer/IDeserializer.hpp>
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010017#include <armnn/utility/Assert.hpp>
Jan Eilers8eb25602020-03-09 12:13:48 +000018#include <armnn/utility/IgnoreUnused.hpp>
19#include <ResolveType.hpp>
20
Jan Eilers8eb25602020-03-09 12:13:48 +000021#include <boost/format.hpp>
22
Kevin May43a799c2019-02-08 16:31:42 +000023
Derek Lamberti0028d1b2019-02-20 13:57:42 +000024using armnnDeserializer::IDeserializer;
Matthew Bentham268509a2019-02-25 13:58:24 +000025using TensorRawPtr = armnnSerializer::TensorInfo*;
Kevin May43a799c2019-02-08 16:31:42 +000026
27struct ParserFlatbuffersSerializeFixture
28{
29 ParserFlatbuffersSerializeFixture() :
Derek Lamberti0028d1b2019-02-20 13:57:42 +000030 m_Parser(IDeserializer::Create()),
Kevin May43a799c2019-02-08 16:31:42 +000031 m_Runtime(armnn::IRuntime::Create(armnn::IRuntime::CreationOptions())),
32 m_NetworkIdentifier(-1)
33 {
34 }
35
36 std::vector<uint8_t> m_GraphBinary;
37 std::string m_JsonString;
Derek Lamberti0028d1b2019-02-20 13:57:42 +000038 std::unique_ptr<IDeserializer, void (*)(IDeserializer* parser)> m_Parser;
Kevin May43a799c2019-02-08 16:31:42 +000039 armnn::IRuntimePtr m_Runtime;
40 armnn::NetworkId m_NetworkIdentifier;
41
42 /// If the single-input-single-output overload of Setup() is called, these will store the input and output name
43 /// so they don't need to be passed to the single-input-single-output overload of RunTest().
44 std::string m_SingleInputName;
45 std::string m_SingleOutputName;
46
47 void Setup()
48 {
49 bool ok = ReadStringToBinary();
50 if (!ok)
51 {
52 throw armnn::Exception("LoadNetwork failed while reading binary input");
53 }
54
55 armnn::INetworkPtr network =
56 m_Parser->CreateNetworkFromBinary(m_GraphBinary);
57
58 if (!network)
59 {
60 throw armnn::Exception("The parser failed to create an ArmNN network");
61 }
62
63 auto optimized = Optimize(*network, {armnn::Compute::CpuRef},
64 m_Runtime->GetDeviceSpec());
65
66 std::string errorMessage;
67 armnn::Status ret = m_Runtime->LoadNetwork(m_NetworkIdentifier, move(optimized), errorMessage);
68
69 if (ret != armnn::Status::Success)
70 {
71 throw armnn::Exception(
72 boost::str(
73 boost::format("The runtime failed to load the network. "
74 "Error was: %1%. in %2% [%3%:%4%]") %
75 errorMessage %
76 __func__ %
77 __FILE__ %
78 __LINE__));
79 }
80
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 {
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());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010099 ARMNN_ASSERT_MSG(ok, "Failed to parse schema file");
Kevin May43a799c2019-02-08 16:31:42 +0000100
101 ok &= parser.Parse(m_JsonString.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100102 ARMNN_ASSERT_MSG(ok, "Failed to parse json input");
Kevin May43a799c2019-02-08 16:31:42 +0000103
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.
Nattapat Chaimanowong9066d3c2019-02-27 17:27:16 +0000119 template<std::size_t NumOutputDimensions,
120 armnn::DataType ArmnnType,
121 typename DataType = armnn::ResolveType<ArmnnType>>
Kevin May43a799c2019-02-08 16:31:42 +0000122 void RunTest(unsigned int layersId,
123 const std::vector<DataType>& inputData,
124 const std::vector<DataType>& expectedOutputData);
125
Nattapat Chaimanowong9066d3c2019-02-27 17:27:16 +0000126 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
Kevin May43a799c2019-02-08 16:31:42 +0000135 /// 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.
Nattapat Chaimanowong9066d3c2019-02-27 17:27:16 +0000137 template<std::size_t NumOutputDimensions,
138 armnn::DataType ArmnnType,
139 typename DataType = armnn::ResolveType<ArmnnType>>
Kevin May43a799c2019-02-08 16:31:42 +0000140 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
Nattapat Chaimanowong9066d3c2019-02-27 17:27:16 +0000144 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
Kevin May43a799c2019-02-08 16:31:42 +0000153 void CheckTensors(const TensorRawPtr& tensors, size_t shapeSize, const std::vector<int32_t>& shape,
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000154 armnnSerializer::TensorInfo tensorType, const std::string& name,
Kevin May43a799c2019-02-08 16:31:42 +0000155 const float scale, const int64_t zeroPoint)
156 {
Jan Eilers8eb25602020-03-09 12:13:48 +0000157 armnn::IgnoreUnused(name);
Kevin May43a799c2019-02-08 16:31:42 +0000158 BOOST_CHECK_EQUAL(shapeSize, tensors->dimensions()->size());
159 BOOST_CHECK_EQUAL_COLLECTIONS(shape.begin(), shape.end(),
160 tensors->dimensions()->begin(), tensors->dimensions()->end());
161 BOOST_CHECK_EQUAL(tensorType.dataType(), tensors->dataType());
162 BOOST_CHECK_EQUAL(scale, tensors->quantizationScale());
163 BOOST_CHECK_EQUAL(zeroPoint, tensors->quantizationOffset());
164 }
165};
166
Nattapat Chaimanowong9066d3c2019-02-27 17:27:16 +0000167template<std::size_t NumOutputDimensions, armnn::DataType ArmnnType, typename DataType>
Kevin May43a799c2019-02-08 16:31:42 +0000168void ParserFlatbuffersSerializeFixture::RunTest(unsigned int layersId,
169 const std::vector<DataType>& inputData,
170 const std::vector<DataType>& expectedOutputData)
171{
Nattapat Chaimanowong9066d3c2019-02-27 17:27:16 +0000172 RunTest<NumOutputDimensions, ArmnnType, ArmnnType, DataType, DataType>(layersId, inputData, expectedOutputData);
Kevin May43a799c2019-02-08 16:31:42 +0000173}
174
Nattapat Chaimanowong9066d3c2019-02-27 17:27:16 +0000175template<std::size_t NumOutputDimensions,
176 armnn::DataType ArmnnInputType,
177 armnn::DataType ArmnnOutputType,
178 typename InputDataType,
179 typename OutputDataType>
180void 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
189template<std::size_t NumOutputDimensions, armnn::DataType ArmnnType, typename DataType>
Kevin May43a799c2019-02-08 16:31:42 +0000190void 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{
Nattapat Chaimanowong9066d3c2019-02-27 17:27:16 +0000194 RunTest<NumOutputDimensions, ArmnnType, ArmnnType, DataType, DataType>(layersId, inputData, expectedOutputData);
195}
196
197template<std::size_t NumOutputDimensions,
198 armnn::DataType ArmnnInputType,
199 armnn::DataType ArmnnOutputType,
200 typename InputDataType,
201 typename OutputDataType>
202void 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{
Derek Lamberti8ddae332019-02-21 16:29:43 +0000207 auto ConvertBindingInfo = [](const armnnDeserializer::BindingPointInfo& bindingInfo)
208 {
209 return std::make_pair(bindingInfo.m_BindingId, bindingInfo.m_TensorInfo);
210 };
211
Kevin May43a799c2019-02-08 16:31:42 +0000212 // Setup the armnn input tensors from the given vectors.
213 armnn::InputTensors inputTensors;
214 for (auto&& it : inputData)
215 {
Jim Flynnb4d7eae2019-05-01 14:44:27 +0100216 armnn::BindingPointInfo bindingInfo = ConvertBindingInfo(
217 m_Parser->GetNetworkInputBindingInfo(layersId, it.first));
Nattapat Chaimanowong9066d3c2019-02-27 17:27:16 +0000218 armnn::VerifyTensorInfoDataType(bindingInfo.second, ArmnnInputType);
Kevin May43a799c2019-02-08 16:31:42 +0000219 inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) });
220 }
221
222 // Allocate storage for the output tensors to be written to and setup the armnn output tensors.
Nattapat Chaimanowong9066d3c2019-02-27 17:27:16 +0000223 std::map<std::string, boost::multi_array<OutputDataType, NumOutputDimensions>> outputStorage;
Kevin May43a799c2019-02-08 16:31:42 +0000224 armnn::OutputTensors outputTensors;
225 for (auto&& it : expectedOutputData)
226 {
Jim Flynnb4d7eae2019-05-01 14:44:27 +0100227 armnn::BindingPointInfo bindingInfo = ConvertBindingInfo(
228 m_Parser->GetNetworkOutputBindingInfo(layersId, it.first));
Nattapat Chaimanowong9066d3c2019-02-27 17:27:16 +0000229 armnn::VerifyTensorInfoDataType(bindingInfo.second, ArmnnOutputType);
230 outputStorage.emplace(it.first, MakeTensor<OutputDataType, NumOutputDimensions>(bindingInfo.second));
Kevin May43a799c2019-02-08 16:31:42 +0000231 outputTensors.push_back(
232 { bindingInfo.first, armnn::Tensor(bindingInfo.second, outputStorage.at(it.first).data()) });
233 }
234
235 m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors);
236
237 // Compare each output tensor to the expected values
238 for (auto&& it : expectedOutputData)
239 {
Jim Flynnb4d7eae2019-05-01 14:44:27 +0100240 armnn::BindingPointInfo bindingInfo = ConvertBindingInfo(
241 m_Parser->GetNetworkOutputBindingInfo(layersId, it.first));
Nattapat Chaimanowong9066d3c2019-02-27 17:27:16 +0000242 auto outputExpected = MakeTensor<OutputDataType, NumOutputDimensions>(bindingInfo.second, it.second);
Kevin May43a799c2019-02-08 16:31:42 +0000243 BOOST_TEST(CompareTensors(outputExpected, outputStorage[it.first]));
244 }
Matthew Bentham268509a2019-02-25 13:58:24 +0000245}