blob: 782c181982e79bf6e4ca944eed2473886670c62c [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
Colm Donelan5b5c2222020-09-09 12:48:16 +010014#include <fmt/format.h>
Aron Virginas-Tar9c5db112018-10-25 11:10:49 +010015
Ferran Balaguer51dd62f2019-01-11 19:29:18 +000016#include <iomanip>
telsoa014fcda012018-03-09 14:13:49 +000017#include <string>
18
telsoa01c577f2c2018-08-31 09:22:23 +010019namespace armnnUtils
20{
surmeh013537c2c2018-05-18 16:31:43 +010021
telsoa014fcda012018-03-09 14:13:49 +000022template<typename TParser>
23struct ParserPrototxtFixture
24{
25 ParserPrototxtFixture()
26 : m_Parser(TParser::Create())
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +000027 , m_Runtime(armnn::IRuntime::Create(armnn::IRuntime::CreationOptions()))
telsoa014fcda012018-03-09 14:13:49 +000028 , m_NetworkIdentifier(-1)
surmeh013537c2c2018-05-18 16:31:43 +010029 {
surmeh013537c2c2018-05-18 16:31:43 +010030 }
telsoa014fcda012018-03-09 14:13:49 +000031
32 /// Parses and loads the network defined by the m_Prototext string.
33 /// @{
34 void SetupSingleInputSingleOutput(const std::string& inputName, const std::string& outputName);
35 void SetupSingleInputSingleOutput(const armnn::TensorShape& inputTensorShape,
36 const std::string& inputName,
37 const std::string& outputName);
Ferran Balaguer51dd62f2019-01-11 19:29:18 +000038 void SetupSingleInputSingleOutput(const armnn::TensorShape& inputTensorShape,
39 const armnn::TensorShape& outputTensorShape,
40 const std::string& inputName,
41 const std::string& outputName);
telsoa014fcda012018-03-09 14:13:49 +000042 void Setup(const std::map<std::string, armnn::TensorShape>& inputShapes,
43 const std::vector<std::string>& requestedOutputs);
telsoa01c577f2c2018-08-31 09:22:23 +010044 void Setup();
narpra016f37f832018-12-21 18:30:00 +000045 armnn::IOptimizedNetworkPtr SetupOptimizedNetwork(
46 const std::map<std::string,armnn::TensorShape>& inputShapes,
47 const std::vector<std::string>& requestedOutputs);
telsoa014fcda012018-03-09 14:13:49 +000048 /// @}
49
50 /// Executes the network with the given input tensor and checks the result against the given output tensor.
telsoa01c577f2c2018-08-31 09:22:23 +010051 /// This overload assumes that the network has a single input and a single output.
telsoa014fcda012018-03-09 14:13:49 +000052 template <std::size_t NumOutputDimensions>
53 void RunTest(const std::vector<float>& inputData, const std::vector<float>& expectedOutputData);
54
kevmay012b4d88e2019-01-24 14:05:09 +000055 /// Executes the network with the given input tensor and checks the result against the given output tensor.
56 /// Calls RunTest with output type of uint8_t for checking comparison operators.
57 template <std::size_t NumOutputDimensions>
58 void RunComparisonTest(const std::map<std::string, std::vector<float>>& inputData,
59 const std::map<std::string, std::vector<uint8_t>>& expectedOutputData);
60
telsoa014fcda012018-03-09 14:13:49 +000061 /// Executes the network with the given input tensors and checks the results against the given output tensors.
62 /// This overload supports multiple inputs and multiple outputs, identified by name.
kevmay012b4d88e2019-01-24 14:05:09 +000063 template <std::size_t NumOutputDimensions, typename T = float>
telsoa014fcda012018-03-09 14:13:49 +000064 void RunTest(const std::map<std::string, std::vector<float>>& inputData,
kevmay012b4d88e2019-01-24 14:05:09 +000065 const std::map<std::string, std::vector<T>>& expectedOutputData);
telsoa014fcda012018-03-09 14:13:49 +000066
surmeh013537c2c2018-05-18 16:31:43 +010067 std::string m_Prototext;
68 std::unique_ptr<TParser, void(*)(TParser* parser)> m_Parser;
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +000069 armnn::IRuntimePtr m_Runtime;
surmeh013537c2c2018-05-18 16:31:43 +010070 armnn::NetworkId m_NetworkIdentifier;
telsoa014fcda012018-03-09 14:13:49 +000071
72 /// If the single-input-single-output overload of Setup() is called, these will store the input and output name
73 /// so they don't need to be passed to the single-input-single-output overload of RunTest().
74 /// @{
75 std::string m_SingleInputName;
76 std::string m_SingleOutputName;
77 /// @}
Ferran Balaguer51dd62f2019-01-11 19:29:18 +000078
79 /// This will store the output shape so it don't need to be passed to the single-input-single-output overload
80 /// of RunTest().
81 armnn::TensorShape m_SingleOutputShape;
telsoa014fcda012018-03-09 14:13:49 +000082};
83
84template<typename TParser>
85void ParserPrototxtFixture<TParser>::SetupSingleInputSingleOutput(const std::string& inputName,
86 const std::string& outputName)
87{
telsoa01c577f2c2018-08-31 09:22:23 +010088 // 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 +000089 m_SingleInputName = inputName;
90 m_SingleOutputName = outputName;
91 Setup({ }, { outputName });
92}
93
94template<typename TParser>
95void ParserPrototxtFixture<TParser>::SetupSingleInputSingleOutput(const armnn::TensorShape& inputTensorShape,
96 const std::string& inputName,
97 const std::string& outputName)
98{
telsoa01c577f2c2018-08-31 09:22:23 +010099 // 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 +0000100 m_SingleInputName = inputName;
101 m_SingleOutputName = outputName;
102 Setup({ { inputName, inputTensorShape } }, { outputName });
103}
104
105template<typename TParser>
Ferran Balaguer51dd62f2019-01-11 19:29:18 +0000106void ParserPrototxtFixture<TParser>::SetupSingleInputSingleOutput(const armnn::TensorShape& inputTensorShape,
107 const armnn::TensorShape& outputTensorShape,
108 const std::string& inputName,
109 const std::string& outputName)
110{
111 // Stores the input name, the output name and the output tensor shape
112 // so they don't need to be passed to the single-input-single-output RunTest().
113 m_SingleInputName = inputName;
114 m_SingleOutputName = outputName;
115 m_SingleOutputShape = outputTensorShape;
116 Setup({ { inputName, inputTensorShape } }, { outputName });
117}
118
119template<typename TParser>
telsoa014fcda012018-03-09 14:13:49 +0000120void ParserPrototxtFixture<TParser>::Setup(const std::map<std::string, armnn::TensorShape>& inputShapes,
121 const std::vector<std::string>& requestedOutputs)
122{
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000123 std::string errorMessage;
telsoa01c577f2c2018-08-31 09:22:23 +0100124
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000125 armnn::INetworkPtr network =
126 m_Parser->CreateNetworkFromString(m_Prototext.c_str(), inputShapes, requestedOutputs);
127 auto optimized = Optimize(*network, { armnn::Compute::CpuRef }, m_Runtime->GetDeviceSpec());
128 armnn::Status ret = m_Runtime->LoadNetwork(m_NetworkIdentifier, move(optimized), errorMessage);
129 if (ret != armnn::Status::Success)
130 {
Colm Donelan5b5c2222020-09-09 12:48:16 +0100131 throw armnn::Exception(fmt::format("LoadNetwork failed with error: '{0}' {1}",
132 errorMessage,
133 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100134 }
135}
136
137template<typename TParser>
138void ParserPrototxtFixture<TParser>::Setup()
139{
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000140 std::string errorMessage;
telsoa01c577f2c2018-08-31 09:22:23 +0100141
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000142 armnn::INetworkPtr network =
143 m_Parser->CreateNetworkFromString(m_Prototext.c_str());
144 auto optimized = Optimize(*network, { armnn::Compute::CpuRef }, m_Runtime->GetDeviceSpec());
145 armnn::Status ret = m_Runtime->LoadNetwork(m_NetworkIdentifier, move(optimized), errorMessage);
146 if (ret != armnn::Status::Success)
147 {
Colm Donelan5b5c2222020-09-09 12:48:16 +0100148 throw armnn::Exception(fmt::format("LoadNetwork failed with error: '{0}' {1}",
149 errorMessage,
150 CHECK_LOCATION().AsString()));
telsoa014fcda012018-03-09 14:13:49 +0000151 }
152}
153
154template<typename TParser>
narpra016f37f832018-12-21 18:30:00 +0000155armnn::IOptimizedNetworkPtr ParserPrototxtFixture<TParser>::SetupOptimizedNetwork(
156 const std::map<std::string,armnn::TensorShape>& inputShapes,
157 const std::vector<std::string>& requestedOutputs)
158{
159 armnn::INetworkPtr network =
160 m_Parser->CreateNetworkFromString(m_Prototext.c_str(), inputShapes, requestedOutputs);
161 auto optimized = Optimize(*network, { armnn::Compute::CpuRef }, m_Runtime->GetDeviceSpec());
162 return optimized;
163}
164
165template<typename TParser>
telsoa014fcda012018-03-09 14:13:49 +0000166template <std::size_t NumOutputDimensions>
167void ParserPrototxtFixture<TParser>::RunTest(const std::vector<float>& inputData,
kevmay012b4d88e2019-01-24 14:05:09 +0000168 const std::vector<float>& expectedOutputData)
telsoa014fcda012018-03-09 14:13:49 +0000169{
170 RunTest<NumOutputDimensions>({ { m_SingleInputName, inputData } }, { { m_SingleOutputName, expectedOutputData } });
171}
172
173template<typename TParser>
174template <std::size_t NumOutputDimensions>
kevmay012b4d88e2019-01-24 14:05:09 +0000175void ParserPrototxtFixture<TParser>::RunComparisonTest(const std::map<std::string, std::vector<float>>& inputData,
176 const std::map<std::string, std::vector<uint8_t>>&
177 expectedOutputData)
178{
179 RunTest<NumOutputDimensions, uint8_t>(inputData, expectedOutputData);
180}
181
182template<typename TParser>
183template <std::size_t NumOutputDimensions, typename T>
telsoa014fcda012018-03-09 14:13:49 +0000184void ParserPrototxtFixture<TParser>::RunTest(const std::map<std::string, std::vector<float>>& inputData,
kevmay012b4d88e2019-01-24 14:05:09 +0000185 const std::map<std::string, std::vector<T>>& expectedOutputData)
telsoa014fcda012018-03-09 14:13:49 +0000186{
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000187 // Sets up the armnn input tensors from the given vectors.
188 armnn::InputTensors inputTensors;
189 for (auto&& it : inputData)
telsoa014fcda012018-03-09 14:13:49 +0000190 {
Jim Flynnb4d7eae2019-05-01 14:44:27 +0100191 armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkInputBindingInfo(it.first);
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000192 inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) });
193 }
telsoa014fcda012018-03-09 14:13:49 +0000194
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000195 // Allocates storage for the output tensors to be written to and sets up the armnn output tensors.
kevmay012b4d88e2019-01-24 14:05:09 +0000196 std::map<std::string, boost::multi_array<T, NumOutputDimensions>> outputStorage;
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000197 armnn::OutputTensors outputTensors;
198 for (auto&& it : expectedOutputData)
199 {
Jim Flynnb4d7eae2019-05-01 14:44:27 +0100200 armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(it.first);
kevmay012b4d88e2019-01-24 14:05:09 +0000201 outputStorage.emplace(it.first, MakeTensor<T, NumOutputDimensions>(bindingInfo.second));
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000202 outputTensors.push_back(
203 { bindingInfo.first, armnn::Tensor(bindingInfo.second, outputStorage.at(it.first).data()) });
204 }
205
206 m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors);
207
208 // Compares each output tensor to the expected values.
209 for (auto&& it : expectedOutputData)
210 {
Jim Flynnb4d7eae2019-05-01 14:44:27 +0100211 armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(it.first);
Aron Virginas-Tar1d67a6902018-11-19 10:58:30 +0000212 if (bindingInfo.second.GetNumElements() != it.second.size())
surmeh013537c2c2018-05-18 16:31:43 +0100213 {
Colm Donelan5b5c2222020-09-09 12:48:16 +0100214 throw armnn::Exception(fmt::format("Output tensor {0} is expected to have {1} elements. "
215 "{2} elements supplied. {3}",
216 it.first,
217 bindingInfo.second.GetNumElements(),
218 it.second.size(),
219 CHECK_LOCATION().AsString()));
surmeh013537c2c2018-05-18 16:31:43 +0100220 }
Ferran Balaguer51dd62f2019-01-11 19:29:18 +0000221
222 // If the expected output shape is set, the output tensor checks will be carried out.
223 if (m_SingleOutputShape.GetNumDimensions() != 0)
224 {
225
226 if (bindingInfo.second.GetShape().GetNumDimensions() == NumOutputDimensions &&
227 bindingInfo.second.GetShape().GetNumDimensions() == m_SingleOutputShape.GetNumDimensions())
228 {
229 for (unsigned int i = 0; i < m_SingleOutputShape.GetNumDimensions(); ++i)
230 {
231 if (m_SingleOutputShape[i] != bindingInfo.second.GetShape()[i])
232 {
Colm Donelan5b5c2222020-09-09 12:48:16 +0100233 // This exception message could not be created by fmt:format because of an oddity in
234 // the operator << of TensorShape.
235 std::stringstream message;
236 message << "Output tensor " << it.first << " is expected to have "
237 << bindingInfo.second.GetShape() << "shape. "
238 << m_SingleOutputShape << " shape supplied. "
239 << CHECK_LOCATION().AsString();
240 throw armnn::Exception(message.str());
Ferran Balaguer51dd62f2019-01-11 19:29:18 +0000241 }
242 }
243 }
244 else
245 {
Colm Donelan5b5c2222020-09-09 12:48:16 +0100246 throw armnn::Exception(fmt::format("Output tensor {0} is expected to have {1} dimensions. "
247 "{2} dimensions supplied. {3}",
248 it.first,
249 bindingInfo.second.GetShape().GetNumDimensions(),
250 NumOutputDimensions,
251 CHECK_LOCATION().AsString()));
Ferran Balaguer51dd62f2019-01-11 19:29:18 +0000252 }
253 }
254
kevmay012b4d88e2019-01-24 14:05:09 +0000255 auto outputExpected = MakeTensor<T, NumOutputDimensions>(bindingInfo.second, it.second);
256 if (std::is_same<T, uint8_t>::value)
257 {
258 BOOST_TEST(CompareTensors(outputExpected, outputStorage[it.first], true));
259 }
260 else
261 {
262 BOOST_TEST(CompareTensors(outputExpected, outputStorage[it.first]));
263 }
telsoa014fcda012018-03-09 14:13:49 +0000264 }
265}
telsoa01c577f2c2018-08-31 09:22:23 +0100266
267} // namespace armnnUtils