IVGCVSW-2581 Create Deserializer
[platform/upstream/armnn.git] / src / armnnDeserializeParser / test / ParserFlatbuffersSerializeFixture.hpp
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
10 #include <armnn/IRuntime.hpp>
11 #include <armnnDeserializeParser/IDeserializeParser.hpp>
12
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
22 #include <Schema_generated.h>
23
24 using armnnDeserializeParser::IDeserializeParser;
25 using TensorRawPtr =  armnn::armnnSerializer::TensorInfo*;
26
27 struct ParserFlatbuffersSerializeFixture
28 {
29     ParserFlatbuffersSerializeFixture() :
30         m_Parser(IDeserializeParser::Create()),
31         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;
38     std::unique_ptr<IDeserializeParser, void (*)(IDeserializeParser* parser)> m_Parser;
39     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,
136                       armnn::armnnSerializer::TensorInfo tensorType, const std::string& name,
137                       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
148 template <std::size_t NumOutputDimensions,
149           armnn::DataType ArmnnType,
150           typename DataType>
151 void 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
160 template <std::size_t NumOutputDimensions,
161           armnn::DataType ArmnnType,
162           typename DataType>
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)
166 {
167     using BindingPointInfo = std::pair<armnn::LayerBindingId, armnn::TensorInfo>;
168
169     // Setup the armnn input tensors from the given vectors.
170     armnn::InputTensors inputTensors;
171     for (auto&& it : inputData)
172     {
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()) });
176     }
177
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)
182     {
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()) });
188     }
189
190     m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors);
191
192     // Compare each output tensor to the expected values
193     for (auto&& it : expectedOutputData)
194     {
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]));
198     }
199 }