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 | #include "InferenceTest.hpp" |
| 6 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 7 | #include <armnn/utility/Assert.hpp> |
Rob Hughes | 9542f90 | 2021-07-14 09:48:54 +0100 | [diff] [blame] | 8 | #include <armnnUtils/Filesystem.hpp> |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 9 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 10 | #include "../src/armnn/Profiling.hpp" |
James Ward | c89829f | 2020-10-12 14:17:36 +0100 | [diff] [blame] | 11 | #include <cxxopts/cxxopts.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 12 | |
| 13 | #include <fstream> |
| 14 | #include <iostream> |
| 15 | #include <iomanip> |
| 16 | #include <array> |
| 17 | |
| 18 | using namespace std; |
| 19 | using namespace std::chrono; |
| 20 | using namespace armnn::test; |
| 21 | |
| 22 | namespace armnn |
| 23 | { |
| 24 | namespace test |
| 25 | { |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 26 | /// Parse the command line of an ArmNN (or referencetests) inference test program. |
| 27 | /// \return false if any error occurred during options processing, otherwise true |
| 28 | bool ParseCommandLine(int argc, char** argv, IInferenceTestCaseProvider& testCaseProvider, |
| 29 | InferenceTestOptions& outParams) |
| 30 | { |
James Ward | c89829f | 2020-10-12 14:17:36 +0100 | [diff] [blame] | 31 | cxxopts::Options options("InferenceTest", "Inference iteration parameters"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 32 | |
| 33 | try |
| 34 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 35 | // Adds generic options needed for all inference tests. |
James Ward | c89829f | 2020-10-12 14:17:36 +0100 | [diff] [blame] | 36 | options |
| 37 | .allow_unrecognised_options() |
| 38 | .add_options() |
| 39 | ("h,help", "Display help messages") |
| 40 | ("i,iterations", "Sets the number of inferences to perform. If unset, will only be run once.", |
| 41 | cxxopts::value<unsigned int>(outParams.m_IterationCount)->default_value("0")) |
| 42 | ("inference-times-file", |
| 43 | "If non-empty, each individual inference time will be recorded and output to this file", |
| 44 | cxxopts::value<std::string>(outParams.m_InferenceTimesFile)->default_value("")) |
| 45 | ("e,event-based-profiling", "Enables built in profiler. If unset, defaults to off.", |
| 46 | cxxopts::value<bool>(outParams.m_EnableProfiling)->default_value("0")); |
| 47 | |
| 48 | std::vector<std::string> required; //to be passed as reference to derived inference tests |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 49 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 50 | // Adds options specific to the ITestCaseProvider. |
James Ward | c89829f | 2020-10-12 14:17:36 +0100 | [diff] [blame] | 51 | testCaseProvider.AddCommandLineOptions(options, required); |
| 52 | |
| 53 | auto result = options.parse(argc, argv); |
| 54 | |
| 55 | if (result.count("help")) |
| 56 | { |
| 57 | std::cout << options.help() << std::endl; |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | CheckRequiredOptions(result, required); |
| 62 | |
| 63 | } |
| 64 | catch (const cxxopts::OptionException& e) |
| 65 | { |
| 66 | std::cerr << e.what() << std::endl << options.help() << std::endl; |
| 67 | return false; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 68 | } |
| 69 | catch (const std::exception& e) |
| 70 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 71 | ARMNN_ASSERT_MSG(false, "Caught unexpected exception"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 72 | std::cerr << "Fatal internal error: " << e.what() << std::endl; |
| 73 | return false; |
| 74 | } |
| 75 | |
Matthew Bentham | 3e68b97 | 2019-04-09 13:10:46 +0100 | [diff] [blame] | 76 | if (!testCaseProvider.ProcessCommandLineOptions(outParams)) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 77 | { |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | bool ValidateDirectory(std::string& dir) |
| 85 | { |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 86 | if (dir.empty()) |
| 87 | { |
| 88 | std::cerr << "No directory specified" << std::endl; |
| 89 | return false; |
| 90 | } |
| 91 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 92 | if (dir[dir.length() - 1] != '/') |
| 93 | { |
| 94 | dir += "/"; |
| 95 | } |
| 96 | |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 97 | if (!fs::exists(dir)) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 98 | { |
| 99 | std::cerr << "Given directory " << dir << " does not exist" << std::endl; |
| 100 | return false; |
| 101 | } |
| 102 | |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 103 | if (!fs::is_directory(dir)) |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 104 | { |
| 105 | std::cerr << "Given directory [" << dir << "] is not a directory" << std::endl; |
| 106 | return false; |
| 107 | } |
| 108 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 109 | return true; |
| 110 | } |
| 111 | |
| 112 | bool InferenceTest(const InferenceTestOptions& params, |
| 113 | const std::vector<unsigned int>& defaultTestCaseIds, |
| 114 | IInferenceTestCaseProvider& testCaseProvider) |
| 115 | { |
| 116 | #if !defined (NDEBUG) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 117 | if (params.m_IterationCount > 0) // If just running a few select images then don't bother to warn. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 118 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 119 | ARMNN_LOG(warning) << "Performance test running in DEBUG build - results may be inaccurate."; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 120 | } |
| 121 | #endif |
| 122 | |
| 123 | double totalTime = 0; |
| 124 | unsigned int nbProcessed = 0; |
| 125 | bool success = true; |
| 126 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 127 | // Opens the file to write inference times too, if needed. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 128 | ofstream inferenceTimesFile; |
| 129 | const bool recordInferenceTimes = !params.m_InferenceTimesFile.empty(); |
| 130 | if (recordInferenceTimes) |
| 131 | { |
| 132 | inferenceTimesFile.open(params.m_InferenceTimesFile.c_str(), ios_base::trunc | ios_base::out); |
| 133 | if (!inferenceTimesFile.good()) |
| 134 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 135 | ARMNN_LOG(error) << "Failed to open inference times file for writing: " |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 136 | << params.m_InferenceTimesFile; |
| 137 | return false; |
| 138 | } |
| 139 | } |
| 140 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 141 | // Create a profiler and register it for the current thread. |
Francis Murtagh | 33199c2 | 2021-02-15 10:11:28 +0000 | [diff] [blame] | 142 | std::unique_ptr<IProfiler> profiler = std::make_unique<IProfiler>(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 143 | ProfilerManager::GetInstance().RegisterProfiler(profiler.get()); |
| 144 | |
| 145 | // Enable profiling if requested. |
| 146 | profiler->EnableProfiling(params.m_EnableProfiling); |
| 147 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 148 | // Run a single test case to 'warm-up' the model. The first one can sometimes take up to 10x longer |
| 149 | std::unique_ptr<IInferenceTestCase> warmupTestCase = testCaseProvider.GetTestCase(0); |
| 150 | if (warmupTestCase == nullptr) |
| 151 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 152 | ARMNN_LOG(error) << "Failed to load test case"; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 153 | return false; |
| 154 | } |
| 155 | |
| 156 | try |
| 157 | { |
| 158 | warmupTestCase->Run(); |
| 159 | } |
| 160 | catch (const TestFrameworkException& testError) |
| 161 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 162 | ARMNN_LOG(error) << testError.what(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 163 | return false; |
| 164 | } |
| 165 | |
| 166 | const unsigned int nbTotalToProcess = params.m_IterationCount > 0 ? params.m_IterationCount |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 167 | : static_cast<unsigned int>(defaultTestCaseIds.size()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 168 | |
| 169 | for (; nbProcessed < nbTotalToProcess; nbProcessed++) |
| 170 | { |
| 171 | const unsigned int testCaseId = params.m_IterationCount > 0 ? nbProcessed : defaultTestCaseIds[nbProcessed]; |
| 172 | std::unique_ptr<IInferenceTestCase> testCase = testCaseProvider.GetTestCase(testCaseId); |
| 173 | |
| 174 | if (testCase == nullptr) |
| 175 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 176 | ARMNN_LOG(error) << "Failed to load test case"; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 177 | return false; |
| 178 | } |
| 179 | |
| 180 | time_point<high_resolution_clock> predictStart; |
| 181 | time_point<high_resolution_clock> predictEnd; |
| 182 | |
| 183 | TestCaseResult result = TestCaseResult::Ok; |
| 184 | |
| 185 | try |
| 186 | { |
| 187 | predictStart = high_resolution_clock::now(); |
| 188 | |
| 189 | testCase->Run(); |
| 190 | |
| 191 | predictEnd = high_resolution_clock::now(); |
| 192 | |
| 193 | // duration<double> will convert the time difference into seconds as a double by default. |
| 194 | double timeTakenS = duration<double>(predictEnd - predictStart).count(); |
| 195 | totalTime += timeTakenS; |
| 196 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 197 | // Outputss inference times, if needed. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 198 | if (recordInferenceTimes) |
| 199 | { |
| 200 | inferenceTimesFile << testCaseId << " " << (timeTakenS * 1000.0) << std::endl; |
| 201 | } |
| 202 | |
| 203 | result = testCase->ProcessResult(params); |
| 204 | |
| 205 | } |
| 206 | catch (const TestFrameworkException& testError) |
| 207 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 208 | ARMNN_LOG(error) << testError.what(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 209 | result = TestCaseResult::Abort; |
| 210 | } |
| 211 | |
| 212 | switch (result) |
| 213 | { |
| 214 | case TestCaseResult::Ok: |
| 215 | break; |
| 216 | case TestCaseResult::Abort: |
| 217 | return false; |
| 218 | case TestCaseResult::Failed: |
| 219 | // This test failed so we will fail the entire program eventually, but keep going for now. |
| 220 | success = false; |
| 221 | break; |
| 222 | default: |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 223 | ARMNN_ASSERT_MSG(false, "Unexpected TestCaseResult"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 224 | return false; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | const double averageTimePerTestCaseMs = totalTime / nbProcessed * 1000.0f; |
| 229 | |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 230 | ARMNN_LOG(info) << std::fixed << std::setprecision(3) << |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 231 | "Total time for " << nbProcessed << " test cases: " << totalTime << " seconds"; |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 232 | ARMNN_LOG(info) << std::fixed << std::setprecision(3) << |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 233 | "Average time per test case: " << averageTimePerTestCaseMs << " ms"; |
| 234 | |
Sadik Armagan | 2b7a158 | 2018-09-05 16:33:58 +0100 | [diff] [blame] | 235 | // if profiling is enabled print out the results |
| 236 | if (profiler && profiler->IsProfilingEnabled()) |
| 237 | { |
| 238 | profiler->Print(std::cout); |
| 239 | } |
| 240 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 241 | if (!success) |
| 242 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 243 | ARMNN_LOG(error) << "One or more test cases failed"; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 244 | return false; |
| 245 | } |
| 246 | |
| 247 | return testCaseProvider.OnInferenceTestFinished(); |
| 248 | } |
| 249 | |
| 250 | } // namespace test |
| 251 | |
| 252 | } // namespace armnn |