IVGCVSW-2467 Remove GetDataType<T> function
[platform/upstream/armnn.git] / src / armnnTfLiteParser / test / ParserFlatbuffersFixture.hpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #pragma once
7
8 #include "Schema.hpp"
9 #include <boost/filesystem.hpp>
10 #include <boost/assert.hpp>
11 #include <boost/format.hpp>
12 #include <experimental/filesystem>
13 #include <armnn/IRuntime.hpp>
14 #include <armnn/TypesUtils.hpp>
15 #include "test/TensorHelpers.hpp"
16
17 #include "TypeUtils.hpp"
18 #include "armnnTfLiteParser/ITfLiteParser.hpp"
19
20 #include <backendsCommon/BackendRegistry.hpp>
21
22 #include "flatbuffers/idl.h"
23 #include "flatbuffers/util.h"
24
25 #include <schema_generated.h>
26 #include <iostream>
27
28 using armnnTfLiteParser::ITfLiteParser;
29 using TensorRawPtr = const tflite::TensorT *;
30
31 struct ParserFlatbuffersFixture
32 {
33     ParserFlatbuffersFixture() :
34         m_Parser(ITfLiteParser::Create()),
35         m_Runtime(armnn::IRuntime::Create(armnn::IRuntime::CreationOptions())),
36         m_NetworkIdentifier(-1)
37     {
38     }
39
40     std::vector<uint8_t> m_GraphBinary;
41     std::string m_JsonString;
42     std::unique_ptr<ITfLiteParser, void (*)(ITfLiteParser *parser)> m_Parser;
43     armnn::IRuntimePtr m_Runtime;
44     armnn::NetworkId m_NetworkIdentifier;
45
46     /// If the single-input-single-output overload of Setup() is called, these will store the input and output name
47     /// so they don't need to be passed to the single-input-single-output overload of RunTest().
48     std::string m_SingleInputName;
49     std::string m_SingleOutputName;
50
51     void Setup()
52     {
53         bool ok = ReadStringToBinary();
54         if (!ok) {
55             throw armnn::Exception("LoadNetwork failed while reading binary input");
56         }
57
58         armnn::INetworkPtr network =
59                 m_Parser->CreateNetworkFromBinary(m_GraphBinary);
60
61         if (!network) {
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         std::string errorMessage;
68
69         armnn::Status ret = m_Runtime->LoadNetwork(m_NetworkIdentifier, move(optimized), errorMessage);
70
71         if (ret != armnn::Status::Success)
72         {
73             throw armnn::Exception(
74                 boost::str(
75                     boost::format("The runtime failed to load the network. "
76                                     "Error was: %1%. in %2% [%3%:%4%]") %
77                     errorMessage %
78                     __func__ %
79                     __FILE__ %
80                     __LINE__));
81         }
82     }
83
84     void SetupSingleInputSingleOutput(const std::string& inputName, const std::string& outputName)
85     {
86         // Store the input and output name so they don't need to be passed to the single-input-single-output RunTest().
87         m_SingleInputName = inputName;
88         m_SingleOutputName = outputName;
89         Setup();
90     }
91
92     bool ReadStringToBinary()
93     {
94         std::string schemafile(&tflite_schema_start, &tflite_schema_end);
95
96         // parse schema first, so we can use it to parse the data after
97         flatbuffers::Parser parser;
98
99         bool ok = parser.Parse(schemafile.c_str());
100         BOOST_ASSERT_MSG(ok, "Failed to parse schema file");
101
102         ok &= parser.Parse(m_JsonString.c_str());
103         BOOST_ASSERT_MSG(ok, "Failed to parse json input");
104
105         if (!ok)
106         {
107             return false;
108         }
109
110         {
111             const uint8_t * bufferPtr = parser.builder_.GetBufferPointer();
112             size_t size = static_cast<size_t>(parser.builder_.GetSize());
113             m_GraphBinary.assign(bufferPtr, bufferPtr+size);
114         }
115         return ok;
116     }
117
118     /// Executes the network with the given input tensor and checks the result against the given output tensor.
119     /// This overload assumes the network has a single input and a single output.
120     template <std::size_t NumOutputDimensions,
121               armnn::DataType ArmnnType,
122               typename DataType = armnn::ResolveType<ArmnnType>>
123     void RunTest(size_t subgraphId,
124                  const std::vector<DataType>& inputData,
125                  const std::vector<DataType>& expectedOutputData);
126
127     /// Executes the network with the given input tensors and checks the results against the given output tensors.
128     /// This overload supports multiple inputs and multiple outputs, identified by name.
129     template <std::size_t NumOutputDimensions,
130               armnn::DataType ArmnnType,
131               typename DataType = armnn::ResolveType<ArmnnType>>
132     void RunTest(size_t subgraphId,
133                  const std::map<std::string, std::vector<DataType>>& inputData,
134                  const std::map<std::string, std::vector<DataType>>& expectedOutputData);
135
136     void CheckTensors(const TensorRawPtr& tensors, size_t shapeSize, const std::vector<int32_t>& shape,
137                       tflite::TensorType tensorType, uint32_t buffer, const std::string& name,
138                       const std::vector<float>& min, const std::vector<float>& max,
139                       const std::vector<float>& scale, const std::vector<int64_t>& zeroPoint)
140     {
141         BOOST_CHECK(tensors);
142         BOOST_CHECK_EQUAL(shapeSize, tensors->shape.size());
143         BOOST_CHECK_EQUAL_COLLECTIONS(shape.begin(), shape.end(), tensors->shape.begin(), tensors->shape.end());
144         BOOST_CHECK_EQUAL(tensorType, tensors->type);
145         BOOST_CHECK_EQUAL(buffer, tensors->buffer);
146         BOOST_CHECK_EQUAL(name, tensors->name);
147         BOOST_CHECK(tensors->quantization);
148         BOOST_CHECK_EQUAL_COLLECTIONS(min.begin(), min.end(), tensors->quantization.get()->min.begin(),
149                                       tensors->quantization.get()->min.end());
150         BOOST_CHECK_EQUAL_COLLECTIONS(max.begin(), max.end(), tensors->quantization.get()->max.begin(),
151                                       tensors->quantization.get()->max.end());
152         BOOST_CHECK_EQUAL_COLLECTIONS(scale.begin(), scale.end(), tensors->quantization.get()->scale.begin(),
153                                       tensors->quantization.get()->scale.end());
154         BOOST_CHECK_EQUAL_COLLECTIONS(zeroPoint.begin(), zeroPoint.end(),
155                                       tensors->quantization.get()->zero_point.begin(),
156                                       tensors->quantization.get()->zero_point.end());
157     }
158 };
159
160 template <std::size_t NumOutputDimensions,
161           armnn::DataType ArmnnType,
162           typename DataType>
163 void ParserFlatbuffersFixture::RunTest(size_t subgraphId,
164                                        const std::vector<DataType>& inputData,
165                                        const std::vector<DataType>& expectedOutputData)
166 {
167     RunTest<NumOutputDimensions, ArmnnType>(subgraphId,
168                                             { { m_SingleInputName, inputData } },
169                                             { { m_SingleOutputName, expectedOutputData } });
170 }
171
172 template <std::size_t NumOutputDimensions,
173           armnn::DataType ArmnnType,
174           typename DataType>
175 void ParserFlatbuffersFixture::RunTest(size_t subgraphId,
176                                        const std::map<std::string, std::vector<DataType>>& inputData,
177                                        const std::map<std::string, std::vector<DataType>>& expectedOutputData)
178 {
179     using BindingPointInfo = std::pair<armnn::LayerBindingId, armnn::TensorInfo>;
180
181     // Setup the armnn input tensors from the given vectors.
182     armnn::InputTensors inputTensors;
183     for (auto&& it : inputData)
184     {
185         BindingPointInfo bindingInfo = m_Parser->GetNetworkInputBindingInfo(subgraphId, it.first);
186         armnn::VerifyTensorInfoDataType<ArmnnType>(bindingInfo.second);
187         inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) });
188     }
189
190     // Allocate storage for the output tensors to be written to and setup the armnn output tensors.
191     std::map<std::string, boost::multi_array<DataType, NumOutputDimensions>> outputStorage;
192     armnn::OutputTensors outputTensors;
193     for (auto&& it : expectedOutputData)
194     {
195         BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first);
196         armnn::VerifyTensorInfoDataType<ArmnnType>(bindingInfo.second);
197         outputStorage.emplace(it.first, MakeTensor<DataType, NumOutputDimensions>(bindingInfo.second));
198         outputTensors.push_back(
199                 { bindingInfo.first, armnn::Tensor(bindingInfo.second, outputStorage.at(it.first).data()) });
200     }
201
202     m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors);
203
204     // Compare each output tensor to the expected values
205     for (auto&& it : expectedOutputData)
206     {
207         BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first);
208         auto outputExpected = MakeTensor<DataType, NumOutputDimensions>(bindingInfo.second, it.second);
209         BOOST_TEST(CompareTensors(outputExpected, outputStorage[it.first]));
210     }
211 }