2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
5 #include "InferenceTest.hpp"
7 #include <boost/algorithm/string.hpp>
8 #include <boost/numeric/conversion/cast.hpp>
9 #include <boost/log/trivial.hpp>
10 #include <boost/filesystem/path.hpp>
11 #include <boost/assert.hpp>
12 #include <boost/format.hpp>
13 #include <boost/program_options.hpp>
14 #include <boost/filesystem/operations.hpp>
22 using namespace std::chrono;
23 using namespace armnn::test;
30 /// Parse the command line of an ArmNN (or referencetests) inference test program.
31 /// \return false if any error occurred during options processing, otherwise true
32 bool ParseCommandLine(int argc, char** argv, IInferenceTestCaseProvider& testCaseProvider,
33 InferenceTestOptions& outParams)
35 namespace po = boost::program_options;
37 std::string computeDeviceStr;
39 po::options_description desc("Options");
43 // Add generic options needed for all inference tests
45 ("help", "Display help messages")
46 ("iterations,i", po::value<unsigned int>(&outParams.m_IterationCount)->default_value(0),
47 "Sets the number number of inferences to perform. If unset, a default number will be ran.")
48 ("inference-times-file", po::value<std::string>(&outParams.m_InferenceTimesFile)->default_value(""),
49 "If non-empty, each individual inference time will be recorded and output to this file");
51 // Add options specific to the ITestCaseProvider
52 testCaseProvider.AddCommandLineOptions(desc);
54 catch (const std::exception& e)
56 // Coverity points out that default_value(...) can throw a bad_lexical_cast,
57 // and that desc.add_options() can throw boost::io::too_few_args.
58 // They really won't in any of these cases.
59 BOOST_ASSERT_MSG(false, "Caught unexpected exception");
60 std::cerr << "Fatal internal error: " << e.what() << std::endl;
68 po::store(po::parse_command_line(argc, argv, desc), vm);
72 std::cout << desc << std::endl;
80 std::cerr << e.what() << std::endl << std::endl;
81 std::cerr << desc << std::endl;
85 if (!testCaseProvider.ProcessCommandLineOptions())
93 bool ValidateDirectory(std::string& dir)
95 if (dir[dir.length() - 1] != '/')
100 if (!boost::filesystem::exists(dir))
102 std::cerr << "Given directory " << dir << " does not exist" << std::endl;
109 bool InferenceTest(const InferenceTestOptions& params,
110 const std::vector<unsigned int>& defaultTestCaseIds,
111 IInferenceTestCaseProvider& testCaseProvider)
113 #if !defined (NDEBUG)
114 if (params.m_IterationCount > 0) // If just running a few select images then don't bother to warn
116 BOOST_LOG_TRIVIAL(warning) << "Performance test running in DEBUG build - results may be inaccurate.";
120 double totalTime = 0;
121 unsigned int nbProcessed = 0;
124 // Open the file to write inference times to, if needed
125 ofstream inferenceTimesFile;
126 const bool recordInferenceTimes = !params.m_InferenceTimesFile.empty();
127 if (recordInferenceTimes)
129 inferenceTimesFile.open(params.m_InferenceTimesFile.c_str(), ios_base::trunc | ios_base::out);
130 if (!inferenceTimesFile.good())
132 BOOST_LOG_TRIVIAL(error) << "Failed to open inference times file for writing: "
133 << params.m_InferenceTimesFile;
138 // Run a single test case to 'warm-up' the model. The first one can sometimes take up to 10x longer
139 std::unique_ptr<IInferenceTestCase> warmupTestCase = testCaseProvider.GetTestCase(0);
140 if (warmupTestCase == nullptr)
142 BOOST_LOG_TRIVIAL(error) << "Failed to load test case";
148 warmupTestCase->Run();
150 catch (const TestFrameworkException& testError)
152 BOOST_LOG_TRIVIAL(error) << testError.what();
156 const unsigned int nbTotalToProcess = params.m_IterationCount > 0 ? params.m_IterationCount
157 : boost::numeric_cast<unsigned int>(defaultTestCaseIds.size());
159 for (; nbProcessed < nbTotalToProcess; nbProcessed++)
161 const unsigned int testCaseId = params.m_IterationCount > 0 ? nbProcessed : defaultTestCaseIds[nbProcessed];
162 std::unique_ptr<IInferenceTestCase> testCase = testCaseProvider.GetTestCase(testCaseId);
164 if (testCase == nullptr)
166 BOOST_LOG_TRIVIAL(error) << "Failed to load test case";
170 time_point<high_resolution_clock> predictStart;
171 time_point<high_resolution_clock> predictEnd;
173 TestCaseResult result = TestCaseResult::Ok;
177 predictStart = high_resolution_clock::now();
181 predictEnd = high_resolution_clock::now();
183 // duration<double> will convert the time difference into seconds as a double by default.
184 double timeTakenS = duration<double>(predictEnd - predictStart).count();
185 totalTime += timeTakenS;
187 // Output inference times if needed
188 if (recordInferenceTimes)
190 inferenceTimesFile << testCaseId << " " << (timeTakenS * 1000.0) << std::endl;
193 result = testCase->ProcessResult(params);
196 catch (const TestFrameworkException& testError)
198 BOOST_LOG_TRIVIAL(error) << testError.what();
199 result = TestCaseResult::Abort;
204 case TestCaseResult::Ok:
206 case TestCaseResult::Abort:
208 case TestCaseResult::Failed:
209 // This test failed so we will fail the entire program eventually, but keep going for now.
213 BOOST_ASSERT_MSG(false, "Unexpected TestCaseResult");
218 const double averageTimePerTestCaseMs = totalTime / nbProcessed * 1000.0f;
220 BOOST_LOG_TRIVIAL(info) << std::fixed << std::setprecision(3) <<
221 "Total time for " << nbProcessed << " test cases: " << totalTime << " seconds";
222 BOOST_LOG_TRIVIAL(info) << std::fixed << std::setprecision(3) <<
223 "Average time per test case: " << averageTimePerTestCaseMs << " ms";
227 BOOST_LOG_TRIVIAL(error) << "One or more test cases failed";
231 return testCaseProvider.OnInferenceTestFinished();