blob: 8db9d5684bfc70aad808fe0b81fb27ddc4aef65b [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"
9
10#include <armnn/IRuntime.hpp>
Derek Lamberti0028d1b2019-02-20 13:57:42 +000011#include <armnnDeserializer/IDeserializer.hpp>
Kevin May43a799c2019-02-08 16:31:42 +000012
13#include <boost/assert.hpp>
14#include <boost/format.hpp>
15
16#include "TypeUtils.hpp"
17#include "test/TensorHelpers.hpp"
18
19#include "flatbuffers/idl.h"
20#include "flatbuffers/util.h"
21
Matthew Bentham268509a2019-02-25 13:58:24 +000022#include <ArmnnSchema_generated.h>
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());
99 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,
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 /// 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);
134
135 void CheckTensors(const TensorRawPtr& tensors, size_t shapeSize, const std::vector<int32_t>& shape,
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000136 armnnSerializer::TensorInfo tensorType, const std::string& name,
Kevin May43a799c2019-02-08 16:31:42 +0000137 const float scale, const int64_t zeroPoint)
138 {
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());
145 }
146};
147
148template <std::size_t NumOutputDimensions,
149 armnn::DataType ArmnnType,
150 typename DataType>
151void ParserFlatbuffersSerializeFixture::RunTest(unsigned int layersId,
152 const std::vector<DataType>& inputData,
153 const std::vector<DataType>& expectedOutputData)
154{
155 RunTest<NumOutputDimensions, ArmnnType>(layersId,
156 { { m_SingleInputName, inputData } },
157 { { m_SingleOutputName, expectedOutputData } });
158}
159
160template <std::size_t NumOutputDimensions,
161 armnn::DataType ArmnnType,
162 typename DataType>
163void 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)
166{
167 using BindingPointInfo = std::pair<armnn::LayerBindingId, armnn::TensorInfo>;
168
Derek Lamberti8ddae332019-02-21 16:29:43 +0000169 auto ConvertBindingInfo = [](const armnnDeserializer::BindingPointInfo& bindingInfo)
170 {
171 return std::make_pair(bindingInfo.m_BindingId, bindingInfo.m_TensorInfo);
172 };
173
Kevin May43a799c2019-02-08 16:31:42 +0000174 // Setup the armnn input tensors from the given vectors.
175 armnn::InputTensors inputTensors;
176 for (auto&& it : inputData)
177 {
Derek Lamberti8ddae332019-02-21 16:29:43 +0000178 BindingPointInfo bindingInfo = ConvertBindingInfo(m_Parser->GetNetworkInputBindingInfo(layersId, it.first));
keidav011b3e2ea2019-02-21 10:07:37 +0000179 armnn::VerifyTensorInfoDataType(bindingInfo.second, ArmnnType);
Kevin May43a799c2019-02-08 16:31:42 +0000180 inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) });
181 }
182
183 // Allocate storage for the output tensors to be written to and setup the armnn output tensors.
184 std::map<std::string, boost::multi_array<DataType, NumOutputDimensions>> outputStorage;
185 armnn::OutputTensors outputTensors;
186 for (auto&& it : expectedOutputData)
187 {
Derek Lamberti8ddae332019-02-21 16:29:43 +0000188 BindingPointInfo bindingInfo = ConvertBindingInfo(m_Parser->GetNetworkOutputBindingInfo(layersId, it.first));
keidav011b3e2ea2019-02-21 10:07:37 +0000189 armnn::VerifyTensorInfoDataType(bindingInfo.second, ArmnnType);
Kevin May43a799c2019-02-08 16:31:42 +0000190 outputStorage.emplace(it.first, MakeTensor<DataType, NumOutputDimensions>(bindingInfo.second));
191 outputTensors.push_back(
192 { bindingInfo.first, armnn::Tensor(bindingInfo.second, outputStorage.at(it.first).data()) });
193 }
194
195 m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors);
196
197 // Compare each output tensor to the expected values
198 for (auto&& it : expectedOutputData)
199 {
Derek Lamberti8ddae332019-02-21 16:29:43 +0000200 BindingPointInfo bindingInfo = ConvertBindingInfo(m_Parser->GetNetworkOutputBindingInfo(layersId, it.first));
Kevin May43a799c2019-02-08 16:31:42 +0000201 auto outputExpected = MakeTensor<DataType, NumOutputDimensions>(bindingInfo.second, it.second);
202 BOOST_TEST(CompareTensors(outputExpected, outputStorage[it.first]));
203 }
Matthew Bentham268509a2019-02-25 13:58:24 +0000204}