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