2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
8 #include "SchemaSerialize.hpp"
10 #include <armnn/IRuntime.hpp>
11 #include <armnnDeserializeParser/IDeserializeParser.hpp>
13 #include <boost/assert.hpp>
14 #include <boost/format.hpp>
16 #include "TypeUtils.hpp"
17 #include "test/TensorHelpers.hpp"
19 #include "flatbuffers/idl.h"
20 #include "flatbuffers/util.h"
22 #include <Schema_generated.h>
24 using armnnDeserializeParser::IDeserializeParser;
25 using TensorRawPtr = armnn::armnnSerializer::TensorInfo*;
27 struct ParserFlatbuffersSerializeFixture
29 ParserFlatbuffersSerializeFixture() :
30 m_Parser(IDeserializeParser::Create()),
31 m_Runtime(armnn::IRuntime::Create(armnn::IRuntime::CreationOptions())),
32 m_NetworkIdentifier(-1)
36 std::vector<uint8_t> m_GraphBinary;
37 std::string m_JsonString;
38 std::unique_ptr<IDeserializeParser, void (*)(IDeserializeParser* parser)> m_Parser;
39 armnn::IRuntimePtr m_Runtime;
40 armnn::NetworkId m_NetworkIdentifier;
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;
49 bool ok = ReadStringToBinary();
52 throw armnn::Exception("LoadNetwork failed while reading binary input");
55 armnn::INetworkPtr network =
56 m_Parser->CreateNetworkFromBinary(m_GraphBinary);
60 throw armnn::Exception("The parser failed to create an ArmNN network");
63 auto optimized = Optimize(*network, {armnn::Compute::CpuRef},
64 m_Runtime->GetDeviceSpec());
66 std::string errorMessage;
67 armnn::Status ret = m_Runtime->LoadNetwork(m_NetworkIdentifier, move(optimized), errorMessage);
69 if (ret != armnn::Status::Success)
71 throw armnn::Exception(
73 boost::format("The runtime failed to load the network. "
74 "Error was: %1%. in %2% [%3%:%4%]") %
83 void SetupSingleInputSingleOutput(const std::string& inputName, const std::string& outputName)
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;
91 bool ReadStringToBinary()
93 std::string schemafile(&deserialize_schema_start, &deserialize_schema_end);
95 // parse schema first, so we can use it to parse the data after
96 flatbuffers::Parser parser;
98 bool ok = parser.Parse(schemafile.c_str());
99 BOOST_ASSERT_MSG(ok, "Failed to parse schema file");
101 ok &= parser.Parse(m_JsonString.c_str());
102 BOOST_ASSERT_MSG(ok, "Failed to parse json input");
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);
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);
126 /// Executes the network with the given input tensors and checks the results against the given output tensors.
127 /// This overload supports multiple inputs and multiple outputs, identified by name.
128 template <std::size_t NumOutputDimensions,
129 armnn::DataType ArmnnType,
130 typename DataType = armnn::ResolveType<ArmnnType>>
131 void RunTest(unsigned int layersId,
132 const std::map<std::string, std::vector<DataType>>& inputData,
133 const std::map<std::string, std::vector<DataType>>& expectedOutputData);
135 void CheckTensors(const TensorRawPtr& tensors, size_t shapeSize, const std::vector<int32_t>& shape,
136 armnn::armnnSerializer::TensorInfo tensorType, const std::string& name,
137 const float scale, const int64_t zeroPoint)
139 BOOST_CHECK_EQUAL(shapeSize, tensors->dimensions()->size());
140 BOOST_CHECK_EQUAL_COLLECTIONS(shape.begin(), shape.end(),
141 tensors->dimensions()->begin(), tensors->dimensions()->end());
142 BOOST_CHECK_EQUAL(tensorType.dataType(), tensors->dataType());
143 BOOST_CHECK_EQUAL(scale, tensors->quantizationScale());
144 BOOST_CHECK_EQUAL(zeroPoint, tensors->quantizationOffset());
148 template <std::size_t NumOutputDimensions,
149 armnn::DataType ArmnnType,
151 void ParserFlatbuffersSerializeFixture::RunTest(unsigned int layersId,
152 const std::vector<DataType>& inputData,
153 const std::vector<DataType>& expectedOutputData)
155 RunTest<NumOutputDimensions, ArmnnType>(layersId,
156 { { m_SingleInputName, inputData } },
157 { { m_SingleOutputName, expectedOutputData } });
160 template <std::size_t NumOutputDimensions,
161 armnn::DataType ArmnnType,
163 void ParserFlatbuffersSerializeFixture::RunTest(unsigned int layersId,
164 const std::map<std::string, std::vector<DataType>>& inputData,
165 const std::map<std::string, std::vector<DataType>>& expectedOutputData)
167 using BindingPointInfo = std::pair<armnn::LayerBindingId, armnn::TensorInfo>;
169 // Setup the armnn input tensors from the given vectors.
170 armnn::InputTensors inputTensors;
171 for (auto&& it : inputData)
173 BindingPointInfo bindingInfo = m_Parser->GetNetworkInputBindingInfo(layersId, it.first);
174 armnn::VerifyTensorInfoDataType<ArmnnType>(bindingInfo.second);
175 inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) });
178 // Allocate storage for the output tensors to be written to and setup the armnn output tensors.
179 std::map<std::string, boost::multi_array<DataType, NumOutputDimensions>> outputStorage;
180 armnn::OutputTensors outputTensors;
181 for (auto&& it : expectedOutputData)
183 BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(layersId, it.first);
184 armnn::VerifyTensorInfoDataType<ArmnnType>(bindingInfo.second);
185 outputStorage.emplace(it.first, MakeTensor<DataType, NumOutputDimensions>(bindingInfo.second));
186 outputTensors.push_back(
187 { bindingInfo.first, armnn::Tensor(bindingInfo.second, outputStorage.at(it.first).data()) });
190 m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors);
192 // Compare each output tensor to the expected values
193 for (auto&& it : expectedOutputData)
195 BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(layersId, it.first);
196 auto outputExpected = MakeTensor<DataType, NumOutputDimensions>(bindingInfo.second, it.second);
197 BOOST_TEST(CompareTensors(outputExpected, outputStorage[it.first]));