blob: cf28fcf51388f4c4cfe20937de1e5b092efb5b5a [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
telsoa014fcda012018-03-09 14:13:49 +00002// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5
6#pragma once
7
Aron Virginas-Tar9c5db112018-10-25 11:10:49 +01008#include <armnn/IRuntime.hpp>
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +00009#include <test/TensorHelpers.hpp>
Aron Virginas-Tar9c5db112018-10-25 11:10:49 +010010
narpra016f37f832018-12-21 18:30:00 +000011#include <Network.hpp>
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +000012#include <VerificationHelpers.hpp>
Aron Virginas-Tar9c5db112018-10-25 11:10:49 +010013
telsoa01c577f2c2018-08-31 09:22:23 +010014#include <boost/format.hpp>
Colm Donelan5b5c2222020-09-09 12:48:16 +010015#include <fmt/format.h>
Aron Virginas-Tar9c5db112018-10-25 11:10:49 +010016
Ferran Balaguer51dd62f2019-01-11 19:29:18 +000017#include <iomanip>
telsoa014fcda012018-03-09 14:13:49 +000018#include <string>
19
telsoa01c577f2c2018-08-31 09:22:23 +010020namespace armnnUtils
21{
surmeh013537c2c2018-05-18 16:31:43 +010022
telsoa014fcda012018-03-09 14:13:49 +000023template<typename TParser>
24struct ParserPrototxtFixture
25{
26 ParserPrototxtFixture()
27 : m_Parser(TParser::Create())
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +000028 , m_Runtime(armnn::IRuntime::Create(armnn::IRuntime::CreationOptions()))
telsoa014fcda012018-03-09 14:13:49 +000029 , m_NetworkIdentifier(-1)
surmeh013537c2c2018-05-18 16:31:43 +010030 {
surmeh013537c2c2018-05-18 16:31:43 +010031 }
telsoa014fcda012018-03-09 14:13:49 +000032
33 /// Parses and loads the network defined by the m_Prototext string.
34 /// @{
35 void SetupSingleInputSingleOutput(const std::string& inputName, const std::string& outputName);
36 void SetupSingleInputSingleOutput(const armnn::TensorShape& inputTensorShape,
37 const std::string& inputName,
38 const std::string& outputName);
Ferran Balaguer51dd62f2019-01-11 19:29:18 +000039 void SetupSingleInputSingleOutput(const armnn::TensorShape& inputTensorShape,
40 const armnn::TensorShape& outputTensorShape,
41 const std::string& inputName,
42 const std::string& outputName);
telsoa014fcda012018-03-09 14:13:49 +000043 void Setup(const std::map<std::string, armnn::TensorShape>& inputShapes,
44 const std::vector<std::string>& requestedOutputs);
telsoa01c577f2c2018-08-31 09:22:23 +010045 void Setup();
narpra016f37f832018-12-21 18:30:00 +000046 armnn::IOptimizedNetworkPtr SetupOptimizedNetwork(
47 const std::map<std::string,armnn::TensorShape>& inputShapes,
48 const std::vector<std::string>& requestedOutputs);
telsoa014fcda012018-03-09 14:13:49 +000049 /// @}
50
51 /// Executes the network with the given input tensor and checks the result against the given output tensor.
telsoa01c577f2c2018-08-31 09:22:23 +010052 /// This overload assumes that the network has a single input and a single output.
telsoa014fcda012018-03-09 14:13:49 +000053 template <std::size_t NumOutputDimensions>
54 void RunTest(const std::vector<float>& inputData, const std::vector<float>& expectedOutputData);
55
kevmay012b4d88e2019-01-24 14:05:09 +000056 /// Executes the network with the given input tensor and checks the result against the given output tensor.
57 /// Calls RunTest with output type of uint8_t for checking comparison operators.
58 template <std::size_t NumOutputDimensions>
59 void RunComparisonTest(const std::map<std::string, std::vector<float>>& inputData,
60 const std::map<std::string, std::vector<uint8_t>>& expectedOutputData);
61
telsoa014fcda012018-03-09 14:13:49 +000062 /// Executes the network with the given input tensors and checks the results against the given output tensors.
63 /// This overload supports multiple inputs and multiple outputs, identified by name.
kevmay012b4d88e2019-01-24 14:05:09 +000064 template <std::size_t NumOutputDimensions, typename T = float>
telsoa014fcda012018-03-09 14:13:49 +000065 void RunTest(const std::map<std::string, std::vector<float>>& inputData,
kevmay012b4d88e2019-01-24 14:05:09 +000066 const std::map<std::string, std::vector<T>>& expectedOutputData);
telsoa014fcda012018-03-09 14:13:49 +000067
surmeh013537c2c2018-05-18 16:31:43 +010068 std::string m_Prototext;
69 std::unique_ptr<TParser, void(*)(TParser* parser)> m_Parser;
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +000070 armnn::IRuntimePtr m_Runtime;
surmeh013537c2c2018-05-18 16:31:43 +010071 armnn::NetworkId m_NetworkIdentifier;
telsoa014fcda012018-03-09 14:13:49 +000072
73 /// If the single-input-single-output overload of Setup() is called, these will store the input and output name
74 /// so they don't need to be passed to the single-input-single-output overload of RunTest().
75 /// @{
76 std::string m_SingleInputName;
77 std::string m_SingleOutputName;
78 /// @}
Ferran Balaguer51dd62f2019-01-11 19:29:18 +000079
80 /// This will store the output shape so it don't need to be passed to the single-input-single-output overload
81 /// of RunTest().
82 armnn::TensorShape m_SingleOutputShape;
telsoa014fcda012018-03-09 14:13:49 +000083};
84
85template<typename TParser>
86void ParserPrototxtFixture<TParser>::SetupSingleInputSingleOutput(const std::string& inputName,
87 const std::string& outputName)
88{
telsoa01c577f2c2018-08-31 09:22:23 +010089 // Stores the input and output name so they don't need to be passed to the single-input-single-output RunTest().
telsoa014fcda012018-03-09 14:13:49 +000090 m_SingleInputName = inputName;
91 m_SingleOutputName = outputName;
92 Setup({ }, { outputName });
93}
94
95template<typename TParser>
96void ParserPrototxtFixture<TParser>::SetupSingleInputSingleOutput(const armnn::TensorShape& inputTensorShape,
97 const std::string& inputName,
98 const std::string& outputName)
99{
telsoa01c577f2c2018-08-31 09:22:23 +0100100 // Stores the input and output name so they don't need to be passed to the single-input-single-output RunTest().
telsoa014fcda012018-03-09 14:13:49 +0000101 m_SingleInputName = inputName;
102 m_SingleOutputName = outputName;
103 Setup({ { inputName, inputTensorShape } }, { outputName });
104}
105
106template<typename TParser>
Ferran Balaguer51dd62f2019-01-11 19:29:18 +0000107void ParserPrototxtFixture<TParser>::SetupSingleInputSingleOutput(const armnn::TensorShape& inputTensorShape,
108 const armnn::TensorShape& outputTensorShape,
109 const std::string& inputName,
110 const std::string& outputName)
111{
112 // Stores the input name, the output name and the output tensor shape
113 // so they don't need to be passed to the single-input-single-output RunTest().
114 m_SingleInputName = inputName;
115 m_SingleOutputName = outputName;
116 m_SingleOutputShape = outputTensorShape;
117 Setup({ { inputName, inputTensorShape } }, { outputName });
118}
119
120template<typename TParser>
telsoa014fcda012018-03-09 14:13:49 +0000121void ParserPrototxtFixture<TParser>::Setup(const std::map<std::string, armnn::TensorShape>& inputShapes,
122 const std::vector<std::string>& requestedOutputs)
123{
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000124 std::string errorMessage;
telsoa01c577f2c2018-08-31 09:22:23 +0100125
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000126 armnn::INetworkPtr network =
127 m_Parser->CreateNetworkFromString(m_Prototext.c_str(), inputShapes, requestedOutputs);
128 auto optimized = Optimize(*network, { armnn::Compute::CpuRef }, m_Runtime->GetDeviceSpec());
129 armnn::Status ret = m_Runtime->LoadNetwork(m_NetworkIdentifier, move(optimized), errorMessage);
130 if (ret != armnn::Status::Success)
131 {
Colm Donelan5b5c2222020-09-09 12:48:16 +0100132 throw armnn::Exception(fmt::format("LoadNetwork failed with error: '{0}' {1}",
133 errorMessage,
134 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100135 }
136}
137
138template<typename TParser>
139void ParserPrototxtFixture<TParser>::Setup()
140{
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000141 std::string errorMessage;
telsoa01c577f2c2018-08-31 09:22:23 +0100142
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000143 armnn::INetworkPtr network =
144 m_Parser->CreateNetworkFromString(m_Prototext.c_str());
145 auto optimized = Optimize(*network, { armnn::Compute::CpuRef }, m_Runtime->GetDeviceSpec());
146 armnn::Status ret = m_Runtime->LoadNetwork(m_NetworkIdentifier, move(optimized), errorMessage);
147 if (ret != armnn::Status::Success)
148 {
Colm Donelan5b5c2222020-09-09 12:48:16 +0100149 throw armnn::Exception(fmt::format("LoadNetwork failed with error: '{0}' {1}",
150 errorMessage,
151 CHECK_LOCATION().AsString()));
telsoa014fcda012018-03-09 14:13:49 +0000152 }
153}
154
155template<typename TParser>
narpra016f37f832018-12-21 18:30:00 +0000156armnn::IOptimizedNetworkPtr ParserPrototxtFixture<TParser>::SetupOptimizedNetwork(
157 const std::map<std::string,armnn::TensorShape>& inputShapes,
158 const std::vector<std::string>& requestedOutputs)
159{
160 armnn::INetworkPtr network =
161 m_Parser->CreateNetworkFromString(m_Prototext.c_str(), inputShapes, requestedOutputs);
162 auto optimized = Optimize(*network, { armnn::Compute::CpuRef }, m_Runtime->GetDeviceSpec());
163 return optimized;
164}
165
166template<typename TParser>
telsoa014fcda012018-03-09 14:13:49 +0000167template <std::size_t NumOutputDimensions>
168void ParserPrototxtFixture<TParser>::RunTest(const std::vector<float>& inputData,
kevmay012b4d88e2019-01-24 14:05:09 +0000169 const std::vector<float>& expectedOutputData)
telsoa014fcda012018-03-09 14:13:49 +0000170{
171 RunTest<NumOutputDimensions>({ { m_SingleInputName, inputData } }, { { m_SingleOutputName, expectedOutputData } });
172}
173
174template<typename TParser>
175template <std::size_t NumOutputDimensions>
kevmay012b4d88e2019-01-24 14:05:09 +0000176void ParserPrototxtFixture<TParser>::RunComparisonTest(const std::map<std::string, std::vector<float>>& inputData,
177 const std::map<std::string, std::vector<uint8_t>>&
178 expectedOutputData)
179{
180 RunTest<NumOutputDimensions, uint8_t>(inputData, expectedOutputData);
181}
182
183template<typename TParser>
184template <std::size_t NumOutputDimensions, typename T>
telsoa014fcda012018-03-09 14:13:49 +0000185void ParserPrototxtFixture<TParser>::RunTest(const std::map<std::string, std::vector<float>>& inputData,
kevmay012b4d88e2019-01-24 14:05:09 +0000186 const std::map<std::string, std::vector<T>>& expectedOutputData)
telsoa014fcda012018-03-09 14:13:49 +0000187{
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000188 // Sets up the armnn input tensors from the given vectors.
189 armnn::InputTensors inputTensors;
190 for (auto&& it : inputData)
telsoa014fcda012018-03-09 14:13:49 +0000191 {
Jim Flynnb4d7eae2019-05-01 14:44:27 +0100192 armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkInputBindingInfo(it.first);
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000193 inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) });
194 }
telsoa014fcda012018-03-09 14:13:49 +0000195
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000196 // Allocates storage for the output tensors to be written to and sets up the armnn output tensors.
kevmay012b4d88e2019-01-24 14:05:09 +0000197 std::map<std::string, boost::multi_array<T, NumOutputDimensions>> outputStorage;
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000198 armnn::OutputTensors outputTensors;
199 for (auto&& it : expectedOutputData)
200 {
Jim Flynnb4d7eae2019-05-01 14:44:27 +0100201 armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(it.first);
kevmay012b4d88e2019-01-24 14:05:09 +0000202 outputStorage.emplace(it.first, MakeTensor<T, NumOutputDimensions>(bindingInfo.second));
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000203 outputTensors.push_back(
204 { bindingInfo.first, armnn::Tensor(bindingInfo.second, outputStorage.at(it.first).data()) });
205 }
206
207 m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors);
208
209 // Compares each output tensor to the expected values.
210 for (auto&& it : expectedOutputData)
211 {
Jim Flynnb4d7eae2019-05-01 14:44:27 +0100212 armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(it.first);
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000213 if (bindingInfo.second.GetNumElements() != it.second.size())
surmeh013537c2c2018-05-18 16:31:43 +0100214 {
Colm Donelan5b5c2222020-09-09 12:48:16 +0100215 throw armnn::Exception(fmt::format("Output tensor {0} is expected to have {1} elements. "
216 "{2} elements supplied. {3}",
217 it.first,
218 bindingInfo.second.GetNumElements(),
219 it.second.size(),
220 CHECK_LOCATION().AsString()));
surmeh013537c2c2018-05-18 16:31:43 +0100221 }
Ferran Balaguer51dd62f2019-01-11 19:29:18 +0000222
223 // If the expected output shape is set, the output tensor checks will be carried out.
224 if (m_SingleOutputShape.GetNumDimensions() != 0)
225 {
226
227 if (bindingInfo.second.GetShape().GetNumDimensions() == NumOutputDimensions &&
228 bindingInfo.second.GetShape().GetNumDimensions() == m_SingleOutputShape.GetNumDimensions())
229 {
230 for (unsigned int i = 0; i < m_SingleOutputShape.GetNumDimensions(); ++i)
231 {
232 if (m_SingleOutputShape[i] != bindingInfo.second.GetShape()[i])
233 {
Colm Donelan5b5c2222020-09-09 12:48:16 +0100234 // This exception message could not be created by fmt:format because of an oddity in
235 // the operator << of TensorShape.
236 std::stringstream message;
237 message << "Output tensor " << it.first << " is expected to have "
238 << bindingInfo.second.GetShape() << "shape. "
239 << m_SingleOutputShape << " shape supplied. "
240 << CHECK_LOCATION().AsString();
241 throw armnn::Exception(message.str());
Ferran Balaguer51dd62f2019-01-11 19:29:18 +0000242 }
243 }
244 }
245 else
246 {
Colm Donelan5b5c2222020-09-09 12:48:16 +0100247 throw armnn::Exception(fmt::format("Output tensor {0} is expected to have {1} dimensions. "
248 "{2} dimensions supplied. {3}",
249 it.first,
250 bindingInfo.second.GetShape().GetNumDimensions(),
251 NumOutputDimensions,
252 CHECK_LOCATION().AsString()));
Ferran Balaguer51dd62f2019-01-11 19:29:18 +0000253 }
254 }
255
kevmay012b4d88e2019-01-24 14:05:09 +0000256 auto outputExpected = MakeTensor<T, NumOutputDimensions>(bindingInfo.second, it.second);
257 if (std::is_same<T, uint8_t>::value)
258 {
259 BOOST_TEST(CompareTensors(outputExpected, outputStorage[it.first], true));
260 }
261 else
262 {
263 BOOST_TEST(CompareTensors(outputExpected, outputStorage[it.first]));
264 }
telsoa014fcda012018-03-09 14:13:49 +0000265 }
266}
telsoa01c577f2c2018-08-31 09:22:23 +0100267
268} // namespace armnnUtils