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 | |
| 8 | #include "armnn/IRuntime.hpp" |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 9 | #include "armnnOnnxParser/IOnnxParser.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 10 | #include "test/TensorHelpers.hpp" |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 11 | #include "VerificationHelpers.hpp" |
| 12 | |
| 13 | #include <boost/format.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 14 | #include <string> |
| 15 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 16 | namespace armnnUtils |
| 17 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 18 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 19 | template<typename TParser> |
| 20 | struct ParserPrototxtFixture |
| 21 | { |
| 22 | ParserPrototxtFixture() |
| 23 | : m_Parser(TParser::Create()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 24 | , m_NetworkIdentifier(-1) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 25 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 26 | armnn::IRuntime::CreationOptions options; |
| 27 | m_Runtimes.push_back(std::make_pair(armnn::IRuntime::Create(options), armnn::Compute::CpuRef)); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 28 | |
| 29 | #if ARMCOMPUTENEON_ENABLED |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 30 | m_Runtimes.push_back(std::make_pair(armnn::IRuntime::Create(options), armnn::Compute::CpuAcc)); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 31 | #endif |
| 32 | |
| 33 | #if ARMCOMPUTECL_ENABLED |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 34 | m_Runtimes.push_back(std::make_pair(armnn::IRuntime::Create(options), armnn::Compute::GpuAcc)); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 35 | #endif |
| 36 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 37 | |
| 38 | /// Parses and loads the network defined by the m_Prototext string. |
| 39 | /// @{ |
| 40 | void SetupSingleInputSingleOutput(const std::string& inputName, const std::string& outputName); |
| 41 | void SetupSingleInputSingleOutput(const armnn::TensorShape& inputTensorShape, |
| 42 | const std::string& inputName, |
| 43 | const std::string& outputName); |
| 44 | void Setup(const std::map<std::string, armnn::TensorShape>& inputShapes, |
| 45 | const std::vector<std::string>& requestedOutputs); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 46 | void Setup(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 47 | /// @} |
| 48 | |
| 49 | /// 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] | 50 | /// 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] | 51 | template <std::size_t NumOutputDimensions> |
| 52 | void RunTest(const std::vector<float>& inputData, const std::vector<float>& expectedOutputData); |
| 53 | |
| 54 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 55 | /// This overload supports multiple inputs and multiple outputs, identified by name. |
| 56 | template <std::size_t NumOutputDimensions> |
| 57 | void RunTest(const std::map<std::string, std::vector<float>>& inputData, |
| 58 | const std::map<std::string, std::vector<float>>& expectedOutputData); |
| 59 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 60 | std::string m_Prototext; |
| 61 | std::unique_ptr<TParser, void(*)(TParser* parser)> m_Parser; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 62 | std::vector<std::pair<armnn::IRuntimePtr, armnn::Compute>> m_Runtimes; |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 63 | armnn::NetworkId m_NetworkIdentifier; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 64 | |
| 65 | /// If the single-input-single-output overload of Setup() is called, these will store the input and output name |
| 66 | /// so they don't need to be passed to the single-input-single-output overload of RunTest(). |
| 67 | /// @{ |
| 68 | std::string m_SingleInputName; |
| 69 | std::string m_SingleOutputName; |
| 70 | /// @} |
| 71 | }; |
| 72 | |
| 73 | template<typename TParser> |
| 74 | void ParserPrototxtFixture<TParser>::SetupSingleInputSingleOutput(const std::string& inputName, |
| 75 | const std::string& outputName) |
| 76 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 77 | // 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] | 78 | m_SingleInputName = inputName; |
| 79 | m_SingleOutputName = outputName; |
| 80 | Setup({ }, { outputName }); |
| 81 | } |
| 82 | |
| 83 | template<typename TParser> |
| 84 | void ParserPrototxtFixture<TParser>::SetupSingleInputSingleOutput(const armnn::TensorShape& inputTensorShape, |
| 85 | const std::string& inputName, |
| 86 | const std::string& outputName) |
| 87 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 88 | // 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] | 89 | m_SingleInputName = inputName; |
| 90 | m_SingleOutputName = outputName; |
| 91 | Setup({ { inputName, inputTensorShape } }, { outputName }); |
| 92 | } |
| 93 | |
| 94 | template<typename TParser> |
| 95 | void ParserPrototxtFixture<TParser>::Setup(const std::map<std::string, armnn::TensorShape>& inputShapes, |
| 96 | const std::vector<std::string>& requestedOutputs) |
| 97 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 98 | for (auto&& runtime : m_Runtimes) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 99 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 100 | std::string errorMessage; |
| 101 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 102 | armnn::INetworkPtr network = |
| 103 | m_Parser->CreateNetworkFromString(m_Prototext.c_str(), inputShapes, requestedOutputs); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 104 | auto optimized = Optimize(*network, { runtime.second, armnn::Compute::CpuRef }, runtime.first->GetDeviceSpec()); |
| 105 | armnn::Status ret = runtime.first->LoadNetwork(m_NetworkIdentifier, move(optimized), errorMessage); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 106 | if (ret != armnn::Status::Success) |
| 107 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 108 | throw armnn::Exception(boost::str( |
| 109 | boost::format("LoadNetwork failed with error: '%1%' %2%") |
| 110 | % errorMessage |
| 111 | % CHECK_LOCATION().AsString())); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | template<typename TParser> |
| 117 | void ParserPrototxtFixture<TParser>::Setup() |
| 118 | { |
| 119 | for (auto&& runtime : m_Runtimes) |
| 120 | { |
| 121 | std::string errorMessage; |
| 122 | |
| 123 | armnn::INetworkPtr network = |
| 124 | m_Parser->CreateNetworkFromString(m_Prototext.c_str()); |
| 125 | auto optimized = Optimize(*network, { runtime.second, armnn::Compute::CpuRef }, runtime.first->GetDeviceSpec()); |
| 126 | armnn::Status ret = runtime.first->LoadNetwork(m_NetworkIdentifier, move(optimized), errorMessage); |
| 127 | if (ret != armnn::Status::Success) |
| 128 | { |
| 129 | throw armnn::Exception(boost::str( |
| 130 | boost::format("LoadNetwork failed with error: '%1%' %2%") |
| 131 | % errorMessage |
| 132 | % CHECK_LOCATION().AsString())); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 133 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | template<typename TParser> |
| 138 | template <std::size_t NumOutputDimensions> |
| 139 | void ParserPrototxtFixture<TParser>::RunTest(const std::vector<float>& inputData, |
| 140 | const std::vector<float>& expectedOutputData) |
| 141 | { |
| 142 | RunTest<NumOutputDimensions>({ { m_SingleInputName, inputData } }, { { m_SingleOutputName, expectedOutputData } }); |
| 143 | } |
| 144 | |
| 145 | template<typename TParser> |
| 146 | template <std::size_t NumOutputDimensions> |
| 147 | void ParserPrototxtFixture<TParser>::RunTest(const std::map<std::string, std::vector<float>>& inputData, |
| 148 | const std::map<std::string, std::vector<float>>& expectedOutputData) |
| 149 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 150 | for (auto&& runtime : m_Runtimes) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 151 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 152 | using BindingPointInfo = std::pair<armnn::LayerBindingId, armnn::TensorInfo>; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 153 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 154 | // Sets up the armnn input tensors from the given vectors. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 155 | armnn::InputTensors inputTensors; |
| 156 | for (auto&& it : inputData) |
| 157 | { |
| 158 | BindingPointInfo bindingInfo = m_Parser->GetNetworkInputBindingInfo(it.first); |
| 159 | inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) }); |
| 160 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 161 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 162 | // Allocates storage for the output tensors to be written to and sets up the armnn output tensors. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 163 | std::map<std::string, boost::multi_array<float, NumOutputDimensions>> outputStorage; |
| 164 | armnn::OutputTensors outputTensors; |
| 165 | for (auto&& it : expectedOutputData) |
| 166 | { |
| 167 | BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(it.first); |
| 168 | outputStorage.emplace(it.first, MakeTensor<float, NumOutputDimensions>(bindingInfo.second)); |
| 169 | outputTensors.push_back( |
| 170 | { bindingInfo.first, armnn::Tensor(bindingInfo.second, outputStorage.at(it.first).data()) }); |
| 171 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 172 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 173 | runtime.first->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 174 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 175 | // Compares each output tensor to the expected values. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 176 | for (auto&& it : expectedOutputData) |
| 177 | { |
| 178 | BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(it.first); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 179 | if (bindingInfo.second.GetNumElements() != it.second.size()) |
| 180 | { |
| 181 | throw armnn::Exception( |
| 182 | boost::str( |
| 183 | boost::format("Output tensor %1% is expected to have %2% elements. " |
| 184 | "%3% elements supplied. %4%") % |
| 185 | it.first % |
| 186 | bindingInfo.second.GetNumElements() % |
| 187 | it.second.size() % |
| 188 | CHECK_LOCATION().AsString())); |
| 189 | } |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 190 | auto outputExpected = MakeTensor<float, NumOutputDimensions>(bindingInfo.second, it.second); |
| 191 | BOOST_TEST(CompareTensors(outputExpected, outputStorage[it.first])); |
| 192 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 195 | |
| 196 | } // namespace armnnUtils |