blob: 5b67aa36666531f43d801b94529ba4a600971cd2 [file] [log] [blame]
telsoa01ce3e84a2018-08-31 09:31:35 +01001//
2// Copyright © 2017 Arm Ltd. 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 <cassert>
21#include <functional>
22#include <string>
23#include <sstream>
24
25using namespace android;
26using namespace std;
27
28namespace armnn_driver
29{
30
Nikhil Raj77605822018-09-03 11:25:56 +010031DriverOptions::DriverOptions(armnn::Compute computeDevice, bool fp16Enabled)
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010032 : m_Backends({computeDevice})
33 , m_VerboseLogging(false)
34 , m_ClTunedParametersMode(armnn::IGpuAccTunedParameters::Mode::UseTunedParameters)
Ruomei Yan689c6ee2019-04-25 17:48:41 +010035 , m_ClTuningLevel(armnn::IGpuAccTunedParameters::TuningLevel::Rapid)
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010036 , m_EnableGpuProfiling(false)
37 , m_fp16Enabled(fp16Enabled)
Mike Kelly7ed56dd2020-09-30 20:22:56 +010038 , m_FastMathEnabled(false)
Sadik Armaganf36e10b2021-01-11 16:34:01 +000039 , m_ShouldExit(false)
40 , m_SaveCachedNetwork(false)
Matthew Sloyancd639c92021-02-11 16:57:38 +000041 , m_NumberOfThreads(0)
Finn Williamsd8fb5402021-05-19 20:52:00 +010042 , m_EnableAsyncModelExecution(false)
43 , m_ArmnnNumberOfThreads(1)
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010044{
45}
46
47DriverOptions::DriverOptions(const std::vector<armnn::BackendId>& backends, bool fp16Enabled)
48 : m_Backends(backends)
telsoa01ce3e84a2018-08-31 09:31:35 +010049 , m_VerboseLogging(false)
50 , m_ClTunedParametersMode(armnn::IGpuAccTunedParameters::Mode::UseTunedParameters)
Ruomei Yan689c6ee2019-04-25 17:48:41 +010051 , m_ClTuningLevel(armnn::IGpuAccTunedParameters::TuningLevel::Rapid)
telsoa01ce3e84a2018-08-31 09:31:35 +010052 , m_EnableGpuProfiling(false)
Nikhil Raj77605822018-09-03 11:25:56 +010053 , m_fp16Enabled(fp16Enabled)
Mike Kelly7ed56dd2020-09-30 20:22:56 +010054 , m_FastMathEnabled(false)
Sadik Armaganf36e10b2021-01-11 16:34:01 +000055 , m_ShouldExit(false)
56 , m_SaveCachedNetwork(false)
Matthew Sloyancd639c92021-02-11 16:57:38 +000057 , m_NumberOfThreads(0)
Finn Williamsd8fb5402021-05-19 20:52:00 +010058 , m_EnableAsyncModelExecution(false)
59 , m_ArmnnNumberOfThreads(1)
telsoa01ce3e84a2018-08-31 09:31:35 +010060{
61}
62
63DriverOptions::DriverOptions(int argc, char** argv)
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010064 : m_VerboseLogging(false)
telsoa01ce3e84a2018-08-31 09:31:35 +010065 , m_ClTunedParametersMode(armnn::IGpuAccTunedParameters::Mode::UseTunedParameters)
Ruomei Yan689c6ee2019-04-25 17:48:41 +010066 , m_ClTuningLevel(armnn::IGpuAccTunedParameters::TuningLevel::Rapid)
telsoa01ce3e84a2018-08-31 09:31:35 +010067 , m_EnableGpuProfiling(false)
68 , m_fp16Enabled(false)
Mike Kelly7ed56dd2020-09-30 20:22:56 +010069 , m_FastMathEnabled(false)
Mike Kelly6df71fd2020-10-13 17:50:05 +010070 , m_ShouldExit(false)
Sadik Armaganf36e10b2021-01-11 16:34:01 +000071 , m_SaveCachedNetwork(false)
Matthew Sloyancd639c92021-02-11 16:57:38 +000072 , m_NumberOfThreads(0)
Finn Williamsd8fb5402021-05-19 20:52:00 +010073 , m_EnableAsyncModelExecution(false)
74 , m_ArmnnNumberOfThreads(1)
telsoa01ce3e84a2018-08-31 09:31:35 +010075{
telsoa01ce3e84a2018-08-31 09:31:35 +010076 std::string unsupportedOperationsAsString;
77 std::string clTunedParametersModeAsString;
Ruomei Yan689c6ee2019-04-25 17:48:41 +010078 std::string clTuningLevelAsString;
Colm Donelan0cc61782020-10-06 21:02:21 +010079 std::vector<std::string> backends;
Mike Kelly6df71fd2020-10-13 17:50:05 +010080 bool showHelp;
81 bool showVersion;
telsoa01ce3e84a2018-08-31 09:31:35 +010082
Mike Kelly6df71fd2020-10-13 17:50:05 +010083 cxxopts::Options optionsDesc(argv[0], "ArmNN Android NN driver for the Android Neural Networks API. The Android NN "
84 "driver will convert Android NNAPI requests and delegate them to available "
85 "ArmNN backends.");
Colm Donelan0cc61782020-10-06 21:02:21 +010086 try
87 {
88 optionsDesc.add_options()
Mike Kelly6df71fd2020-10-13 17:50:05 +010089
90 ("a,enable-fast-math", "Enables fast_math options in backends that support it. Using the fast_math flag can "
91 "lead to performance improvements but may result in reduced or different precision.",
92 cxxopts::value<bool>(m_FastMathEnabled)->default_value("false"))
93
Colm Donelan0cc61782020-10-06 21:02:21 +010094 ("c,compute",
95 "Comma separated list of backends to run layers on. Examples of possible values are: CpuRef, CpuAcc, GpuAcc",
96 cxxopts::value<std::vector<std::string>>(backends))
telsoa01ce3e84a2018-08-31 09:31:35 +010097
Colm Donelan0cc61782020-10-06 21:02:21 +010098 ("d,request-inputs-and-outputs-dump-dir",
99 "If non-empty, the directory where request inputs and outputs should be dumped",
100 cxxopts::value<std::string>(m_RequestInputsAndOutputsDumpDir)->default_value(""))
telsoa01ce3e84a2018-08-31 09:31:35 +0100101
Mike Kelly6df71fd2020-10-13 17:50:05 +0100102 ("f,fp16-enabled", "Enables support for relaxed computation from Float32 to Float16",
103 cxxopts::value<bool>(m_fp16Enabled)->default_value("false"))
Kevin Mayabc95d02020-05-15 15:34:03 +0100104
Mike Kelly6df71fd2020-10-13 17:50:05 +0100105 ("h,help", "Show this help",
106 cxxopts::value<bool>(showHelp)->default_value("false"))
telsoa01ce3e84a2018-08-31 09:31:35 +0100107
Colm Donelan0cc61782020-10-06 21:02:21 +0100108 ("m,cl-tuned-parameters-mode",
telsoa01ce3e84a2018-08-31 09:31:35 +0100109 "If 'UseTunedParameters' (the default), will read CL tuned parameters from the file specified by "
110 "--cl-tuned-parameters-file. "
111 "If 'UpdateTunedParameters', will also find the optimum parameters when preparing new networks and update "
Colm Donelan0cc61782020-10-06 21:02:21 +0100112 "the file accordingly.",
113 cxxopts::value<std::string>(clTunedParametersModeAsString)->default_value("UseTunedParameters"))
telsoa01ce3e84a2018-08-31 09:31:35 +0100114
Finn Williamsf5ca16c2021-02-12 14:26:23 +0000115 ("g,mlgo-cl-tuned-parameters-file",
116 "If non-empty, the given file will be used to load/save MLGO CL tuned parameters. ",
117 cxxopts::value<std::string>(m_ClMLGOTunedParametersFile)->default_value(""))
118
Mike Kelly6df71fd2020-10-13 17:50:05 +0100119 ("n,service-name",
120 "If non-empty, the driver service name to be registered",
121 cxxopts::value<std::string>(m_ServiceName)->default_value("armnn"))
122
Colm Donelan0cc61782020-10-06 21:02:21 +0100123 ("o,cl-tuning-level",
Ruomei Yan689c6ee2019-04-25 17:48:41 +0100124 "exhaustive: all lws values are tested "
125 "normal: reduced number of lws values but enough to still have the performance really close to the "
126 "exhaustive approach "
Colm Donelan0cc61782020-10-06 21:02:21 +0100127 "rapid: only 3 lws values should be tested for each kernel ",
128 cxxopts::value<std::string>(clTuningLevelAsString)->default_value("rapid"))
Ruomei Yan689c6ee2019-04-25 17:48:41 +0100129
Colm Donelan0cc61782020-10-06 21:02:21 +0100130 ("p,gpu-profiling", "Turns GPU profiling on",
131 cxxopts::value<bool>(m_EnableGpuProfiling)->default_value("false"))
telsoa01ce3e84a2018-08-31 09:31:35 +0100132
Sadik Armaganf36e10b2021-01-11 16:34:01 +0000133 ("q,cached-network-file", "If non-empty, the given file will be used to load/save cached network. "
134 "If save-cached-network option is given will save the cached network to given file."
135 "If save-cached-network option is not given will load the cached network from given "
136 "file.",
137 cxxopts::value<std::string>(m_CachedNetworkFilePath)->default_value(""))
138
139 ("s,save-cached-network", "Enables saving the cached network to the file given with cached-network-file option."
140 " See also --cached-network-file",
141 cxxopts::value<bool>(m_SaveCachedNetwork)->default_value("false"))
142
Matthew Sloyancd639c92021-02-11 16:57:38 +0000143 ("number-of-threads",
144 "Assign the number of threads used by the CpuAcc backend. "
145 "Input value must be between 1 and 64. "
146 "Default is set to 0 (Backend will decide number of threads to use).",
147 cxxopts::value<unsigned int>(m_NumberOfThreads)->default_value("0"))
148
Mike Kelly6df71fd2020-10-13 17:50:05 +0100149 ("t,cl-tuned-parameters-file",
150 "If non-empty, the given file will be used to load/save CL tuned parameters. "
151 "See also --cl-tuned-parameters-mode",
152 cxxopts::value<std::string>(m_ClTunedParametersFile)->default_value(""))
153
154 ("u,unsupported-operations",
155 "If non-empty, a comma-separated list of operation indices which the driver will forcibly "
156 "consider unsupported",
157 cxxopts::value<std::string>(unsupportedOperationsAsString)->default_value(""))
158
159 ("v,verbose-logging", "Turns verbose logging on",
160 cxxopts::value<bool>(m_VerboseLogging)->default_value("false"))
161
162 ("V,version", "Show version information",
Finn Williamsd8fb5402021-05-19 20:52:00 +0100163 cxxopts::value<bool>(showVersion)->default_value("false"))
164
165 ("A,asyncModelExecution", "Enable AsynModel Execution",
166 cxxopts::value<bool>(m_EnableAsyncModelExecution)->default_value("false"))
167
168 ("T,armnn-threads",
169 "Assign the number of threads used by ArmNN. "
170 "Input value must be at least 1. "
171 "Default is set to 1.",
172 cxxopts::value<unsigned int>(m_ArmnnNumberOfThreads)->default_value("1"));
Colm Donelan0cc61782020-10-06 21:02:21 +0100173 }
174 catch (const std::exception& e)
175 {
Mike Kelly6df71fd2020-10-13 17:50:05 +0100176 ALOGE("An error occurred attempting to construct options: %s", e.what());
177 std::cout << "An error occurred attempting to construct options: %s" << std::endl;
178 m_ExitCode = EXIT_FAILURE;
179 return;
Colm Donelan0cc61782020-10-06 21:02:21 +0100180 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100181
telsoa01ce3e84a2018-08-31 09:31:35 +0100182 try
183 {
Colm Donelan0cc61782020-10-06 21:02:21 +0100184 cxxopts::ParseResult result = optionsDesc.parse(argc, argv);
telsoa01ce3e84a2018-08-31 09:31:35 +0100185 }
Colm Donelan0cc61782020-10-06 21:02:21 +0100186 catch (const cxxopts::OptionException& e)
telsoa01ce3e84a2018-08-31 09:31:35 +0100187 {
Mike Kelly6df71fd2020-10-13 17:50:05 +0100188 ALOGW("An exception occurred attempting to parse program options: %s", e.what());
189 std::cout << optionsDesc.help() << std::endl
190 << "An exception occurred while parsing program options: " << std::endl
191 << e.what() << std::endl;
Mike Kellyc24bb032020-10-20 15:29:19 +0100192 m_ShouldExit = true;
193 m_ExitCode = EXIT_FAILURE;
194 return;
Mike Kelly6df71fd2020-10-13 17:50:05 +0100195 }
196 if (showHelp)
197 {
198 ALOGW("Showing help and exiting");
199 std::cout << optionsDesc.help() << std::endl;
200 m_ShouldExit = true;
201 m_ExitCode = EXIT_SUCCESS;
202 return;
203 }
204 if (showVersion)
205 {
206 ALOGW("Showing version and exiting");
207 std::cout << "ArmNN Android NN driver for the Android Neural Networks API.\n"
208 "ArmNN v" << ARMNN_VERSION << std::endl;
209 m_ShouldExit = true;
210 m_ExitCode = EXIT_SUCCESS;
211 return;
telsoa01ce3e84a2018-08-31 09:31:35 +0100212 }
213
Colm Donelan0cc61782020-10-06 21:02:21 +0100214 // Convert the string backend names into backendId's.
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100215 m_Backends.reserve(backends.size());
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100216 for (auto&& backend : backends)
telsoa01ce3e84a2018-08-31 09:31:35 +0100217 {
Mike Kelly6df71fd2020-10-13 17:50:05 +0100218 m_Backends.emplace_back(backend);
219 }
220
221 // If no backends have been specified then the default value is GpuAcc.
222 if (backends.empty())
223 {
224 ALOGE("No backends have been specified:");
225 std::cout << optionsDesc.help() << std::endl
226 << "Unable to start:" << std::endl
227 << "No backends have been specified" << std::endl;
228 m_ShouldExit = true;
229 m_ExitCode = EXIT_FAILURE;
230 return;
telsoa01ce3e84a2018-08-31 09:31:35 +0100231 }
232
233 if (!unsupportedOperationsAsString.empty())
234 {
235 std::istringstream argStream(unsupportedOperationsAsString);
236
237 std::string s;
238 while (!argStream.eof())
239 {
240 std::getline(argStream, s, ',');
241 try
242 {
243 unsigned int operationIdx = std::stoi(s);
244 m_ForcedUnsupportedOperations.insert(operationIdx);
245 }
246 catch (const std::invalid_argument&)
247 {
248 ALOGW("Ignoring invalid integer argument in -u/--unsupported-operations value: %s", s.c_str());
249 }
250 }
251 }
252
253 if (!m_ClTunedParametersFile.empty())
254 {
255 // The mode is only relevant if the file path has been provided
256 if (clTunedParametersModeAsString == "UseTunedParameters")
257 {
258 m_ClTunedParametersMode = armnn::IGpuAccTunedParameters::Mode::UseTunedParameters;
259 }
260 else if (clTunedParametersModeAsString == "UpdateTunedParameters")
261 {
262 m_ClTunedParametersMode = armnn::IGpuAccTunedParameters::Mode::UpdateTunedParameters;
263 }
264 else
265 {
266 ALOGW("Requested unknown cl-tuned-parameters-mode '%s'. Defaulting to UseTunedParameters",
267 clTunedParametersModeAsString.c_str());
268 }
Ruomei Yan689c6ee2019-04-25 17:48:41 +0100269
270 if (clTuningLevelAsString == "exhaustive")
271 {
272 m_ClTuningLevel = armnn::IGpuAccTunedParameters::TuningLevel::Exhaustive;
273 }
274 else if (clTuningLevelAsString == "normal")
275 {
276 m_ClTuningLevel = armnn::IGpuAccTunedParameters::TuningLevel::Normal;
277 }
278 else if (clTuningLevelAsString == "rapid")
279 {
280 m_ClTuningLevel = armnn::IGpuAccTunedParameters::TuningLevel::Rapid;
281 }
282 else
283 {
284 ALOGW("Requested unknown cl-tuner-mode '%s'. Defaulting to rapid",
285 clTuningLevelAsString.c_str());
286 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100287 }
288}
289
290} // namespace armnn_driver