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: |
Finn Williams | f806c4d | 2021-02-22 15:13:12 +0000 | [diff] [blame] | 113 | using TContainer = |
| 114 | mapbox::util::variant<std::vector<float>, std::vector<int>, std::vector<unsigned char>, std::vector<int8_t>>; |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 115 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 116 | InferenceModelTestCase(TModel& model, |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 117 | unsigned int testCaseId, |
| 118 | const std::vector<TContainer>& inputs, |
| 119 | const std::vector<unsigned int>& outputSizes) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 120 | : m_Model(model) |
| 121 | , m_TestCaseId(testCaseId) |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 122 | , m_Inputs(std::move(inputs)) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 123 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 124 | // Initialize output vector |
| 125 | const size_t numOutputs = outputSizes.size(); |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 126 | m_Outputs.reserve(numOutputs); |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 127 | |
| 128 | for (size_t i = 0; i < numOutputs; i++) |
| 129 | { |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 130 | m_Outputs.push_back(std::vector<typename TModel::DataType>(outputSizes[i])); |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 131 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | virtual void Run() override |
| 135 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 136 | m_Model.Run(m_Inputs, m_Outputs); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | protected: |
| 140 | unsigned int GetTestCaseId() const { return m_TestCaseId; } |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 141 | const std::vector<TContainer>& GetOutputs() const { return m_Outputs; } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 142 | |
| 143 | private: |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 144 | TModel& m_Model; |
| 145 | unsigned int m_TestCaseId; |
| 146 | std::vector<TContainer> m_Inputs; |
| 147 | std::vector<TContainer> m_Outputs; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | template <typename TTestCaseDatabase, typename TModel> |
| 151 | class ClassifierTestCase : public InferenceModelTestCase<TModel> |
| 152 | { |
| 153 | public: |
| 154 | ClassifierTestCase(int& numInferencesRef, |
| 155 | int& numCorrectInferencesRef, |
| 156 | const std::vector<unsigned int>& validationPredictions, |
| 157 | std::vector<unsigned int>* validationPredictionsOut, |
| 158 | TModel& model, |
| 159 | unsigned int testCaseId, |
| 160 | unsigned int label, |
| 161 | std::vector<typename TModel::DataType> modelInput); |
| 162 | |
| 163 | virtual TestCaseResult ProcessResult(const InferenceTestOptions& params) override; |
| 164 | |
| 165 | private: |
| 166 | unsigned int m_Label; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 167 | InferenceModelInternal::QuantizationParams m_QuantizationParams; |
| 168 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 169 | /// These fields reference the corresponding member in the ClassifierTestCaseProvider. |
| 170 | /// @{ |
| 171 | int& m_NumInferencesRef; |
| 172 | int& m_NumCorrectInferencesRef; |
| 173 | const std::vector<unsigned int>& m_ValidationPredictions; |
| 174 | std::vector<unsigned int>* m_ValidationPredictionsOut; |
| 175 | /// @} |
| 176 | }; |
| 177 | |
| 178 | template <typename TDatabase, typename InferenceModel> |
| 179 | class ClassifierTestCaseProvider : public IInferenceTestCaseProvider |
| 180 | { |
| 181 | public: |
| 182 | template <typename TConstructDatabaseCallable, typename TConstructModelCallable> |
| 183 | ClassifierTestCaseProvider(TConstructDatabaseCallable constructDatabase, TConstructModelCallable constructModel); |
| 184 | |
James Ward | c89829f | 2020-10-12 14:17:36 +0100 | [diff] [blame] | 185 | virtual void AddCommandLineOptions(cxxopts::Options& options, std::vector<std::string>& required) override; |
Matthew Bentham | 3e68b97 | 2019-04-09 13:10:46 +0100 | [diff] [blame] | 186 | virtual bool ProcessCommandLineOptions(const InferenceTestOptions &commonOptions) override; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 187 | virtual std::unique_ptr<IInferenceTestCase> GetTestCase(unsigned int testCaseId) override; |
| 188 | virtual bool OnInferenceTestFinished() override; |
| 189 | |
| 190 | private: |
| 191 | void ReadPredictions(); |
| 192 | |
| 193 | typename InferenceModel::CommandLineOptions m_ModelCommandLineOptions; |
Matthew Bentham | 3e68b97 | 2019-04-09 13:10:46 +0100 | [diff] [blame] | 194 | std::function<std::unique_ptr<InferenceModel>(const InferenceTestOptions& commonOptions, |
| 195 | typename InferenceModel::CommandLineOptions)> m_ConstructModel; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 196 | std::unique_ptr<InferenceModel> m_Model; |
| 197 | |
| 198 | std::string m_DataDir; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 199 | std::function<TDatabase(const char*, const InferenceModel&)> m_ConstructDatabase; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 200 | std::unique_ptr<TDatabase> m_Database; |
| 201 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 202 | int m_NumInferences; // Referenced by test cases. |
| 203 | int m_NumCorrectInferences; // Referenced by test cases. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 204 | |
| 205 | std::string m_ValidationFileIn; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 206 | std::vector<unsigned int> m_ValidationPredictions; // Referenced by test cases. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 207 | |
| 208 | std::string m_ValidationFileOut; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 209 | std::vector<unsigned int> m_ValidationPredictionsOut; // Referenced by test cases. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | bool ParseCommandLine(int argc, char** argv, IInferenceTestCaseProvider& testCaseProvider, |
| 213 | InferenceTestOptions& outParams); |
| 214 | |
| 215 | bool ValidateDirectory(std::string& dir); |
| 216 | |
| 217 | bool InferenceTest(const InferenceTestOptions& params, |
| 218 | const std::vector<unsigned int>& defaultTestCaseIds, |
| 219 | IInferenceTestCaseProvider& testCaseProvider); |
| 220 | |
| 221 | template<typename TConstructTestCaseProvider> |
| 222 | int InferenceTestMain(int argc, |
| 223 | char* argv[], |
| 224 | const std::vector<unsigned int>& defaultTestCaseIds, |
| 225 | TConstructTestCaseProvider constructTestCaseProvider); |
| 226 | |
| 227 | template<typename TDatabase, |
| 228 | typename TParser, |
| 229 | typename TConstructDatabaseCallable> |
| 230 | int ClassifierInferenceTestMain(int argc, char* argv[], const char* modelFilename, bool isModelBinary, |
| 231 | const char* inputBindingName, const char* outputBindingName, |
| 232 | const std::vector<unsigned int>& defaultTestCaseIds, |
| 233 | TConstructDatabaseCallable constructDatabase, |
| 234 | const armnn::TensorShape* inputTensorShape = nullptr); |
| 235 | |
| 236 | } // namespace test |
| 237 | } // namespace armnn |
| 238 | |
| 239 | #include "InferenceTest.inl" |