blob: 0018d975436d6568ccc6ef04cf60a0e7a3b00877 [file] [log] [blame]
telsoa01ce3e84a2018-08-31 09:31:35 +01001//
Mike Kellye2d611e2021-10-14 12:35:58 +01002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
David Beck93e48982018-09-05 13:05:09 +01003// SPDX-License-Identifier: MIT
telsoa01ce3e84a2018-08-31 09:31:35 +01004//
5
6#define LOG_TAG "ArmnnDriver"
7
8#include "DriverOptions.hpp"
9#include "Utils.hpp"
10
Mike Kelly6df71fd2020-10-13 17:50:05 +010011#include <armnn/Version.hpp>
telsoa01ce3e84a2018-08-31 09:31:35 +010012#include <log/log.h>
13#include "SystemPropertiesUtils.hpp"
14
15#include <OperationsUtils.h>
16
Colm Donelan0cc61782020-10-06 21:02:21 +010017#include <cxxopts/cxxopts.hpp>
telsoa01ce3e84a2018-08-31 09:31:35 +010018
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010019#include <algorithm>
telsoa01ce3e84a2018-08-31 09:31:35 +010020#include <functional>
21#include <string>
22#include <sstream>
23
24using namespace android;
25using namespace std;
26
27namespace armnn_driver
28{
29
Nikhil Raj77605822018-09-03 11:25:56 +010030DriverOptions::DriverOptions(armnn::Compute computeDevice, bool fp16Enabled)
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010031 : m_Backends({computeDevice})
32 , m_VerboseLogging(false)
33 , m_ClTunedParametersMode(armnn::IGpuAccTunedParameters::Mode::UseTunedParameters)
Ruomei Yan689c6ee2019-04-25 17:48:41 +010034 , m_ClTuningLevel(armnn::IGpuAccTunedParameters::TuningLevel::Rapid)
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010035 , m_EnableGpuProfiling(false)
36 , m_fp16Enabled(fp16Enabled)
Mike Kelly7ed56dd2020-09-30 20:22:56 +010037 , m_FastMathEnabled(false)
Sadik Armaganf36e10b2021-01-11 16:34:01 +000038 , m_ShouldExit(false)
39 , m_SaveCachedNetwork(false)
Matthew Sloyancd639c92021-02-11 16:57:38 +000040 , m_NumberOfThreads(0)
Finn Williamsd8fb5402021-05-19 20:52:00 +010041 , m_EnableAsyncModelExecution(false)
42 , m_ArmnnNumberOfThreads(1)
Narumol Prangnawarat558a1d42022-02-07 13:12:24 +000043 , m_EnableImport(false)
44 , m_EnableExport(true)
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010045{
46}
47
48DriverOptions::DriverOptions(const std::vector<armnn::BackendId>& backends, bool fp16Enabled)
49 : m_Backends(backends)
telsoa01ce3e84a2018-08-31 09:31:35 +010050 , m_VerboseLogging(false)
51 , m_ClTunedParametersMode(armnn::IGpuAccTunedParameters::Mode::UseTunedParameters)
Ruomei Yan689c6ee2019-04-25 17:48:41 +010052 , m_ClTuningLevel(armnn::IGpuAccTunedParameters::TuningLevel::Rapid)
telsoa01ce3e84a2018-08-31 09:31:35 +010053 , m_EnableGpuProfiling(false)
Nikhil Raj77605822018-09-03 11:25:56 +010054 , m_fp16Enabled(fp16Enabled)
Mike Kelly7ed56dd2020-09-30 20:22:56 +010055 , m_FastMathEnabled(false)
Sadik Armaganf36e10b2021-01-11 16:34:01 +000056 , m_ShouldExit(false)
57 , m_SaveCachedNetwork(false)
Matthew Sloyancd639c92021-02-11 16:57:38 +000058 , m_NumberOfThreads(0)
Finn Williamsd8fb5402021-05-19 20:52:00 +010059 , m_EnableAsyncModelExecution(false)
60 , m_ArmnnNumberOfThreads(1)
Narumol Prangnawarat558a1d42022-02-07 13:12:24 +000061 , m_EnableImport(false)
62 , m_EnableExport(true)
telsoa01ce3e84a2018-08-31 09:31:35 +010063{
64}
65
66DriverOptions::DriverOptions(int argc, char** argv)
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010067 : m_VerboseLogging(false)
telsoa01ce3e84a2018-08-31 09:31:35 +010068 , m_ClTunedParametersMode(armnn::IGpuAccTunedParameters::Mode::UseTunedParameters)
Ruomei Yan689c6ee2019-04-25 17:48:41 +010069 , m_ClTuningLevel(armnn::IGpuAccTunedParameters::TuningLevel::Rapid)
telsoa01ce3e84a2018-08-31 09:31:35 +010070 , m_EnableGpuProfiling(false)
71 , m_fp16Enabled(false)
Mike Kelly7ed56dd2020-09-30 20:22:56 +010072 , m_FastMathEnabled(false)
Mike Kelly6df71fd2020-10-13 17:50:05 +010073 , m_ShouldExit(false)
Sadik Armaganf36e10b2021-01-11 16:34:01 +000074 , m_SaveCachedNetwork(false)
Matthew Sloyancd639c92021-02-11 16:57:38 +000075 , m_NumberOfThreads(0)
Finn Williamsd8fb5402021-05-19 20:52:00 +010076 , m_EnableAsyncModelExecution(false)
77 , m_ArmnnNumberOfThreads(1)
Narumol Prangnawarat558a1d42022-02-07 13:12:24 +000078 , m_EnableImport(false)
79 , m_EnableExport(true)
telsoa01ce3e84a2018-08-31 09:31:35 +010080{
telsoa01ce3e84a2018-08-31 09:31:35 +010081 std::string unsupportedOperationsAsString;
82 std::string clTunedParametersModeAsString;
Ruomei Yan689c6ee2019-04-25 17:48:41 +010083 std::string clTuningLevelAsString;
Colm Donelan0cc61782020-10-06 21:02:21 +010084 std::vector<std::string> backends;
Mike Kelly6df71fd2020-10-13 17:50:05 +010085 bool showHelp;
86 bool showVersion;
telsoa01ce3e84a2018-08-31 09:31:35 +010087
Mike Kelly6df71fd2020-10-13 17:50:05 +010088 cxxopts::Options optionsDesc(argv[0], "ArmNN Android NN driver for the Android Neural Networks API. The Android NN "
89 "driver will convert Android NNAPI requests and delegate them to available "
90 "ArmNN backends.");
Colm Donelan0cc61782020-10-06 21:02:21 +010091 try
92 {
93 optionsDesc.add_options()
Mike Kelly6df71fd2020-10-13 17:50:05 +010094
95 ("a,enable-fast-math", "Enables fast_math options in backends that support it. Using the fast_math flag can "
96 "lead to performance improvements but may result in reduced or different precision.",
97 cxxopts::value<bool>(m_FastMathEnabled)->default_value("false"))
98
Colm Donelan0cc61782020-10-06 21:02:21 +010099 ("c,compute",
100 "Comma separated list of backends to run layers on. Examples of possible values are: CpuRef, CpuAcc, GpuAcc",
101 cxxopts::value<std::vector<std::string>>(backends))
telsoa01ce3e84a2018-08-31 09:31:35 +0100102
Colm Donelan0cc61782020-10-06 21:02:21 +0100103 ("d,request-inputs-and-outputs-dump-dir",
104 "If non-empty, the directory where request inputs and outputs should be dumped",
105 cxxopts::value<std::string>(m_RequestInputsAndOutputsDumpDir)->default_value(""))
telsoa01ce3e84a2018-08-31 09:31:35 +0100106
Mike Kelly6df71fd2020-10-13 17:50:05 +0100107 ("f,fp16-enabled", "Enables support for relaxed computation from Float32 to Float16",
108 cxxopts::value<bool>(m_fp16Enabled)->default_value("false"))
Kevin Mayabc95d02020-05-15 15:34:03 +0100109
Mike Kelly6df71fd2020-10-13 17:50:05 +0100110 ("h,help", "Show this help",
111 cxxopts::value<bool>(showHelp)->default_value("false"))
telsoa01ce3e84a2018-08-31 09:31:35 +0100112
Colm Donelan0cc61782020-10-06 21:02:21 +0100113 ("m,cl-tuned-parameters-mode",
telsoa01ce3e84a2018-08-31 09:31:35 +0100114 "If 'UseTunedParameters' (the default), will read CL tuned parameters from the file specified by "
115 "--cl-tuned-parameters-file. "
116 "If 'UpdateTunedParameters', will also find the optimum parameters when preparing new networks and update "
Colm Donelan0cc61782020-10-06 21:02:21 +0100117 "the file accordingly.",
118 cxxopts::value<std::string>(clTunedParametersModeAsString)->default_value("UseTunedParameters"))
telsoa01ce3e84a2018-08-31 09:31:35 +0100119
Finn Williamsf5ca16c2021-02-12 14:26:23 +0000120 ("g,mlgo-cl-tuned-parameters-file",
121 "If non-empty, the given file will be used to load/save MLGO CL tuned parameters. ",
122 cxxopts::value<std::string>(m_ClMLGOTunedParametersFile)->default_value(""))
123
Mike Kelly6df71fd2020-10-13 17:50:05 +0100124 ("n,service-name",
125 "If non-empty, the driver service name to be registered",
126 cxxopts::value<std::string>(m_ServiceName)->default_value("armnn"))
127
Colm Donelan0cc61782020-10-06 21:02:21 +0100128 ("o,cl-tuning-level",
Ruomei Yan689c6ee2019-04-25 17:48:41 +0100129 "exhaustive: all lws values are tested "
130 "normal: reduced number of lws values but enough to still have the performance really close to the "
131 "exhaustive approach "
Colm Donelan0cc61782020-10-06 21:02:21 +0100132 "rapid: only 3 lws values should be tested for each kernel ",
133 cxxopts::value<std::string>(clTuningLevelAsString)->default_value("rapid"))
Ruomei Yan689c6ee2019-04-25 17:48:41 +0100134
Colm Donelan0cc61782020-10-06 21:02:21 +0100135 ("p,gpu-profiling", "Turns GPU profiling on",
136 cxxopts::value<bool>(m_EnableGpuProfiling)->default_value("false"))
telsoa01ce3e84a2018-08-31 09:31:35 +0100137
Sadik Armaganf36e10b2021-01-11 16:34:01 +0000138 ("q,cached-network-file", "If non-empty, the given file will be used to load/save cached network. "
139 "If save-cached-network option is given will save the cached network to given file."
140 "If save-cached-network option is not given will load the cached network from given "
141 "file.",
142 cxxopts::value<std::string>(m_CachedNetworkFilePath)->default_value(""))
143
144 ("s,save-cached-network", "Enables saving the cached network to the file given with cached-network-file option."
145 " See also --cached-network-file",
146 cxxopts::value<bool>(m_SaveCachedNetwork)->default_value("false"))
147
Matthew Sloyancd639c92021-02-11 16:57:38 +0000148 ("number-of-threads",
149 "Assign the number of threads used by the CpuAcc backend. "
150 "Input value must be between 1 and 64. "
151 "Default is set to 0 (Backend will decide number of threads to use).",
152 cxxopts::value<unsigned int>(m_NumberOfThreads)->default_value("0"))
153
Mike Kelly6df71fd2020-10-13 17:50:05 +0100154 ("t,cl-tuned-parameters-file",
155 "If non-empty, the given file will be used to load/save CL tuned parameters. "
156 "See also --cl-tuned-parameters-mode",
157 cxxopts::value<std::string>(m_ClTunedParametersFile)->default_value(""))
158
159 ("u,unsupported-operations",
160 "If non-empty, a comma-separated list of operation indices which the driver will forcibly "
161 "consider unsupported",
162 cxxopts::value<std::string>(unsupportedOperationsAsString)->default_value(""))
163
164 ("v,verbose-logging", "Turns verbose logging on",
165 cxxopts::value<bool>(m_VerboseLogging)->default_value("false"))
166
167 ("V,version", "Show version information",
Finn Williamsd8fb5402021-05-19 20:52:00 +0100168 cxxopts::value<bool>(showVersion)->default_value("false"))
169
170 ("A,asyncModelExecution", "Enable AsynModel Execution",
171 cxxopts::value<bool>(m_EnableAsyncModelExecution)->default_value("false"))
172
173 ("T,armnn-threads",
174 "Assign the number of threads used by ArmNN. "
175 "Input value must be at least 1. "
176 "Default is set to 1.",
Narumol Prangnawarat558a1d42022-02-07 13:12:24 +0000177 cxxopts::value<unsigned int>(m_ArmnnNumberOfThreads)->default_value("1"))
178
179 ("I,enableImport", "Enable Importing of input buffers",
180 cxxopts::value<bool>(m_EnableImport)->default_value("false"))
181
182 ("E,enableExport", "Enable Exporting of output buffers",
183 cxxopts::value<bool>(m_EnableExport)->default_value("true"));
Colm Donelan0cc61782020-10-06 21:02:21 +0100184 }
185 catch (const std::exception& e)
186 {
Mike Kelly6df71fd2020-10-13 17:50:05 +0100187 ALOGE("An error occurred attempting to construct options: %s", e.what());
188 std::cout << "An error occurred attempting to construct options: %s" << std::endl;
189 m_ExitCode = EXIT_FAILURE;
190 return;
Colm Donelan0cc61782020-10-06 21:02:21 +0100191 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100192
telsoa01ce3e84a2018-08-31 09:31:35 +0100193 try
194 {
Colm Donelan0cc61782020-10-06 21:02:21 +0100195 cxxopts::ParseResult result = optionsDesc.parse(argc, argv);
telsoa01ce3e84a2018-08-31 09:31:35 +0100196 }
Colm Donelan0cc61782020-10-06 21:02:21 +0100197 catch (const cxxopts::OptionException& e)
telsoa01ce3e84a2018-08-31 09:31:35 +0100198 {
Mike Kelly6df71fd2020-10-13 17:50:05 +0100199 ALOGW("An exception occurred attempting to parse program options: %s", e.what());
200 std::cout << optionsDesc.help() << std::endl
201 << "An exception occurred while parsing program options: " << std::endl
202 << e.what() << std::endl;
Mike Kellyc24bb032020-10-20 15:29:19 +0100203 m_ShouldExit = true;
204 m_ExitCode = EXIT_FAILURE;
205 return;
Mike Kelly6df71fd2020-10-13 17:50:05 +0100206 }
207 if (showHelp)
208 {
209 ALOGW("Showing help and exiting");
210 std::cout << optionsDesc.help() << std::endl;
211 m_ShouldExit = true;
212 m_ExitCode = EXIT_SUCCESS;
213 return;
214 }
215 if (showVersion)
216 {
217 ALOGW("Showing version and exiting");
218 std::cout << "ArmNN Android NN driver for the Android Neural Networks API.\n"
219 "ArmNN v" << ARMNN_VERSION << std::endl;
220 m_ShouldExit = true;
221 m_ExitCode = EXIT_SUCCESS;
222 return;
telsoa01ce3e84a2018-08-31 09:31:35 +0100223 }
224
Colm Donelan0cc61782020-10-06 21:02:21 +0100225 // Convert the string backend names into backendId's.
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100226 m_Backends.reserve(backends.size());
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100227 for (auto&& backend : backends)
telsoa01ce3e84a2018-08-31 09:31:35 +0100228 {
Mike Kelly6df71fd2020-10-13 17:50:05 +0100229 m_Backends.emplace_back(backend);
230 }
231
232 // If no backends have been specified then the default value is GpuAcc.
233 if (backends.empty())
234 {
235 ALOGE("No backends have been specified:");
236 std::cout << optionsDesc.help() << std::endl
237 << "Unable to start:" << std::endl
238 << "No backends have been specified" << std::endl;
239 m_ShouldExit = true;
240 m_ExitCode = EXIT_FAILURE;
241 return;
telsoa01ce3e84a2018-08-31 09:31:35 +0100242 }
243
244 if (!unsupportedOperationsAsString.empty())
245 {
246 std::istringstream argStream(unsupportedOperationsAsString);
247
248 std::string s;
249 while (!argStream.eof())
250 {
251 std::getline(argStream, s, ',');
252 try
253 {
254 unsigned int operationIdx = std::stoi(s);
255 m_ForcedUnsupportedOperations.insert(operationIdx);
256 }
257 catch (const std::invalid_argument&)
258 {
259 ALOGW("Ignoring invalid integer argument in -u/--unsupported-operations value: %s", s.c_str());
260 }
261 }
262 }
263
264 if (!m_ClTunedParametersFile.empty())
265 {
266 // The mode is only relevant if the file path has been provided
267 if (clTunedParametersModeAsString == "UseTunedParameters")
268 {
269 m_ClTunedParametersMode = armnn::IGpuAccTunedParameters::Mode::UseTunedParameters;
270 }
271 else if (clTunedParametersModeAsString == "UpdateTunedParameters")
272 {
273 m_ClTunedParametersMode = armnn::IGpuAccTunedParameters::Mode::UpdateTunedParameters;
274 }
275 else
276 {
277 ALOGW("Requested unknown cl-tuned-parameters-mode '%s'. Defaulting to UseTunedParameters",
278 clTunedParametersModeAsString.c_str());
279 }
Ruomei Yan689c6ee2019-04-25 17:48:41 +0100280
281 if (clTuningLevelAsString == "exhaustive")
282 {
283 m_ClTuningLevel = armnn::IGpuAccTunedParameters::TuningLevel::Exhaustive;
284 }
285 else if (clTuningLevelAsString == "normal")
286 {
287 m_ClTuningLevel = armnn::IGpuAccTunedParameters::TuningLevel::Normal;
288 }
289 else if (clTuningLevelAsString == "rapid")
290 {
291 m_ClTuningLevel = armnn::IGpuAccTunedParameters::TuningLevel::Rapid;
292 }
293 else
294 {
295 ALOGW("Requested unknown cl-tuner-mode '%s'. Defaulting to rapid",
296 clTuningLevelAsString.c_str());
297 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100298 }
299}
300
301} // namespace armnn_driver