telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Aron Virginas-Tar | 9c5db11 | 2018-10-25 11:10:49 +0100 | [diff] [blame] | 8 | #include <armnn/IRuntime.hpp> |
narpra01 | 6f37f83 | 2018-12-21 18:30:00 +0000 | [diff] [blame] | 9 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 10 | #include <test/TensorHelpers.hpp> |
Aron Virginas-Tar | 9c5db11 | 2018-10-25 11:10:49 +0100 | [diff] [blame] | 11 | |
narpra01 | 6f37f83 | 2018-12-21 18:30:00 +0000 | [diff] [blame] | 12 | #include <Network.hpp> |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 13 | #include <VerificationHelpers.hpp> |
Aron Virginas-Tar | 9c5db11 | 2018-10-25 11:10:49 +0100 | [diff] [blame] | 14 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 15 | #include <boost/format.hpp> |
Aron Virginas-Tar | 9c5db11 | 2018-10-25 11:10:49 +0100 | [diff] [blame] | 16 | |
Ferran Balaguer | 51dd62f | 2019-01-11 19:29:18 +0000 | [diff] [blame] | 17 | #include <iomanip> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 18 | #include <string> |
| 19 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 20 | namespace armnnUtils |
| 21 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 22 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 23 | template<typename TParser> |
| 24 | struct ParserPrototxtFixture |
| 25 | { |
| 26 | ParserPrototxtFixture() |
| 27 | : m_Parser(TParser::Create()) |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 28 | , m_Runtime(armnn::IRuntime::Create(armnn::IRuntime::CreationOptions())) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 29 | , m_NetworkIdentifier(-1) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 30 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 31 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 32 | |
| 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 Balaguer | 51dd62f | 2019-01-11 19:29:18 +0000 | [diff] [blame] | 39 | void SetupSingleInputSingleOutput(const armnn::TensorShape& inputTensorShape, |
| 40 | const armnn::TensorShape& outputTensorShape, |
| 41 | const std::string& inputName, |
| 42 | const std::string& outputName); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 43 | void Setup(const std::map<std::string, armnn::TensorShape>& inputShapes, |
| 44 | const std::vector<std::string>& requestedOutputs); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 45 | void Setup(); |
narpra01 | 6f37f83 | 2018-12-21 18:30:00 +0000 | [diff] [blame] | 46 | armnn::IOptimizedNetworkPtr SetupOptimizedNetwork( |
| 47 | const std::map<std::string,armnn::TensorShape>& inputShapes, |
| 48 | const std::vector<std::string>& requestedOutputs); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 49 | /// @} |
| 50 | |
| 51 | /// Executes the network with the given input tensor and checks the result against the given output tensor. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 52 | /// This overload assumes that the network has a single input and a single output. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 53 | template <std::size_t NumOutputDimensions> |
| 54 | void RunTest(const std::vector<float>& inputData, const std::vector<float>& expectedOutputData); |
| 55 | |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 56 | /// 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 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 62 | /// 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. |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 64 | template <std::size_t NumOutputDimensions, typename T = float> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 65 | void RunTest(const std::map<std::string, std::vector<float>>& inputData, |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 66 | const std::map<std::string, std::vector<T>>& expectedOutputData); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 67 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 68 | std::string m_Prototext; |
| 69 | std::unique_ptr<TParser, void(*)(TParser* parser)> m_Parser; |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 70 | armnn::IRuntimePtr m_Runtime; |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 71 | armnn::NetworkId m_NetworkIdentifier; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 72 | |
| 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 Balaguer | 51dd62f | 2019-01-11 19:29:18 +0000 | [diff] [blame] | 79 | |
| 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; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | template<typename TParser> |
| 86 | void ParserPrototxtFixture<TParser>::SetupSingleInputSingleOutput(const std::string& inputName, |
| 87 | const std::string& outputName) |
| 88 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 89 | // Stores the input and output name so they don't need to be passed to the single-input-single-output RunTest(). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 90 | m_SingleInputName = inputName; |
| 91 | m_SingleOutputName = outputName; |
| 92 | Setup({ }, { outputName }); |
| 93 | } |
| 94 | |
| 95 | template<typename TParser> |
| 96 | void ParserPrototxtFixture<TParser>::SetupSingleInputSingleOutput(const armnn::TensorShape& inputTensorShape, |
| 97 | const std::string& inputName, |
| 98 | const std::string& outputName) |
| 99 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 100 | // Stores the input and output name so they don't need to be passed to the single-input-single-output RunTest(). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 101 | m_SingleInputName = inputName; |
| 102 | m_SingleOutputName = outputName; |
| 103 | Setup({ { inputName, inputTensorShape } }, { outputName }); |
| 104 | } |
| 105 | |
| 106 | template<typename TParser> |
Ferran Balaguer | 51dd62f | 2019-01-11 19:29:18 +0000 | [diff] [blame] | 107 | void 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 | |
| 120 | template<typename TParser> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 121 | void ParserPrototxtFixture<TParser>::Setup(const std::map<std::string, armnn::TensorShape>& inputShapes, |
| 122 | const std::vector<std::string>& requestedOutputs) |
| 123 | { |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 124 | std::string errorMessage; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 125 | |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 126 | 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 | { |
| 132 | throw armnn::Exception(boost::str( |
| 133 | boost::format("LoadNetwork failed with error: '%1%' %2%") |
| 134 | % errorMessage |
| 135 | % CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | template<typename TParser> |
| 140 | void ParserPrototxtFixture<TParser>::Setup() |
| 141 | { |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 142 | std::string errorMessage; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 143 | |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 144 | armnn::INetworkPtr network = |
| 145 | m_Parser->CreateNetworkFromString(m_Prototext.c_str()); |
| 146 | auto optimized = Optimize(*network, { armnn::Compute::CpuRef }, m_Runtime->GetDeviceSpec()); |
| 147 | armnn::Status ret = m_Runtime->LoadNetwork(m_NetworkIdentifier, move(optimized), errorMessage); |
| 148 | if (ret != armnn::Status::Success) |
| 149 | { |
| 150 | throw armnn::Exception(boost::str( |
| 151 | boost::format("LoadNetwork failed with error: '%1%' %2%") |
| 152 | % errorMessage |
| 153 | % CHECK_LOCATION().AsString())); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | template<typename TParser> |
narpra01 | 6f37f83 | 2018-12-21 18:30:00 +0000 | [diff] [blame] | 158 | armnn::IOptimizedNetworkPtr ParserPrototxtFixture<TParser>::SetupOptimizedNetwork( |
| 159 | const std::map<std::string,armnn::TensorShape>& inputShapes, |
| 160 | const std::vector<std::string>& requestedOutputs) |
| 161 | { |
| 162 | armnn::INetworkPtr network = |
| 163 | m_Parser->CreateNetworkFromString(m_Prototext.c_str(), inputShapes, requestedOutputs); |
| 164 | auto optimized = Optimize(*network, { armnn::Compute::CpuRef }, m_Runtime->GetDeviceSpec()); |
| 165 | return optimized; |
| 166 | } |
| 167 | |
| 168 | template<typename TParser> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 169 | template <std::size_t NumOutputDimensions> |
| 170 | void ParserPrototxtFixture<TParser>::RunTest(const std::vector<float>& inputData, |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 171 | const std::vector<float>& expectedOutputData) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 172 | { |
| 173 | RunTest<NumOutputDimensions>({ { m_SingleInputName, inputData } }, { { m_SingleOutputName, expectedOutputData } }); |
| 174 | } |
| 175 | |
| 176 | template<typename TParser> |
| 177 | template <std::size_t NumOutputDimensions> |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 178 | void ParserPrototxtFixture<TParser>::RunComparisonTest(const std::map<std::string, std::vector<float>>& inputData, |
| 179 | const std::map<std::string, std::vector<uint8_t>>& |
| 180 | expectedOutputData) |
| 181 | { |
| 182 | RunTest<NumOutputDimensions, uint8_t>(inputData, expectedOutputData); |
| 183 | } |
| 184 | |
| 185 | template<typename TParser> |
| 186 | template <std::size_t NumOutputDimensions, typename T> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 187 | void ParserPrototxtFixture<TParser>::RunTest(const std::map<std::string, std::vector<float>>& inputData, |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 188 | const std::map<std::string, std::vector<T>>& expectedOutputData) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 189 | { |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 190 | using BindingPointInfo = std::pair<armnn::LayerBindingId, armnn::TensorInfo>; |
| 191 | |
| 192 | // Sets up the armnn input tensors from the given vectors. |
| 193 | armnn::InputTensors inputTensors; |
| 194 | for (auto&& it : inputData) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 195 | { |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 196 | BindingPointInfo bindingInfo = m_Parser->GetNetworkInputBindingInfo(it.first); |
| 197 | inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) }); |
| 198 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 199 | |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 200 | // Allocates storage for the output tensors to be written to and sets up the armnn output tensors. |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 201 | std::map<std::string, boost::multi_array<T, NumOutputDimensions>> outputStorage; |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 202 | armnn::OutputTensors outputTensors; |
| 203 | for (auto&& it : expectedOutputData) |
| 204 | { |
| 205 | BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(it.first); |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 206 | outputStorage.emplace(it.first, MakeTensor<T, NumOutputDimensions>(bindingInfo.second)); |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 207 | outputTensors.push_back( |
| 208 | { bindingInfo.first, armnn::Tensor(bindingInfo.second, outputStorage.at(it.first).data()) }); |
| 209 | } |
| 210 | |
| 211 | m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors); |
| 212 | |
| 213 | // Compares each output tensor to the expected values. |
| 214 | for (auto&& it : expectedOutputData) |
| 215 | { |
| 216 | BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(it.first); |
| 217 | if (bindingInfo.second.GetNumElements() != it.second.size()) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 218 | { |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 219 | throw armnn::Exception( |
Ferran Balaguer | 51dd62f | 2019-01-11 19:29:18 +0000 | [diff] [blame] | 220 | boost::str(boost::format("Output tensor %1% is expected to have %2% elements. " |
| 221 | "%3% elements supplied. %4%") % |
| 222 | it.first % |
| 223 | bindingInfo.second.GetNumElements() % |
| 224 | it.second.size() % |
| 225 | CHECK_LOCATION().AsString())); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 226 | } |
Ferran Balaguer | 51dd62f | 2019-01-11 19:29:18 +0000 | [diff] [blame] | 227 | |
| 228 | // If the expected output shape is set, the output tensor checks will be carried out. |
| 229 | if (m_SingleOutputShape.GetNumDimensions() != 0) |
| 230 | { |
| 231 | |
| 232 | if (bindingInfo.second.GetShape().GetNumDimensions() == NumOutputDimensions && |
| 233 | bindingInfo.second.GetShape().GetNumDimensions() == m_SingleOutputShape.GetNumDimensions()) |
| 234 | { |
| 235 | for (unsigned int i = 0; i < m_SingleOutputShape.GetNumDimensions(); ++i) |
| 236 | { |
| 237 | if (m_SingleOutputShape[i] != bindingInfo.second.GetShape()[i]) |
| 238 | { |
| 239 | throw armnn::Exception( |
| 240 | boost::str(boost::format("Output tensor %1% is expected to have %2% shape. " |
| 241 | "%3% shape supplied. %4%") % |
| 242 | it.first % |
| 243 | bindingInfo.second.GetShape() % |
| 244 | m_SingleOutputShape % |
| 245 | CHECK_LOCATION().AsString())); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | else |
| 250 | { |
| 251 | throw armnn::Exception( |
| 252 | boost::str(boost::format("Output tensor %1% is expected to have %2% dimensions. " |
| 253 | "%3% dimensions supplied. %4%") % |
| 254 | it.first % |
| 255 | bindingInfo.second.GetShape().GetNumDimensions() % |
| 256 | NumOutputDimensions % |
| 257 | CHECK_LOCATION().AsString())); |
| 258 | } |
| 259 | } |
| 260 | |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 261 | auto outputExpected = MakeTensor<T, NumOutputDimensions>(bindingInfo.second, it.second); |
| 262 | if (std::is_same<T, uint8_t>::value) |
| 263 | { |
| 264 | BOOST_TEST(CompareTensors(outputExpected, outputStorage[it.first], true)); |
| 265 | } |
| 266 | else |
| 267 | { |
| 268 | BOOST_TEST(CompareTensors(outputExpected, outputStorage[it.first])); |
| 269 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 272 | |
| 273 | } // namespace armnnUtils |