blob: 5f2cf91a45de8cd3f5402c8b39adf1a92a72d9f2 [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
telsoa014fcda012018-03-09 14:13:49 +00002// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5#include "InferenceTest.hpp"
6
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01007#include <armnn/utility/Assert.hpp>
Rob Hughes9542f902021-07-14 09:48:54 +01008#include <armnnUtils/Filesystem.hpp>
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01009
telsoa01c577f2c2018-08-31 09:22:23 +010010#include "../src/armnn/Profiling.hpp"
James Wardc89829f2020-10-12 14:17:36 +010011#include <cxxopts/cxxopts.hpp>
telsoa014fcda012018-03-09 14:13:49 +000012
13#include <fstream>
14#include <iostream>
15#include <iomanip>
16#include <array>
17
18using namespace std;
19using namespace std::chrono;
20using namespace armnn::test;
21
22namespace armnn
23{
24namespace test
25{
telsoa014fcda012018-03-09 14:13:49 +000026/// Parse the command line of an ArmNN (or referencetests) inference test program.
27/// \return false if any error occurred during options processing, otherwise true
28bool ParseCommandLine(int argc, char** argv, IInferenceTestCaseProvider& testCaseProvider,
29 InferenceTestOptions& outParams)
30{
James Wardc89829f2020-10-12 14:17:36 +010031 cxxopts::Options options("InferenceTest", "Inference iteration parameters");
telsoa014fcda012018-03-09 14:13:49 +000032
33 try
34 {
telsoa01c577f2c2018-08-31 09:22:23 +010035 // Adds generic options needed for all inference tests.
James Wardc89829f2020-10-12 14:17:36 +010036 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
telsoa014fcda012018-03-09 14:13:49 +000049
telsoa01c577f2c2018-08-31 09:22:23 +010050 // Adds options specific to the ITestCaseProvider.
James Wardc89829f2020-10-12 14:17:36 +010051 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;
telsoa014fcda012018-03-09 14:13:49 +000068 }
69 catch (const std::exception& e)
70 {
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010071 ARMNN_ASSERT_MSG(false, "Caught unexpected exception");
telsoa014fcda012018-03-09 14:13:49 +000072 std::cerr << "Fatal internal error: " << e.what() << std::endl;
73 return false;
74 }
75
Matthew Bentham3e68b972019-04-09 13:10:46 +010076 if (!testCaseProvider.ProcessCommandLineOptions(outParams))
telsoa014fcda012018-03-09 14:13:49 +000077 {
78 return false;
79 }
80
81 return true;
82}
83
84bool ValidateDirectory(std::string& dir)
85{
Éanna Ó Catháina4247d52019-05-08 14:00:45 +010086 if (dir.empty())
87 {
88 std::cerr << "No directory specified" << std::endl;
89 return false;
90 }
91
telsoa014fcda012018-03-09 14:13:49 +000092 if (dir[dir.length() - 1] != '/')
93 {
94 dir += "/";
95 }
96
Francis Murtagh532a29d2020-06-29 11:50:01 +010097 if (!fs::exists(dir))
telsoa014fcda012018-03-09 14:13:49 +000098 {
99 std::cerr << "Given directory " << dir << " does not exist" << std::endl;
100 return false;
101 }
102
Francis Murtagh532a29d2020-06-29 11:50:01 +0100103 if (!fs::is_directory(dir))
Éanna Ó Catháina4247d52019-05-08 14:00:45 +0100104 {
105 std::cerr << "Given directory [" << dir << "] is not a directory" << std::endl;
106 return false;
107 }
108
telsoa014fcda012018-03-09 14:13:49 +0000109 return true;
110}
111
112bool InferenceTest(const InferenceTestOptions& params,
113 const std::vector<unsigned int>& defaultTestCaseIds,
114 IInferenceTestCaseProvider& testCaseProvider)
115{
116#if !defined (NDEBUG)
telsoa01c577f2c2018-08-31 09:22:23 +0100117 if (params.m_IterationCount > 0) // If just running a few select images then don't bother to warn.
telsoa014fcda012018-03-09 14:13:49 +0000118 {
Derek Lamberti08446972019-11-26 16:38:31 +0000119 ARMNN_LOG(warning) << "Performance test running in DEBUG build - results may be inaccurate.";
telsoa014fcda012018-03-09 14:13:49 +0000120 }
121#endif
122
123 double totalTime = 0;
124 unsigned int nbProcessed = 0;
125 bool success = true;
126
telsoa01c577f2c2018-08-31 09:22:23 +0100127 // Opens the file to write inference times too, if needed.
telsoa014fcda012018-03-09 14:13:49 +0000128 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 Lamberti08446972019-11-26 16:38:31 +0000135 ARMNN_LOG(error) << "Failed to open inference times file for writing: "
telsoa014fcda012018-03-09 14:13:49 +0000136 << params.m_InferenceTimesFile;
137 return false;
138 }
139 }
140
telsoa01c577f2c2018-08-31 09:22:23 +0100141 // Create a profiler and register it for the current thread.
Francis Murtagh33199c22021-02-15 10:11:28 +0000142 std::unique_ptr<IProfiler> profiler = std::make_unique<IProfiler>();
telsoa01c577f2c2018-08-31 09:22:23 +0100143 ProfilerManager::GetInstance().RegisterProfiler(profiler.get());
144
145 // Enable profiling if requested.
146 profiler->EnableProfiling(params.m_EnableProfiling);
147
telsoa014fcda012018-03-09 14:13:49 +0000148 // 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 Lamberti08446972019-11-26 16:38:31 +0000152 ARMNN_LOG(error) << "Failed to load test case";
telsoa014fcda012018-03-09 14:13:49 +0000153 return false;
154 }
155
156 try
157 {
158 warmupTestCase->Run();
159 }
160 catch (const TestFrameworkException& testError)
161 {
Derek Lamberti08446972019-11-26 16:38:31 +0000162 ARMNN_LOG(error) << testError.what();
telsoa014fcda012018-03-09 14:13:49 +0000163 return false;
164 }
165
166 const unsigned int nbTotalToProcess = params.m_IterationCount > 0 ? params.m_IterationCount
surmeh013537c2c2018-05-18 16:31:43 +0100167 : static_cast<unsigned int>(defaultTestCaseIds.size());
telsoa014fcda012018-03-09 14:13:49 +0000168
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 Lamberti08446972019-11-26 16:38:31 +0000176 ARMNN_LOG(error) << "Failed to load test case";
telsoa014fcda012018-03-09 14:13:49 +0000177 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
telsoa01c577f2c2018-08-31 09:22:23 +0100197 // Outputss inference times, if needed.
telsoa014fcda012018-03-09 14:13:49 +0000198 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 Lamberti08446972019-11-26 16:38:31 +0000208 ARMNN_LOG(error) << testError.what();
telsoa014fcda012018-03-09 14:13:49 +0000209 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 Prangnawaratac2770a2020-04-01 16:51:23 +0100223 ARMNN_ASSERT_MSG(false, "Unexpected TestCaseResult");
telsoa014fcda012018-03-09 14:13:49 +0000224 return false;
225 }
226 }
227
228 const double averageTimePerTestCaseMs = totalTime / nbProcessed * 1000.0f;
229
Derek Lamberti08446972019-11-26 16:38:31 +0000230 ARMNN_LOG(info) << std::fixed << std::setprecision(3) <<
telsoa014fcda012018-03-09 14:13:49 +0000231 "Total time for " << nbProcessed << " test cases: " << totalTime << " seconds";
Derek Lamberti08446972019-11-26 16:38:31 +0000232 ARMNN_LOG(info) << std::fixed << std::setprecision(3) <<
telsoa014fcda012018-03-09 14:13:49 +0000233 "Average time per test case: " << averageTimePerTestCaseMs << " ms";
234
Sadik Armagan2b7a1582018-09-05 16:33:58 +0100235 // if profiling is enabled print out the results
236 if (profiler && profiler->IsProfilingEnabled())
237 {
238 profiler->Print(std::cout);
239 }
240
telsoa014fcda012018-03-09 14:13:49 +0000241 if (!success)
242 {
Derek Lamberti08446972019-11-26 16:38:31 +0000243 ARMNN_LOG(error) << "One or more test cases failed";
telsoa014fcda012018-03-09 14:13:49 +0000244 return false;
245 }
246
247 return testCaseProvider.OnInferenceTestFinished();
248}
249
250} // namespace test
251
252} // namespace armnn