Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 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 | #pragma once |
| 6 | |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 7 | #include "InferenceModel.hpp" |
| 8 | |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 9 | #include <armnn/ArmNN.hpp> |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 10 | #include <armnn/Logging.hpp> |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 11 | #include <armnn/TypesUtils.hpp> |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 12 | #include <armnn/utility/IgnoreUnused.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 13 | |
James Ward | c89829f | 2020-10-12 14:17:36 +0100 | [diff] [blame] | 14 | #include <cxxopts/cxxopts.hpp> |
| 15 | #include <fmt/format.h> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 16 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 17 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 18 | namespace armnn |
| 19 | { |
| 20 | |
| 21 | inline std::istream& operator>>(std::istream& in, armnn::Compute& compute) |
| 22 | { |
| 23 | std::string token; |
| 24 | in >> token; |
| 25 | compute = armnn::ParseComputeDevice(token.c_str()); |
| 26 | if (compute == armnn::Compute::Undefined) |
| 27 | { |
| 28 | in.setstate(std::ios_base::failbit); |
James Ward | c89829f | 2020-10-12 14:17:36 +0100 | [diff] [blame] | 29 | throw cxxopts::OptionException(fmt::format("Unrecognised compute device: {}", token)); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 30 | } |
| 31 | return in; |
| 32 | } |
| 33 | |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 34 | inline std::istream& operator>>(std::istream& in, armnn::BackendId& backend) |
| 35 | { |
| 36 | std::string token; |
| 37 | in >> token; |
| 38 | armnn::Compute compute = armnn::ParseComputeDevice(token.c_str()); |
| 39 | if (compute == armnn::Compute::Undefined) |
| 40 | { |
| 41 | in.setstate(std::ios_base::failbit); |
James Ward | c89829f | 2020-10-12 14:17:36 +0100 | [diff] [blame] | 42 | throw cxxopts::OptionException(fmt::format("Unrecognised compute device: {}", token)); |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 43 | } |
| 44 | backend = compute; |
| 45 | return in; |
| 46 | } |
| 47 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 48 | namespace test |
| 49 | { |
| 50 | |
| 51 | class TestFrameworkException : public Exception |
| 52 | { |
| 53 | public: |
| 54 | using Exception::Exception; |
| 55 | }; |
| 56 | |
| 57 | struct InferenceTestOptions |
| 58 | { |
| 59 | unsigned int m_IterationCount; |
| 60 | std::string m_InferenceTimesFile; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 61 | bool m_EnableProfiling; |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 62 | std::string m_DynamicBackendsPath; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 63 | |
| 64 | InferenceTestOptions() |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 65 | : m_IterationCount(0) |
| 66 | , m_EnableProfiling(0) |
| 67 | , m_DynamicBackendsPath() |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 68 | {} |
| 69 | }; |
| 70 | |
| 71 | enum class TestCaseResult |
| 72 | { |
| 73 | /// The test completed without any errors. |
| 74 | Ok, |
| 75 | /// The test failed (e.g. the prediction didn't match the validation file). |
| 76 | /// This will eventually fail the whole program but the remaining test cases will still be run. |
| 77 | Failed, |
| 78 | /// The test failed with a fatal error. The remaining tests will not be run. |
| 79 | Abort |
| 80 | }; |
| 81 | |
| 82 | class IInferenceTestCase |
| 83 | { |
| 84 | public: |
| 85 | virtual ~IInferenceTestCase() {} |
| 86 | |
| 87 | virtual void Run() = 0; |
| 88 | virtual TestCaseResult ProcessResult(const InferenceTestOptions& options) = 0; |
| 89 | }; |
| 90 | |
| 91 | class IInferenceTestCaseProvider |
| 92 | { |
| 93 | public: |
| 94 | virtual ~IInferenceTestCaseProvider() {} |
| 95 | |
James Ward | c89829f | 2020-10-12 14:17:36 +0100 | [diff] [blame] | 96 | virtual void AddCommandLineOptions(cxxopts::Options& options, std::vector<std::string>& required) |
Derek Lamberti | eb1fce0 | 2019-12-10 21:20:10 +0000 | [diff] [blame] | 97 | { |
James Ward | c89829f | 2020-10-12 14:17:36 +0100 | [diff] [blame] | 98 | IgnoreUnused(options, required); |
Derek Lamberti | eb1fce0 | 2019-12-10 21:20:10 +0000 | [diff] [blame] | 99 | }; |
| 100 | virtual bool ProcessCommandLineOptions(const InferenceTestOptions &commonOptions) |
| 101 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 102 | IgnoreUnused(commonOptions); |
Derek Lamberti | eb1fce0 | 2019-12-10 21:20:10 +0000 | [diff] [blame] | 103 | return true; |
| 104 | }; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 105 | virtual std::unique_ptr<IInferenceTestCase> GetTestCase(unsigned int testCaseId) = 0; |
| 106 | virtual bool OnInferenceTestFinished() { return true; }; |
| 107 | }; |
| 108 | |
| 109 | template <typename TModel> |
| 110 | class InferenceModelTestCase : public IInferenceTestCase |
| 111 | { |
| 112 | public: |
James Ward | 6d9f5c5 | 2020-09-28 11:56:35 +0100 | [diff] [blame] | 113 | using TContainer = mapbox::util::variant<std::vector<float>, std::vector<int>, std::vector<unsigned char>>; |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 114 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 115 | InferenceModelTestCase(TModel& model, |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 116 | unsigned int testCaseId, |
| 117 | const std::vector<TContainer>& inputs, |
| 118 | const std::vector<unsigned int>& outputSizes) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 119 | : m_Model(model) |
| 120 | , m_TestCaseId(testCaseId) |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 121 | , m_Inputs(std::move(inputs)) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 122 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 123 | // Initialize output vector |
| 124 | const size_t numOutputs = outputSizes.size(); |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 125 | m_Outputs.reserve(numOutputs); |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 126 | |
| 127 | for (size_t i = 0; i < numOutputs; i++) |
| 128 | { |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 129 | m_Outputs.push_back(std::vector<typename TModel::DataType>(outputSizes[i])); |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 130 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | virtual void Run() override |
| 134 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 135 | m_Model.Run(m_Inputs, m_Outputs); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | protected: |
| 139 | unsigned int GetTestCaseId() const { return m_TestCaseId; } |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 140 | const std::vector<TContainer>& GetOutputs() const { return m_Outputs; } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 141 | |
| 142 | private: |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 143 | TModel& m_Model; |
| 144 | unsigned int m_TestCaseId; |
| 145 | std::vector<TContainer> m_Inputs; |
| 146 | std::vector<TContainer> m_Outputs; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | template <typename TTestCaseDatabase, typename TModel> |
| 150 | class ClassifierTestCase : public InferenceModelTestCase<TModel> |
| 151 | { |
| 152 | public: |
| 153 | ClassifierTestCase(int& numInferencesRef, |
| 154 | int& numCorrectInferencesRef, |
| 155 | const std::vector<unsigned int>& validationPredictions, |
| 156 | std::vector<unsigned int>* validationPredictionsOut, |
| 157 | TModel& model, |
| 158 | unsigned int testCaseId, |
| 159 | unsigned int label, |
| 160 | std::vector<typename TModel::DataType> modelInput); |
| 161 | |
| 162 | virtual TestCaseResult ProcessResult(const InferenceTestOptions& params) override; |
| 163 | |
| 164 | private: |
| 165 | unsigned int m_Label; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 166 | InferenceModelInternal::QuantizationParams m_QuantizationParams; |
| 167 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 168 | /// These fields reference the corresponding member in the ClassifierTestCaseProvider. |
| 169 | /// @{ |
| 170 | int& m_NumInferencesRef; |
| 171 | int& m_NumCorrectInferencesRef; |
| 172 | const std::vector<unsigned int>& m_ValidationPredictions; |
| 173 | std::vector<unsigned int>* m_ValidationPredictionsOut; |
| 174 | /// @} |
| 175 | }; |
| 176 | |
| 177 | template <typename TDatabase, typename InferenceModel> |
| 178 | class ClassifierTestCaseProvider : public IInferenceTestCaseProvider |
| 179 | { |
| 180 | public: |
| 181 | template <typename TConstructDatabaseCallable, typename TConstructModelCallable> |
| 182 | ClassifierTestCaseProvider(TConstructDatabaseCallable constructDatabase, TConstructModelCallable constructModel); |
| 183 | |
James Ward | c89829f | 2020-10-12 14:17:36 +0100 | [diff] [blame] | 184 | virtual void AddCommandLineOptions(cxxopts::Options& options, std::vector<std::string>& required) override; |
Matthew Bentham | 3e68b97 | 2019-04-09 13:10:46 +0100 | [diff] [blame] | 185 | virtual bool ProcessCommandLineOptions(const InferenceTestOptions &commonOptions) override; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 186 | virtual std::unique_ptr<IInferenceTestCase> GetTestCase(unsigned int testCaseId) override; |
| 187 | virtual bool OnInferenceTestFinished() override; |
| 188 | |
| 189 | private: |
| 190 | void ReadPredictions(); |
| 191 | |
| 192 | typename InferenceModel::CommandLineOptions m_ModelCommandLineOptions; |
Matthew Bentham | 3e68b97 | 2019-04-09 13:10:46 +0100 | [diff] [blame] | 193 | std::function<std::unique_ptr<InferenceModel>(const InferenceTestOptions& commonOptions, |
| 194 | typename InferenceModel::CommandLineOptions)> m_ConstructModel; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 195 | std::unique_ptr<InferenceModel> m_Model; |
| 196 | |
| 197 | std::string m_DataDir; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 198 | std::function<TDatabase(const char*, const InferenceModel&)> m_ConstructDatabase; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 199 | std::unique_ptr<TDatabase> m_Database; |
| 200 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 201 | int m_NumInferences; // Referenced by test cases. |
| 202 | int m_NumCorrectInferences; // Referenced by test cases. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 203 | |
| 204 | std::string m_ValidationFileIn; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 205 | std::vector<unsigned int> m_ValidationPredictions; // Referenced by test cases. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 206 | |
| 207 | std::string m_ValidationFileOut; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 208 | std::vector<unsigned int> m_ValidationPredictionsOut; // Referenced by test cases. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 209 | }; |
| 210 | |
| 211 | bool ParseCommandLine(int argc, char** argv, IInferenceTestCaseProvider& testCaseProvider, |
| 212 | InferenceTestOptions& outParams); |
| 213 | |
| 214 | bool ValidateDirectory(std::string& dir); |
| 215 | |
| 216 | bool InferenceTest(const InferenceTestOptions& params, |
| 217 | const std::vector<unsigned int>& defaultTestCaseIds, |
| 218 | IInferenceTestCaseProvider& testCaseProvider); |
| 219 | |
| 220 | template<typename TConstructTestCaseProvider> |
| 221 | int InferenceTestMain(int argc, |
| 222 | char* argv[], |
| 223 | const std::vector<unsigned int>& defaultTestCaseIds, |
| 224 | TConstructTestCaseProvider constructTestCaseProvider); |
| 225 | |
| 226 | template<typename TDatabase, |
| 227 | typename TParser, |
| 228 | typename TConstructDatabaseCallable> |
| 229 | int ClassifierInferenceTestMain(int argc, char* argv[], const char* modelFilename, bool isModelBinary, |
| 230 | const char* inputBindingName, const char* outputBindingName, |
| 231 | const std::vector<unsigned int>& defaultTestCaseIds, |
| 232 | TConstructDatabaseCallable constructDatabase, |
| 233 | const armnn::TensorShape* inputTensorShape = nullptr); |
| 234 | |
| 235 | } // namespace test |
| 236 | } // namespace armnn |
| 237 | |
| 238 | #include "InferenceTest.inl" |