blob: 8d0ee01aa94fca305b1b1c1493004c0c3fd3f842 [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
5
6#pragma once
7
Matthew Bentham6c8e8e72019-01-15 17:57:00 +00008#include "Schema.hpp"
telsoa01c577f2c2018-08-31 09:22:23 +01009#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
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +000017#include "TypeUtils.hpp"
telsoa01c577f2c2018-08-31 09:22:23 +010018#include "armnnTfLiteParser/ITfLiteParser.hpp"
19
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +000020#include <backendsCommon/BackendRegistry.hpp>
Aron Virginas-Tar54e95722018-10-25 11:47:31 +010021
telsoa01c577f2c2018-08-31 09:22:23 +010022#include "flatbuffers/idl.h"
23#include "flatbuffers/util.h"
24
25#include <schema_generated.h>
26#include <iostream>
27
28using armnnTfLiteParser::ITfLiteParser;
29using TensorRawPtr = const tflite::TensorT *;
30
31struct ParserFlatbuffersFixture
32{
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +000033 ParserFlatbuffersFixture() :
34 m_Parser(ITfLiteParser::Create()),
35 m_Runtime(armnn::IRuntime::Create(armnn::IRuntime::CreationOptions())),
36 m_NetworkIdentifier(-1)
telsoa01c577f2c2018-08-31 09:22:23 +010037 {
telsoa01c577f2c2018-08-31 09:22:23 +010038 }
39
40 std::vector<uint8_t> m_GraphBinary;
41 std::string m_JsonString;
42 std::unique_ptr<ITfLiteParser, void (*)(ITfLiteParser *parser)> m_Parser;
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +000043 armnn::IRuntimePtr m_Runtime;
telsoa01c577f2c2018-08-31 09:22:23 +010044 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
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +000058 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)
telsoa01c577f2c2018-08-31 09:22:23 +010072 {
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +000073 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__));
telsoa01c577f2c2018-08-31 09:22:23 +010081 }
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 {
Matthew Bentham6c8e8e72019-01-15 17:57:00 +000094 std::string schemafile(&tflite_schema_start, &tflite_schema_end);
telsoa01c577f2c2018-08-31 09:22:23 +010095
96 // parse schema first, so we can use it to parse the data after
97 flatbuffers::Parser parser;
98
Matthew Bentham6c8e8e72019-01-15 17:57:00 +000099 bool ok = parser.Parse(schemafile.c_str());
telsoa01c577f2c2018-08-31 09:22:23 +0100100 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.
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000120 template <std::size_t NumOutputDimensions,
121 armnn::DataType ArmnnType,
122 typename DataType = armnn::ResolveType<ArmnnType>>
telsoa01c577f2c2018-08-31 09:22:23 +0100123 void RunTest(size_t subgraphId,
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000124 const std::vector<DataType>& inputData,
125 const std::vector<DataType>& expectedOutputData);
telsoa01c577f2c2018-08-31 09:22:23 +0100126
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.
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000129 template <std::size_t NumOutputDimensions,
130 armnn::DataType ArmnnType,
131 typename DataType = armnn::ResolveType<ArmnnType>>
telsoa01c577f2c2018-08-31 09:22:23 +0100132 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
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000160template <std::size_t NumOutputDimensions,
161 armnn::DataType ArmnnType,
162 typename DataType>
telsoa01c577f2c2018-08-31 09:22:23 +0100163void ParserFlatbuffersFixture::RunTest(size_t subgraphId,
164 const std::vector<DataType>& inputData,
165 const std::vector<DataType>& expectedOutputData)
166{
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000167 RunTest<NumOutputDimensions, ArmnnType>(subgraphId,
168 { { m_SingleInputName, inputData } },
169 { { m_SingleOutputName, expectedOutputData } });
telsoa01c577f2c2018-08-31 09:22:23 +0100170}
171
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000172template <std::size_t NumOutputDimensions,
173 armnn::DataType ArmnnType,
174 typename DataType>
175void 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)
telsoa01c577f2c2018-08-31 09:22:23 +0100178{
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000179 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)
telsoa01c577f2c2018-08-31 09:22:23 +0100184 {
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000185 BindingPointInfo bindingInfo = m_Parser->GetNetworkInputBindingInfo(subgraphId, it.first);
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000186 armnn::VerifyTensorInfoDataType<ArmnnType>(bindingInfo.second);
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000187 inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) });
188 }
telsoa01c577f2c2018-08-31 09:22:23 +0100189
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000190 // 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);
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000196 armnn::VerifyTensorInfoDataType<ArmnnType>(bindingInfo.second);
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000197 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 }
telsoa01c577f2c2018-08-31 09:22:23 +0100201
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000202 m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors);
telsoa01c577f2c2018-08-31 09:22:23 +0100203
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000204 // 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]));
telsoa01c577f2c2018-08-31 09:22:23 +0100210 }
211}