blob: 8fd5c4779c1c600f69b3789c8969ef6369e0a24f [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)
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010043{
44}
45
46DriverOptions::DriverOptions(const std::vector<armnn::BackendId>& backends, bool fp16Enabled)
47 : m_Backends(backends)
telsoa01ce3e84a2018-08-31 09:31:35 +010048 , m_VerboseLogging(false)
49 , m_ClTunedParametersMode(armnn::IGpuAccTunedParameters::Mode::UseTunedParameters)
Ruomei Yan689c6ee2019-04-25 17:48:41 +010050 , m_ClTuningLevel(armnn::IGpuAccTunedParameters::TuningLevel::Rapid)
telsoa01ce3e84a2018-08-31 09:31:35 +010051 , m_EnableGpuProfiling(false)
Nikhil Raj77605822018-09-03 11:25:56 +010052 , m_fp16Enabled(fp16Enabled)
Mike Kelly7ed56dd2020-09-30 20:22:56 +010053 , m_FastMathEnabled(false)
Sadik Armaganf36e10b2021-01-11 16:34:01 +000054 , m_ShouldExit(false)
55 , m_SaveCachedNetwork(false)
Matthew Sloyancd639c92021-02-11 16:57:38 +000056 , m_NumberOfThreads(0)
Finn Williamsd8fb5402021-05-19 20:52:00 +010057 , m_EnableAsyncModelExecution(false)
58 , m_ArmnnNumberOfThreads(1)
telsoa01ce3e84a2018-08-31 09:31:35 +010059{
60}
61
62DriverOptions::DriverOptions(int argc, char** argv)
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010063 : m_VerboseLogging(false)
telsoa01ce3e84a2018-08-31 09:31:35 +010064 , m_ClTunedParametersMode(armnn::IGpuAccTunedParameters::Mode::UseTunedParameters)
Ruomei Yan689c6ee2019-04-25 17:48:41 +010065 , m_ClTuningLevel(armnn::IGpuAccTunedParameters::TuningLevel::Rapid)
telsoa01ce3e84a2018-08-31 09:31:35 +010066 , m_EnableGpuProfiling(false)
67 , m_fp16Enabled(false)
Mike Kelly7ed56dd2020-09-30 20:22:56 +010068 , m_FastMathEnabled(false)
Mike Kelly6df71fd2020-10-13 17:50:05 +010069 , m_ShouldExit(false)
Sadik Armaganf36e10b2021-01-11 16:34:01 +000070 , m_SaveCachedNetwork(false)
Matthew Sloyancd639c92021-02-11 16:57:38 +000071 , m_NumberOfThreads(0)
Finn Williamsd8fb5402021-05-19 20:52:00 +010072 , m_EnableAsyncModelExecution(false)
73 , m_ArmnnNumberOfThreads(1)
telsoa01ce3e84a2018-08-31 09:31:35 +010074{
telsoa01ce3e84a2018-08-31 09:31:35 +010075 std::string unsupportedOperationsAsString;
76 std::string clTunedParametersModeAsString;
Ruomei Yan689c6ee2019-04-25 17:48:41 +010077 std::string clTuningLevelAsString;
Colm Donelan0cc61782020-10-06 21:02:21 +010078 std::vector<std::string> backends;
Mike Kelly6df71fd2020-10-13 17:50:05 +010079 bool showHelp;
80 bool showVersion;
telsoa01ce3e84a2018-08-31 09:31:35 +010081
Mike Kelly6df71fd2020-10-13 17:50:05 +010082 cxxopts::Options optionsDesc(argv[0], "ArmNN Android NN driver for the Android Neural Networks API. The Android NN "
83 "driver will convert Android NNAPI requests and delegate them to available "
84 "ArmNN backends.");
Colm Donelan0cc61782020-10-06 21:02:21 +010085 try
86 {
87 optionsDesc.add_options()
Mike Kelly6df71fd2020-10-13 17:50:05 +010088
89 ("a,enable-fast-math", "Enables fast_math options in backends that support it. Using the fast_math flag can "
90 "lead to performance improvements but may result in reduced or different precision.",
91 cxxopts::value<bool>(m_FastMathEnabled)->default_value("false"))
92
Colm Donelan0cc61782020-10-06 21:02:21 +010093 ("c,compute",
94 "Comma separated list of backends to run layers on. Examples of possible values are: CpuRef, CpuAcc, GpuAcc",
95 cxxopts::value<std::vector<std::string>>(backends))
telsoa01ce3e84a2018-08-31 09:31:35 +010096
Colm Donelan0cc61782020-10-06 21:02:21 +010097 ("d,request-inputs-and-outputs-dump-dir",
98 "If non-empty, the directory where request inputs and outputs should be dumped",
99 cxxopts::value<std::string>(m_RequestInputsAndOutputsDumpDir)->default_value(""))
telsoa01ce3e84a2018-08-31 09:31:35 +0100100
Mike Kelly6df71fd2020-10-13 17:50:05 +0100101 ("f,fp16-enabled", "Enables support for relaxed computation from Float32 to Float16",
102 cxxopts::value<bool>(m_fp16Enabled)->default_value("false"))
Kevin Mayabc95d02020-05-15 15:34:03 +0100103
Mike Kelly6df71fd2020-10-13 17:50:05 +0100104 ("h,help", "Show this help",
105 cxxopts::value<bool>(showHelp)->default_value("false"))
telsoa01ce3e84a2018-08-31 09:31:35 +0100106
Colm Donelan0cc61782020-10-06 21:02:21 +0100107 ("m,cl-tuned-parameters-mode",
telsoa01ce3e84a2018-08-31 09:31:35 +0100108 "If 'UseTunedParameters' (the default), will read CL tuned parameters from the file specified by "
109 "--cl-tuned-parameters-file. "
110 "If 'UpdateTunedParameters', will also find the optimum parameters when preparing new networks and update "
Colm Donelan0cc61782020-10-06 21:02:21 +0100111 "the file accordingly.",
112 cxxopts::value<std::string>(clTunedParametersModeAsString)->default_value("UseTunedParameters"))
telsoa01ce3e84a2018-08-31 09:31:35 +0100113
Finn Williamsf5ca16c2021-02-12 14:26:23 +0000114 ("g,mlgo-cl-tuned-parameters-file",
115 "If non-empty, the given file will be used to load/save MLGO CL tuned parameters. ",
116 cxxopts::value<std::string>(m_ClMLGOTunedParametersFile)->default_value(""))
117
Mike Kelly6df71fd2020-10-13 17:50:05 +0100118 ("n,service-name",
119 "If non-empty, the driver service name to be registered",
120 cxxopts::value<std::string>(m_ServiceName)->default_value("armnn"))
121
Colm Donelan0cc61782020-10-06 21:02:21 +0100122 ("o,cl-tuning-level",
Ruomei Yan689c6ee2019-04-25 17:48:41 +0100123 "exhaustive: all lws values are tested "
124 "normal: reduced number of lws values but enough to still have the performance really close to the "
125 "exhaustive approach "
Colm Donelan0cc61782020-10-06 21:02:21 +0100126 "rapid: only 3 lws values should be tested for each kernel ",
127 cxxopts::value<std::string>(clTuningLevelAsString)->default_value("rapid"))
Ruomei Yan689c6ee2019-04-25 17:48:41 +0100128
Colm Donelan0cc61782020-10-06 21:02:21 +0100129 ("p,gpu-profiling", "Turns GPU profiling on",
130 cxxopts::value<bool>(m_EnableGpuProfiling)->default_value("false"))
telsoa01ce3e84a2018-08-31 09:31:35 +0100131
Sadik Armaganf36e10b2021-01-11 16:34:01 +0000132 ("q,cached-network-file", "If non-empty, the given file will be used to load/save cached network. "
133 "If save-cached-network option is given will save the cached network to given file."
134 "If save-cached-network option is not given will load the cached network from given "
135 "file.",
136 cxxopts::value<std::string>(m_CachedNetworkFilePath)->default_value(""))
137
138 ("s,save-cached-network", "Enables saving the cached network to the file given with cached-network-file option."
139 " See also --cached-network-file",
140 cxxopts::value<bool>(m_SaveCachedNetwork)->default_value("false"))
141
Matthew Sloyancd639c92021-02-11 16:57:38 +0000142 ("number-of-threads",
143 "Assign the number of threads used by the CpuAcc backend. "
144 "Input value must be between 1 and 64. "
145 "Default is set to 0 (Backend will decide number of threads to use).",
146 cxxopts::value<unsigned int>(m_NumberOfThreads)->default_value("0"))
147
Mike Kelly6df71fd2020-10-13 17:50:05 +0100148 ("t,cl-tuned-parameters-file",
149 "If non-empty, the given file will be used to load/save CL tuned parameters. "
150 "See also --cl-tuned-parameters-mode",
151 cxxopts::value<std::string>(m_ClTunedParametersFile)->default_value(""))
152
153 ("u,unsupported-operations",
154 "If non-empty, a comma-separated list of operation indices which the driver will forcibly "
155 "consider unsupported",
156 cxxopts::value<std::string>(unsupportedOperationsAsString)->default_value(""))
157
158 ("v,verbose-logging", "Turns verbose logging on",
159 cxxopts::value<bool>(m_VerboseLogging)->default_value("false"))
160
161 ("V,version", "Show version information",
Finn Williamsd8fb5402021-05-19 20:52:00 +0100162 cxxopts::value<bool>(showVersion)->default_value("false"))
163
164 ("A,asyncModelExecution", "Enable AsynModel Execution",
165 cxxopts::value<bool>(m_EnableAsyncModelExecution)->default_value("false"))
166
167 ("T,armnn-threads",
168 "Assign the number of threads used by ArmNN. "
169 "Input value must be at least 1. "
170 "Default is set to 1.",
171 cxxopts::value<unsigned int>(m_ArmnnNumberOfThreads)->default_value("1"));
Colm Donelan0cc61782020-10-06 21:02:21 +0100172 }
173 catch (const std::exception& e)
174 {
Mike Kelly6df71fd2020-10-13 17:50:05 +0100175 ALOGE("An error occurred attempting to construct options: %s", e.what());
176 std::cout << "An error occurred attempting to construct options: %s" << std::endl;
177 m_ExitCode = EXIT_FAILURE;
178 return;
Colm Donelan0cc61782020-10-06 21:02:21 +0100179 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100180
telsoa01ce3e84a2018-08-31 09:31:35 +0100181 try
182 {
Colm Donelan0cc61782020-10-06 21:02:21 +0100183 cxxopts::ParseResult result = optionsDesc.parse(argc, argv);
telsoa01ce3e84a2018-08-31 09:31:35 +0100184 }
Colm Donelan0cc61782020-10-06 21:02:21 +0100185 catch (const cxxopts::OptionException& e)
telsoa01ce3e84a2018-08-31 09:31:35 +0100186 {
Mike Kelly6df71fd2020-10-13 17:50:05 +0100187 ALOGW("An exception occurred attempting to parse program options: %s", e.what());
188 std::cout << optionsDesc.help() << std::endl
189 << "An exception occurred while parsing program options: " << std::endl
190 << e.what() << std::endl;
Mike Kellyc24bb032020-10-20 15:29:19 +0100191 m_ShouldExit = true;
192 m_ExitCode = EXIT_FAILURE;
193 return;
Mike Kelly6df71fd2020-10-13 17:50:05 +0100194 }
195 if (showHelp)
196 {
197 ALOGW("Showing help and exiting");
198 std::cout << optionsDesc.help() << std::endl;
199 m_ShouldExit = true;
200 m_ExitCode = EXIT_SUCCESS;
201 return;
202 }
203 if (showVersion)
204 {
205 ALOGW("Showing version and exiting");
206 std::cout << "ArmNN Android NN driver for the Android Neural Networks API.\n"
207 "ArmNN v" << ARMNN_VERSION << std::endl;
208 m_ShouldExit = true;
209 m_ExitCode = EXIT_SUCCESS;
210 return;
telsoa01ce3e84a2018-08-31 09:31:35 +0100211 }
212
Colm Donelan0cc61782020-10-06 21:02:21 +0100213 // Convert the string backend names into backendId's.
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100214 m_Backends.reserve(backends.size());
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100215 for (auto&& backend : backends)
telsoa01ce3e84a2018-08-31 09:31:35 +0100216 {
Mike Kelly6df71fd2020-10-13 17:50:05 +0100217 m_Backends.emplace_back(backend);
218 }
219
220 // If no backends have been specified then the default value is GpuAcc.
221 if (backends.empty())
222 {
223 ALOGE("No backends have been specified:");
224 std::cout << optionsDesc.help() << std::endl
225 << "Unable to start:" << std::endl
226 << "No backends have been specified" << std::endl;
227 m_ShouldExit = true;
228 m_ExitCode = EXIT_FAILURE;
229 return;
telsoa01ce3e84a2018-08-31 09:31:35 +0100230 }
231
232 if (!unsupportedOperationsAsString.empty())
233 {
234 std::istringstream argStream(unsupportedOperationsAsString);
235
236 std::string s;
237 while (!argStream.eof())
238 {
239 std::getline(argStream, s, ',');
240 try
241 {
242 unsigned int operationIdx = std::stoi(s);
243 m_ForcedUnsupportedOperations.insert(operationIdx);
244 }
245 catch (const std::invalid_argument&)
246 {
247 ALOGW("Ignoring invalid integer argument in -u/--unsupported-operations value: %s", s.c_str());
248 }
249 }
250 }
251
252 if (!m_ClTunedParametersFile.empty())
253 {
254 // The mode is only relevant if the file path has been provided
255 if (clTunedParametersModeAsString == "UseTunedParameters")
256 {
257 m_ClTunedParametersMode = armnn::IGpuAccTunedParameters::Mode::UseTunedParameters;
258 }
259 else if (clTunedParametersModeAsString == "UpdateTunedParameters")
260 {
261 m_ClTunedParametersMode = armnn::IGpuAccTunedParameters::Mode::UpdateTunedParameters;
262 }
263 else
264 {
265 ALOGW("Requested unknown cl-tuned-parameters-mode '%s'. Defaulting to UseTunedParameters",
266 clTunedParametersModeAsString.c_str());
267 }
Ruomei Yan689c6ee2019-04-25 17:48:41 +0100268
269 if (clTuningLevelAsString == "exhaustive")
270 {
271 m_ClTuningLevel = armnn::IGpuAccTunedParameters::TuningLevel::Exhaustive;
272 }
273 else if (clTuningLevelAsString == "normal")
274 {
275 m_ClTuningLevel = armnn::IGpuAccTunedParameters::TuningLevel::Normal;
276 }
277 else if (clTuningLevelAsString == "rapid")
278 {
279 m_ClTuningLevel = armnn::IGpuAccTunedParameters::TuningLevel::Rapid;
280 }
281 else
282 {
283 ALOGW("Requested unknown cl-tuner-mode '%s'. Defaulting to rapid",
284 clTuningLevelAsString.c_str());
285 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100286 }
287}
288
289} // namespace armnn_driver